-
Notifications
You must be signed in to change notification settings - Fork 351
/
store.go
205 lines (172 loc) · 4.32 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
package mem
import (
"bytes"
"context"
"encoding/base64"
"fmt"
"sort"
"sync"
"github.com/treeverse/lakefs/pkg/kv"
kvparams "github.com/treeverse/lakefs/pkg/kv/params"
)
type Driver struct{}
// PartitionMap holds key-value pairs of a given partition
type PartitionMap map[string]kv.Entry
type Store struct {
m map[string]PartitionMap
mu sync.RWMutex
}
type EntriesIterator struct {
entry *kv.Entry
err error
start []byte
partition string
store *Store
}
const DriverName = "mem"
//nolint:gochecknoinits
func init() {
kv.Register(DriverName, &Driver{})
}
func (d *Driver) Open(_ context.Context, _ kvparams.KV) (kv.Store, error) {
return &Store{
m: make(map[string]PartitionMap, 0),
}, nil
}
func encodeKey(key []byte) string {
return base64.StdEncoding.EncodeToString(key)
}
func (s *Store) Get(_ context.Context, partitionKey, key []byte) (*kv.ValueWithPredicate, error) {
if len(partitionKey) == 0 {
return nil, kv.ErrMissingPartitionKey
}
if len(key) == 0 {
return nil, kv.ErrMissingKey
}
s.mu.RLock()
defer s.mu.RUnlock()
sKey := encodeKey(key)
value, ok := s.m[string(partitionKey)][sKey]
if !ok {
return nil, fmt.Errorf("partition=%s, key=%v, encoding=%s: %w", partitionKey, key, sKey, kv.ErrNotFound)
}
return &kv.ValueWithPredicate{
Value: value.Value,
Predicate: kv.Predicate(value.Value),
}, nil
}
func (s *Store) Set(_ context.Context, partitionKey, key, value []byte) error {
if len(partitionKey) == 0 {
return kv.ErrMissingPartitionKey
}
if len(key) == 0 {
return kv.ErrMissingKey
}
if value == nil {
return kv.ErrMissingValue
}
s.mu.Lock()
defer s.mu.Unlock()
s.internalSet(partitionKey, key, value)
return nil
}
func (s *Store) internalSet(partitionKey, key, value []byte) {
sKey := encodeKey(key)
if _, ok := s.m[string(partitionKey)]; !ok {
s.m[string(partitionKey)] = make(map[string]kv.Entry)
}
s.m[string(partitionKey)][sKey] = kv.Entry{
PartitionKey: partitionKey,
Key: key,
Value: value,
}
}
func (s *Store) SetIf(_ context.Context, partitionKey, key, value []byte, valuePredicate kv.Predicate) error {
if len(partitionKey) == 0 {
return kv.ErrMissingPartitionKey
}
if len(key) == 0 {
return kv.ErrMissingKey
}
if value == nil {
return kv.ErrMissingValue
}
s.mu.Lock()
defer s.mu.Unlock()
sKey := encodeKey(key)
curr, currOK := s.m[string(partitionKey)][sKey]
if valuePredicate == nil {
if currOK {
return fmt.Errorf("key=%v: %w", key, kv.ErrPredicateFailed)
}
} else if !bytes.Equal(valuePredicate.([]byte), curr.Value) {
return fmt.Errorf("%w: partition=%s, key=%v, encoding=%s", kv.ErrPredicateFailed, partitionKey, key, sKey)
}
s.internalSet(partitionKey, key, value)
return nil
}
func (s *Store) Delete(_ context.Context, partitionKey, key []byte) error {
if len(partitionKey) == 0 {
return kv.ErrMissingPartitionKey
}
if len(key) == 0 {
return kv.ErrMissingKey
}
s.mu.Lock()
defer s.mu.Unlock()
sKey := encodeKey(key)
if _, ok := s.m[string(partitionKey)][sKey]; !ok {
return nil
}
delete(s.m[string(partitionKey)], sKey)
return nil
}
func (s *Store) Scan(_ context.Context, partitionKey, start []byte) (kv.EntriesIterator, error) {
if len(partitionKey) == 0 {
return nil, kv.ErrMissingPartitionKey
}
return &EntriesIterator{
store: s,
start: start,
partition: string(partitionKey),
}, nil
}
func (s *Store) Close() {}
func (e *EntriesIterator) Next() bool {
if e.err != nil || e.start == nil { // start is nil only if last iteration we reached end of keys
return false
}
e.store.mu.RLock()
defer e.store.mu.RUnlock()
l := make([]*kv.Entry, 0)
if _, ok := e.store.m[e.partition]; ok {
for _, v := range e.store.m[e.partition] {
if bytes.Compare(v.Key, e.start) >= 0 {
entry := v
l = append(l, &entry)
}
}
}
if len(l) == 0 { // No results
e.start = nil
return false
}
if len(l) == 1 { // only 1 key >= start, set start to nil, so to indicate next call to return false immediately.
e.start = nil
e.entry = l[0]
return true
}
sort.Slice(l, func(i, j int) bool { return bytes.Compare(l[i].Key, l[j].Key) < 0 })
e.start = l[1].Key
e.entry = l[0]
return true
}
func (e *EntriesIterator) Entry() *kv.Entry {
return e.entry
}
func (e *EntriesIterator) Err() error {
return e.err
}
func (e *EntriesIterator) Close() {
e.err = kv.ErrClosedEntries
}