-
Notifications
You must be signed in to change notification settings - Fork 666
/
db.go
126 lines (105 loc) · 3.08 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
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package corruptabledb
import (
"fmt"
"sync"
"github.com/ava-labs/avalanchego/database"
)
var (
_ database.Database = &Database{}
_ database.Batch = &batch{}
)
// 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() (interface{}, error) {
if err := db.corrupted(); err != nil {
return nil, err
}
return db.Database.HealthCheck()
}
func (db *Database) NewBatch() database.Batch {
return &batch{
Batch: db.Database.NewBatch(),
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())
}