This repository has been archived by the owner on Apr 7, 2024. It is now read-only.
forked from dgraph-io/badger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
banyandb.go
286 lines (263 loc) · 7.14 KB
/
banyandb.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
283
284
285
286
package badger
import (
"bytes"
"fmt"
"math"
"sync"
"sync/atomic"
"time"
"github.com/dgraph-io/badger/v3/options"
"github.com/dgraph-io/badger/v3/table"
"github.com/dgraph-io/badger/v3/y"
"github.com/pkg/errors"
)
// HandoverIterator MUST run serially.
func (db *DB) HandoverIterator(it y.Iterator) error {
db.lock.Lock()
defer db.lock.Unlock()
var entries []*Entry
for it.Rewind(); it.Valid(); it.Next() {
v := it.Value()
e := &Entry{
Key: it.Key(),
Value: v.Value,
ExpiresAt: v.ExpiresAt,
UserMeta: v.UserMeta,
}
entries = append(entries, e)
}
req := &request{
Entries: entries,
}
reqs := []*request{req}
db.pub.sendUpdates(reqs)
for {
err := db.handleFlushTask(flushTask{itr: it})
if err == nil {
break
}
// Encountered error. Retry indefinitely.
db.opt.Errorf("Failure while flushing iterator to disk: %v. Retrying...\n", err)
time.Sleep(time.Second)
}
return nil
}
func (db *DB) NewIterator(opt IteratorOptions) y.Iterator {
if db.IsClosed() {
panic(ErrDBClosed.Error())
}
tables, decr := db.getMemTables()
defer decr()
db.vlog.incrIteratorCount()
var iters []y.Iterator
for i := 0; i < len(tables); i++ {
iters = append(iters, tables[i].sl.NewUniIterator(opt.Reverse))
}
iters = append(iters, db.lc.iterators(&opt)...) // This will increment references.
return table.NewMergeIterator(iters, opt.Reverse)
}
func (db *DB) Get(key []byte) (y.ValueStruct, error) {
if len(key) < 1 {
return y.ValueStruct{}, ErrEmptyKey
}
vs, err := db.get(key)
if err != nil {
return y.ValueStruct{}, y.Wrapf(err, "DB::Get key: %q", key)
}
if vs.Value == nil && vs.Meta == 0 {
return y.ValueStruct{}, ErrKeyNotFound
}
if isDeletedOrExpired(vs.Meta, vs.ExpiresAt) {
return y.ValueStruct{}, ErrKeyNotFound
}
result := y.ValueStruct{}
result.Meta = vs.Meta
result.UserMeta = vs.UserMeta
result.Version = y.ParseTs(key)
result.ExpiresAt = vs.ExpiresAt
result.Value = y.Copy(vs.Value)
return result, nil
}
func (db *DB) Put(key, val []byte) error {
req, err := db.write(key, val)
if err != nil {
return err
}
return req.Wait()
}
func (db *DB) PutAsync(key, val []byte, f func(error)) error {
req, err := db.write(key, val)
if err != nil {
return err
}
if f == nil {
return nil
}
go func() {
err := req.Wait()
f(err)
}()
return nil
}
func (db *DB) write(key, val []byte) (*request, error) {
switch {
case len(key) == 0:
return nil, ErrEmptyKey
case bytes.HasPrefix(key, badgerPrefix):
return nil, ErrInvalidKey
case len(key) > maxKeySize:
return nil, exceedsSize("Key", maxKeySize, key)
case int64(len(val)) > db.opt.ValueLogFileSize:
return nil, exceedsSize("Value", db.opt.ValueLogFileSize, val)
}
entry := &Entry{
Key: key,
Value: val,
}
req, err := db.sendToWriteCh([]*Entry{entry})
if err != nil {
return nil, fmt.Errorf("failed to send entry to write channel: %v", err)
}
return req, nil
}
type TableBuilderSizeKeyType int
const (
TableBuilderSizeKeyCompressedBlockNum TableBuilderSizeKeyType = iota
TableBuilderSizeKeyCompressedEntryNum
TableBuilderSizeKeyCompressedSize
TableBuilderSizeKeyUncompressedSize
TableBuilderSizeKeyEncodedBlockNum
TableBuilderSizeKeyEncodedEntryNum
TableBuilderSizeKeyEncodedSize
TableBuilderSizeKeyUnEncodedSize
)
type TableBuilderSizeKey struct {
Type TableBuilderSizeKeyType
FromLevel int
ToLevel int
}
func (db *DB) compressAndEncodeStat(opts table.Options, from, to int) table.Options {
if opts.Compression != options.None {
if opts.State == nil {
opts.State = &table.State{}
}
var size interface{}
size, _ = db.stat.TableBuilderSize.LoadOrStore(TableBuilderSizeKey{
Type: TableBuilderSizeKeyUncompressedSize,
FromLevel: from,
ToLevel: to,
}, &atomic.Int64{})
opts.State.UnCompressedSize = size.(*atomic.Int64)
size, _ = db.stat.TableBuilderSize.LoadOrStore(TableBuilderSizeKey{
Type: TableBuilderSizeKeyCompressedSize,
FromLevel: from,
ToLevel: to,
}, &atomic.Int64{})
opts.State.CompressedSize = size.(*atomic.Int64)
size, _ = db.stat.TableBuilderSize.LoadOrStore(TableBuilderSizeKey{
Type: TableBuilderSizeKeyCompressedBlockNum,
FromLevel: from,
ToLevel: to,
}, &atomic.Int64{})
opts.State.CompressedBlockNum = size.(*atomic.Int64)
size, _ = db.stat.TableBuilderSize.LoadOrStore(TableBuilderSizeKey{
Type: TableBuilderSizeKeyCompressedEntryNum,
FromLevel: from,
ToLevel: to,
}, &atomic.Int64{})
opts.State.CompressedEntryNum = size.(*atomic.Int64)
}
if opts.EncoderPool != nil {
if opts.State == nil {
opts.State = &table.State{}
}
var size interface{}
size, _ = db.stat.TableBuilderSize.LoadOrStore(TableBuilderSizeKey{
Type: TableBuilderSizeKeyUnEncodedSize,
FromLevel: from,
ToLevel: to,
}, &atomic.Int64{})
opts.State.UnEncodedSize = size.(*atomic.Int64)
size, _ = db.stat.TableBuilderSize.LoadOrStore(TableBuilderSizeKey{
Type: TableBuilderSizeKeyEncodedSize,
FromLevel: from,
ToLevel: to,
}, &atomic.Int64{})
opts.State.EncodedSize = size.(*atomic.Int64)
size, _ = db.stat.TableBuilderSize.LoadOrStore(TableBuilderSizeKey{
Type: TableBuilderSizeKeyEncodedBlockNum,
FromLevel: from,
ToLevel: to,
}, &atomic.Int64{})
opts.State.EncodedBlockNum = size.(*atomic.Int64)
size, _ = db.stat.TableBuilderSize.LoadOrStore(TableBuilderSizeKey{
Type: TableBuilderSizeKeyEncodedEntryNum,
FromLevel: from,
ToLevel: to,
}, &atomic.Int64{})
opts.State.EncodedEntryNum = size.(*atomic.Int64)
}
return opts
}
type Statistics struct {
TableBuilderSize *sync.Map
}
func (db *DB) CollectStats() (s *Statistics) {
return db.stat
}
var ErrTSetInvalidTS = errors.New("Timestamp should be greater than 0")
// TSet is a time-series set, which leverages internal version to store the timestamp.
type TSet struct {
db *DB
}
func NewTSet(db *DB) *TSet {
db.opt.NumVersionsToKeep = math.MaxInt64
tSet := &TSet{db: db}
return tSet
}
func (s *TSet) Get(key []byte, ts uint64) (val []byte, err error) {
db := s.db
if db.IsClosed() {
return nil, ErrDBClosed
}
tables, decr := db.getMemTables() // Lock should be released.
defer decr()
version := ts
y.NumGetsAdd(db.opt.MetricsEnabled, 1)
for i := 0; i < len(tables); i++ {
vs := tables[i].sl.Get(y.KeyWithTs(key, ts))
y.NumMemtableGetsAdd(db.opt.MetricsEnabled, 1)
if vs.Meta == 0 && vs.Value == nil {
continue
}
if vs.Version == version {
return vs.Value, nil
}
}
vs, errLC := s.db.lc.get(y.KeyWithTs(key, ts), y.ValueStruct{}, 0)
if errLC != nil {
return nil, fmt.Errorf("faliled to get val from leved files: %v", errLC)
}
if vs.Value == nil {
return nil, nil
}
return vs.Value, nil
}
func (s *TSet) seekMemTables(prefix []byte) [][]byte {
tables, decr := s.db.getMemTables()
defer decr()
s.db.vlog.incrIteratorCount()
var iters []y.Iterator
for i := 0; i < len(tables); i++ {
iters = append(iters, tables[i].sl.NewUniIterator(false))
}
it := table.NewMergeIterator(iters, false)
defer func(it y.Iterator) {
_ = it.Close()
}(it)
r := make([][]byte, 0)
for it.Seek(y.KeyWithTs(prefix, math.MaxUint64)); it.Valid() && bytes.HasPrefix(it.Key(), prefix); it.Next() {
r = append(r, it.Value().Value)
}
return r
}