2014-05-15 10:19:48 +04:00
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
type DB struct {
|
2014-05-16 05:42:27 +04:00
|
|
|
cfg *DBConfig
|
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
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 {
|
2014-05-15 10:19:48 +04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-05-16 05:42:27 +04:00
|
|
|
return OpenDBWithConfig(&cfg)
|
2014-05-15 10:19:48 +04:00
|
|
|
}
|
|
|
|
|
2014-05-16 05:42:27 +04:00
|
|
|
func OpenDBWithConfig(cfg *DBConfig) (*DB, error) {
|
|
|
|
db, err := leveldb.OpenWithConfig(&cfg.DataDB)
|
2014-05-15 10:19:48 +04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
d := new(DB)
|
|
|
|
|
2014-05-16 05:42:27 +04:00
|
|
|
d.cfg = cfg
|
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
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()
|
|
|
|
}
|