Beispiel #1
0
func (db *RocksDB) Clear() error {
	if db.rkdb != nil {
		db.rkdb.Close()
		db.rkdb = nil
		db.opts.SetCreateIfMissing(true)
		db.opts.SetErrorIfExists(true)
		if err := gorocks.DestroyDatabase(db.path, db.opts); err != nil {
			return errors.Trace(err)
		} else if db.rkdb, err = gorocks.Open(db.path, db.opts); err != nil {
			return errors.Trace(err)
		}
	}
	return nil
}
Beispiel #2
0
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
}