update config format

This commit is contained in:
siddontang 2014-05-16 09:42:27 +08:00
parent b2ec29083d
commit 6152d75160
4 changed files with 37 additions and 30 deletions

View File

@ -1,6 +1,7 @@
{ {
"addr": "127.0.0.1:6380", "addr": "127.0.0.1:6380",
"leveldb": { "db": {
"data_db" : {
"path": "/tmp/ledisdb", "path": "/tmp/ledisdb",
"compression": false, "compression": false,
"block_size": 32768, "block_size": 32768,
@ -8,3 +9,4 @@
"cache_size": 524288000 "cache_size": 524288000
} }
} }
}

View File

@ -45,8 +45,8 @@ func startTestApp() {
var d = []byte(` var d = []byte(`
{ {
"addr" : "127.0.0.1:16380", "addr" : "127.0.0.1:16380",
"leveldb" : "db" : {
{ "data_db" : {
"path" : "/tmp/testdb", "path" : "/tmp/testdb",
"compression":true, "compression":true,
"block_size" : 32768, "block_size" : 32768,
@ -54,6 +54,7 @@ func startTestApp() {
"cache_size" : 20971520 "cache_size" : 20971520
} }
} }
}
`) `)
cfg, err := NewConfig(d) cfg, err := NewConfig(d)

View File

@ -2,14 +2,13 @@ package ledis
import ( import (
"encoding/json" "encoding/json"
"github.com/siddontang/go-leveldb/leveldb"
"io/ioutil" "io/ioutil"
) )
type Config struct { type Config struct {
Addr string `json:"addr"` Addr string `json:"addr"`
DB leveldb.Config `json:"leveldb"` DB DBConfig `json:"db"`
} }
func NewConfig(data json.RawMessage) (*Config, error) { func NewConfig(data json.RawMessage) (*Config, error) {

View File

@ -5,7 +5,13 @@ import (
"github.com/siddontang/go-leveldb/leveldb" "github.com/siddontang/go-leveldb/leveldb"
) )
type DBConfig struct {
DataDB leveldb.Config `json:"data_db"`
}
type DB struct { type DB struct {
cfg *DBConfig
db *leveldb.DB db *leveldb.DB
kvTx *tx kvTx *tx
@ -15,26 +21,25 @@ type DB struct {
} }
func OpenDB(configJson json.RawMessage) (*DB, error) { 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 { if err != nil {
return nil, err 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 := new(DB)
d.cfg = cfg
d.db = db d.db = db
d.kvTx = &tx{wb: db.NewWriteBatch()} d.kvTx = &tx{wb: db.NewWriteBatch()}