-
Notifications
You must be signed in to change notification settings - Fork 13
/
kv.go
462 lines (416 loc) · 12.6 KB
/
kv.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
Copyright © 2020 FairOS Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package collection
import (
"bufio"
"bytes"
"fmt"
"io"
"strconv"
"strings"
"sync"
"github.com/fairdatasociety/fairOS-dfs/pkg/account"
"github.com/fairdatasociety/fairOS-dfs/pkg/blockstore"
"github.com/fairdatasociety/fairOS-dfs/pkg/feed"
"github.com/fairdatasociety/fairOS-dfs/pkg/logging"
"github.com/fairdatasociety/fairOS-dfs/pkg/utils"
)
const (
kvFile = "key_value_tables"
defaultCollectionName = "KV"
// CSVHeaderKey is the key used to store the header of the csv file
CSVHeaderKey = "__csv_header__"
)
// KeyValue is the main object used to do all operation on the key value tables.
type KeyValue struct {
podName string
fd *feed.API
ai *account.Info
user utils.Address
client blockstore.Client
openKVTables map[string]*KVTable
openKVTMu sync.RWMutex
iterator *Iterator
logger logging.Logger
}
// KVTable is the object used to do all operation on a key value table.
type KVTable struct {
index *Index
indexType IndexType
columns []string
}
// TableKeyCount is the object used to store the count of keys in a table.
type TableKeyCount struct {
Count uint64 `json:"count"`
TableName string `json:"tableName"`
}
// NewKeyValueStore is the main object used to do all operation on the key value tables.
func NewKeyValueStore(podName string, fd *feed.API, ai *account.Info, user utils.Address, client blockstore.Client, logger logging.Logger) *KeyValue {
return &KeyValue{
podName: podName,
fd: fd,
ai: ai,
user: user,
client: client,
openKVTables: make(map[string]*KVTable),
logger: logger,
}
}
// CreateKVTable creates the key value table with a given index type.
func (kv *KeyValue) CreateKVTable(name, encryptionPassword string, indexType IndexType) error {
if kv.fd.IsReadOnlyFeed() { // skipcq: TCV-001
return ErrReadOnlyIndex
}
// load the existing db's and see if this name is already there
kvtables, err := kv.LoadKVTables(encryptionPassword)
if err != nil { // skipcq: TCV-001
return err
}
if _, ok := kvtables[name]; ok {
return ErrKvTableAlreadyPresent
}
// since this tables is not present already, create the index required for this table
err = CreateIndex(kv.podName, defaultCollectionName, name, encryptionPassword, indexType, kv.fd, kv.user, kv.client, true)
if err != nil { // skipcq: TCV-001
return err
}
// record the table as created
kvtables[name] = []string{indexType.String()}
return kv.storeKVTables(kvtables, encryptionPassword)
}
// DeleteKVTable deletes a given key value table with all it's index and data entries.
func (kv *KeyValue) DeleteKVTable(name, encryptionPassword string) error {
if kv.fd.IsReadOnlyFeed() { // skipcq: TCV-001
return ErrReadOnlyIndex
}
kvtables, err := kv.LoadKVTables(encryptionPassword)
if err != nil { // skipcq: TCV-001
return err
}
if _, ok := kvtables[name]; !ok {
return ErrKVTableNotPresent
}
kv.openKVTMu.Lock()
defer kv.openKVTMu.Unlock()
if table, ok := kv.openKVTables[name]; ok {
err = table.index.DeleteIndex(encryptionPassword)
if err != nil { // skipcq: TCV-001
return err
}
delete(kv.openKVTables, name)
} else {
idx, err := OpenIndex(kv.podName, defaultCollectionName, name, encryptionPassword, kv.fd, kv.ai, kv.user, kv.client, kv.logger)
if err != nil { // skipcq: TCV-001
return err
}
err = idx.DeleteIndex(encryptionPassword)
if err != nil { // skipcq: TCV-001
return err
}
}
delete(kvtables, name)
return kv.storeKVTables(kvtables, encryptionPassword)
}
func (kv *KeyValue) Commit() {
kv.fd.CommitFeeds()
}
// DeleteAllKVTables deletes all key value tables with all their index and data entries.
func (kv *KeyValue) DeleteAllKVTables(encryptionPassword string) error {
if kv.fd.IsReadOnlyFeed() { // skipcq: TCV-001
return ErrReadOnlyIndex
}
kvtables, err := kv.LoadKVTables(encryptionPassword)
if err != nil { // skipcq: TCV-001
return err
}
kv.openKVTMu.Lock()
defer kv.openKVTMu.Unlock()
for name := range kvtables {
if _, ok := kvtables[name]; !ok {
return ErrKVTableNotPresent
}
if table, ok := kv.openKVTables[name]; ok {
err = table.index.DeleteIndex(encryptionPassword)
if err != nil { // skipcq: TCV-001
return err
}
delete(kv.openKVTables, name)
} else {
idx, err := OpenIndex(kv.podName, defaultCollectionName, name, encryptionPassword, kv.fd, kv.ai, kv.user, kv.client, kv.logger)
if err != nil { // skipcq: TCV-001
if err == ErrIndexNotPresent {
continue
}
return err
}
err = idx.DeleteIndex(encryptionPassword)
if err != nil { // skipcq: TCV-001
return err
}
}
delete(kvtables, name)
}
return kv.storeKVTables(kvtables, encryptionPassword)
}
// OpenKVTable open a given key value table and loads the index.
func (kv *KeyValue) OpenKVTable(name, encryptionPassword string) error {
kvtables, err := kv.LoadKVTables(encryptionPassword)
if err != nil { // skipcq: TCV-001
return err
}
values, ok := kvtables[name]
if !ok {
return ErrKVTableNotPresent
}
idxType := toIndexTypeEnum(values[0])
idx, err := OpenIndex(kv.podName, defaultCollectionName, name, encryptionPassword, kv.fd, kv.ai, kv.user, kv.client, kv.logger)
if err != nil { // skipcq: TCV-001
return err
}
hdr, err := idx.Get(CSVHeaderKey)
var columns []string
if err == nil && len(hdr) >= 1 { // skipcq: TCV-001
columns = strings.Split(string(hdr[0]), ",")
}
kv.openKVTMu.Lock()
defer kv.openKVTMu.Unlock()
kvTable := &KVTable{
index: idx,
indexType: idxType,
columns: columns,
}
kv.openKVTables[name] = kvTable
return nil
}
// KVCount counts the number of entries in the given key value table.
func (kv *KeyValue) KVCount(name string) (*TableKeyCount, error) {
kv.openKVTMu.Lock()
defer kv.openKVTMu.Unlock()
if table, ok := kv.openKVTables[name]; ok {
return &TableKeyCount{
Count: table.index.count,
TableName: name,
}, nil
}
return nil, ErrKVTableNotOpened
}
// IsEmpty checks if the given key value table is empty.
func (kv *KeyValue) IsEmpty(name string) (bool, error) {
kv.openKVTMu.Lock()
defer kv.openKVTMu.Unlock()
if table, ok := kv.openKVTables[name]; ok {
return table.index.IsEmpty(table.index.encryptionPassword)
}
return false, ErrKVTableNotOpened
}
// KVPut inserts a given key and value in to the KV table.
func (kv *KeyValue) KVPut(name, key string, value []byte) error {
if kv.fd.IsReadOnlyFeed() { // skipcq: TCV-001
return ErrReadOnlyIndex
}
kv.openKVTMu.Lock()
table, ok := kv.openKVTables[name]
kv.openKVTMu.Unlock()
if ok {
switch table.indexType {
case StringIndex:
return table.index.Put(key, value, StringIndex, false)
case NumberIndex:
fkey, err := strconv.ParseFloat(key, 64)
if err != nil {
return ErrKVKeyNotANumber
}
return table.index.PutNumber(fkey, value, NumberIndex, false)
case BytesIndex:
ref, err := kv.client.UploadBlob(value, 0, true)
if err != nil { // skipcq: TCV-001
return err
}
return table.index.Put(key, ref, StringIndex, false)
default: // skipcq: TCV-001
return ErrKVInvalidIndexType
}
}
return ErrKVTableNotOpened
}
// KVGet retrieves a value from the KV table given a key.
func (kv *KeyValue) KVGet(name, key string) ([]string, []byte, error) {
kv.openKVTMu.Lock()
table, ok := kv.openKVTables[name]
kv.openKVTMu.Unlock()
if ok {
value, err := table.index.Get(key)
if err != nil {
return nil, nil, err
}
if table.indexType == BytesIndex {
data, _, err := kv.client.DownloadBlob(value[0])
if err != nil { // skipcq: TCV-001
return nil, nil, err
}
value[0] = data
}
return table.columns, value[0], nil
}
return nil, nil, ErrKVTableNotOpened
}
// KVDelete removed a key value entry from the KV table given a key.
func (kv *KeyValue) KVDelete(name, key string) ([]byte, error) {
if kv.fd.IsReadOnlyFeed() { // skipcq: TCV-001
return nil, ErrReadOnlyIndex
}
kv.openKVTMu.Lock()
defer kv.openKVTMu.Unlock()
if table, ok := kv.openKVTables[name]; ok {
refs, err := table.index.Delete(key)
if err != nil {
return nil, err
}
return refs[0], err
}
return nil, ErrKVTableNotOpened // skipcq: TCV-001
}
// KVBatch prepares the index to do a batch insert if keys and values.
func (kv *KeyValue) KVBatch(name string, columns []string) (*Batch, error) {
if kv.fd.IsReadOnlyFeed() { // skipcq: TCV-001
return nil, ErrReadOnlyIndex
}
kv.openKVTMu.Lock()
defer kv.openKVTMu.Unlock()
if table, ok := kv.openKVTables[name]; ok {
table.columns = columns
return NewBatch(table.index)
}
return nil, ErrKVTableNotOpened
}
// KVBatchPut inserts a key and value in to the memory for batch.
func (kv *KeyValue) KVBatchPut(batch *Batch, key string, value []byte) error {
if kv.fd.IsReadOnlyFeed() { // skipcq: TCV-001
return ErrReadOnlyIndex
}
if key == CSVHeaderKey { // skipcq: TCV-001
kv.openKVTMu.Lock()
defer kv.openKVTMu.Unlock()
if table, ok := kv.openKVTables[batch.idx.name]; ok {
table.columns = strings.Split(string(value), ",")
}
}
return batch.Put(key, value, false, false)
}
// KVBatchWrite commits all the batch entries in to the key value table.
func (kv *KeyValue) KVBatchWrite(batch *Batch) error {
if kv.fd.IsReadOnlyFeed() { // skipcq: TCV-001
return ErrReadOnlyIndex
}
_, err := batch.Write("")
return err
}
// KVSeek seek to given key with start prefix and prepare for iterating the table.
func (kv *KeyValue) KVSeek(name, start, end string, limit int64) (*Iterator, error) {
kv.openKVTMu.Lock()
defer kv.openKVTMu.Unlock()
if table, ok := kv.openKVTables[name]; ok {
switch table.indexType {
case StringIndex:
itr, err := table.index.NewStringIterator(start, end, limit)
if err != nil { // skipcq: TCV-001
return nil, err
}
kv.iterator = itr
return itr, nil
case NumberIndex:
startInt, err := strconv.ParseInt(start, 10, 64)
if err != nil { // skipcq: TCV-001
return nil, err
}
endInt, err := strconv.ParseInt(end, 10, 64)
if err != nil { // skipcq: TCV-001
return nil, err
}
itr, err := table.index.NewIntIterator(startInt, endInt, limit)
if err != nil { // skipcq: TCV-001
return nil, err
}
kv.iterator = itr
return itr, nil
case BytesIndex:
return nil, ErrKVIndexTypeNotSupported
default:
return nil, ErrKVInvalidIndexType
}
}
return nil, ErrKVTableNotOpened
}
// KVGetNext retrieve the next key value pair in the iteration.
func (kv *KeyValue) KVGetNext(name string) ([]string, string, []byte, error) {
kv.openKVTMu.Lock()
defer kv.openKVTMu.Unlock()
if table, ok := kv.openKVTables[name]; ok {
if kv.iterator == nil {
return nil, "", nil, ErrKVNilIterator
}
ok := kv.iterator.Next()
if !ok {
return nil, "", nil, ErrNoNextElement
}
return table.columns, kv.iterator.StringKey(), kv.iterator.Value(), nil
}
return nil, "", nil, ErrKVTableNotOpened
}
// LoadKVTables Loads the list of KV tables.
func (kv *KeyValue) LoadKVTables(encryptionPassword string) (map[string][]string, error) {
collections := make(map[string][]string)
topic := utils.HashString(kvFile)
_, data, err := kv.fd.GetFeedData(topic, kv.user, []byte(encryptionPassword), false)
if err != nil {
if err.Error() != "feed does not exist or was not updated yet" { // skipcq: TCV-001
return collections, err
}
}
buf := bytes.NewBuffer(data)
rd := bufio.NewReader(buf)
for {
line, err := rd.ReadString('\n')
if err == io.EOF {
break
}
if err != nil { // skipcq: TCV-001
return nil, fmt.Errorf("loading collections: %w", err)
}
line = strings.Trim(line, "\n")
lines := strings.Split(line, ",")
collections[lines[0]] = lines[1:]
}
return collections, nil
}
func (kv *KeyValue) storeKVTables(collections map[string][]string, encryptionPassword string) error {
buf := bytes.NewBuffer(nil)
collectionLen := len(collections)
if collectionLen > 0 {
for k, v := range collections {
indexes := strings.Join(v, ",")
line := fmt.Sprintf("%s,%s", k, indexes)
buf.WriteString(line + "\n")
}
}
topic := utils.HashString(kvFile)
data := buf.Bytes()
if buf.Len() == 0 {
data = []byte(utils.DeletedFeedMagicWord)
}
err := kv.fd.UpdateFeed(kv.user, topic, data, []byte(encryptionPassword), false)
if err != nil { // skipcq: TCV-001
return err
}
return nil
}