-
Notifications
You must be signed in to change notification settings - Fork 199
/
storerMock.go
115 lines (92 loc) · 2.03 KB
/
storerMock.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
package mock
import (
"encoding/base64"
"errors"
"fmt"
"sync"
)
// StorerMock -
type StorerMock struct {
mut sync.Mutex
data map[string][]byte
}
// NewStorerMock -
func NewStorerMock() *StorerMock {
return &StorerMock{
data: make(map[string][]byte),
}
}
// Close -
func (sm *StorerMock) Close() error {
return nil
}
// Put -
func (sm *StorerMock) Put(key, data []byte) error {
sm.mut.Lock()
defer sm.mut.Unlock()
sm.data[string(key)] = data
return nil
}
// PutInEpoch -
func (sm *StorerMock) PutInEpoch(key, data []byte, _ uint32) error {
return sm.Put(key, data)
}
// Get -
func (sm *StorerMock) Get(key []byte) ([]byte, error) {
sm.mut.Lock()
defer sm.mut.Unlock()
val, ok := sm.data[string(key)]
if !ok {
return nil, fmt.Errorf("key: %s not found", base64.StdEncoding.EncodeToString(key))
}
return val, nil
}
// GetFromEpoch -
func (sm *StorerMock) GetFromEpoch(key []byte, _ uint32) ([]byte, error) {
return sm.Get(key)
}
// GetBulkFromEpoch -
func (sm *StorerMock) GetBulkFromEpoch(keys [][]byte, _ uint32) (map[string][]byte, error) {
retValue := map[string][]byte{}
for _, key := range keys {
value, err := sm.Get(key)
if err != nil {
continue
}
retValue[string(key)] = value
}
return retValue, nil
}
// SearchFirst -
func (sm *StorerMock) SearchFirst(_ []byte) ([]byte, error) {
return nil, errors.New("not implemented")
}
// Has -
func (sm *StorerMock) Has(_ []byte) error {
return errors.New("not implemented")
}
// Remove -
func (sm *StorerMock) Remove(key []byte) error {
sm.mut.Lock()
defer sm.mut.Unlock()
delete(sm.data, string(key))
return nil
}
// GetOldestEpoch -
func (sm *StorerMock) GetOldestEpoch() (uint32, error) {
return 0, nil
}
// ClearCache -
func (sm *StorerMock) ClearCache() {
}
// DestroyUnit -
func (sm *StorerMock) DestroyUnit() error {
return nil
}
// RangeKeys -
func (sm *StorerMock) RangeKeys(_ func(key []byte, val []byte) bool) {
}
// IsInterfaceNil returns true if there is no value under the interface
func (sm *StorerMock) IsInterfaceNil() bool {
return sm == nil
}