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,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
}
}
}

View File

@ -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
}
}
}
`)

View File

@ -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) {

View File

@ -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()}