-
Notifications
You must be signed in to change notification settings - Fork 351
/
store.go
228 lines (210 loc) · 6.45 KB
/
store.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
package local
import (
"bytes"
"context"
"errors"
"time"
"github.com/dgraph-io/badger/v3"
"github.com/treeverse/lakefs/pkg/kv"
"github.com/treeverse/lakefs/pkg/logging"
)
func partitionRange(partitionKey []byte) []byte {
return append(partitionKey, kv.PathDelimiter[0])
}
func composeKey(partitionKey, key []byte) []byte {
return append(partitionRange(partitionKey), key...)
}
type Store struct {
db *badger.DB
logger logging.Logger
prefetchSize int
refCount int
path string
}
func (s *Store) Get(ctx context.Context, partitionKey, key []byte) (*kv.ValueWithPredicate, error) {
k := composeKey(partitionKey, key)
start := time.Now()
log := s.logger.WithField("key", string(k)).WithField("op", "get").WithContext(ctx)
log.Trace("performing operation")
if len(partitionKey) == 0 {
log.WithError(kv.ErrMissingPartitionKey).Warn("got empty partition key")
return nil, kv.ErrMissingPartitionKey
}
if len(key) == 0 {
log.WithError(kv.ErrMissingKey).Warn("got empty key")
return nil, kv.ErrMissingKey
}
var value []byte
err := s.db.View(func(txn *badger.Txn) error {
item, err := txn.Get(composeKey(partitionKey, key))
if errors.Is(err, badger.ErrKeyNotFound) {
return kv.ErrNotFound
}
if err != nil {
log.WithError(err).Error("error getting key")
return err
}
value, err = item.ValueCopy(nil)
if err != nil {
log.WithError(err).Error("error getting value for key")
return err
}
return nil
})
log.WithField("took", time.Since(start)).WithError(err).WithField("size", len(value)).Trace("operation complete")
if err != nil {
return nil, err
}
return &kv.ValueWithPredicate{
Value: value,
Predicate: kv.Predicate(value),
}, nil
}
func (s *Store) Set(ctx context.Context, partitionKey, key, value []byte) error {
k := composeKey(partitionKey, key)
start := time.Now()
log := s.logger.WithField("key", string(k)).WithField("op", "set").WithContext(ctx)
log.Trace("performing operation")
if len(partitionKey) == 0 {
log.WithError(kv.ErrMissingPartitionKey).Warn("got empty partition key")
return kv.ErrMissingPartitionKey
}
if len(key) == 0 {
log.WithError(kv.ErrMissingKey).Warn("got empty key")
return kv.ErrMissingKey
}
if value == nil {
log.WithError(kv.ErrMissingValue).Warn("got nil value")
return kv.ErrMissingValue
}
err := s.db.Update(func(txn *badger.Txn) error {
return txn.Set(k, value)
})
if err != nil {
log.WithError(err).Error("error setting value")
return err
}
log.WithField("took", time.Since(start)).Trace("done setting value")
return nil
}
func (s *Store) SetIf(ctx context.Context, partitionKey, key, value []byte, valuePredicate kv.Predicate) error {
k := composeKey(partitionKey, key)
start := time.Now()
log := s.logger.WithField("key", string(k)).WithField("op", "set_if").WithContext(ctx)
log.Trace("performing operation")
if len(partitionKey) == 0 {
log.WithError(kv.ErrMissingPartitionKey).Warn("got empty partition key")
return kv.ErrMissingPartitionKey
}
if len(key) == 0 {
log.WithError(kv.ErrMissingKey).Warn("got empty key")
return kv.ErrMissingKey
}
if value == nil {
log.WithError(kv.ErrMissingValue).Warn("got nil value")
return kv.ErrMissingValue
}
err := s.db.Update(func(txn *badger.Txn) error {
item, err := txn.Get(k)
if err != nil && !errors.Is(err, badger.ErrKeyNotFound) {
log.WithError(err).Error("could not get key for predicate")
return err
}
if valuePredicate != nil {
if errors.Is(err, badger.ErrKeyNotFound) {
log.WithField("predicate", nil).Trace("predicate condition failed")
return kv.ErrPredicateFailed
}
if valuePredicate != kv.PrecondConditionalExists {
val, err := item.ValueCopy(nil)
if err != nil {
log.WithError(err).Error("could not get byte value for predicate")
return err
}
if !bytes.Equal(val, valuePredicate.([]byte)) {
log.WithField("predicate", valuePredicate).WithField("value", val).Trace("predicate condition failed")
return kv.ErrPredicateFailed
}
}
} else if !errors.Is(err, badger.ErrKeyNotFound) {
log.WithField("predicate", valuePredicate).Trace("predicate condition failed (key not found)")
return kv.ErrPredicateFailed
}
return txn.Set(composeKey(partitionKey, key), value)
})
if errors.Is(err, badger.ErrConflict) { // Return predicate failed on transaction conflict - to retry
log.WithError(err).Trace("transaction conflict")
err = kv.ErrPredicateFailed
}
took := time.Since(start)
log.WithField("took", took).Trace("operation complete")
return err
}
func (s *Store) Delete(ctx context.Context, partitionKey, key []byte) error {
k := composeKey(partitionKey, key)
start := time.Now()
log := s.logger.
WithField("key", string(k)).
WithField("op", "delete").
WithContext(ctx)
log.Trace("performing operation")
if len(partitionKey) == 0 {
log.WithError(kv.ErrMissingPartitionKey).Warn("got empty partition key")
return kv.ErrMissingPartitionKey
}
if len(key) == 0 {
log.WithError(kv.ErrMissingKey).Warn("got empty key")
return kv.ErrMissingKey
}
err := s.db.Update(func(txn *badger.Txn) error {
return txn.Delete(k)
})
took := time.Since(start)
log = log.WithField("took", took)
if err != nil {
log.WithError(err).Trace("operation failed")
return err
}
log.Trace("operation complete")
return nil
}
func (s *Store) Scan(ctx context.Context, partitionKey []byte, options kv.ScanOptions) (kv.EntriesIterator, error) {
log := s.logger.WithFields(logging.Fields{
"partition_key": string(partitionKey),
"start_key": string(options.KeyStart),
"op": "scan",
}).WithContext(ctx)
log.Trace("performing operation")
if len(partitionKey) == 0 {
log.WithError(kv.ErrMissingPartitionKey).Warn("got empty partition key")
return nil, kv.ErrMissingPartitionKey
}
prefix := partitionRange(partitionKey)
txn := s.db.NewTransaction(false)
opts := badger.DefaultIteratorOptions
opts.PrefetchSize = s.prefetchSize
if options.BatchSize != 0 && opts.PrefetchSize != 0 && options.BatchSize < opts.PrefetchSize {
opts.PrefetchSize = options.BatchSize
}
if opts.PrefetchSize > 0 {
opts.PrefetchValues = true
}
opts.Prefix = prefix
iter := txn.NewIterator(opts)
return &EntriesIterator{
iter: iter,
partitionKey: partitionKey,
start: composeKey(partitionKey, options.KeyStart),
logger: log,
txn: txn,
}, nil
}
func (s *Store) Close() {
driverLock.Lock()
defer driverLock.Unlock()
s.refCount--
if s.refCount <= 0 {
_ = s.db.Close()
delete(dbMap, s.path)
}
}