diff --git a/etc/ledis.json b/etc/ledis.json index 613426a..92d9f93 100644 --- a/etc/ledis.json +++ b/etc/ledis.json @@ -1,10 +1,12 @@ { "addr": "127.0.0.1:6380", - "leveldb": { - "path": "/tmp/ledisdb", - "compression": false, - "block_size": 32768, - "write_buffer_size": 67108864, - "cache_size": 524288000 + "db": { + "data_db" : { + "path": "/tmp/ledisdb", + "compression": false, + "block_size": 32768, + "write_buffer_size": 67108864, + "cache_size": 524288000 + } } } \ No newline at end of file diff --git a/ledis/app_test.go b/ledis/app_test.go index ba605a3..c80fccb 100644 --- a/ledis/app_test.go +++ b/ledis/app_test.go @@ -45,14 +45,15 @@ func startTestApp() { var d = []byte(` { "addr" : "127.0.0.1:16380", - "leveldb" : - { - "path" : "/tmp/testdb", - "compression":true, - "block_size" : 32768, - "write_buffer_size" : 2097152, - "cache_size" : 20971520 - } + "db" : { + "data_db" : { + "path" : "/tmp/testdb", + "compression":true, + "block_size" : 32768, + "write_buffer_size" : 2097152, + "cache_size" : 20971520 + } + } } `) diff --git a/ledis/config.go b/ledis/config.go index 9172e77..78abf7d 100644 --- a/ledis/config.go +++ b/ledis/config.go @@ -2,14 +2,13 @@ package ledis import ( "encoding/json" - "github.com/siddontang/go-leveldb/leveldb" "io/ioutil" ) type Config struct { Addr string `json:"addr"` - DB leveldb.Config `json:"leveldb"` + DB DBConfig `json:"db"` } func NewConfig(data json.RawMessage) (*Config, error) { diff --git a/ledis/db.go b/ledis/db.go index 4973722..391de74 100644 --- a/ledis/db.go +++ b/ledis/db.go @@ -5,7 +5,13 @@ import ( "github.com/siddontang/go-leveldb/leveldb" ) +type DBConfig struct { + DataDB leveldb.Config `json:"data_db"` +} + type DB struct { + cfg *DBConfig + db *leveldb.DB kvTx *tx @@ -15,26 +21,25 @@ type DB struct { } func OpenDB(configJson json.RawMessage) (*DB, error) { - db, err := leveldb.Open(configJson) + var cfg DBConfig + + if err := json.Unmarshal(configJson, &cfg); err != nil { + return nil, err + } + + return OpenDBWithConfig(&cfg) +} + +func OpenDBWithConfig(cfg *DBConfig) (*DB, error) { + db, err := leveldb.OpenWithConfig(&cfg.DataDB) if err != nil { return nil, err } - return newDB(db) -} - -func OpenDBWithConfig(cfg *leveldb.Config) (*DB, error) { - db, err := leveldb.OpenWithConfig(cfg) - if err != nil { - return nil, err - } - - return newDB(db) -} - -func newDB(db *leveldb.DB) (*DB, error) { d := new(DB) + d.cfg = cfg + d.db = db d.kvTx = &tx{wb: db.NewWriteBatch()}