MCPcopy
hub / github.com/dgraph-io/badger / Open

Function Open

db.go:194–418  ·  view source on GitHub ↗

Open returns a new DB object.

(opt Options)

Source from the content-addressed store, hash-verified

192
193// Open returns a new DB object.
194func Open(opt Options) (db *DB, err error) {
195 if opt.InMemory && (opt.Dir != "" || opt.ValueDir != "") {
196 return nil, errors.New("Cannot use badger in Disk-less mode with Dir or ValueDir set")
197 }
198 opt.maxBatchSize = (15 * opt.MaxTableSize) / 100
199 opt.maxBatchCount = opt.maxBatchSize / int64(skl.MaxNodeSize)
200
201 // We are limiting opt.ValueThreshold to maxValueThreshold for now.
202 if opt.ValueThreshold > maxValueThreshold {
203 return nil, errors.Errorf("Invalid ValueThreshold, must be less or equal to %d",
204 maxValueThreshold)
205 }
206
207 // If ValueThreshold is greater than opt.maxBatchSize, we won't be able to push any data using
208 // the transaction APIs. Transaction batches entries into batches of size opt.maxBatchSize.
209 if int64(opt.ValueThreshold) > opt.maxBatchSize {
210 return nil, errors.Errorf("Valuethreshold greater than max batch size of %d. Either "+
211 "reduce opt.ValueThreshold or increase opt.MaxTableSize.", opt.maxBatchSize)
212 }
213 if !(opt.ValueLogFileSize <= 2<<30 && opt.ValueLogFileSize >= 1<<20) {
214 return nil, ErrValueLogSize
215 }
216 if !(opt.ValueLogLoadingMode == options.FileIO ||
217 opt.ValueLogLoadingMode == options.MemoryMap) {
218 return nil, ErrInvalidLoadingMode
219 }
220
221 // Return error if badger is built without cgo and compression is set to ZSTD.
222 if opt.Compression == options.ZSTD && !y.CgoEnabled {
223 return nil, y.ErrZstdCgo
224 }
225 // Keep L0 in memory if either KeepL0InMemory is set or if InMemory is set.
226 opt.KeepL0InMemory = opt.KeepL0InMemory || opt.InMemory
227
228 // Compact L0 on close if either it is set or if KeepL0InMemory is set. When
229 // keepL0InMemory is set we need to compact L0 on close otherwise we might lose data.
230 opt.CompactL0OnClose = opt.CompactL0OnClose || opt.KeepL0InMemory
231
232 if opt.ReadOnly {
233 // Can't truncate if the DB is read only.
234 opt.Truncate = false
235 // Do not perform compaction in read only mode.
236 opt.CompactL0OnClose = false
237 }
238 var dirLockGuard, valueDirLockGuard *directoryLockGuard
239
240 // Create directories and acquire lock on it only if badger is not running in InMemory mode.
241 // We don't have any directories/files in InMemory mode so we don't need to acquire
242 // any locks on them.
243 if !opt.InMemory {
244 if err := createDirs(opt); err != nil {
245 return nil, err
246 }
247 if !opt.BypassLockGuard {
248 dirLockGuard, err = acquireDirectoryLock(opt.Dir, lockFile, opt.ReadOnly)
249 if err != nil {
250 return nil, err
251 }

Callers 15

TestWriteBatchFunction · 0.85
TestManifestBasicFunction · 0.85
TestDropAllManagedFunction · 0.85
TestDropAllFunction · 0.85
TestDropAllTwiceFunction · 0.85
TestDropReadOnlyFunction · 0.85
TestWriteAfterCloseFunction · 0.85
TestDropAllRaceFunction · 0.85

Calls 15

releaseMethod · 0.95
DecodeMethod · 0.95
SignalAndWaitMethod · 0.95
NewCloserFunction · 0.92
NewSkiplistFunction · 0.92
KeyWithTsFunction · 0.92
WrapfFunction · 0.92
createDirsFunction · 0.85
openOrCreateManifestFileFunction · 0.85
newOracleFunction · 0.85
newPublisherFunction · 0.85
OpenKeyRegistryFunction · 0.85

Tested by 15

TestWriteBatchFunction · 0.68
TestManifestBasicFunction · 0.68
TestDropAllManagedFunction · 0.68
TestDropAllFunction · 0.68
TestDropAllTwiceFunction · 0.68
TestDropReadOnlyFunction · 0.68
TestWriteAfterCloseFunction · 0.68
TestDropAllRaceFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…