forked from ccbrown/gggtracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamodb_database.go
More file actions
142 lines (126 loc) · 3.39 KB
/
Copy pathdynamodb_database.go
File metadata and controls
142 lines (126 loc) · 3.39 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
package server
import (
"context"
"encoding/base64"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
)
type DynamoDBDatabase struct {
client *dynamodb.Client
tableName string
}
func NewDynamoDBDatabase(client *dynamodb.Client, tableName string) (*DynamoDBDatabase, error) {
return &DynamoDBDatabase{
client: client,
tableName: tableName,
}, nil
}
func dynamoDBActivityHashKey(locale *Locale) []byte {
return []byte("activity_by_locale:" + locale.Subdomain)
}
func (db *DynamoDBDatabase) AddActivity(activity []Activity) error {
for _, locale := range Locales {
var remaining []Activity
for _, a := range activity {
if locale.ActivityFilter(a) {
remaining = append(remaining, a)
}
}
for len(remaining) > 0 {
batch := remaining
const maxBatchSize = 25
if len(batch) > maxBatchSize {
batch = batch[:maxBatchSize]
}
writeRequests := make([]dynamodb.WriteRequest, len(batch))
for i, a := range batch {
k, v, err := marshalActivity(a)
if err != nil {
return err
}
writeRequests[i] = dynamodb.WriteRequest{
PutRequest: &dynamodb.PutRequest{
Item: map[string]dynamodb.AttributeValue{
"hk": dynamodb.AttributeValue{
B: dynamoDBActivityHashKey(locale),
},
"rk": dynamodb.AttributeValue{
B: []byte(k),
},
"v": dynamodb.AttributeValue{
B: []byte(v),
},
},
},
}
}
unprocessed := map[string][]dynamodb.WriteRequest{
db.tableName: writeRequests,
}
for len(unprocessed) > 0 {
result, err := db.client.BatchWriteItemRequest(&dynamodb.BatchWriteItemInput{
RequestItems: unprocessed,
}).Send(context.Background())
if err != nil {
return err
}
unprocessed = result.UnprocessedItems
}
remaining = remaining[len(batch):]
}
}
return nil
}
func (db *DynamoDBDatabase) Activity(locale *Locale, start string, count int) ([]Activity, string, error) {
var activity []Activity
var startKey map[string]dynamodb.AttributeValue
if start != "" {
rk, _ := base64.RawURLEncoding.DecodeString(start)
startKey = map[string]dynamodb.AttributeValue{
"hk": dynamodb.AttributeValue{
B: dynamoDBActivityHashKey(locale),
},
"rk": dynamodb.AttributeValue{
B: rk,
},
}
}
condition := "hk = :hash"
attributeValues := map[string]dynamodb.AttributeValue{
":hash": dynamodb.AttributeValue{
B: dynamoDBActivityHashKey(locale),
},
}
for len(activity) < count {
result, err := db.client.QueryRequest(&dynamodb.QueryInput{
TableName: aws.String(db.tableName),
KeyConditionExpression: aws.String(condition),
ExpressionAttributeValues: attributeValues,
ExclusiveStartKey: startKey,
Limit: aws.Int64(int64(count - len(activity))),
ScanIndexForward: aws.Bool(false),
}).Send(context.Background())
if err != nil {
return nil, "", err
}
for _, item := range result.Items {
if a, err := unmarshalActivity(item["rk"].B, item["v"].B); err != nil {
return nil, "", err
} else if a != nil {
activity = append(activity, a)
}
}
if result.LastEvaluatedKey == nil {
break
}
startKey = result.LastEvaluatedKey
}
var next string
if startKey != nil {
next = base64.RawURLEncoding.EncodeToString(startKey["rk"].B)
}
return activity, next, nil
}
func (db *DynamoDBDatabase) Close() error {
return nil
}