示例#1
0
文件: snapshot.go 项目: CowLeo/qdb
func newSnapshot(db *LevelDB) *Snapshot {
	snap := db.lvdb.NewSnapshot()
	ropt := levigo.NewReadOptions()
	ropt.SetFillCache(false)
	ropt.SetSnapshot(snap)
	return &Snapshot{
		db:   db,
		snap: snap,
		ropt: ropt,
	}
}
示例#2
0
文件: database.go 项目: CowLeo/qdb
func (db *LevelDB) 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 := levigo.NewOptions()
	opts.SetCreateIfMissing(true)
	opts.SetErrorIfExists(false)

	opts.SetCompression(levigo.SnappyCompression)
	opts.SetBlockSize(conf.BlockSize)
	opts.SetWriteBufferSize(conf.WriteBufferSize)
	opts.SetMaxOpenFiles(conf.MaxOpenFiles)

	cache := levigo.NewLRUCache(conf.CacheSize)
	opts.SetCache(cache)

	bloom := levigo.NewBloomFilter(conf.BloomFilterSize)
	opts.SetFilterPolicy(bloom)

	db.path = path
	db.opts = opts
	db.ropt = levigo.NewReadOptions()
	db.wopt = levigo.NewWriteOptions()
	db.cache = cache
	db.bloom = bloom

	if repair {
		if err := levigo.RepairDatabase(db.path, db.opts); err != nil {
			return errors.Trace(err)
		}
	}

	var err error
	if db.lvdb, err = levigo.Open(db.path, db.opts); err != nil {
		return errors.Trace(err)
	}
	return nil
}