Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
331 lines (270 loc) · 7.85 KB
/
Copy pathmain.go
File metadata and controls
331 lines (270 loc) · 7.85 KB
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package dynamodb
import (
"errors"
"fmt"
"strconv"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
awsDynamodb "github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/guregu/dynamo"
)
// DynamodbConfig :
type DynamodbConfig struct {
Endpoint string
Region string
}
// DynamodbResponse :
type DynamodbResponse struct {
}
// DynamodbPaged :
type DynamodbPaged struct {
Limit int
PageKeys []*DynamodbAttributeValue
}
// ScanFilter :
type ScanFilter struct {
Expr string
Value interface{}
}
// DynamodbOperator is an operation to apply in key comparisons.
type DynamodbOperator int
type DynamodbAttributeValue struct {
Key string
Value interface{}
}
// Operators used for comparing against the range key in queries.
const (
DynamodbEqual DynamodbOperator = iota
DynamodbNotEqual
DynamodbLess
DynamodbLessOrEqual
DynamodbGreater
DynamodbGreaterOrEqual
DynamodbBeginsWith
DynamodbBetween
)
func (o *DynamodbOperator) value() dynamo.Operator {
if o == nil {
return dynamo.Equal
}
return [...]dynamo.Operator{dynamo.Equal, dynamo.NotEqual, dynamo.Less, dynamo.LessOrEqual, dynamo.Greater, dynamo.GreaterOrEqual, dynamo.BeginsWith, dynamo.Between}[*o]
}
// DynamodbOrder :
type DynamodbOrder bool
// Order
const (
DynamodbOrderAsc DynamodbOrder = true
DynamodbOrderDesc DynamodbOrder = false
)
// DynamodbOptions :
type DynamodbOptions struct {
Operator *DynamodbOperator
Order *DynamodbOrder
}
func (o *DynamodbOrder) value() dynamo.Order {
if o == nil {
return true
}
if *o {
return true
}
return false
}
// LocalSecondaryIndexName :
type LocalSecondaryIndexName string
// DynamodbKey :
type DynamodbKey struct {
Hash func() (string, interface{})
Range func() (string, interface{}, *DynamodbOptions)
LocalSecondaryIndex func() (LocalSecondaryIndexName, string, interface{}, *DynamodbOptions)
}
// Dynamodb :
type Dynamodb interface {
Get(tableName string, key DynamodbKey, result interface{}) error
GetAll(tableName string, key DynamodbKey, result interface{}) error
BatchGet(tableName string, keys []*DynamodbKey, result interface{}) error
Count(tableName string, key DynamodbKey) (int64, error)
Paging(tableName string, key DynamodbKey, paged DynamodbPaged, result interface{}) error
Put(tableName string, item interface{}) (*DynamodbResponse, error)
Delete(tableName string, key DynamodbKey) (*DynamodbResponse, error)
Scan(tableName string, result interface{}, filters ...ScanFilter) error
ExistsTable(name string) bool
CreateTable(name string, entity interface{}) error
CreateTableWithLocalSecondaryIndex(name string, entity interface{}, indexName string) error
DeleteTable(name string) error
}
type dynamodb struct {
db *dynamo.DB
}
func New(sess *session.Session, config *DynamodbConfig) (Dynamodb, error) {
return BuildDynamodb(sess, config)
}
// BuildDynamodb :
func BuildDynamodb(sess *session.Session, config *DynamodbConfig) (Dynamodb, error) {
client, err := connectDynamodb(sess, config)
if err != nil {
return nil, err
}
return &dynamodb{client}, nil
}
func query(table *dynamo.Table, key DynamodbKey) *dynamo.Query {
hKey, hValue := key.Hash()
req := table.Get(hKey, hValue)
if key.Range != nil {
rKey, rValue, option := key.Range()
op := DynamodbEqual
if option != nil {
if _op := option.Operator; _op != nil {
op = *_op
}
if order := option.Order; order != nil {
return req.Range(rKey, op.value(), rValue).Order(order.value())
}
}
return req.Range(rKey, op.value(), rValue)
}
if key.LocalSecondaryIndex != nil {
lName, lKey, lValue, option := key.LocalSecondaryIndex()
op := DynamodbEqual
if option != nil {
if _op := option.Operator; _op != nil {
op = *_op
}
if order := option.Order; order != nil {
return req.Index(string(lName)).Range(lKey, op.value(), lValue).Order(order.value())
}
}
return req.Index(string(lName)).Range(lKey, op.value(), lValue)
}
return req
}
func (con *dynamodb) Get(tableName string, key DynamodbKey, result interface{}) error {
table := con.db.Table(tableName)
return query(&table, key).One(result)
}
func (con *dynamodb) GetAll(tableName string, key DynamodbKey, result interface{}) error {
table := con.db.Table(tableName)
return query(&table, key).All(result)
}
func (con *dynamodb) BatchGet(tableName string, keys []*DynamodbKey, result interface{}) error {
if len(keys) < 1 {
return errors.New("key empty")
}
m := make(map[interface{}]bool)
uniqKeys := []*DynamodbKey{}
for _, key := range keys {
_, hValue := key.Hash()
if !m[hValue] {
m[hValue] = true
uniqKeys = append(uniqKeys, key)
}
}
itemKeyNames := make([]string, 2)
itemKeys := make([]dynamo.Keyed, len(uniqKeys))
for i, k := range uniqKeys {
hKey, hValue := k.Hash()
if i == 0 {
itemKeyNames[0] = hKey
}
if f := k.Range; f != nil {
rKey, rValue, _ := f()
if i == 0 {
itemKeyNames[1] = rKey
}
itemKeys[i] = dynamo.Keys{hValue, rValue}
} else {
itemKeys[i] = dynamo.Keys{hValue}
}
}
table := con.db.Table(tableName)
if err := table.Batch(itemKeyNames...).Get(itemKeys...).All(result); err != nil {
return err
}
return nil
}
func (con *dynamodb) Count(tableName string, key DynamodbKey) (int64, error) {
table := con.db.Table(tableName)
return query(&table, key).Count()
}
func (con *dynamodb) Paging(tableName string, key DynamodbKey, paged DynamodbPaged, result interface{}) error {
pagingKey := map[string]*awsDynamodb.AttributeValue{}
for _, attr := range paged.PageKeys {
switch value := attr.Value.(type) {
case int:
case int8:
case int16:
case int32:
case int64:
pagingKey[attr.Key] = &awsDynamodb.AttributeValue{
N: aws.String(strconv.Itoa(int(value))),
}
case string:
pagingKey[attr.Key] = &awsDynamodb.AttributeValue{
S: aws.String(value),
}
}
}
table := con.db.Table(tableName)
return query(&table, key).StartFrom(pagingKey).Limit(int64(paged.Limit)).All(result)
}
func (con *dynamodb) Put(tableName string, item interface{}) (*DynamodbResponse, error) {
err := con.db.Table(tableName).Put(item).Run()
return &DynamodbResponse{}, err
}
func (con *dynamodb) Delete(tableName string, key DynamodbKey) (*DynamodbResponse, error) {
hKey, hValue := key.Hash()
req := con.db.Table(tableName).Delete(hKey, hValue)
var err error
if key.Range != nil {
rKey, rValue, _ := key.Range()
err = req.Range(rKey, rValue).Run()
} else {
err = req.Run()
}
return &DynamodbResponse{}, err
}
// ScanFilter is only for script. Do not use from application.
// Expr is
// attribute_exists (path)
// attribute_not_exists (path)
// attribute_type (path, type)
// begins_with (path, substr)
// contains (path, operand)
// size (path)
func (con *dynamodb) Scan(tableName string, result interface{}, filters ...ScanFilter) error {
if len(filters) > 0 {
tmp := con.db.Table(tableName).Scan()
for _, f := range filters {
tmp.Filter(f.Expr, f.Value)
}
return tmp.All(result)
}
return con.db.Table(tableName).Scan().All(result)
}
func connectDynamodb(sess *session.Session, dbConfig *DynamodbConfig) (*dynamo.DB, error) {
config := aws.NewConfig().WithRegion(dbConfig.Region)
if len(dbConfig.Endpoint) > 0 {
config = config.WithEndpoint(dbConfig.Endpoint)
}
db := dynamo.New(sess, config)
return db, nil
}
func (con *dynamodb) ExistsTable(name string) bool {
list, _ := con.db.ListTables().All()
for _, tableName := range list {
fmt.Println(tableName)
if tableName == name {
return true
}
}
return false
}
func (con *dynamodb) CreateTable(name string, entity interface{}) error {
return con.db.CreateTable(name, entity).Run()
}
func (con *dynamodb) CreateTableWithLocalSecondaryIndex(name string, entity interface{}, indexName string) error {
return con.db.CreateTable(name, entity).Project(indexName, dynamo.KeysOnlyProjection).Run()
}
func (con *dynamodb) DeleteTable(name string) error {
return con.db.Table(name).DeleteTable().Run()
}