-
Notifications
You must be signed in to change notification settings - Fork 246
/
storage.go
75 lines (63 loc) · 1.94 KB
/
storage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package db
import (
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/iterator"
"github.com/syndtr/goleveldb/leveldb/storage"
"github.com/syndtr/goleveldb/leveldb/util"
)
// Storage is an interface for common db operations.
type Storage interface {
Put([]byte, []byte) error
Delete([]byte) error
Get([]byte) ([]byte, error)
NewIterator(*util.Range) iterator.Iterator
}
// CommitStorage allows to write all tx/batched values atomically.
type CommitStorage interface {
Storage
Commit() error
}
// TransactionalStorage adds transaction features on top of regular storage.
type TransactionalStorage interface {
Storage
NewTx() CommitStorage
}
// NewMemoryLevelDBStorage returns LevelDBStorage instance with in memory leveldb backend.
func NewMemoryLevelDBStorage() (LevelDBStorage, error) {
mdb, err := leveldb.Open(storage.NewMemStorage(), nil)
if err != nil {
return LevelDBStorage{}, err
}
return NewLevelDBStorage(mdb), nil
}
// NewLevelDBStorage creates new LevelDBStorage instance.
func NewLevelDBStorage(db *leveldb.DB) LevelDBStorage {
return LevelDBStorage{db: db}
}
// LevelDBStorage wrapper around leveldb.DB.
type LevelDBStorage struct {
db *leveldb.DB
}
// Put upserts given key/value pair.
func (db LevelDBStorage) Put(key, buf []byte) error {
return db.db.Put(key, buf, nil)
}
// Delete removes given key from database..
func (db LevelDBStorage) Delete(key []byte) error {
return db.db.Delete(key, nil)
}
// Get returns value for a given key.
func (db LevelDBStorage) Get(key []byte) ([]byte, error) {
return db.db.Get(key, nil)
}
// NewIterator returns new leveldb iterator.Iterator instance for a given range.
func (db LevelDBStorage) NewIterator(slice *util.Range) iterator.Iterator {
return db.db.NewIterator(slice, nil)
}
// NewTx is a wrapper around leveldb.Batch that allows to write atomically.
func (db LevelDBStorage) NewTx() CommitStorage {
return LevelDBTx{
batch: &leveldb.Batch{},
db: db,
}
}