forked from tendermint/tm-db
-
Notifications
You must be signed in to change notification settings - Fork 8
/
memdb.go
208 lines (175 loc) · 4.92 KB
/
memdb.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package db
import (
"bytes"
"fmt"
"sync"
"github.com/google/btree"
)
const (
// The approximate number of items and children per B-tree node. Tuned with benchmarks.
bTreeDegree = 32
)
func init() {
registerDBCreator(MemDBBackend, func(name, dir string) (DB, error) {
return NewMemDB(), nil
}, false)
}
// item is a btree.Item with byte slices as keys and values
type item struct {
key []byte
value []byte
}
// Less implements btree.Item.
func (i item) Less(other btree.Item) bool {
// this considers nil == []byte{}, but that's ok since we handle nil endpoints
// in iterators specially anyway
return bytes.Compare(i.key, other.(item).key) == -1
}
// newKey creates a new key item.
func newKey(key []byte) item {
return item{key: key}
}
// newPair creates a new pair item.
func newPair(key, value []byte) item {
return item{key: key, value: value}
}
// MemDB is an in-memory database backend using a B-tree for storage.
//
// For performance reasons, all given and returned keys and values are pointers to the in-memory
// database, so modifying them will cause the stored values to be modified as well. All DB methods
// already specify that keys and values should be considered read-only, but this is especially
// important with MemDB.
type MemDB struct {
mtx sync.RWMutex
btree *btree.BTree
}
var _ DB = (*MemDB)(nil)
// NewMemDB creates a new in-memory database.
func NewMemDB() *MemDB {
database := &MemDB{
btree: btree.New(bTreeDegree),
}
return database
}
// Get implements DB.
func (db *MemDB) Get(key []byte) ([]byte, error) {
if len(key) == 0 {
return nil, errKeyEmpty
}
db.mtx.RLock()
defer db.mtx.RUnlock()
i := db.btree.Get(newKey(key))
if i != nil {
return i.(item).value, nil
}
return nil, nil
}
// Has implements DB.
func (db *MemDB) Has(key []byte) (bool, error) {
if len(key) == 0 {
return false, errKeyEmpty
}
db.mtx.RLock()
defer db.mtx.RUnlock()
return db.btree.Has(newKey(key)), nil
}
// Set implements DB.
func (db *MemDB) Set(key []byte, value []byte) error {
if len(key) == 0 {
return errKeyEmpty
}
if value == nil {
return errValueNil
}
db.mtx.Lock()
defer db.mtx.Unlock()
db.set(key, value)
return nil
}
// set sets a value without locking the mutex.
func (db *MemDB) set(key []byte, value []byte) {
db.btree.ReplaceOrInsert(newPair(key, value))
}
// SetSync implements DB.
func (db *MemDB) SetSync(key []byte, value []byte) error {
return db.Set(key, value)
}
// Delete implements DB.
func (db *MemDB) Delete(key []byte) error {
if len(key) == 0 {
return errKeyEmpty
}
db.mtx.Lock()
defer db.mtx.Unlock()
db.delete(key)
return nil
}
// delete deletes a key without locking the mutex.
func (db *MemDB) delete(key []byte) {
db.btree.Delete(newKey(key))
}
// DeleteSync implements DB.
func (db *MemDB) DeleteSync(key []byte) error {
return db.Delete(key)
}
// Close implements DB.
func (db *MemDB) Close() error {
// Close is a noop since for an in-memory database, we don't have a destination to flush
// contents to nor do we want any data loss on invoking Close().
// See the discussion in https://github.com/tendermint/tendermint/libs/pull/56
return nil
}
// Print implements DB.
func (db *MemDB) Print() error {
db.mtx.RLock()
defer db.mtx.RUnlock()
db.btree.Ascend(func(i btree.Item) bool {
item := i.(item)
fmt.Printf("[%X]:\t[%X]\n", item.key, item.value)
return true
})
return nil
}
// Stats implements DB.
func (db *MemDB) Stats() map[string]string {
db.mtx.RLock()
defer db.mtx.RUnlock()
stats := make(map[string]string)
stats["database.type"] = "memDB"
stats["database.size"] = fmt.Sprintf("%d", db.btree.Len())
return stats
}
// NewBatch implements DB.
func (db *MemDB) NewBatch() Batch {
return newMemDBBatch(db)
}
// Iterator implements DB.
// Takes out a read-lock on the database until the iterator is closed.
func (db *MemDB) Iterator(start, end []byte) (Iterator, error) {
if (start != nil && len(start) == 0) || (end != nil && len(end) == 0) {
return nil, errKeyEmpty
}
return newMemDBIterator(db, start, end, false), nil
}
// ReverseIterator implements DB.
// Takes out a read-lock on the database until the iterator is closed.
func (db *MemDB) ReverseIterator(start, end []byte) (Iterator, error) {
if (start != nil && len(start) == 0) || (end != nil && len(end) == 0) {
return nil, errKeyEmpty
}
return newMemDBIterator(db, start, end, true), nil
}
// IteratorNoMtx makes an iterator with no mutex.
func (db *MemDB) IteratorNoMtx(start, end []byte) (Iterator, error) {
if (start != nil && len(start) == 0) || (end != nil && len(end) == 0) {
return nil, errKeyEmpty
}
return newMemDBIteratorMtxChoice(db, start, end, false, false), nil
}
// ReverseIteratorNoMtx makes an iterator with no mutex.
func (db *MemDB) ReverseIteratorNoMtx(start, end []byte) (Iterator, error) {
if (start != nil && len(start) == 0) || (end != nil && len(end) == 0) {
return nil, errKeyEmpty
}
return newMemDBIteratorMtxChoice(db, start, end, true, false), nil
}