2014-07-25 13:58:00 +04:00
|
|
|
package goleveldb
|
|
|
|
|
|
|
|
import (
|
2014-07-29 04:22:47 +04:00
|
|
|
"github.com/siddontang/goleveldb/leveldb"
|
|
|
|
"github.com/siddontang/goleveldb/leveldb/cache"
|
|
|
|
"github.com/siddontang/goleveldb/leveldb/filter"
|
|
|
|
"github.com/siddontang/goleveldb/leveldb/opt"
|
2014-08-07 12:49:48 +04:00
|
|
|
"github.com/siddontang/ledisdb/config"
|
2014-07-25 13:58:00 +04:00
|
|
|
"github.com/siddontang/ledisdb/store/driver"
|
2014-08-07 12:49:48 +04:00
|
|
|
|
2014-07-25 13:58:00 +04:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
const defaultFilterBits int = 10
|
|
|
|
|
2014-08-07 12:49:48 +04:00
|
|
|
type Store struct {
|
|
|
|
}
|
2014-07-25 13:58:00 +04:00
|
|
|
|
2014-08-07 12:49:48 +04:00
|
|
|
func (s Store) String() string {
|
|
|
|
return "goleveldb"
|
2014-07-25 13:58:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
type DB struct {
|
2014-08-07 12:49:48 +04:00
|
|
|
path string
|
|
|
|
|
|
|
|
cfg *config.LevelDBConfig
|
2014-07-25 13:58:00 +04:00
|
|
|
|
|
|
|
db *leveldb.DB
|
|
|
|
|
|
|
|
opts *opt.Options
|
|
|
|
|
|
|
|
iteratorOpts *opt.ReadOptions
|
|
|
|
|
|
|
|
cache cache.Cache
|
|
|
|
|
|
|
|
filter filter.Filter
|
|
|
|
}
|
|
|
|
|
2014-08-07 12:49:48 +04:00
|
|
|
func (s Store) Open(path string, cfg *config.Config) (driver.IDB, error) {
|
|
|
|
if err := os.MkdirAll(path, os.ModePerm); err != nil {
|
2014-07-25 13:58:00 +04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
db := new(DB)
|
2014-08-07 12:49:48 +04:00
|
|
|
db.path = path
|
|
|
|
db.cfg = &cfg.LevelDB
|
2014-07-25 13:58:00 +04:00
|
|
|
|
|
|
|
if err := db.open(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return db, nil
|
|
|
|
}
|
|
|
|
|
2014-08-07 12:49:48 +04:00
|
|
|
func (s Store) Repair(path string, cfg *config.Config) error {
|
|
|
|
db, err := leveldb.RecoverFile(path, newOptions(&cfg.LevelDB))
|
2014-07-25 13:58:00 +04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
db.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) open() error {
|
|
|
|
db.opts = newOptions(db.cfg)
|
|
|
|
|
|
|
|
db.iteratorOpts = &opt.ReadOptions{}
|
|
|
|
db.iteratorOpts.DontFillCache = true
|
|
|
|
|
|
|
|
var err error
|
2014-08-07 12:49:48 +04:00
|
|
|
db.db, err = leveldb.OpenFile(db.path, db.opts)
|
2014-07-25 13:58:00 +04:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-07 12:49:48 +04:00
|
|
|
func newOptions(cfg *config.LevelDBConfig) *opt.Options {
|
2014-07-25 13:58:00 +04:00
|
|
|
opts := &opt.Options{}
|
|
|
|
opts.ErrorIfMissing = false
|
|
|
|
|
2014-08-07 12:49:48 +04:00
|
|
|
cfg.Adjust()
|
|
|
|
|
|
|
|
opts.BlockCache = cache.NewLRUCache(cfg.CacheSize)
|
2014-07-25 13:58:00 +04:00
|
|
|
|
|
|
|
//we must use bloomfilter
|
|
|
|
opts.Filter = filter.NewBloomFilter(defaultFilterBits)
|
|
|
|
|
|
|
|
if !cfg.Compression {
|
|
|
|
opts.Compression = opt.NoCompression
|
|
|
|
} else {
|
|
|
|
opts.Compression = opt.SnappyCompression
|
|
|
|
}
|
|
|
|
|
2014-08-07 12:49:48 +04:00
|
|
|
opts.BlockSize = cfg.BlockSize
|
|
|
|
opts.WriteBuffer = cfg.WriteBufferSize
|
2014-07-25 13:58:00 +04:00
|
|
|
|
|
|
|
return opts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) Close() error {
|
|
|
|
return db.db.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) Put(key, value []byte) error {
|
|
|
|
return db.db.Put(key, value, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) Get(key []byte) ([]byte, error) {
|
|
|
|
v, err := db.db.Get(key, nil)
|
|
|
|
if err == leveldb.ErrNotFound {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) Delete(key []byte) error {
|
|
|
|
return db.db.Delete(key, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) NewWriteBatch() driver.IWriteBatch {
|
|
|
|
wb := &WriteBatch{
|
|
|
|
db: db,
|
|
|
|
wbatch: new(leveldb.Batch),
|
|
|
|
}
|
|
|
|
return wb
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) NewIterator() driver.IIterator {
|
|
|
|
it := &Iterator{
|
|
|
|
db.db.NewIterator(nil, db.iteratorOpts),
|
|
|
|
}
|
|
|
|
|
|
|
|
return it
|
|
|
|
}
|
2014-07-29 13:29:51 +04:00
|
|
|
|
|
|
|
func (db *DB) Begin() (driver.Tx, error) {
|
|
|
|
return nil, driver.ErrTxSupport
|
|
|
|
}
|