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",
"leveldb": {
"db": {
"data_db" : {
"path": "/tmp/ledisdb",
"compression": false,
"block_size": 32768,
@ -8,3 +9,4 @@
"cache_size": 524288000
}
}
}

View File

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

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