Пример #1
0
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
func (db *LevelDB) init(path string, conf *Config, create, repair bool) error {
	if conf == nil {
		conf = NewDefaultConfig()
	}
	opts := levigo.NewOptions()
	if create {
		opts.SetCreateIfMissing(true)
		opts.SetErrorIfExists(true)
	} else {
		opts.SetCreateIfMissing(false)
		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 create {
		if err := os.MkdirAll(db.path, 0700); err != nil {
			return errors.Trace(err)
		}
	} else 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
}