Open returns a new DB object.
(opt Options)
| 192 | |
| 193 | // Open returns a new DB object. |
| 194 | func 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 | } |
searching dependent graphs…