Exemplo n.º 1
0
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,
	}
}
Exemplo n.º 2
0
func (db *RocksDB) init(path string, conf *Config, create, repair bool) error {
	if conf == nil {
		conf = NewDefaultConfig()
	}
	opts := gorocks.NewOptions()
	if create {
		opts.SetCreateIfMissing(true)
		opts.SetErrorIfExists(true)
	} else {
		opts.SetCreateIfMissing(false)
		opts.SetErrorIfExists(false)
	}

	opts.SetCompression(gorocks.Lz4Compression)
	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 create {
		if err := os.MkdirAll(db.path, 0700); err != nil {
			return errors.Trace(err)
		}
	} else 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
}