2014-08-21 20:06:11 +00:00
|
|
|
package index
|
|
|
|
|
|
|
|
import (
|
2014-09-09 12:36:26 +00:00
|
|
|
"encoding"
|
|
|
|
|
2014-08-21 20:06:11 +00:00
|
|
|
"github.com/syndtr/goleveldb/leveldb"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/cache"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/filter"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/opt"
|
|
|
|
)
|
|
|
|
|
2014-09-10 16:41:52 +00:00
|
|
|
// LevelDB is a LevelDB-backed sorted KeyValueStore.
|
2014-08-21 20:06:11 +00:00
|
|
|
type LevelDB struct {
|
|
|
|
storage *leveldb.DB
|
|
|
|
readOpts *opt.ReadOptions
|
|
|
|
writeOpts *opt.WriteOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
type LevelDBOptions struct {
|
|
|
|
Path string
|
|
|
|
CacheSizeBytes int
|
|
|
|
}
|
|
|
|
|
2014-09-10 16:41:52 +00:00
|
|
|
func NewLevelDB(o LevelDBOptions) (KeyValueStore, error) {
|
2014-08-21 20:06:11 +00:00
|
|
|
options := &opt.Options{
|
|
|
|
Compression: opt.SnappyCompression,
|
|
|
|
BlockCache: cache.NewLRUCache(o.CacheSizeBytes),
|
|
|
|
Filter: filter.NewBloomFilter(10),
|
|
|
|
}
|
|
|
|
|
|
|
|
storage, err := leveldb.OpenFile(o.Path, options)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &LevelDB{
|
|
|
|
storage: storage,
|
|
|
|
readOpts: &opt.ReadOptions{},
|
|
|
|
writeOpts: &opt.WriteOptions{},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *LevelDB) NewBatch() Batch {
|
2014-09-10 16:41:52 +00:00
|
|
|
return &LevelDBBatch{
|
2014-08-21 20:06:11 +00:00
|
|
|
batch: &leveldb.Batch{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *LevelDB) Close() error {
|
|
|
|
return l.storage.Close()
|
|
|
|
}
|
|
|
|
|
2014-09-09 12:36:26 +00:00
|
|
|
func (l *LevelDB) Get(key encoding.BinaryMarshaler, value encoding.BinaryUnmarshaler) (bool, error) {
|
|
|
|
k, err := key.MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
raw, err := l.storage.Get(k, l.readOpts)
|
2014-08-21 20:06:11 +00:00
|
|
|
if err == leveldb.ErrNotFound {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2014-09-09 12:36:26 +00:00
|
|
|
if value == nil {
|
2014-08-21 20:06:11 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
2014-09-09 12:36:26 +00:00
|
|
|
return true, value.UnmarshalBinary(raw)
|
2014-08-21 20:06:11 +00:00
|
|
|
}
|
|
|
|
|
2014-09-09 12:36:26 +00:00
|
|
|
func (l *LevelDB) Has(key encoding.BinaryMarshaler) (has bool, err error) {
|
|
|
|
return l.Get(key, nil)
|
2014-08-21 20:06:11 +00:00
|
|
|
}
|
|
|
|
|
2014-09-09 12:36:26 +00:00
|
|
|
func (l *LevelDB) Delete(key encoding.BinaryMarshaler) error {
|
|
|
|
k, err := key.MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return l.storage.Delete(k, l.writeOpts)
|
2014-08-21 20:06:11 +00:00
|
|
|
}
|
|
|
|
|
2014-09-09 12:36:26 +00:00
|
|
|
func (l *LevelDB) Put(key, value encoding.BinaryMarshaler) error {
|
|
|
|
k, err := key.MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v, err := value.MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return l.storage.Put(k, v, l.writeOpts)
|
2014-08-21 20:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *LevelDB) Commit(b Batch) error {
|
2014-09-10 16:41:52 +00:00
|
|
|
return l.storage.Write(b.(*LevelDBBatch).batch, l.writeOpts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LevelDBBatch is a Batch implementation for LevelDB.
|
|
|
|
type LevelDBBatch struct {
|
|
|
|
batch *leveldb.Batch
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *LevelDBBatch) Put(key, value encoding.BinaryMarshaler) error {
|
|
|
|
k, err := key.MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v, err := value.MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b.batch.Put(k, v)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *LevelDBBatch) Delete(key encoding.BinaryMarshaler) error {
|
|
|
|
k, err := key.MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b.batch.Delete(k)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *LevelDBBatch) Reset() {
|
|
|
|
b.batch.Reset()
|
2014-08-21 20:06:11 +00:00
|
|
|
}
|