From 03c8e4c24c04994903e746f4752f57255831c902 Mon Sep 17 00:00:00 2001 From: siddontang Date: Mon, 1 Sep 2014 17:45:47 +0800 Subject: [PATCH] store add memory support based on goleveldb --- store/goleveldb/const.go | 1 + store/goleveldb/db.go | 44 +++++++++++++++++++++++++++++++++------- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/store/goleveldb/const.go b/store/goleveldb/const.go index 6486e3f..2fffa7c 100644 --- a/store/goleveldb/const.go +++ b/store/goleveldb/const.go @@ -1,3 +1,4 @@ package goleveldb const DBName = "goleveldb" +const MemDBName = "memory" diff --git a/store/goleveldb/db.go b/store/goleveldb/db.go index ca2de60..e873feb 100644 --- a/store/goleveldb/db.go +++ b/store/goleveldb/db.go @@ -5,6 +5,8 @@ import ( "github.com/siddontang/goleveldb/leveldb/cache" "github.com/siddontang/goleveldb/leveldb/filter" "github.com/siddontang/goleveldb/leveldb/opt" + "github.com/siddontang/goleveldb/leveldb/storage" + "github.com/siddontang/ledisdb/config" "github.com/siddontang/ledisdb/store/driver" @@ -20,6 +22,13 @@ func (s Store) String() string { return DBName } +type MemStore struct { +} + +func (s MemStore) String() string { + return MemDBName +} + type DB struct { path string @@ -45,7 +54,12 @@ func (s Store) Open(path string, cfg *config.Config) (driver.IDB, error) { db.path = path db.cfg = &cfg.LevelDB - if err := db.open(); err != nil { + db.initOpts() + + var err error + db.db, err = leveldb.OpenFile(db.path, db.opts) + + if err != nil { return nil, err } @@ -62,16 +76,31 @@ func (s Store) Repair(path string, cfg *config.Config) error { return nil } -func (db *DB) open() error { +func (s MemStore) Open(path string, cfg *config.Config) (driver.IDB, error) { + db := new(DB) + db.path = path + db.cfg = &cfg.LevelDB + + db.initOpts() + + var err error + db.db, err = leveldb.Open(storage.NewMemStorage(), db.opts) + if err != nil { + return nil, err + } + + return db, nil +} + +func (s MemStore) Repair(path string, cfg *config.Config) error { + return nil +} + +func (db *DB) initOpts() { db.opts = newOptions(db.cfg) db.iteratorOpts = &opt.ReadOptions{} db.iteratorOpts.DontFillCache = true - - var err error - db.db, err = leveldb.OpenFile(db.path, db.opts) - - return err } func newOptions(cfg *config.LevelDBConfig) *opt.Options { @@ -153,4 +182,5 @@ func (db *DB) NewSnapshot() (driver.ISnapshot, error) { func init() { driver.Register(Store{}) + driver.Register(MemStore{}) }