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-12 16:22:08 -08:00
|
|
|
panicNilKey(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-12 16:22:08 -08:00
|
|
|
panicNilKey(key)
|
2017-12-12 14:00:03 -08:00
|
|
|
panic("not implemented yet")
|
|
|
|
}
|
|
|
|
|
2016-11-30 20:15:29 -08:00
|
|
|
func (db *CLevelDB) Set(key []byte, value []byte) {
|
2017-12-12 16:22:08 -08:00
|
|
|
panicNilKey(key)
|
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-12 16:22:08 -08:00
|
|
|
panicNilKey(key)
|
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-12 16:22:08 -08:00
|
|
|
panicNilKey(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-12 16:22:08 -08:00
|
|
|
panicNilKey(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-12 14:00:03 -08:00
|
|
|
itr := db.Iterator(BeginningKey(), EndingKey())
|
|
|
|
defer itr.Release()
|
|
|
|
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-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)
|
|
|
|
if len(start) > 0 {
|
|
|
|
itr.Seek(start)
|
|
|
|
} else {
|
|
|
|
itr.SeekToFirst()
|
|
|
|
}
|
2017-12-13 19:28:37 -08:00
|
|
|
return &cLevelDBIterator{
|
2017-12-12 17:58:35 -08:00
|
|
|
itr: itr,
|
|
|
|
start: start,
|
|
|
|
end: end,
|
|
|
|
}
|
2017-12-12 14:00:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (db *CLevelDB) ReverseIterator(start, end []byte) Iterator {
|
|
|
|
// XXX
|
|
|
|
return nil
|
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-12 17:58:35 -08:00
|
|
|
itr *levigo.Iterator
|
|
|
|
start, end []byte
|
2017-12-13 19:28:37 -08:00
|
|
|
invalid bool
|
2017-11-09 14:42:32 -08:00
|
|
|
}
|
|
|
|
|
2017-12-13 19:28:37 -08:00
|
|
|
func (c *cLevelDBIterator) Domain() ([]byte, []byte) {
|
2017-12-12 17:58:35 -08:00
|
|
|
return c.start, c.end
|
2017-11-09 14:42:32 -08:00
|
|
|
}
|
|
|
|
|
2017-12-13 19:28:37 -08:00
|
|
|
func (c *cLevelDBIterator) Valid() bool {
|
2017-12-12 19:19:26 -08:00
|
|
|
c.assertNoError()
|
2017-12-13 19:28:37 -08:00
|
|
|
if c.invalid {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
c.invalid = !c.itr.Valid()
|
|
|
|
return !c.invalid
|
2017-11-09 14:42:32 -08:00
|
|
|
}
|
|
|
|
|
2017-12-13 19:28:37 -08:00
|
|
|
func (c *cLevelDBIterator) Key() []byte {
|
|
|
|
if !c.Valid() {
|
2017-11-09 14:42:32 -08:00
|
|
|
panic("cLevelDBIterator Key() called when invalid")
|
|
|
|
}
|
|
|
|
return c.itr.Key()
|
|
|
|
}
|
|
|
|
|
2017-12-13 19:28:37 -08:00
|
|
|
func (c *cLevelDBIterator) Value() []byte {
|
|
|
|
if !c.Valid() {
|
2017-11-09 14:42:32 -08:00
|
|
|
panic("cLevelDBIterator Value() called when invalid")
|
|
|
|
}
|
|
|
|
return c.itr.Value()
|
|
|
|
}
|
|
|
|
|
2017-12-13 19:28:37 -08:00
|
|
|
func (c *cLevelDBIterator) Next() {
|
|
|
|
if !c.Valid() {
|
2017-11-09 14:42:32 -08:00
|
|
|
panic("cLevelDBIterator Next() called when invalid")
|
|
|
|
}
|
|
|
|
c.itr.Next()
|
2017-12-13 19:28:37 -08:00
|
|
|
c.checkEndKey() // if we've exceeded the range, we're now invalid
|
|
|
|
}
|
|
|
|
|
|
|
|
// levigo has no upper bound when iterating, so need to check ourselves
|
|
|
|
func (c *cLevelDBIterator) checkEndKey() []byte {
|
|
|
|
key := c.itr.Key()
|
|
|
|
if c.end != nil && bytes.Compare(key, c.end) > 0 {
|
|
|
|
c.invalid = true
|
|
|
|
}
|
|
|
|
return key
|
2017-11-09 14:42:32 -08:00
|
|
|
}
|
|
|
|
|
2017-12-13 19:28:37 -08:00
|
|
|
func (c *cLevelDBIterator) Release() {
|
2017-11-09 14:42:32 -08:00
|
|
|
c.itr.Close()
|
|
|
|
}
|
|
|
|
|
2017-12-13 19:28:37 -08:00
|
|
|
func (c *cLevelDBIterator) assertNoError() {
|
2017-12-12 19:19:26 -08:00
|
|
|
if err := c.itr.GetError(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-11-09 14:42:32 -08:00
|
|
|
}
|