ledisdb/ledis/db.go

56 lines
841 B
Go
Raw Normal View History

package ledis
import (
"encoding/json"
"github.com/siddontang/go-leveldb/leveldb"
)
2014-05-16 05:42:27 +04:00
type DBConfig struct {
DataDB leveldb.Config `json:"data_db"`
}
type DB struct {
2014-05-16 05:42:27 +04:00
cfg *DBConfig
db *leveldb.DB
kvTx *tx
listTx *tx
hashTx *tx
zsetTx *tx
}
func OpenDB(configJson json.RawMessage) (*DB, error) {
2014-05-16 05:42:27 +04:00
var cfg DBConfig
if err := json.Unmarshal(configJson, &cfg); err != nil {
return nil, err
}
2014-05-16 05:42:27 +04:00
return OpenDBWithConfig(&cfg)
}
2014-05-16 05:42:27 +04:00
func OpenDBWithConfig(cfg *DBConfig) (*DB, error) {
db, err := leveldb.OpenWithConfig(&cfg.DataDB)
if err != nil {
return nil, err
}
d := new(DB)
2014-05-16 05:42:27 +04:00
d.cfg = cfg
d.db = db
d.kvTx = &tx{wb: db.NewWriteBatch()}
d.listTx = &tx{wb: db.NewWriteBatch()}
d.hashTx = &tx{wb: db.NewWriteBatch()}
d.zsetTx = &tx{wb: db.NewWriteBatch()}
return d, nil
}
func (db *DB) Close() {
db.db.Close()
}