-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
282 lines (237 loc) · 8.12 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package leveldb
import (
"bytes"
"sync/atomic"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/errors"
"github.com/syndtr/goleveldb/leveldb/filter"
"github.com/syndtr/goleveldb/leveldb/iterator"
"github.com/syndtr/goleveldb/leveldb/opt"
"github.com/syndtr/goleveldb/leveldb/util"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/utils"
"github.com/ava-labs/avalanchego/utils/logging"
)
const (
// Name is the name of this database for database switches
Name = "leveldb"
// BlockCacheSize is the number of bytes to use for block caching in
// leveldb.
BlockCacheSize = 12 * opt.MiB
// WriteBufferSize is the number of bytes to use for buffers in leveldb.
WriteBufferSize = 12 * opt.MiB
// HandleCap is the number of files descriptors to cap levelDB to use.
HandleCap = 64
// BitsPerKey is the number of bits to add to the bloom filter per key.
BitsPerKey = 10
// levelDBByteOverhead is the number of bytes of constant overhead that
// should be added to a batch size per operation.
levelDBByteOverhead = 8
)
var (
_ database.Database = &Database{}
_ database.Batch = &batch{}
_ database.Iterator = &iter{}
)
// Database is a persistent key-value store. Apart from basic data storage
// functionality it also supports batch writes and iterating over the keyspace
// in binary-alphabetical order.
type Database struct {
*leveldb.DB
log logging.Logger
// 1 if there was previously an error other than "not found" or "closed"
// while performing a db operation. If [errored] == 1, Has, Get, Put,
// Delete and batch writes fail with ErrAvoidCorruption.
errored uint64
}
// New returns a wrapped LevelDB object.
func New(file string, log logging.Logger) (database.Database, error) {
// Open the db and recover any potential corruptions
db, err := leveldb.OpenFile(file, &opt.Options{
OpenFilesCacheCapacity: HandleCap,
BlockCacheCapacity: BlockCacheSize,
// There are two buffers of size WriteBuffer used.
WriteBuffer: WriteBufferSize / 2,
Filter: filter.NewBloomFilter(BitsPerKey),
})
if _, corrupted := err.(*errors.ErrCorrupted); corrupted {
db, err = leveldb.RecoverFile(file, nil)
}
if err != nil {
return nil, err
}
return &Database{
DB: db,
log: log,
}, nil
}
// Has returns if the key is set in the database
func (db *Database) Has(key []byte) (bool, error) {
if db.corrupted() {
return false, database.ErrAvoidCorruption
}
has, err := db.DB.Has(key, nil)
return has, db.handleError(err)
}
// Get returns the value the key maps to in the database
func (db *Database) Get(key []byte) ([]byte, error) {
if db.corrupted() {
return nil, database.ErrAvoidCorruption
}
value, err := db.DB.Get(key, nil)
return value, db.handleError(err)
}
// Put sets the value of the provided key to the provided value
func (db *Database) Put(key []byte, value []byte) error {
if db.corrupted() {
return database.ErrAvoidCorruption
}
return db.handleError(db.DB.Put(key, value, nil))
}
// Delete removes the key from the database
func (db *Database) Delete(key []byte) error {
if db.corrupted() {
return database.ErrAvoidCorruption
}
return db.handleError(db.DB.Delete(key, nil))
}
// NewBatch creates a write/delete-only buffer that is atomically committed to
// the database when write is called
func (db *Database) NewBatch() database.Batch { return &batch{db: db} }
// NewIterator creates a lexicographically ordered iterator over the database
func (db *Database) NewIterator() database.Iterator {
return &iter{db.DB.NewIterator(new(util.Range), nil)}
}
// NewIteratorWithStart creates a lexicographically ordered iterator over the
// database starting at the provided key
func (db *Database) NewIteratorWithStart(start []byte) database.Iterator {
return &iter{db.DB.NewIterator(&util.Range{Start: start}, nil)}
}
// NewIteratorWithPrefix creates a lexicographically ordered iterator over the
// database ignoring keys that do not start with the provided prefix
func (db *Database) NewIteratorWithPrefix(prefix []byte) database.Iterator {
return &iter{db.DB.NewIterator(util.BytesPrefix(prefix), nil)}
}
// NewIteratorWithStartAndPrefix creates a lexicographically ordered iterator
// over the database starting at start and ignoring keys that do not start with
// the provided prefix
func (db *Database) NewIteratorWithStartAndPrefix(start, prefix []byte) database.Iterator {
iterRange := util.BytesPrefix(prefix)
if bytes.Compare(start, prefix) == 1 {
iterRange.Start = start
}
return &iter{db.DB.NewIterator(iterRange, nil)}
}
// Stat returns a particular internal stat of the database.
func (db *Database) Stat(property string) (string, error) {
stat, err := db.DB.GetProperty(property)
return stat, db.handleError(err)
}
// This comment is basically copy pasted from the underlying levelDB library:
// Compact the underlying DB for the given key range.
// Specifically, deleted and overwritten versions are discarded,
// and the data is rearranged to reduce the cost of operations
// needed to access the data. This operation should typically only
// be invoked by users who understand the underlying implementation.
//
// A nil start is treated as a key before all keys in the DB.
// And a nil limit is treated as a key after all keys in the DB.
// Therefore if both are nil then it will compact entire DB.
func (db *Database) Compact(start []byte, limit []byte) error {
return db.handleError(db.DB.CompactRange(util.Range{Start: start, Limit: limit}))
}
// Close implements the Database interface
func (db *Database) Close() error { return db.handleError(db.DB.Close()) }
func (db *Database) corrupted() bool {
return atomic.LoadUint64(&db.errored) == 1
}
func (db *Database) handleError(err error) error {
err = updateError(err)
switch err {
case nil, database.ErrNotFound, database.ErrClosed:
// If we get an error other than "not found" or "closed", disallow future
// database operations to avoid possible corruption
default:
db.log.Fatal("leveldb error: %s", err)
atomic.StoreUint64(&db.errored, 1)
}
return err
}
// batch is a wrapper around a levelDB batch to contain sizes.
type batch struct {
leveldb.Batch
db *Database
size int
}
// Put the value into the batch for later writing
func (b *batch) Put(key, value []byte) error {
b.Batch.Put(key, value)
b.size += len(key) + len(value) + levelDBByteOverhead
return nil
}
// Delete the key during writing
func (b *batch) Delete(key []byte) error {
b.Batch.Delete(key)
b.size += len(key) + levelDBByteOverhead
return nil
}
// Size retrieves the amount of data queued up for writing.
func (b *batch) Size() int { return b.size }
// Write flushes any accumulated data to disk.
func (b *batch) Write() error {
if b.db.corrupted() {
return database.ErrAvoidCorruption
}
return b.db.handleError(b.db.DB.Write(&b.Batch, nil))
}
// Reset resets the batch for reuse.
func (b *batch) Reset() {
b.Batch.Reset()
b.size = 0
}
// Replay the batch contents.
func (b *batch) Replay(w database.KeyValueWriter) error {
replay := &replayer{writer: w}
if err := b.Batch.Replay(replay); err != nil {
// Never actually returns an error, because Replay just returns nil
return err
}
return replay.err
}
// Inner returns itself
func (b *batch) Inner() database.Batch { return b }
type replayer struct {
writer database.KeyValueWriter
err error
}
func (r *replayer) Put(key, value []byte) {
if r.err != nil {
return
}
r.err = r.writer.Put(key, value)
}
func (r *replayer) Delete(key []byte) {
if r.err != nil {
return
}
r.err = r.writer.Delete(key)
}
type iter struct{ iterator.Iterator }
// Error implements the Iterator interface
func (it *iter) Error() error { return updateError(it.Iterator.Error()) }
// Key implements the Iterator interface
func (it *iter) Key() []byte { return utils.CopyBytes(it.Iterator.Key()) }
// Value implements the Iterator interface
func (it *iter) Value() []byte { return utils.CopyBytes(it.Iterator.Value()) }
func updateError(err error) error {
switch err {
case leveldb.ErrClosed:
return database.ErrClosed
case leveldb.ErrNotFound:
return database.ErrNotFound
default:
return err
}
}