2014-08-21 13:06:11 -07:00
|
|
|
package index
|
|
|
|
|
|
|
|
import (
|
2014-09-09 05:36:26 -07:00
|
|
|
"encoding"
|
|
|
|
|
2014-08-21 13:06:11 -07:00
|
|
|
"github.com/syndtr/goleveldb/leveldb"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/cache"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/filter"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/opt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// LevelDB is a LevelDB-backed sorted key-value store.
|
|
|
|
type LevelDB struct {
|
|
|
|
storage *leveldb.DB
|
|
|
|
readOpts *opt.ReadOptions
|
|
|
|
writeOpts *opt.WriteOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
type LevelDBOptions struct {
|
|
|
|
Path string
|
|
|
|
CacheSizeBytes int
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewLevelDB(o LevelDBOptions) (*LevelDB, error) {
|
|
|
|
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 {
|
|
|
|
return &batch{
|
|
|
|
batch: &leveldb.Batch{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *LevelDB) Close() error {
|
|
|
|
return l.storage.Close()
|
|
|
|
}
|
|
|
|
|
2014-09-09 05:36:26 -07: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 13:06:11 -07:00
|
|
|
if err == leveldb.ErrNotFound {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2014-09-09 05:36:26 -07:00
|
|
|
if value == nil {
|
2014-08-21 13:06:11 -07:00
|
|
|
return true, nil
|
|
|
|
}
|
2014-09-09 05:36:26 -07:00
|
|
|
return true, value.UnmarshalBinary(raw)
|
2014-08-21 13:06:11 -07:00
|
|
|
}
|
|
|
|
|
2014-09-09 05:36:26 -07:00
|
|
|
func (l *LevelDB) Has(key encoding.BinaryMarshaler) (has bool, err error) {
|
|
|
|
return l.Get(key, nil)
|
2014-08-21 13:06:11 -07:00
|
|
|
}
|
|
|
|
|
2014-09-09 05:36:26 -07: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 13:06:11 -07:00
|
|
|
}
|
|
|
|
|
2014-09-09 05:36:26 -07: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 13:06:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *LevelDB) Commit(b Batch) error {
|
|
|
|
return l.storage.Write(b.(*batch).batch, l.writeOpts)
|
|
|
|
}
|