-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstrings.go
308 lines (262 loc) · 9.6 KB
/
strings.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
package redimo
import (
"context"
"strings"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
)
const emptySK = "/"
// GET fetches the value at the given key. If the key does not exist, the ReturnValue will be Empty().
//
// Works similar to https://redis.io/commands/get
func (c Client) GET(key string) (val ReturnValue, err error) {
resp, err := c.ddbClient.GetItemRequest(&dynamodb.GetItemInput{
ConsistentRead: aws.Bool(c.consistentReads),
Key: keyDef{pk: key, sk: emptySK}.toAV(c),
TableName: aws.String(c.table),
}).Send(context.TODO())
if err != nil || len(resp.Item) == 0 {
return
}
val = ReturnValue{resp.Item[vk]}
return
}
// SET stores the given Value at the given key. If called as SET("key", "value", None), SET is
// unconditional and is not expected to fail.
//
// The condition flags IfNotExists and IfAlreadyExists can be specified, and if they are
// the SET becomes conditional and will return false if the condition fails.
//
// Works similar to https://redis.io/commands/set
func (c Client) SET(key string, value Value, flag Flag) (ok bool, err error) {
builder := newExpresionBuilder()
builder.updateSET(vk, value)
if flag == IfNotExists {
builder.addConditionNotExists(c.pk)
}
if flag == IfAlreadyExists {
builder.addConditionExists(c.pk)
}
_, err = c.ddbClient.UpdateItemRequest(&dynamodb.UpdateItemInput{
ConditionExpression: builder.conditionExpression(),
ExpressionAttributeNames: builder.expressionAttributeNames(),
ExpressionAttributeValues: builder.expressionAttributeValues(),
UpdateExpression: builder.updateExpression(),
Key: keyDef{
pk: key,
sk: emptySK,
}.toAV(c),
TableName: aws.String(c.table),
}).Send(context.TODO())
if conditionFailureError(err) {
return false, nil
}
if err != nil {
return
}
return true, nil
}
// SETNX is equivalent to SET(key, value, Flags{IfNotExists})
//
// Works similar to https://redis.io/commands/setnx
func (c Client) SETNX(key string, value Value) (ok bool, err error) {
return c.SET(key, value, IfNotExists)
}
// GETSET gets the value at the key and atomically sets it to a new value.
//
// Works similar to https://redis.io/commands/getset
func (c Client) GETSET(key string, value Value) (oldValue ReturnValue, err error) {
builder := newExpresionBuilder()
builder.updateSET(vk, value)
resp, err := c.ddbClient.UpdateItemRequest(&dynamodb.UpdateItemInput{
ConditionExpression: builder.conditionExpression(),
ExpressionAttributeNames: builder.expressionAttributeNames(),
ExpressionAttributeValues: builder.expressionAttributeValues(),
UpdateExpression: builder.updateExpression(),
Key: keyDef{
pk: key,
sk: emptySK,
}.toAV(c),
ReturnValues: dynamodb.ReturnValueAllOld,
TableName: aws.String(c.table),
}).Send(context.TODO())
if err != nil || len(resp.Attributes) == 0 {
return
}
oldValue = parseItem(resp.Attributes, c).val
return
}
// MGET fetches the given keys atomically in a transaction. The call is limited to 25 keys and 4MB.
// See https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_TransactGetItems.html
//
// Works similar to https://redis.io/commands/mget
func (c Client) MGET(keys ...string) (values map[string]ReturnValue, err error) {
values = make(map[string]ReturnValue)
inputRequests := make([]dynamodb.TransactGetItem, len(keys))
for i, key := range keys {
inputRequests[i] = dynamodb.TransactGetItem{
Get: &dynamodb.Get{
Key: keyDef{
pk: key,
sk: emptySK,
}.toAV(c),
ProjectionExpression: aws.String(strings.Join([]string{vk, c.pk}, ", ")),
TableName: aws.String(c.table),
},
}
}
resp, err := c.ddbClient.TransactGetItemsRequest(&dynamodb.TransactGetItemsInput{
TransactItems: inputRequests,
}).Send(context.TODO())
if err != nil {
return
}
for _, item := range resp.Responses {
pi := parseItem(item.Item, c)
values[pi.pk] = pi.val
}
return
}
// MSET sets the given keys and values atomically in a transaction. The call is limited to 25 keys and 4MB.
// See https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_TransactWriteItems.html
//
// Works similar to https://redis.io/commands/mset
func (c Client) MSET(data map[string]Value) (err error) {
_, err = c.mset(data, Flags{})
return
}
// MSETNX sets the given keys and values atomically in a transaction, but only if none of the given
// keys exist. If one or more of the keys already exist, nothing will be changed and MSETNX will return false.
//
// Works similar to https://redis.io/commands/msetnx
func (c Client) MSETNX(data map[string]Value) (ok bool, err error) {
return c.mset(data, Flags{IfNotExists})
}
func (c Client) mset(data map[string]Value, flags Flags) (ok bool, err error) {
inputs := make([]dynamodb.TransactWriteItem, 0, len(data))
for k, v := range data {
builder := newExpresionBuilder()
if flags.has(IfNotExists) {
builder.addConditionNotExists(c.pk)
}
builder.updateSET(vk, v)
inputs = append(inputs, dynamodb.TransactWriteItem{
Update: &dynamodb.Update{
ConditionExpression: builder.conditionExpression(),
ExpressionAttributeNames: builder.expressionAttributeNames(),
ExpressionAttributeValues: builder.expressionAttributeValues(),
Key: keyDef{
pk: k,
sk: emptySK,
}.toAV(c),
TableName: aws.String(c.table),
UpdateExpression: builder.updateExpression(),
},
})
}
_, err = c.ddbClient.TransactWriteItemsRequest(&dynamodb.TransactWriteItemsInput{
ClientRequestToken: nil,
TransactItems: inputs,
}).Send(context.TODO())
if conditionFailureError(err) {
return false, nil
}
if err != nil {
return false, err
}
return true, nil
}
// INCRBYFLOAT increments the number stored at the key with the given float64 delta (n = n + delta) and returns
// the new value. If the key does not exist, it will be initialized with zero before applying
// the operation.
//
// The delta can be positive or negative, and a zero delta is effectively a no-op.
//
// If there is an existing value at the key with a non-numeric type (string, bytes, etc.)
// the operation will throw an error. If the existing value is numeric, the operation
// can continue irrespective of how it was initially set.
//
// Cost is O(1) or 1 WCU.
//
// Works similar to https://redis.io/commands/incrbyfloat
func (c Client) INCRBYFLOAT(key string, delta float64) (after float64, err error) {
rv, err := c.incr(key, FloatValue{delta})
if err == nil {
after = rv.Float()
}
return
}
func (c Client) incr(key string, value Value) (newValue ReturnValue, err error) {
builder := newExpresionBuilder()
builder.keys[vk] = struct{}{}
resp, err := c.ddbClient.UpdateItemRequest(&dynamodb.UpdateItemInput{
ExpressionAttributeNames: builder.expressionAttributeNames(),
ExpressionAttributeValues: map[string]dynamodb.AttributeValue{
":delta": value.ToAV(),
},
Key: keyDef{pk: key, sk: emptySK}.toAV(c),
ReturnValues: dynamodb.ReturnValueAllNew,
TableName: aws.String(c.table),
UpdateExpression: aws.String("ADD #val :delta"),
}).Send(context.TODO())
if err == nil {
newValue = ReturnValue{resp.UpdateItemOutput.Attributes[vk]}
}
return
}
// INCR increments the number stored at the key by 1 (n = n + 1) and returns the new value. If the
// key does not exist, it will be initialized with zero before applying the operation.
//
// If there is an existing value at the key with a non-numeric type (string, bytes, etc.)
// the operation will throw an error. If the existing value is numeric, the operation
// can continue irrespective of how it was initially set.
//
// Cost is O(1) or 1 WCU.
//
// Works similar to https://redis.io/commands/incr
func (c Client) INCR(key string) (after int64, err error) {
return c.INCRBY(key, 1)
}
// DECR decrements the number stored at the key by 1 (n = n - 1) and returns the new value. If the
// key does not exist, it will be initialized with zero before applying the operation.
//
// If there is an existing value at the key with a non-numeric type (string, bytes, etc.)
// the operation will throw an error. If the existing value is numeric, the operation
// can continue irrespective of how it was initially set.
//
// Cost is O(1) or 1 WCU.
//
// Works similar to https://redis.io/commands/decr
func (c Client) DECR(key string) (after int64, err error) {
return c.INCRBY(key, -1)
}
// INCRBY increments the number stored at the key with the given delta (n = n + delta) and returns the new value. If the
// key does not exist, it will be initialized with zero before applying the operation.
//
// If there is an existing value at the key with a non-numeric type (string, bytes, etc.)
// the operation will throw an error. If the existing value is numeric, the operation
// can continue irrespective of how it was initially set.
//
// Cost is O(1) or 1 WCU.
//
// Works similar to https://redis.io/commands/incrby
func (c Client) INCRBY(key string, delta int64) (after int64, err error) {
rv, err := c.incr(key, IntValue{delta})
if err == nil {
after = rv.Int()
}
return
}
// DECRBY decrements the number stored at the key with the given delta (n = n - delta) and returns the new value. If the
// key does not exist, it will be initialized with zero before applying the operation.
//
// If there is an existing value at the key with a non-numeric type (string, bytes, etc.)
// the operation will throw an error. If the existing value is numeric, the operation
// can continue irrespective of how it was initially set.
//
// Cost is O(1) or 1 WCU.
//
// Works similar to https://redis.io/commands/decrby
func (c Client) DECRBY(key string, delta int64) (after int64, err error) {
return c.INCRBY(key, -delta)
}