/
table.go
223 lines (184 loc) · 5.66 KB
/
table.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
// Copyright 2019 ChainSafe Systems (ON) Corp.
// This file is part of gossamer.
//
// The gossamer library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The gossamer library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the gossamer library. If not, see <http://www.gnu.org/licenses/>.
package chaindb
import (
"bytes"
"context"
"fmt"
log "github.com/ChainSafe/log15"
"github.com/dgraph-io/badger/v4"
"github.com/dgraph-io/badger/v4/pb"
)
type table struct {
db Database
prefix string
}
var _ Database = (*table)(nil)
var _ Iterator = (*tableIterator)(nil)
type tableBatch struct {
batch Batch
prefix string
}
// NewTable returns a Database object that prefixes all keys with a given
// string.
func NewTable(db Database, prefix string) Database {
return &table{db: db, prefix: prefix}
}
// ClearAll - This method is not implemented for table. Use delete instead.
func (dt *table) ClearAll() error {
return fmt.Errorf("this method is not implemented for table")
}
// Put adds keys with the prefix value given to NewTable
func (dt *table) Put(key []byte, value []byte) error {
return dt.db.Put(append([]byte(dt.prefix), key...), value)
}
// Has checks keys with the prefix value given to NewTable
func (dt *table) Has(key []byte) (bool, error) {
return dt.db.Has(append([]byte(dt.prefix), key...))
}
// Get retrieves keys with the prefix value given to NewTable
func (dt *table) Get(key []byte) ([]byte, error) {
return dt.db.Get(append([]byte(dt.prefix), key...))
}
// Del removes keys with the prefix value given to NewTable
func (dt *table) Del(key []byte) error {
return dt.db.Del(append([]byte(dt.prefix), key...))
}
// Flush commits pending writes to disk
func (dt *table) Flush() error {
return dt.db.Flush()
}
// Close closes table db
func (dt *table) Close() error {
return dt.db.Close()
}
// NewIterator initializes type Iterator
func (dt *table) NewIterator() Iterator {
if db, ok := dt.db.(*BadgerDB); ok {
db.lock.Lock()
defer db.lock.Unlock()
txn := db.db.NewTransaction(false)
opts := badger.DefaultIteratorOptions
iter := txn.NewIterator(opts)
tableIter := &tableIterator{
prefix: []byte(dt.prefix),
}
tableIter.txn = txn
tableIter.iter = iter
iter.Rewind()
iter.Seek([]byte(dt.prefix))
return tableIter
}
return nil
}
// Path returns table prefix
func (dt *table) Path() string {
return dt.prefix
}
func removePrefix(key, prefix []byte) []byte {
if bytes.Equal(key[:len(prefix)], prefix) {
return key[len(prefix):]
}
return key
}
type tableIterator struct {
BadgerIterator
prefix []byte
}
// Release closes the iterator and discards the created transaction.
func (i *tableIterator) Release() {
i.lock.Lock()
defer i.lock.Unlock()
i.iter.Close()
i.txn.Discard()
}
// Valid returns whether the current iterator position has an item.
func (i *tableIterator) Valid() bool {
return i.iter.Valid()
}
// Next rewinds the iterator to the zero-th position if uninitialized, and then will advance the iterator by one
// returns bool to ensure access to the item
func (i *tableIterator) Next() bool {
i.lock.RLock()
defer i.lock.RUnlock()
for {
if !i.iter.Valid() {
break
}
i.iter.Next()
if i.iter.ValidForPrefix(i.prefix) {
return true
}
}
return false
}
// Seek will look for the provided key if present and go to that position. If
// absent, it would seek to the next smallest key
func (i *tableIterator) Seek(key []byte) {
i.lock.RLock()
defer i.lock.RUnlock()
i.iter.Seek(key)
}
func (i *tableIterator) rawKey() []byte {
i.lock.RLock()
defer i.lock.RUnlock()
return i.iter.Item().Key()
}
// Key returns an item key without the table prefix
func (i *tableIterator) Key() []byte {
return removePrefix(i.rawKey(), i.prefix)
}
// Value returns a copy of the value of the item
func (i *tableIterator) Value() []byte {
i.lock.RLock()
defer i.lock.RUnlock()
val, err := i.iter.Item().ValueCopy(nil)
if err != nil {
log.Warn("value retrieval error ", "error", err)
}
return val
}
// NewTableBatch returns a Batch object which prefixes all keys with a given string.
func NewTableBatch(db Database, prefix string) Batch {
return &tableBatch{db.NewBatch(), prefix}
}
// NewBatch returns tableBatch with a Batch type and the given prefix
func (dt *table) NewBatch() Batch {
return &tableBatch{dt.db.NewBatch(), dt.prefix}
}
// Put encodes key-values with prefix given to NewBatchTable and adds them to a mapping for batch writes, sets the size of item value
func (tb *tableBatch) Put(key, value []byte) error {
return tb.batch.Put(append([]byte(tb.prefix), key...), value)
}
// Flush performs batched writes with the provided prefix
func (tb *tableBatch) Flush() error {
return tb.batch.Flush()
}
// ValueSize returns the amount of data in the batch accounting for the given prefix
func (tb *tableBatch) ValueSize() int {
return tb.batch.ValueSize()
}
// Reset clears batch key-values and resets the size to zero
func (tb *tableBatch) Reset() {
tb.batch.Reset()
}
// Del removes the key from the batch and database
func (tb *tableBatch) Del(k []byte) error {
return tb.batch.Del(append([]byte(tb.prefix), k...))
}
func (dt *table) Subscribe(ctx context.Context, cb func(kv *KVList) error, prefixes []pb.Match) error {
return dt.db.Subscribe(ctx, cb, prefixes)
}