-
Notifications
You must be signed in to change notification settings - Fork 671
/
db.go
178 lines (150 loc) · 4.17 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
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package corruptabledb
import (
"context"
"fmt"
"sync"
"github.com/ava-labs/avalanchego/database"
)
var (
_ database.Database = (*Database)(nil)
_ database.Batch = (*batch)(nil)
)
// CorruptableDB is a wrapper around Database
// it prevents any future calls in case of a corruption occurs
type Database struct {
database.Database
// initialError stores the error other than "not found" or "closed" while
// performing a db operation. If not nil, Has, Get, Put, Delete and batch
// writes will fail with initialError.
errorLock sync.RWMutex
initialError error
}
// New returns a new prefixed database
func New(db database.Database) *Database {
return &Database{Database: db}
}
// Has returns if the key is set in the database
func (db *Database) Has(key []byte) (bool, error) {
if err := db.corrupted(); err != nil {
return false, err
}
has, err := db.Database.Has(key)
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 err := db.corrupted(); err != nil {
return nil, err
}
value, err := db.Database.Get(key)
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 err := db.corrupted(); err != nil {
return err
}
return db.handleError(db.Database.Put(key, value))
}
// Delete removes the key from the database
func (db *Database) Delete(key []byte) error {
if err := db.corrupted(); err != nil {
return err
}
return db.handleError(db.Database.Delete(key))
}
func (db *Database) Compact(start []byte, limit []byte) error {
return db.handleError(db.Database.Compact(start, limit))
}
func (db *Database) Close() error {
return db.handleError(db.Database.Close())
}
func (db *Database) HealthCheck(ctx context.Context) (interface{}, error) {
if err := db.corrupted(); err != nil {
return nil, err
}
return db.Database.HealthCheck(ctx)
}
func (db *Database) NewBatch() database.Batch {
return &batch{
Batch: db.Database.NewBatch(),
db: db,
}
}
func (db *Database) NewIterator() database.Iterator {
return &iterator{
Iterator: db.Database.NewIterator(),
db: db,
}
}
func (db *Database) NewIteratorWithStart(start []byte) database.Iterator {
return &iterator{
Iterator: db.Database.NewIteratorWithStart(start),
db: db,
}
}
func (db *Database) NewIteratorWithPrefix(prefix []byte) database.Iterator {
return &iterator{
Iterator: db.Database.NewIteratorWithPrefix(prefix),
db: db,
}
}
func (db *Database) NewIteratorWithStartAndPrefix(start, prefix []byte) database.Iterator {
return &iterator{
Iterator: db.Database.NewIteratorWithStartAndPrefix(start, prefix),
db: db,
}
}
func (db *Database) corrupted() error {
db.errorLock.RLock()
defer db.errorLock.RUnlock()
return db.initialError
}
func (db *Database) handleError(err error) error {
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.errorLock.Lock()
defer db.errorLock.Unlock()
// Set the initial error to the first unexpected error. Don't call
// corrupted() here since it would deadlock.
if db.initialError == nil {
db.initialError = fmt.Errorf("closed to avoid possible corruption, init error: %w", err)
}
}
return err
}
// batch is a wrapper around the batch to contain sizes.
type batch struct {
database.Batch
db *Database
}
// Write flushes any accumulated data to disk.
func (b *batch) Write() error {
if err := b.db.corrupted(); err != nil {
return err
}
return b.db.handleError(b.Batch.Write())
}
type iterator struct {
database.Iterator
db *Database
}
func (it *iterator) Next() bool {
if err := it.db.corrupted(); err != nil {
return false
}
val := it.Iterator.Next()
_ = it.db.handleError(it.Iterator.Error())
return val
}
func (it *iterator) Error() error {
if err := it.db.corrupted(); err != nil {
return err
}
return it.db.handleError(it.Iterator.Error())
}