forked from absmach/magistrala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
twins.go
259 lines (209 loc) · 5.39 KB
/
twins.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
// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
package mocks
import (
"context"
"sort"
"strconv"
"strings"
"sync"
"github.com/mainflux/mainflux/pkg/errors"
"github.com/mainflux/mainflux/pkg/uuid"
"github.com/mainflux/mainflux/twins"
)
var _ twins.TwinRepository = (*twinRepositoryMock)(nil)
type twinRepositoryMock struct {
mu sync.Mutex
twins map[string]twins.Twin
}
// NewTwinRepository creates in-memory twin repository.
func NewTwinRepository() twins.TwinRepository {
return &twinRepositoryMock{
twins: make(map[string]twins.Twin),
}
}
func (trm *twinRepositoryMock) Save(ctx context.Context, twin twins.Twin) (string, error) {
trm.mu.Lock()
defer trm.mu.Unlock()
for _, tw := range trm.twins {
if tw.ID == twin.ID {
return "", errors.ErrConflict
}
}
trm.twins[key(twin.Owner, twin.ID)] = twin
return twin.ID, nil
}
func (trm *twinRepositoryMock) Update(ctx context.Context, twin twins.Twin) error {
trm.mu.Lock()
defer trm.mu.Unlock()
dbKey := key(twin.Owner, twin.ID)
if _, ok := trm.twins[dbKey]; !ok {
return errors.ErrNotFound
}
trm.twins[dbKey] = twin
return nil
}
func (trm *twinRepositoryMock) RetrieveByID(_ context.Context, twinID string) (twins.Twin, error) {
trm.mu.Lock()
defer trm.mu.Unlock()
for k, v := range trm.twins {
if twinID == v.ID {
return trm.twins[k], nil
}
}
return twins.Twin{}, errors.ErrNotFound
}
func (trm *twinRepositoryMock) RetrieveByAttribute(ctx context.Context, channel, subtopic string) ([]string, error) {
var ids []string
for _, twin := range trm.twins {
def := twin.Definitions[len(twin.Definitions)-1]
for _, attr := range def.Attributes {
if attr.Channel == channel && (attr.Subtopic == twins.SubtopicWildcard || attr.Subtopic == subtopic) {
ids = append(ids, twin.ID)
break
}
}
}
return ids, nil
}
func (trm *twinRepositoryMock) RetrieveAll(_ context.Context, owner string, offset uint64, limit uint64, name string, metadata twins.Metadata) (twins.Page, error) {
trm.mu.Lock()
defer trm.mu.Unlock()
items := make([]twins.Twin, 0)
if limit <= 0 {
return twins.Page{}, nil
}
for k, v := range trm.twins {
if (uint64)(len(items)) >= limit {
break
}
if len(name) > 0 && v.Name != name {
continue
}
if !strings.HasPrefix(k, owner) {
continue
}
suffix := string(v.ID[len(uuid.Prefix):])
id, _ := strconv.ParseUint(suffix, 10, 64)
if id > offset && id <= offset+limit {
items = append(items, v)
}
}
sort.SliceStable(items, func(i, j int) bool {
return items[i].ID < items[j].ID
})
total := uint64(len(trm.twins))
page := twins.Page{
Twins: items,
PageMetadata: twins.PageMetadata{
Total: total,
Offset: offset,
Limit: limit,
},
}
return page, nil
}
func (trm *twinRepositoryMock) Remove(ctx context.Context, twinID string) error {
trm.mu.Lock()
defer trm.mu.Unlock()
for k, v := range trm.twins {
if twinID == v.ID {
delete(trm.twins, k)
return nil
}
}
return nil
}
type twinCacheMock struct {
mu sync.Mutex
attrIds map[string]map[string]bool
idAttrs map[string]map[string]bool
}
// NewTwinCache returns mock cache instance.
func NewTwinCache() twins.TwinCache {
return &twinCacheMock{
attrIds: make(map[string]map[string]bool),
idAttrs: make(map[string]map[string]bool),
}
}
func (tcm *twinCacheMock) Save(_ context.Context, twin twins.Twin) error {
tcm.mu.Lock()
defer tcm.mu.Unlock()
if len(twin.Definitions) < 1 {
return nil
}
def := twin.Definitions[len(twin.Definitions)-1]
tcm.save(def, twin.ID)
return nil
}
func (tcm *twinCacheMock) SaveIDs(ctx context.Context, channel, subtopic string, ids []string) error {
tcm.mu.Lock()
defer tcm.mu.Unlock()
for _, id := range ids {
attrKey := channel + subtopic
if _, ok := tcm.attrIds[attrKey]; !ok {
tcm.attrIds[attrKey] = make(map[string]bool)
}
tcm.attrIds[attrKey][id] = true
if _, ok := tcm.idAttrs[id]; !ok {
tcm.idAttrs[id] = make(map[string]bool)
}
tcm.idAttrs[id][attrKey] = true
}
return nil
}
func (tcm *twinCacheMock) Update(_ context.Context, twin twins.Twin) error {
tcm.mu.Lock()
defer tcm.mu.Unlock()
if err := tcm.remove(twin.ID); err != nil {
return nil
}
if len(twin.Definitions) < 1 {
return nil
}
def := twin.Definitions[len(twin.Definitions)-1]
tcm.save(def, twin.ID)
return nil
}
func (tcm *twinCacheMock) IDs(_ context.Context, channel, subtopic string) ([]string, error) {
tcm.mu.Lock()
defer tcm.mu.Unlock()
var ids []string
for k := range tcm.attrIds[channel+subtopic] {
ids = append(ids, k)
}
for k := range tcm.attrIds[channel+twins.SubtopicWildcard] {
ids = append(ids, k)
}
return ids, nil
}
func (tcm *twinCacheMock) Remove(_ context.Context, twinID string) error {
tcm.mu.Lock()
defer tcm.mu.Unlock()
return tcm.remove(twinID)
}
func (tcm *twinCacheMock) remove(twinID string) error {
attrKeys, ok := tcm.idAttrs[twinID]
if !ok {
return nil
}
delete(tcm.idAttrs, twinID)
for attrKey := range attrKeys {
delete(tcm.attrIds[attrKey], twinID)
}
return nil
}
func (tcm *twinCacheMock) save(def twins.Definition, twinID string) {
for _, attr := range def.Attributes {
attrKey := attr.Channel + attr.Subtopic
if _, ok := tcm.attrIds[attrKey]; !ok {
tcm.attrIds[attrKey] = make(map[string]bool)
}
tcm.attrIds[attrKey][twinID] = true
idKey := twinID
if _, ok := tcm.idAttrs[idKey]; !ok {
tcm.idAttrs[idKey] = make(map[string]bool)
}
tcm.idAttrs[idKey][attrKey] = true
}
}