This repository has been archived by the owner on Mar 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
store.go
759 lines (598 loc) · 19.6 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
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
// Package mysql implements a storage interface for Aries (aries-framework-go).
//
package mysql
import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"strings"
"sync"
// Add as per the documentation - https://github.com/go-sql-driver/mysql
_ "github.com/go-sql-driver/mysql" //nolint:gci // False positive, seemingly caused by the MySQL driver comment.
"github.com/hyperledger/aries-framework-go/spi/storage"
)
const (
createDBQuery = "CREATE DATABASE IF NOT EXISTS `%s`"
tagMapKey = "TagMap"
storeConfigKey = "StoreConfig"
expressionTagNameOnlyLength = 1
expressionTagNameAndValueLength = 2
invalidQueryExpressionFormat = `"%s" is not in a valid expression format. ` +
"it must be in the following format: TagName:TagValue"
invalidTagName = `"%s" is an invalid tag name since it contains one or more ':' characters`
invalidTagValue = `"%s" is an invalid tag value since it contains one or more ':' characters`
)
// TODO (#67): Fully implement all methods.
// ErrKeyRequired is returned when key is mandatory.
var ErrKeyRequired = errors.New("key is mandatory")
type closer func(storeName string)
type tagMapping map[string]map[string]struct{} // map[TagName](Set of database Keys)
type dbEntry struct {
Value []byte `json:"value,omitempty"`
Tags []storage.Tag `json:"tags,omitempty"`
}
// Provider represents a MySQL DB implementation of the storage.Provider interface.
type Provider struct {
dbURL string
db *sql.DB
dbs map[string]*store
dbPrefix string
lock sync.RWMutex
}
// Option configures the couchdb provider.
type Option func(opts *Provider)
// WithDBPrefix option is for adding prefix to db name.
func WithDBPrefix(dbPrefix string) Option {
return func(opts *Provider) {
opts.dbPrefix = dbPrefix
}
}
// NewProvider instantiates Provider.
// Example DB Path root:my-secret-pw@tcp(127.0.0.1:3306)/?interpolateParams=true&multiStatements=true
// This provider's CreateStore(name) implementation creates stores that are backed by a table under a schema
// with the same name as the table. The fully qualified name of the table is thus `name.name`. The fully qualified
// name of the table needs to be used with the store's `Query()` method.
// Use of `Batch()` has additional considerations - please read the docs for Batch() accordingly..
func NewProvider(dbPath string, opts ...Option) (*Provider, error) {
if dbPath == "" {
return nil, errBlankDBPath
}
db, err := sql.Open("mysql", dbPath)
if err != nil {
return nil, fmt.Errorf(failureWhileOpeningMySQLConnectionErrMsg, dbPath, err)
}
err = db.Ping()
if err != nil {
return nil, fmt.Errorf(failureWhilePingingMySQLErrMsg, dbPath, err)
}
p := &Provider{
dbURL: dbPath,
db: db,
dbs: map[string]*store{},
}
for _, opt := range opts {
opt(p)
}
return p, nil
}
// OpenStore opens a store with the given name and returns a handle.
// If the store has never been opened before, then it is created.
// Store names are not case-sensitive. If name is blank, then an error will be returned.
// WARNING: This method will create a database and table based on the given name. Those database calls may be
// vulnerable to an SQL injection attack. Be very careful if you use a user-provided string in the store name!
func (p *Provider) OpenStore(name string) (storage.Store, error) {
if name == "" {
return nil, errBlankStoreName
}
name = strings.ToLower(name)
if p.dbPrefix != "" {
name = p.dbPrefix + "_" + name
}
p.lock.Lock()
defer p.lock.Unlock()
// Check cache first
cachedStore, existsInCache := p.dbs[name]
if existsInCache {
return cachedStore, nil
}
// creating the database
_, err := p.db.Exec(fmt.Sprintf(createDBQuery, name))
if err != nil {
return nil, fmt.Errorf(failureWhileCreatingDBErrMsg, name, err)
}
createTableStmt := fmt.Sprintf("CREATE Table IF NOT EXISTS `%s`.`%s` (`key` varchar(255) NOT NULL ,"+
"`value` MEDIUMBLOB, PRIMARY KEY (`key`))", name, name)
// creating key-value table inside the database
_, err = p.db.Exec(createTableStmt)
if err != nil {
return nil, fmt.Errorf(failureWhileCreatingTableErrMsg, name, err)
}
// Opening new DB connection
storeDB, err := sql.Open("mysql", p.dbURL)
if err != nil {
return nil, fmt.Errorf(failureWhileOpeningMySQLConnectionErrMsg, p.dbURL, err)
}
store := &store{
db: storeDB,
name: name,
tableName: fmt.Sprintf("`%s`.`%s`", name, name),
close: p.removeStore,
}
p.dbs[name] = store
return store, nil
}
// SetStoreConfig sets the configuration on a store. This must be done before storing any data in order to make use
// of the Query method.
// TODO (#67): Use proper MySQL indexing instead of the "Tag Map".
func (p *Provider) SetStoreConfig(name string, config storage.StoreConfiguration) error {
for _, tagName := range config.TagNames {
if strings.Contains(tagName, ":") {
return fmt.Errorf(invalidTagName, tagName)
}
}
name = strings.ToLower(name)
if p.dbPrefix != "" {
name = p.dbPrefix + "_" + name
}
openStore, ok := p.dbs[name]
if !ok {
return storage.ErrStoreNotFound
}
configBytes, err := json.Marshal(config)
if err != nil {
return fmt.Errorf("failed to marshal store configuration: %w", err)
}
err = openStore.Put(storeConfigKey, configBytes)
if err != nil {
return fmt.Errorf("failed to put store store configuration: %w", err)
}
return nil
}
// GetStoreConfig returns the store's configuration.
// TODO (#167): Check for underlying database's existence instead of looking at in-memory stores.
func (p *Provider) GetStoreConfig(name string) (storage.StoreConfiguration, error) {
name = strings.ToLower(name)
if p.dbPrefix != "" {
name = p.dbPrefix + "_" + name
}
openStore, ok := p.dbs[name]
if !ok {
return storage.StoreConfiguration{}, storage.ErrStoreNotFound
}
storeConfigBytes, err := openStore.Get(storeConfigKey)
if err != nil {
return storage.StoreConfiguration{},
fmt.Errorf(`failed to get store configuration for "%s": %w`, name, err)
}
var storeConfig storage.StoreConfiguration
err = json.Unmarshal(storeConfigBytes, &storeConfig)
if err != nil {
return storage.StoreConfiguration{}, fmt.Errorf("failed to unmarshal store configuration: %w", err)
}
return storeConfig, nil
}
// GetOpenStores is currently not implemented.
func (p *Provider) GetOpenStores() []storage.Store {
panic("not implemented")
}
// Close closes all stores created under this store provider.
func (p *Provider) Close() error {
p.lock.RLock()
openStoresSnapshot := make([]*store, len(p.dbs))
var counter int
for _, openStore := range p.dbs {
openStoresSnapshot[counter] = openStore
counter++
}
p.lock.RUnlock()
for _, openStore := range openStoresSnapshot {
err := openStore.Close()
if err != nil {
return fmt.Errorf(`failed to close open store with name "%s": %w`, openStore.name, err)
}
}
return nil
}
func (p *Provider) removeStore(name string) {
p.lock.Lock()
defer p.lock.Unlock()
_, ok := p.dbs[name]
if ok {
delete(p.dbs, name)
}
}
type store struct {
db *sql.DB
name string
tableName string
close closer
lock sync.RWMutex
}
func (s *store) Put(key string, value []byte, tags ...storage.Tag) error {
errInputValidation := validatePutInput(key, value, tags)
if errInputValidation != nil {
return errInputValidation
}
var newDBEntry dbEntry
newDBEntry.Value = value
if len(tags) > 0 {
newDBEntry.Tags = tags
err := s.updateTagMap(key, tags)
if err != nil {
return fmt.Errorf("failed to update tag map: %w", err)
}
}
entryBytes, err := json.Marshal(newDBEntry)
if err != nil {
return fmt.Errorf("failed to marshal new DB entry: %w", err)
}
// create upsert query to insert the record, checking whether the key is already mapped to a value in the store.
insertStmt := "INSERT INTO " + s.tableName + " VALUES (?, ?) ON DUPLICATE KEY UPDATE value=?" //nolint:gosec
// executing the prepared insert statement
_, err = s.db.Exec(insertStmt, key, entryBytes, entryBytes)
if err != nil {
return fmt.Errorf(failureWhileExecutingInsertStatementErrMsg, s.tableName, err)
}
return nil
}
func (s *store) Get(k string) ([]byte, error) {
retrievedDBEntry, err := s.getDBEntry(k)
if err != nil {
return nil, fmt.Errorf("failed to get DB entry: %w", err)
}
return retrievedDBEntry.Value, nil
}
func (s *store) GetTags(key string) ([]storage.Tag, error) {
retrievedDBEntry, err := s.getDBEntry(key)
if err != nil {
return nil, fmt.Errorf("failed to get DB entry: %w", err)
}
return retrievedDBEntry.Tags, nil
}
func (s *store) GetBulk(...string) ([][]byte, error) {
return nil, errors.New("not implemented")
}
// This provider doesn't currently support any of the current query options.
// spi.WithPageSize will simply be ignored since it only relates to performance and not the actual end result.
// spi.WithInitialPageNum and spi.WithSortOrder will result in an error being returned since those options do
// affect the results that the Iterator returns.
func (s *store) Query(expression string, options ...storage.QueryOption) (storage.Iterator, error) {
err := checkForUnsupportedQueryOptions(options)
if err != nil {
return nil, err
}
if expression == "" {
return nil, fmt.Errorf(invalidQueryExpressionFormat, expression)
}
expressionSplit := strings.Split(expression, ":")
var expressionTagName string
var expressionTagValue string
switch len(expressionSplit) {
case expressionTagNameOnlyLength:
expressionTagName = expressionSplit[0]
case expressionTagNameAndValueLength:
expressionTagName = expressionSplit[0]
expressionTagValue = expressionSplit[1]
default:
return nil, fmt.Errorf(invalidQueryExpressionFormat, expression)
}
matchingDatabaseKeys, err := s.getDatabaseKeysMatchingQuery(expressionTagName, expressionTagValue)
if err != nil {
return nil, fmt.Errorf("failed to get database keys matching query: %w", err)
}
return &iterator{keys: matchingDatabaseKeys, store: s}, nil
}
// Delete will delete record with k key.
func (s *store) Delete(k string) error {
if k == "" {
return ErrKeyRequired
}
// delete query to delete the record by key
_, err := s.db.Exec("DELETE FROM "+s.tableName+" WHERE `key`= ?", k) //nolint:gosec
if err != nil {
return fmt.Errorf(storage.ErrDataNotFound.Error(), err)
}
err = s.removeFromTagMap(k)
if err != nil {
return fmt.Errorf("failed to remove key from tag map: %w", err)
}
return nil
}
// Batch performs batch upserts and deletions preserving the batch's ordering.
// Batch is a no-op if the batch is empty.
// Batch needs both `interpolateParams` and `multiStatements` enabled in the dataSourceName
// - see the following guide: https://github.com/go-sql-driver/mysql#parameters.
// All operations in the batch are executed in a single multi-statement query due to the ordering
// requirements. This means we cannot optimize INSERT statements as per
// https://dev.mysql.com/doc/refman/8.0/en/insert-optimization.html.
// Executing a single multi-statement query requires O(N) space for the query string and an additional
// slice with O(2*N) space holding the values for the query. Callers should take care of this additional
// memory usage by limiting the size of the batch.
func (s *store) Batch(batch []storage.Operation) error {
if len(batch) == 0 {
return errors.New("batch requires at least one operation")
}
var (
query string
values []interface{}
)
for i := range batch {
b := batch[i]
if b.Key == "" {
return errors.New("key cannot be empty")
}
err := s.bulkAppendToQuery(b, &query, &values)
if err != nil {
return fmt.Errorf(failureWhileExecutingBatchStatementErrMsg, s.tableName, err)
}
if len(b.Value) > 0 {
err = s.updateTagMap(b.Key, b.Tags)
if err != nil && !errors.Is(err, storage.ErrDataNotFound) {
return fmt.Errorf("failed to update tag map: %w", err)
}
} else {
err = s.removeFromTagMap(b.Key)
if err != nil && !errors.Is(err, storage.ErrDataNotFound) {
return fmt.Errorf("failed to remove key from tag map: %w", err)
}
}
}
_, err := s.db.Exec(query, values...)
if err != nil {
return fmt.Errorf(failureWhileExecutingBatchStatementErrMsg, s.tableName, err)
}
return nil
}
func (s *store) bulkAppendToQuery(b storage.Operation, query *string, values *[]interface{}) error {
if len(b.Value) > 0 {
value, err := json.Marshal(dbEntry{
Value: b.Value,
Tags: b.Tags,
})
if err != nil {
return fmt.Errorf("failed to marshal dbEntry: %w", err)
}
*query += fmt.Sprintf(
"INSERT INTO %s (`key`, `value`) VALUES (?, ?) ON DUPLICATE KEY UPDATE value=VALUES(value);\n",
s.tableName,
)
*values = append(*values, b.Key, value)
} else {
*query += fmt.Sprintf("DELETE FROM %s WHERE `KEY` = ?;\n", s.tableName)
*values = append(*values, b.Key)
}
return nil
}
// SQL store doesn't queue values, so there's never anything to flush.
func (s *store) Flush() error {
return nil
}
func (s *store) Close() error {
s.close(s.name)
err := s.db.Close()
if err != nil {
return fmt.Errorf(failureWhileClosingMySQLConnection, err)
}
return nil
}
func (s *store) updateTagMap(key string, tags []storage.Tag) error {
s.lock.Lock()
defer s.lock.Unlock()
tagMap, err := s.getTagMap(true)
if err != nil {
return fmt.Errorf("failed to get tag map: %w", err)
}
for _, tag := range tags {
if tagMap[tag.Name] == nil {
tagMap[tag.Name] = make(map[string]struct{})
}
tagMap[tag.Name][key] = struct{}{}
}
tagMapBytes, err := json.Marshal(tagMap)
if err != nil {
return fmt.Errorf("failed to marshal updated tag map: %w", err)
}
err = s.Put(tagMapKey, tagMapBytes)
if err != nil {
return fmt.Errorf("failed to put updated tag map back into the store: %w", err)
}
return nil
}
func (s *store) getTagMap(createIfDoesNotExist bool) (tagMapping, error) {
tagMapBytes, err := s.Get(tagMapKey)
if err != nil {
if createIfDoesNotExist && errors.Is(err, storage.ErrDataNotFound) {
// Create the tag map if it has never been created before.
err = s.Put(tagMapKey, []byte("{}"))
if err != nil {
return nil, fmt.Errorf(`failed to create tag map for "%s": %w`, s.name, err)
}
tagMapBytes = []byte("{}")
} else {
return nil, fmt.Errorf("failed to get data: %w", err)
}
}
var tagMap tagMapping
err = json.Unmarshal(tagMapBytes, &tagMap)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal tag map bytes: %w", err)
}
return tagMap, nil
}
func (s *store) getDBEntry(key string) (dbEntry, error) {
if key == "" {
return dbEntry{}, ErrKeyRequired
}
var retrievedDBEntryBytes []byte
// select query to fetch the record by key
err := s.db.QueryRow("SELECT `value` FROM "+s.tableName+" "+
" WHERE `key` = ?", key).Scan(&retrievedDBEntryBytes)
if err != nil {
if strings.Contains(err.Error(), valueNotFoundErrMsgFromMySQL) {
return dbEntry{}, storage.ErrDataNotFound
}
return dbEntry{}, fmt.Errorf(failureWhileQueryingRowErrMsg, err)
}
var retrievedDBEntry dbEntry
err = json.Unmarshal(retrievedDBEntryBytes, &retrievedDBEntry)
if err != nil {
return dbEntry{}, fmt.Errorf("failed to unmarshaled retrieved DB entry: %w", err)
}
return retrievedDBEntry, nil
}
func (s *store) removeFromTagMap(keyToRemove string) error {
s.lock.Lock()
defer s.lock.Unlock()
tagMap, err := s.getTagMap(false)
if err != nil {
// If there's no tag map, then this means that tags have never been used. This means that there's no tag map
// to update. This isn't a problem.
if errors.Is(err, storage.ErrDataNotFound) {
return nil
}
return fmt.Errorf("failed to get tag map: %w", err)
}
for _, tagNameToKeys := range tagMap {
delete(tagNameToKeys, keyToRemove)
}
tagMapBytes, err := json.Marshal(tagMap)
if err != nil {
return fmt.Errorf("failed to marshal updated tag map: %w", err)
}
err = s.Put(tagMapKey, tagMapBytes)
if err != nil {
return fmt.Errorf("failed to put updated tag map back into the store: %w", err)
}
return nil
}
func (s *store) getDatabaseKeysMatchingQuery(expressionTagName, expressionTagValue string) ([]string, error) {
tagMap, err := s.getTagMap(false)
if err != nil {
// If there's no tag map, then this means that tags have never been used, and therefore no matching results.
if errors.Is(err, storage.ErrDataNotFound) {
return nil, nil
}
return nil, fmt.Errorf("failed to get tag map: %w", err)
}
if expressionTagValue == "" {
return getDatabaseKeysMatchingTagName(tagMap, expressionTagName), nil
}
matchingDatabaseKeys, err := s.getDatabaseKeysMatchingTagNameAndValue(tagMap, expressionTagName, expressionTagValue)
if err != nil {
return nil, fmt.Errorf("failed to get database keys matching tag name and value: %w", err)
}
return matchingDatabaseKeys, nil
}
func (s *store) getDatabaseKeysMatchingTagNameAndValue(tagMap tagMapping,
expressionTagName, expressionTagValue string) ([]string, error) {
var matchingDatabaseKeys []string
for tagName, databaseKeysSet := range tagMap {
if tagName == expressionTagName {
for databaseKey := range databaseKeysSet {
tags, err := s.GetTags(databaseKey)
if err != nil {
return nil, fmt.Errorf("failed to get tags: %w", err)
}
for _, tag := range tags {
if tag.Name == expressionTagName && tag.Value == expressionTagValue {
matchingDatabaseKeys = append(matchingDatabaseKeys, databaseKey)
break
}
}
}
break
}
}
return matchingDatabaseKeys, nil
}
type iterator struct {
keys []string
currentIndex int
currentKey string
store *store
}
func (i *iterator) Next() (bool, error) {
if len(i.keys) == i.currentIndex || len(i.keys) == 0 {
if len(i.keys) == i.currentIndex || len(i.keys) == 0 {
return false, nil
}
}
i.currentKey = i.keys[i.currentIndex]
i.currentIndex++
return true, nil
}
func (i *iterator) Key() (string, error) {
return i.currentKey, nil
}
func (i *iterator) Value() ([]byte, error) {
value, err := i.store.Get(i.currentKey)
if err != nil {
return nil, fmt.Errorf("failed to get value from store: %w", err)
}
return value, nil
}
func (i *iterator) Tags() ([]storage.Tag, error) {
tags, err := i.store.GetTags(i.currentKey)
if err != nil {
return nil, fmt.Errorf("failed to get tags from store: %w", err)
}
return tags, nil
}
func (i *iterator) TotalItems() (int, error) {
return len(i.keys), nil
}
func (i *iterator) Close() error {
return nil
}
func validatePutInput(key string, value []byte, tags []storage.Tag) error {
if key == "" {
return errors.New("key cannot be empty")
}
if value == nil {
return errors.New("value cannot be nil")
}
for _, tag := range tags {
if strings.Contains(tag.Name, ":") {
return fmt.Errorf(invalidTagName, tag.Name)
}
if strings.Contains(tag.Value, ":") {
return fmt.Errorf(invalidTagValue, tag.Value)
}
}
return nil
}
func checkForUnsupportedQueryOptions(options []storage.QueryOption) error {
querySettings := getQueryOptions(options)
if querySettings.InitialPageNum != 0 {
return errors.New("mySQL provider does not currently support " +
"setting the initial page number of query results")
}
if querySettings.SortOptions != nil {
return errors.New("mySQL provider does not currently support custom sort options for query results")
}
return nil
}
func getQueryOptions(options []storage.QueryOption) storage.QueryOptions {
var queryOptions storage.QueryOptions
for _, option := range options {
option(&queryOptions)
}
return queryOptions
}
func getDatabaseKeysMatchingTagName(tagMap tagMapping, expressionTagName string) []string {
var matchingDatabaseKeys []string
for tagName, databaseKeysSet := range tagMap {
if tagName == expressionTagName {
for databaseKey := range databaseKeysSet {
matchingDatabaseKeys = append(matchingDatabaseKeys, databaseKey)
}
break
}
}
return matchingDatabaseKeys
}