forked from hyperledger-archives/aries-framework-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prefix_wrapper.go
84 lines (67 loc) · 2.3 KB
/
prefix_wrapper.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
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
// package prefix offers provider.Store wrappers for stores with certain constraints on ID values such as
// reserved prefix character values (eg IDs starting with underscore) by prefixing it with IDPrefix.
package prefix
import (
"errors"
"github.com/Universal-Health-Chain/aries-framework-go/pkg/storage"
)
// StorageKIDPrefix is the KID prefix for key IDs (used by localkms).
const StorageKIDPrefix = "k"
// NewPrefixStoreWrapper creates a new StorePrefixWrapper of store.
func NewPrefixStoreWrapper(store storage.Store, prefix string) (*StorePrefixWrapper, error) {
if prefix == "" {
return nil, errors.New("newPrefixStoreWrapper: prefix is empty")
}
return &StorePrefixWrapper{store: store, prefix: prefix}, nil
}
// StorePrefixWrapper is a wrapper store that prepends IDPrefix to IDs.
// IDs will be stored in the embedded store with IDPrefix as prefix while the user of this wrapper store will interact
// the original unchanged ID.
type StorePrefixWrapper struct {
store storage.Store
prefix string
}
// Put stores v with k ID by prefixing it with IDPrefix.
func (b *StorePrefixWrapper) Put(k string, v []byte) error {
if k != "" {
k = b.prefix + k
}
return b.store.Put(k, v)
}
// Get fetches the record based on k by first prefixing it with IDPrefix.
func (b *StorePrefixWrapper) Get(k string) ([]byte, error) {
if k != "" {
k = b.prefix + k
}
return b.store.Get(k)
}
// Iterator returns an iterator for the latest snapshot of the underlying store.
//
// Args:
//
// startKey: Start of the key range, include in the range by converting it from base64 to IDPrefix encoded value first.
// endKey: End of the key range, not include in the range by converting it from base64 to IDPrefix encoded value first.
//
// Returns:
//
// StoreIterator: a wrapped iterator for result range.
func (b *StorePrefixWrapper) Iterator(startKey, endKey string) storage.StoreIterator {
if startKey != "" {
startKey = b.prefix + startKey
}
if endKey != "" {
endKey = b.prefix + endKey
}
return b.store.Iterator(startKey, endKey)
}
// Delete will delete a record with k by prefixing it with IDPrefix first.
func (b *StorePrefixWrapper) Delete(k string) error {
if k != "" {
k = b.prefix + k
}
return b.store.Delete(k)
}