2014-09-19 09:18:44 -07:00
|
|
|
// Copyright 2014 Prometheus Team
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
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"
|
2014-09-18 08:14:14 -07:00
|
|
|
leveldb_cache "github.com/syndtr/goleveldb/leveldb/cache"
|
|
|
|
leveldb_filter "github.com/syndtr/goleveldb/leveldb/filter"
|
|
|
|
leveldb_iterator "github.com/syndtr/goleveldb/leveldb/iterator"
|
|
|
|
leveldb_opt "github.com/syndtr/goleveldb/leveldb/opt"
|
|
|
|
leveldb_util "github.com/syndtr/goleveldb/leveldb/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
keyspace = &leveldb_util.Range{
|
|
|
|
Start: nil,
|
|
|
|
Limit: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
iteratorOpts = &leveldb_opt.ReadOptions{
|
|
|
|
DontFillCache: true,
|
|
|
|
}
|
2014-08-21 13:06:11 -07:00
|
|
|
)
|
|
|
|
|
2014-09-10 09:41:52 -07:00
|
|
|
// LevelDB is a LevelDB-backed sorted KeyValueStore.
|
2014-08-21 13:06:11 -07:00
|
|
|
type LevelDB struct {
|
|
|
|
storage *leveldb.DB
|
2014-09-18 08:14:14 -07:00
|
|
|
readOpts *leveldb_opt.ReadOptions
|
|
|
|
writeOpts *leveldb_opt.WriteOptions
|
2014-08-21 13:06:11 -07:00
|
|
|
}
|
|
|
|
|
2014-09-16 06:47:24 -07:00
|
|
|
// LevelDBOptions provides options for a LevelDB.
|
2014-08-21 13:06:11 -07:00
|
|
|
type LevelDBOptions struct {
|
2014-09-16 06:47:24 -07:00
|
|
|
Path string // Base path to store files.
|
2014-08-21 13:06:11 -07:00
|
|
|
CacheSizeBytes int
|
|
|
|
}
|
|
|
|
|
2014-09-16 06:47:24 -07:00
|
|
|
// NewLevelDB returns a newly allocated LevelDB-backed KeyValueStore ready to
|
|
|
|
// use.
|
2014-09-10 09:41:52 -07:00
|
|
|
func NewLevelDB(o LevelDBOptions) (KeyValueStore, error) {
|
2014-09-18 08:14:14 -07:00
|
|
|
options := &leveldb_opt.Options{
|
|
|
|
Compression: leveldb_opt.SnappyCompression,
|
|
|
|
BlockCache: leveldb_cache.NewLRUCache(o.CacheSizeBytes),
|
|
|
|
Filter: leveldb_filter.NewBloomFilter(10),
|
2014-08-21 13:06:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
storage, err := leveldb.OpenFile(o.Path, options)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &LevelDB{
|
|
|
|
storage: storage,
|
2014-09-18 08:14:14 -07:00
|
|
|
readOpts: &leveldb_opt.ReadOptions{},
|
|
|
|
writeOpts: &leveldb_opt.WriteOptions{},
|
2014-08-21 13:06:11 -07:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2014-09-16 06:47:24 -07:00
|
|
|
// NewBatch implements KeyValueStore.
|
2014-08-21 13:06:11 -07:00
|
|
|
func (l *LevelDB) NewBatch() Batch {
|
2014-09-10 09:41:52 -07:00
|
|
|
return &LevelDBBatch{
|
2014-08-21 13:06:11 -07:00
|
|
|
batch: &leveldb.Batch{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-16 06:47:24 -07:00
|
|
|
// Close implements KeyValueStore.
|
2014-08-21 13:06:11 -07:00
|
|
|
func (l *LevelDB) Close() error {
|
|
|
|
return l.storage.Close()
|
|
|
|
}
|
|
|
|
|
2014-09-16 06:47:24 -07:00
|
|
|
// Get implements KeyValueStore.
|
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 {
|
2014-09-14 06:33:56 -07:00
|
|
|
return false, err
|
2014-09-09 05:36:26 -07:00
|
|
|
}
|
|
|
|
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-16 06:47:24 -07:00
|
|
|
// Has implements KeyValueStore.
|
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-16 06:47:24 -07:00
|
|
|
// Delete implements KeyValueStore.
|
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-16 06:47:24 -07:00
|
|
|
// Put implements KeyValueStore.
|
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
|
|
|
}
|
|
|
|
|
2014-09-16 06:47:24 -07:00
|
|
|
// Commit implements KeyValueStore.
|
2014-08-21 13:06:11 -07:00
|
|
|
func (l *LevelDB) Commit(b Batch) error {
|
2014-09-10 09:41:52 -07:00
|
|
|
return l.storage.Write(b.(*LevelDBBatch).batch, l.writeOpts)
|
|
|
|
}
|
|
|
|
|
2014-09-18 08:14:14 -07:00
|
|
|
func (l *LevelDB) ForEach(cb func(kv KeyValueAccessor) error) error {
|
|
|
|
it, err := l.NewIterator(true)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer it.Close()
|
|
|
|
|
|
|
|
for valid := it.SeekToFirst(); valid; valid = it.Next() {
|
|
|
|
if err = it.Error(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := cb(it); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-09-10 09:41:52 -07:00
|
|
|
// LevelDBBatch is a Batch implementation for LevelDB.
|
|
|
|
type LevelDBBatch struct {
|
|
|
|
batch *leveldb.Batch
|
|
|
|
}
|
|
|
|
|
2014-09-16 06:47:24 -07:00
|
|
|
// Put implements Batch.
|
2014-09-10 09:41:52 -07:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-09-16 06:47:24 -07:00
|
|
|
// Delete implements Batch.
|
2014-09-10 09:41:52 -07:00
|
|
|
func (b *LevelDBBatch) Delete(key encoding.BinaryMarshaler) error {
|
|
|
|
k, err := key.MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b.batch.Delete(k)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-09-16 06:47:24 -07:00
|
|
|
// Reset implements Batch.
|
2014-09-10 09:41:52 -07:00
|
|
|
func (b *LevelDBBatch) Reset() {
|
|
|
|
b.batch.Reset()
|
2014-08-21 13:06:11 -07:00
|
|
|
}
|
2014-09-18 08:14:14 -07:00
|
|
|
|
|
|
|
// levelDBIterator implements Iterator.
|
|
|
|
type levelDBIterator struct {
|
|
|
|
it leveldb_iterator.Iterator
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *levelDBIterator) Error() error {
|
|
|
|
return i.it.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *levelDBIterator) Valid() bool {
|
|
|
|
return i.it.Valid()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *levelDBIterator) SeekToFirst() bool {
|
|
|
|
return i.it.First()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *levelDBIterator) SeekToLast() bool {
|
|
|
|
return i.it.Last()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *levelDBIterator) Seek(k encoding.BinaryMarshaler) bool {
|
|
|
|
key, err := k.MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return i.it.Seek(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *levelDBIterator) Next() bool {
|
|
|
|
return i.it.Next()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *levelDBIterator) Previous() bool {
|
|
|
|
return i.it.Prev()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *levelDBIterator) Key(key encoding.BinaryUnmarshaler) error {
|
|
|
|
return key.UnmarshalBinary(i.it.Key())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *levelDBIterator) Value(value encoding.BinaryUnmarshaler) error {
|
|
|
|
return value.UnmarshalBinary(i.it.Value())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*levelDBIterator) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type snapshottedIterator struct {
|
|
|
|
levelDBIterator
|
|
|
|
snap *leveldb.Snapshot
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *snapshottedIterator) Close() error {
|
|
|
|
i.snap.Release()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// newIterator creates a new LevelDB iterator which is optionally based on a
|
|
|
|
// snapshot of the current DB state.
|
|
|
|
//
|
|
|
|
// For each of the iterator methods that have a return signature of (ok bool),
|
|
|
|
// if ok == false, the iterator may not be used any further and must be closed.
|
|
|
|
// Further work with the database requires the creation of a new iterator.
|
|
|
|
func (l *LevelDB) NewIterator(snapshotted bool) (Iterator, error) {
|
|
|
|
if !snapshotted {
|
|
|
|
return &levelDBIterator{
|
|
|
|
it: l.storage.NewIterator(keyspace, iteratorOpts),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
snap, err := l.storage.GetSnapshot()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &snapshottedIterator{
|
|
|
|
levelDBIterator: levelDBIterator{
|
|
|
|
it: snap.NewIterator(keyspace, iteratorOpts),
|
|
|
|
},
|
|
|
|
snap: snap,
|
|
|
|
}, nil
|
|
|
|
}
|