2016-11-30 20:15:29 -08:00
|
|
|
// +build gcc
|
|
|
|
|
2016-11-29 16:06:36 -08:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path"
|
|
|
|
|
|
|
|
"github.com/jmhodges/levigo"
|
|
|
|
|
|
|
|
. "github.com/tendermint/go-common"
|
|
|
|
)
|
|
|
|
|
2016-11-30 20:15:29 -08:00
|
|
|
func init() {
|
2016-11-30 20:22:35 -08:00
|
|
|
dbCreator := func(name string, dir string) (DB, error) {
|
2016-11-30 20:15:29 -08:00
|
|
|
return NewCLevelDB(name, dir)
|
2016-11-30 20:22:35 -08:00
|
|
|
}
|
|
|
|
registerDBCreator(LevelDBBackendStr, dbCreator, true)
|
|
|
|
registerDBCreator(CLevelDBBackendStr, dbCreator, false)
|
2016-11-30 20:15:29 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
type CLevelDB struct {
|
2016-11-29 16:06:36 -08:00
|
|
|
db *levigo.DB
|
|
|
|
ro *levigo.ReadOptions
|
|
|
|
wo *levigo.WriteOptions
|
|
|
|
woSync *levigo.WriteOptions
|
|
|
|
}
|
|
|
|
|
2016-11-30 20:15:29 -08:00
|
|
|
func NewCLevelDB(name string, dir string) (*CLevelDB, error) {
|
|
|
|
dbPath := path.Join(dir, name+".db")
|
2016-11-29 16:06:36 -08:00
|
|
|
|
|
|
|
opts := levigo.NewOptions()
|
|
|
|
opts.SetCache(levigo.NewLRUCache(1 << 30))
|
|
|
|
opts.SetCreateIfMissing(true)
|
|
|
|
db, err := levigo.Open(dbPath, opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ro := levigo.NewReadOptions()
|
|
|
|
wo := levigo.NewWriteOptions()
|
|
|
|
woSync := levigo.NewWriteOptions()
|
|
|
|
woSync.SetSync(true)
|
2016-11-30 20:15:29 -08:00
|
|
|
database := &CLevelDB{
|
2016-11-29 16:06:36 -08:00
|
|
|
db: db,
|
|
|
|
ro: ro,
|
|
|
|
wo: wo,
|
|
|
|
woSync: woSync,
|
|
|
|
}
|
|
|
|
return database, nil
|
|
|
|
}
|
|
|
|
|
2016-11-30 20:15:29 -08:00
|
|
|
func (db *CLevelDB) Get(key []byte) []byte {
|
2016-11-29 16:06:36 -08:00
|
|
|
res, err := db.db.Get(db.ro, key)
|
|
|
|
if err != nil {
|
|
|
|
PanicCrisis(err)
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2016-11-30 20:15:29 -08:00
|
|
|
func (db *CLevelDB) Set(key []byte, value []byte) {
|
2016-11-29 16:06:36 -08:00
|
|
|
err := db.db.Put(db.wo, key, value)
|
|
|
|
if err != nil {
|
|
|
|
PanicCrisis(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-30 20:15:29 -08:00
|
|
|
func (db *CLevelDB) SetSync(key []byte, value []byte) {
|
2016-11-29 16:06:36 -08:00
|
|
|
err := db.db.Put(db.woSync, key, value)
|
|
|
|
if err != nil {
|
|
|
|
PanicCrisis(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-30 20:15:29 -08:00
|
|
|
func (db *CLevelDB) Delete(key []byte) {
|
2016-11-29 16:06:36 -08:00
|
|
|
err := db.db.Delete(db.wo, key)
|
|
|
|
if err != nil {
|
|
|
|
PanicCrisis(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-30 20:15:29 -08:00
|
|
|
func (db *CLevelDB) DeleteSync(key []byte) {
|
2016-11-29 16:06:36 -08:00
|
|
|
err := db.db.Delete(db.woSync, key)
|
|
|
|
if err != nil {
|
|
|
|
PanicCrisis(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-30 20:15:29 -08:00
|
|
|
func (db *CLevelDB) DB() *levigo.DB {
|
2016-11-29 16:06:36 -08:00
|
|
|
return db.db
|
|
|
|
}
|
|
|
|
|
2016-11-30 20:15:29 -08:00
|
|
|
func (db *CLevelDB) Close() {
|
2016-11-29 16:06:36 -08:00
|
|
|
db.db.Close()
|
|
|
|
db.ro.Close()
|
|
|
|
db.wo.Close()
|
|
|
|
db.woSync.Close()
|
|
|
|
}
|
|
|
|
|
2016-11-30 20:15:29 -08:00
|
|
|
func (db *CLevelDB) Print() {
|
2016-11-29 16:06:36 -08:00
|
|
|
iter := db.db.NewIterator(db.ro)
|
|
|
|
defer iter.Close()
|
|
|
|
for iter.Seek(nil); iter.Valid(); iter.Next() {
|
|
|
|
key := iter.Key()
|
|
|
|
value := iter.Value()
|
|
|
|
fmt.Printf("[%X]:\t[%X]\n", key, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-23 08:37:46 -07:00
|
|
|
func (db *CLevelDB) Stats() map[string]string {
|
|
|
|
keys := []string{}
|
|
|
|
|
|
|
|
stats := make(map[string]string)
|
|
|
|
for _, key := range keys {
|
|
|
|
str, err := db.db.GetProperty(key)
|
|
|
|
if err == nil {
|
|
|
|
stats[key] = str
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return stats
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *CLevelDB) Iterator() Iterator {
|
|
|
|
return db.db.NewIterator(nil, nil)
|
|
|
|
}
|
|
|
|
|
2016-11-30 20:15:29 -08:00
|
|
|
func (db *CLevelDB) NewBatch() Batch {
|
2016-11-29 16:06:36 -08:00
|
|
|
batch := levigo.NewWriteBatch()
|
2016-11-30 20:15:29 -08:00
|
|
|
return &cLevelDBBatch{db, batch}
|
2016-11-29 16:06:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------
|
|
|
|
|
2016-11-30 20:15:29 -08:00
|
|
|
type cLevelDBBatch struct {
|
|
|
|
db *CLevelDB
|
2016-11-29 16:06:36 -08:00
|
|
|
batch *levigo.WriteBatch
|
|
|
|
}
|
|
|
|
|
2016-11-30 20:15:29 -08:00
|
|
|
func (mBatch *cLevelDBBatch) Set(key, value []byte) {
|
2016-11-29 16:06:36 -08:00
|
|
|
mBatch.batch.Put(key, value)
|
|
|
|
}
|
|
|
|
|
2016-11-30 20:15:29 -08:00
|
|
|
func (mBatch *cLevelDBBatch) Delete(key []byte) {
|
2016-11-29 16:06:36 -08:00
|
|
|
mBatch.batch.Delete(key)
|
|
|
|
}
|
|
|
|
|
2016-11-30 20:15:29 -08:00
|
|
|
func (mBatch *cLevelDBBatch) Write() {
|
2016-11-29 16:06:36 -08:00
|
|
|
err := mBatch.db.db.Write(mBatch.db.wo, mBatch.batch)
|
|
|
|
if err != nil {
|
|
|
|
PanicCrisis(err)
|
|
|
|
}
|
|
|
|
}
|