2016-11-30 20:15:29 -08:00
|
|
|
// +build gcc
|
|
|
|
|
2016-11-29 16:06:36 -08:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
2017-12-13 19:28:37 -08:00
|
|
|
"bytes"
|
2016-11-29 16:06:36 -08:00
|
|
|
"fmt"
|
2017-12-13 17:11:11 -08:00
|
|
|
"path/filepath"
|
2016-11-29 16:06:36 -08:00
|
|
|
|
|
|
|
"github.com/jmhodges/levigo"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-12-12 17:58:35 -08:00
|
|
|
var _ DB = (*CLevelDB)(nil)
|
|
|
|
|
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) {
|
2017-12-13 17:11:11 -08:00
|
|
|
dbPath := filepath.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 {
|
2017-12-17 13:11:28 -08:00
|
|
|
key = nonNilBytes(key)
|
2016-11-29 16:06:36 -08:00
|
|
|
res, err := db.db.Get(db.ro, key)
|
|
|
|
if err != nil {
|
2017-11-09 14:42:32 -08:00
|
|
|
panic(err)
|
2016-11-29 16:06:36 -08:00
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2017-12-12 14:00:03 -08:00
|
|
|
func (db *CLevelDB) Has(key []byte) bool {
|
2017-12-17 13:11:28 -08:00
|
|
|
return db.Get(key) != nil
|
2017-12-12 14:00:03 -08:00
|
|
|
}
|
|
|
|
|
2016-11-30 20:15:29 -08:00
|
|
|
func (db *CLevelDB) Set(key []byte, value []byte) {
|
2017-12-17 13:11:28 -08:00
|
|
|
key = nonNilBytes(key)
|
|
|
|
value = nonNilBytes(value)
|
2016-11-29 16:06:36 -08:00
|
|
|
err := db.db.Put(db.wo, key, value)
|
|
|
|
if err != nil {
|
2017-11-09 14:42:32 -08:00
|
|
|
panic(err)
|
2016-11-29 16:06:36 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-30 20:15:29 -08:00
|
|
|
func (db *CLevelDB) SetSync(key []byte, value []byte) {
|
2017-12-17 13:11:28 -08:00
|
|
|
key = nonNilBytes(key)
|
|
|
|
value = nonNilBytes(value)
|
2016-11-29 16:06:36 -08:00
|
|
|
err := db.db.Put(db.woSync, key, value)
|
|
|
|
if err != nil {
|
2017-11-09 14:42:32 -08:00
|
|
|
panic(err)
|
2016-11-29 16:06:36 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-30 20:15:29 -08:00
|
|
|
func (db *CLevelDB) Delete(key []byte) {
|
2017-12-17 13:11:28 -08:00
|
|
|
key = nonNilBytes(key)
|
2016-11-29 16:06:36 -08:00
|
|
|
err := db.db.Delete(db.wo, key)
|
|
|
|
if err != nil {
|
2017-11-09 14:42:32 -08:00
|
|
|
panic(err)
|
2016-11-29 16:06:36 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-30 20:15:29 -08:00
|
|
|
func (db *CLevelDB) DeleteSync(key []byte) {
|
2017-12-17 13:11:28 -08:00
|
|
|
key = nonNilBytes(key)
|
2016-11-29 16:06:36 -08:00
|
|
|
err := db.db.Delete(db.woSync, key)
|
|
|
|
if err != nil {
|
2017-11-09 14:42:32 -08:00
|
|
|
panic(err)
|
2016-11-29 16:06:36 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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() {
|
2017-12-17 13:11:28 -08:00
|
|
|
itr := db.Iterator(nil, nil)
|
2017-12-15 01:48:40 -08:00
|
|
|
defer itr.Close()
|
2017-12-12 14:00:03 -08:00
|
|
|
for ; itr.Valid(); itr.Next() {
|
2017-11-09 14:42:32 -08:00
|
|
|
key := itr.Key()
|
|
|
|
value := itr.Value()
|
2016-11-29 16:06:36 -08:00
|
|
|
fmt.Printf("[%X]:\t[%X]\n", key, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-23 08:37:46 -07:00
|
|
|
func (db *CLevelDB) Stats() map[string]string {
|
2017-04-01 12:44:41 -07:00
|
|
|
// TODO: Find the available properties for the C LevelDB implementation
|
2017-03-23 08:37:46 -07:00
|
|
|
keys := []string{}
|
|
|
|
|
|
|
|
stats := make(map[string]string)
|
|
|
|
for _, key := range keys {
|
2017-11-09 14:42:32 -08:00
|
|
|
str := db.db.PropertyValue(key)
|
|
|
|
stats[key] = str
|
2017-03-23 08:37:46 -07:00
|
|
|
}
|
|
|
|
return stats
|
|
|
|
}
|
|
|
|
|
2017-11-09 14:42:32 -08:00
|
|
|
//----------------------------------------
|
|
|
|
// Batch
|
|
|
|
|
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 {
|
2017-11-09 14:42:32 -08:00
|
|
|
panic(err)
|
2016-11-29 16:06:36 -08:00
|
|
|
}
|
|
|
|
}
|
2017-11-09 14:42:32 -08:00
|
|
|
|
|
|
|
//----------------------------------------
|
|
|
|
// Iterator
|
2017-12-17 13:11:28 -08:00
|
|
|
// NOTE This is almost identical to db/go_level_db.Iterator
|
|
|
|
// Before creating a third version, refactor.
|
2017-11-09 14:42:32 -08:00
|
|
|
|
2017-12-12 14:00:03 -08:00
|
|
|
func (db *CLevelDB) Iterator(start, end []byte) Iterator {
|
2017-12-12 17:58:35 -08:00
|
|
|
itr := db.db.NewIterator(db.ro)
|
2017-12-17 13:11:28 -08:00
|
|
|
return newCLevelDBIterator(itr, start, end, false)
|
2017-12-12 14:00:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (db *CLevelDB) ReverseIterator(start, end []byte) Iterator {
|
2017-12-17 13:11:28 -08:00
|
|
|
panic("not implemented yet") // XXX
|
2017-11-09 14:42:32 -08:00
|
|
|
}
|
|
|
|
|
2017-12-12 17:58:35 -08:00
|
|
|
var _ Iterator = (*cLevelDBIterator)(nil)
|
|
|
|
|
2017-11-09 14:42:32 -08:00
|
|
|
type cLevelDBIterator struct {
|
2017-12-17 13:11:28 -08:00
|
|
|
source *levigo.Iterator
|
2017-12-12 17:58:35 -08:00
|
|
|
start, end []byte
|
2017-12-17 13:11:28 -08:00
|
|
|
isReverse bool
|
|
|
|
isInvalid bool
|
2017-11-09 14:42:32 -08:00
|
|
|
}
|
|
|
|
|
2017-12-17 13:11:28 -08:00
|
|
|
func newCLevelDBIterator(source *levigo.Iterator, start, end []byte, isReverse bool) *cLevelDBIterator {
|
|
|
|
if isReverse {
|
|
|
|
panic("not implemented yet") // XXX
|
|
|
|
}
|
|
|
|
if start != nil {
|
|
|
|
source.Seek(start)
|
2017-12-15 12:58:23 -08:00
|
|
|
} else {
|
2017-12-17 13:11:28 -08:00
|
|
|
source.SeekToFirst()
|
2017-12-15 12:58:23 -08:00
|
|
|
}
|
|
|
|
return &cLevelDBIterator{
|
2017-12-17 13:11:28 -08:00
|
|
|
source: source,
|
|
|
|
start: start,
|
|
|
|
end: end,
|
|
|
|
isReverse: isReverse,
|
|
|
|
isInvalid: false,
|
2017-12-15 12:58:23 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-17 13:11:28 -08:00
|
|
|
func (itr *cLevelDBIterator) Domain() ([]byte, []byte) {
|
|
|
|
return itr.start, itr.end
|
2017-11-09 14:42:32 -08:00
|
|
|
}
|
|
|
|
|
2017-12-17 13:11:28 -08:00
|
|
|
func (itr *cLevelDBIterator) Valid() bool {
|
|
|
|
|
|
|
|
// Once invalid, forever invalid.
|
|
|
|
if itr.isInvalid {
|
2017-12-13 19:28:37 -08:00
|
|
|
return false
|
|
|
|
}
|
2017-11-09 14:42:32 -08:00
|
|
|
|
2017-12-17 13:11:28 -08:00
|
|
|
// Panic on DB error. No way to recover.
|
|
|
|
itr.assertNoError()
|
|
|
|
|
|
|
|
// If source is invalid, invalid.
|
|
|
|
if !itr.source.Valid() {
|
|
|
|
itr.isInvalid = true
|
|
|
|
return false
|
2017-11-09 14:42:32 -08:00
|
|
|
}
|
|
|
|
|
2017-12-17 13:11:28 -08:00
|
|
|
// If key is end or past it, invalid.
|
|
|
|
var end = itr.end
|
|
|
|
var key = itr.source.Key()
|
|
|
|
if end != nil && bytes.Compare(end, key) <= 0 {
|
|
|
|
itr.isInvalid = true
|
|
|
|
return false
|
2017-11-09 14:42:32 -08:00
|
|
|
}
|
2017-12-17 13:11:28 -08:00
|
|
|
|
|
|
|
// Valid
|
|
|
|
return true
|
2017-11-09 14:42:32 -08:00
|
|
|
}
|
|
|
|
|
2017-12-17 13:11:28 -08:00
|
|
|
func (itr *cLevelDBIterator) Key() []byte {
|
|
|
|
itr.assertNoError()
|
|
|
|
itr.assertIsValid()
|
|
|
|
return itr.source.Key()
|
2017-12-13 19:28:37 -08:00
|
|
|
}
|
|
|
|
|
2017-12-17 13:11:28 -08:00
|
|
|
func (itr *cLevelDBIterator) Value() []byte {
|
|
|
|
itr.assertNoError()
|
|
|
|
itr.assertIsValid()
|
|
|
|
return itr.source.Value()
|
|
|
|
}
|
2017-12-15 12:58:23 -08:00
|
|
|
|
2017-12-17 13:11:28 -08:00
|
|
|
func (itr *cLevelDBIterator) Next() {
|
|
|
|
itr.assertNoError()
|
|
|
|
itr.assertIsValid()
|
|
|
|
itr.source.Next()
|
2017-11-09 14:42:32 -08:00
|
|
|
}
|
|
|
|
|
2017-12-17 13:11:28 -08:00
|
|
|
func (itr *cLevelDBIterator) Close() {
|
|
|
|
itr.source.Close()
|
2017-11-09 14:42:32 -08:00
|
|
|
}
|
|
|
|
|
2017-12-17 13:11:28 -08:00
|
|
|
func (itr *cLevelDBIterator) assertNoError() {
|
|
|
|
if err := itr.source.GetError(); err != nil {
|
2017-12-12 19:19:26 -08:00
|
|
|
panic(err)
|
|
|
|
}
|
2017-11-09 14:42:32 -08:00
|
|
|
}
|
2017-12-17 13:11:28 -08:00
|
|
|
|
|
|
|
func (itr cLevelDBIterator) assertIsValid() {
|
|
|
|
if !itr.Valid() {
|
|
|
|
panic("cLevelDBIterator is invalid")
|
|
|
|
}
|
|
|
|
}
|