forked from cometbft/cometbft-db
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prefixdb.go
204 lines (173 loc) · 4.01 KB
/
prefixdb.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
package db
import (
"fmt"
"sync"
)
// PrefixDB wraps a namespace of another database as a logical database.
type PrefixDB struct {
mtx sync.Mutex
prefix []byte
db DB
}
var _ DB = (*PrefixDB)(nil)
// NewPrefixDB lets you namespace multiple DBs within a single DB.
func NewPrefixDB(db DB, prefix []byte) *PrefixDB {
return &PrefixDB{
prefix: prefix,
db: db,
}
}
// Get implements DB.
func (pdb *PrefixDB) Get(key []byte) ([]byte, error) {
if len(key) == 0 {
return nil, errKeyEmpty
}
pdb.mtx.Lock()
defer pdb.mtx.Unlock()
pkey := pdb.prefixed(key)
value, err := pdb.db.Get(pkey)
if err != nil {
return nil, err
}
return value, nil
}
// Has implements DB.
func (pdb *PrefixDB) Has(key []byte) (bool, error) {
if len(key) == 0 {
return false, errKeyEmpty
}
pdb.mtx.Lock()
defer pdb.mtx.Unlock()
ok, err := pdb.db.Has(pdb.prefixed(key))
if err != nil {
return ok, err
}
return ok, nil
}
// Set implements DB.
func (pdb *PrefixDB) Set(key []byte, value []byte) error {
if len(key) == 0 {
return errKeyEmpty
}
if value == nil {
return errValueNil
}
pdb.mtx.Lock()
defer pdb.mtx.Unlock()
pkey := pdb.prefixed(key)
if err := pdb.db.Set(pkey, value); err != nil {
return err
}
return nil
}
// SetSync implements DB.
func (pdb *PrefixDB) SetSync(key []byte, value []byte) error {
if len(key) == 0 {
return errKeyEmpty
}
if value == nil {
return errValueNil
}
pdb.mtx.Lock()
defer pdb.mtx.Unlock()
return pdb.db.SetSync(pdb.prefixed(key), value)
}
// Delete implements DB.
func (pdb *PrefixDB) Delete(key []byte) error {
if len(key) == 0 {
return errKeyEmpty
}
pdb.mtx.Lock()
defer pdb.mtx.Unlock()
return pdb.db.Delete(pdb.prefixed(key))
}
// DeleteSync implements DB.
func (pdb *PrefixDB) DeleteSync(key []byte) error {
if len(key) == 0 {
return errKeyEmpty
}
pdb.mtx.Lock()
defer pdb.mtx.Unlock()
return pdb.db.DeleteSync(pdb.prefixed(key))
}
// Iterator implements DB.
func (pdb *PrefixDB) Iterator(start, end []byte) (Iterator, error) {
if (start != nil && len(start) == 0) || (end != nil && len(end) == 0) {
return nil, errKeyEmpty
}
pdb.mtx.Lock()
defer pdb.mtx.Unlock()
var pstart, pend []byte
pstart = append(cp(pdb.prefix), start...)
if end == nil {
pend = cpIncr(pdb.prefix)
} else {
pend = append(cp(pdb.prefix), end...)
}
itr, err := pdb.db.Iterator(pstart, pend)
if err != nil {
return nil, err
}
return newPrefixIterator(pdb.prefix, start, end, itr)
}
// ReverseIterator implements DB.
func (pdb *PrefixDB) ReverseIterator(start, end []byte) (Iterator, error) {
if (start != nil && len(start) == 0) || (end != nil && len(end) == 0) {
return nil, errKeyEmpty
}
pdb.mtx.Lock()
defer pdb.mtx.Unlock()
var pstart, pend []byte
pstart = append(cp(pdb.prefix), start...)
if end == nil {
pend = cpIncr(pdb.prefix)
} else {
pend = append(cp(pdb.prefix), end...)
}
ritr, err := pdb.db.ReverseIterator(pstart, pend)
if err != nil {
return nil, err
}
return newPrefixIterator(pdb.prefix, start, end, ritr)
}
// NewBatch implements DB.
func (pdb *PrefixDB) NewBatch() Batch {
pdb.mtx.Lock()
defer pdb.mtx.Unlock()
return newPrefixBatch(pdb.prefix, pdb.db.NewBatch())
}
// Close implements DB.
func (pdb *PrefixDB) Close() error {
pdb.mtx.Lock()
defer pdb.mtx.Unlock()
return pdb.db.Close()
}
// Print implements DB.
func (pdb *PrefixDB) Print() error {
fmt.Printf("prefix: %X\n", pdb.prefix)
itr, err := pdb.Iterator(nil, nil)
if err != nil {
return err
}
defer itr.Close()
for ; itr.Valid(); itr.Next() {
key := itr.Key()
value := itr.Value()
fmt.Printf("[%X]:\t[%X]\n", key, value)
}
return nil
}
// Stats implements DB.
func (pdb *PrefixDB) Stats() map[string]string {
stats := make(map[string]string)
stats["prefixdb.prefix.string"] = string(pdb.prefix)
stats["prefixdb.prefix.hex"] = fmt.Sprintf("%X", pdb.prefix)
source := pdb.db.Stats()
for key, value := range source {
stats["prefixdb.source."+key] = value
}
return stats
}
func (pdb *PrefixDB) prefixed(key []byte) []byte {
return append(cp(pdb.prefix), key...)
}