func newSnapshot(db *RocksDB, fillcache bool) *Snapshot { snap := db.rkdb.NewSnapshot() ropt := gorocks.NewReadOptions() ropt.SetFillCache(fillcache) ropt.SetSnapshot(snap) return &Snapshot{ db: db, snap: snap, ropt: ropt, } }
func (db *RocksDB) init(path string, conf *Config, repair bool) error { if conf == nil { conf = NewDefaultConfig() } // Create path if not exists first if err := os.MkdirAll(path, 0700); err != nil { return errors.Trace(err) } opts := gorocks.NewOptions() opts.SetCreateIfMissing(true) opts.SetErrorIfExists(false) // TODO: use lz4 opts.SetCompression(gorocks.Lz4Compression) // opts.SetCompression(gorocks.NoCompression) opts.SetBlockSize(conf.BlockSize) opts.SetWriteBufferSize(conf.WriteBufferSize) opts.SetMaxOpenFiles(conf.MaxOpenFiles) opts.SetNumLevels(conf.NumLevels) opts.SetMaxWriteBufferNumber(conf.MaxWriteBufferNumber) opts.SetMinWriteBufferNumberToMerge(conf.MinWriteBufferNumberToMerge) opts.SetLevel0FileNumCompactionTrigger(conf.Level0FileNumCompactionTrigger) opts.SetLevel0SlowdownWritesTrigger(conf.Level0SlowdownWritesTrigger) opts.SetLevel0StopWritesTrigger(conf.Level0StopWritesTrigger) opts.SetTargetFileSizeBase(conf.TargetFileSizeBase) opts.SetTargetFileSizeMultiplier(conf.TargetFileSizeMultiplier) opts.SetMaxBytesForLevelBase(conf.MaxBytesForLevelBase) opts.SetMaxBytesForLevelMultiplier(conf.MaxBytesForLevelMultiplier) opts.SetDisableAutoCompactions(conf.DisableAutoCompactions) opts.SetDisableDataSync(conf.DisableDataSync) opts.SetUseFsync(conf.UseFsync) opts.SetMaxBackgroundCompactions(conf.MaxBackgroundCompactions) opts.SetMaxBackgroundFlushes(conf.MaxBackgroundFlushes) opts.SetAllowOSBuffer(conf.AllowOSBuffer) topts := gorocks.NewTableOptions() topts.SetBlockSize(conf.BlockSize) cache := gorocks.NewLRUCache(conf.CacheSize) topts.SetCache(cache) topts.SetFilterPolicy(gorocks.NewBloomFilter(conf.BloomFilterSize)) opts.SetBlockBasedTableFactory(topts) env := gorocks.NewDefaultEnv() env.SetBackgroundThreads(conf.BackgroundThreads) env.SetHighPriorityBackgroundThreads(conf.HighPriorityBackgroundThreads) opts.SetEnv(env) db.path = path db.opts = opts db.ropt = gorocks.NewReadOptions() db.wopt = gorocks.NewWriteOptions() db.env = env db.topts = topts db.cache = cache db.snapshotFillCache = conf.SnapshotFillCache if repair { if err := gorocks.RepairDatabase(db.path, db.opts); err != nil { return errors.Trace(err) } } var err error if db.rkdb, err = gorocks.Open(db.path, db.opts); err != nil { return errors.Trace(err) } return nil }