forked from edgexfoundry/app-functions-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
245 lines (203 loc) · 6.62 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
/*******************************************************************************
* Copyright 2019 Dell Inc.
*
* 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.
*******************************************************************************/
// redis provides the Redis implementation of the StoreClient interface.
package redis
import (
"errors"
"fmt"
"sync"
"time"
"github.com/migelankodra/app-functions-sdk-go/internal/store/contracts"
"github.com/migelankodra/app-functions-sdk-go/internal/store/db"
"github.com/migelankodra/app-functions-sdk-go/internal/store/db/interfaces"
"github.com/migelankodra/app-functions-sdk-go/internal/store/db/redis/models"
"github.com/gomodule/redigo/redis"
)
var currClient *Client // a singleton so Readings can be de-referenced
var once sync.Once
const nameSpace = "store"
// Client provides an implementation for the Client interface for Redis
type Client struct {
Pool *redis.Pool // A thread-safe pool of connections to Redis
BatchSize int
}
// Store persists a stored object to the data store. Three ("Three shall be the number thou shalt
// count, and the number of the counting shall be three") keys are used:
// * the object id to point to a STRING which is the marshal'ed JSON.
// * the object AppServiceKey to point to a SET containing all object ids associated with this
// app service. Note the key is prefixed to avoid key collisions.
// * the object id to point to a HASH which contains the object AppServiceKey.
func (c Client) Store(o contracts.StoredObject) (string, error) {
err := o.ValidateContract(false)
if err != nil {
return "", err
}
conn := c.Pool.Get()
defer conn.Close()
exists, err := redis.Bool(conn.Do("EXISTS", o.ID))
if err != nil {
return "", err
} else if exists {
return "", errors.New("object exists in database")
}
var model models.StoredObject
model.FromContract(o)
json, err := model.MarshalJSON()
if err != nil {
return "", err
}
_ = conn.Send("MULTI")
_ = conn.Send("SET", model.ID, json)
_ = conn.Send("SADD", nameSpace+":idl:"+model.AppServiceKey, model.ID)
_ = conn.Send("HSET", nameSpace+":ask:"+model.ID, "ASK", model.AppServiceKey)
_, err = conn.Do("EXEC")
if err != nil {
return "", err
}
if model.ID == "" {
return "", errors.New("no ID produced")
}
return model.ID, nil
}
// RetrieveFromStore gets an object from the data store.
func (c Client) RetrieveFromStore(appServiceKey string) (objects []contracts.StoredObject, err error) {
// do not satisfy requests for a blank ASK
if appServiceKey == "" {
return nil, errors.New("no AppServiceKey provided")
}
conn := c.Pool.Get()
defer conn.Close()
ids, err := redis.Values(conn.Do("SMEMBERS", nameSpace+":idl:"+appServiceKey))
if err != nil {
return nil, err
}
if len(ids) == 0 {
return nil, nil
}
values, err := redis.ByteSlices(conn.Do("MGET", ids...))
if err != nil {
return nil, err
}
var model models.StoredObject
for _, bytes := range values {
err = model.UnmarshalJSON(bytes)
if err != nil {
return nil, err
}
objects = append(objects, model.ToContract())
}
return objects, nil
}
// Update replaces the data currently in the store with the provided data.
func (c Client) Update(o contracts.StoredObject) error {
err := o.ValidateContract(true)
if err != nil {
return err
}
conn := c.Pool.Get()
defer conn.Close()
// retrieve the current AppServiceKey for this store object
currentASK, err := redis.String(conn.Do("HGET", nameSpace+":ask:"+o.ID, "ASK"))
if err != nil {
return err
}
_ = conn.Send("MULTI")
// ASK has changed, update the ASK registry
if o.AppServiceKey != currentASK {
_ = conn.Send("SREM", nameSpace+":idl:"+currentASK, o.ID)
_ = conn.Send("SADD", nameSpace+":idl:"+o.AppServiceKey, o.ID)
_ = conn.Send("HSET", nameSpace+":ask:"+o.ID, "ASK", o.AppServiceKey)
}
var update models.StoredObject
update.FromContract(o)
json, err := update.MarshalJSON()
if err != nil {
return err
}
_ = conn.Send("SET", update.ID, json)
_, err = conn.Do("EXEC")
if err != nil {
return err
}
return nil
}
// RemoveFromStore removes an object from the data store.
func (c Client) RemoveFromStore(o contracts.StoredObject) error {
err := o.ValidateContract(true)
if err != nil {
return err
}
conn := c.Pool.Get()
defer conn.Close()
_ = conn.Send("MULTI")
// remove the object's representation
_ = conn.Send("UNLINK", o.ID)
// remove the association with the ASK
_ = conn.Send("SREM", nameSpace+":idl:"+o.AppServiceKey, o.ID)
_ = conn.Send("UNLINK", nameSpace+":ask:"+o.ID)
res, err := redis.Values(conn.Do("EXEC"))
if err != nil {
return err
}
exists, _ := redis.Bool(res[0], nil)
if !exists {
return errors.New("could not remove object from store")
}
return nil
}
// Disconnect ends the connection.
func (c Client) Disconnect() error {
return c.Pool.Close()
}
// NewClient provides a factory for building a StoreClient
func NewClient(config db.DatabaseInfo) (interfaces.StoreClient, error) {
var retErr error
once.Do(func() {
connectionString := fmt.Sprintf("%s:%d", config.Host, config.Port)
connectTimeout, err := time.ParseDuration(config.Timeout)
if err != nil {
retErr = fmt.Errorf("config.Timeout failed to parse: %v", err)
return
}
opts := []redis.DialOption{
redis.DialPassword(config.Password),
redis.DialConnectTimeout(connectTimeout),
}
dialFunc := func() (redis.Conn, error) {
conn, err := redis.Dial(
"tcp", connectionString, opts...,
)
if err != nil {
return nil, fmt.Errorf("Could not dial Redis: %s", err)
}
return conn, nil
}
currClient = &Client{
Pool: &redis.Pool{
IdleTimeout: connectTimeout,
/* The current implementation processes nested structs using concurrent connections.
* With the deepest nesting level being 3, three shall be the number of maximum open
* idle connections in the pool, to allow reuse.
* TODO: Once we have a concurrent benchmark, this should be revisited.
* TODO: Longer term, once the objects are clean of external dependencies, the use
* of another serializer should make this moot.
*/
MaxIdle: config.MaxIdle,
Dial: dialFunc,
},
BatchSize: config.BatchSize,
}
})
return currClient, retErr
}