-
Notifications
You must be signed in to change notification settings - Fork 664
/
db.go
256 lines (211 loc) · 4.86 KB
/
db.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package memdb
import (
"context"
"strings"
"sync"
"golang.org/x/exp/slices"
"github.com/ava-labs/avalanchego/database"
)
const (
// Name is the name of this database for database switches
Name = "memdb"
// DefaultSize is the default initial size of the memory database
DefaultSize = 1024
)
var (
_ database.Database = (*Database)(nil)
_ database.Batch = (*batch)(nil)
_ database.Iterator = (*iterator)(nil)
)
// Database is an ephemeral key-value store that implements the Database
// interface.
type Database struct {
lock sync.RWMutex
db map[string][]byte
}
// New returns a map with the Database interface methods implemented.
func New() *Database {
return NewWithSize(DefaultSize)
}
// NewWithSize returns a map pre-allocated to the provided size with the
// Database interface methods implemented.
func NewWithSize(size int) *Database {
return &Database{db: make(map[string][]byte, size)}
}
func (db *Database) Close() error {
db.lock.Lock()
defer db.lock.Unlock()
if db.db == nil {
return database.ErrClosed
}
db.db = nil
return nil
}
func (db *Database) isClosed() bool {
db.lock.RLock()
defer db.lock.RUnlock()
return db.db == nil
}
func (db *Database) Has(key []byte) (bool, error) {
db.lock.RLock()
defer db.lock.RUnlock()
if db.db == nil {
return false, database.ErrClosed
}
_, ok := db.db[string(key)]
return ok, nil
}
func (db *Database) Get(key []byte) ([]byte, error) {
db.lock.RLock()
defer db.lock.RUnlock()
if db.db == nil {
return nil, database.ErrClosed
}
if entry, ok := db.db[string(key)]; ok {
return slices.Clone(entry), nil
}
return nil, database.ErrNotFound
}
func (db *Database) Put(key []byte, value []byte) error {
db.lock.Lock()
defer db.lock.Unlock()
if db.db == nil {
return database.ErrClosed
}
db.db[string(key)] = slices.Clone(value)
return nil
}
func (db *Database) Delete(key []byte) error {
db.lock.Lock()
defer db.lock.Unlock()
if db.db == nil {
return database.ErrClosed
}
delete(db.db, string(key))
return nil
}
func (db *Database) NewBatch() database.Batch {
return &batch{db: db}
}
func (db *Database) NewIterator() database.Iterator {
return db.NewIteratorWithStartAndPrefix(nil, nil)
}
func (db *Database) NewIteratorWithStart(start []byte) database.Iterator {
return db.NewIteratorWithStartAndPrefix(start, nil)
}
func (db *Database) NewIteratorWithPrefix(prefix []byte) database.Iterator {
return db.NewIteratorWithStartAndPrefix(nil, prefix)
}
func (db *Database) NewIteratorWithStartAndPrefix(start, prefix []byte) database.Iterator {
db.lock.RLock()
defer db.lock.RUnlock()
if db.db == nil {
return &database.IteratorError{
Err: database.ErrClosed,
}
}
startString := string(start)
prefixString := string(prefix)
keys := make([]string, 0, len(db.db))
for key := range db.db {
if strings.HasPrefix(key, prefixString) && key >= startString {
keys = append(keys, key)
}
}
slices.Sort(keys) // Keys need to be in sorted order
values := make([][]byte, 0, len(keys))
for _, key := range keys {
values = append(values, db.db[key])
}
return &iterator{
db: db,
keys: keys,
values: values,
}
}
func (db *Database) Compact(_, _ []byte) error {
db.lock.RLock()
defer db.lock.RUnlock()
if db.db == nil {
return database.ErrClosed
}
return nil
}
func (db *Database) HealthCheck(context.Context) (interface{}, error) {
if db.isClosed() {
return nil, database.ErrClosed
}
return nil, nil
}
type batch struct {
database.BatchOps
db *Database
}
func (b *batch) Write() error {
b.db.lock.Lock()
defer b.db.lock.Unlock()
if b.db.db == nil {
return database.ErrClosed
}
for _, op := range b.Ops {
if op.Delete {
delete(b.db.db, string(op.Key))
} else {
b.db.db[string(op.Key)] = op.Value
}
}
return nil
}
func (b *batch) Inner() database.Batch {
return b
}
type iterator struct {
db *Database
initialized bool
keys []string
values [][]byte
err error
}
func (it *iterator) Next() bool {
// Short-circuit and set an error if the underlying database has been closed.
if it.db.isClosed() {
it.keys = nil
it.values = nil
it.err = database.ErrClosed
return false
}
// If the iterator was not yet initialized, do it now
if !it.initialized {
it.initialized = true
return len(it.keys) > 0
}
// Iterator already initialize, advance it
if len(it.keys) > 0 {
it.keys[0] = ""
it.keys = it.keys[1:]
it.values[0] = nil
it.values = it.values[1:]
}
return len(it.keys) > 0
}
func (it *iterator) Error() error {
return it.err
}
func (it *iterator) Key() []byte {
if len(it.keys) > 0 {
return []byte(it.keys[0])
}
return nil
}
func (it *iterator) Value() []byte {
if len(it.values) > 0 {
return it.values[0]
}
return nil
}
func (it *iterator) Release() {
it.keys = nil
it.values = nil
}