forked from brightcove/playback_ddbsync
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.go
More file actions
151 lines (132 loc) · 3.38 KB
/
db.go
File metadata and controls
151 lines (132 loc) · 3.38 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
package ddbsync
import (
"errors"
"fmt"
"strconv"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbiface"
"github.com/zencoder/ddbsync/models"
)
type database struct {
client AWSDynamoer
tableName string
}
func NewDatabaseFromDDBAPI(ddb dynamodbiface.DynamoDBAPI, tableName string) DBer {
return &database{
client: ddb,
tableName: tableName,
}
}
func NewDatabase(tableName string, region string, endpoint string, disableSSL bool) DBer {
return &database{
client: dynamodb.New(session.New(&aws.Config{
Endpoint: &endpoint,
Region: ®ion,
DisableSSL: &disableSSL,
})),
tableName: tableName,
}
}
var _ DBer = (*database)(nil) // Forces compile time checking of the interface
var _ AWSDynamoer = (*dynamodb.DynamoDB)(nil) // Forces compile time checking of the interface
type DBer interface {
Put(string, int64) error
Get(string) (*models.Item, error)
Delete(string) error
}
type AWSDynamoer interface {
PutItem(*dynamodb.PutItemInput) (*dynamodb.PutItemOutput, error)
Query(*dynamodb.QueryInput) (*dynamodb.QueryOutput, error)
DeleteItem(*dynamodb.DeleteItemInput) (*dynamodb.DeleteItemOutput, error)
}
func (db *database) Put(name string, created int64) error {
i := map[string]*dynamodb.AttributeValue{
"Name": {
S: aws.String(name),
},
"Created": {
N: aws.String(strconv.FormatInt(created, 10)),
},
}
e := map[string]*dynamodb.ExpectedAttributeValue{
"Name": {
Exists: aws.Bool(false),
},
}
pit := &dynamodb.PutItemInput{
TableName: aws.String(db.tableName),
Item: i,
Expected: e,
}
_, err := db.client.PutItem(pit)
return err
}
func (db *database) Get(name string) (*models.Item, error) {
kc := map[string]*dynamodb.Condition{
"Name": {
AttributeValueList: []*dynamodb.AttributeValue{
{
S: aws.String(name),
},
},
ComparisonOperator: aws.String("EQ"),
},
}
qi := &dynamodb.QueryInput{
TableName: aws.String(db.tableName),
ConsistentRead: aws.Bool(true),
Select: aws.String("SPECIFIC_ATTRIBUTES"),
AttributesToGet: []*string{aws.String("Name"), aws.String("Created")},
KeyConditions: kc,
}
qo, err := db.client.Query(qi)
if err != nil {
return nil, err
}
// Make sure that no or 1 item is returned from DynamoDB
if qo.Count != nil {
if *qo.Count == 0 {
return nil, fmt.Errorf("No item for Name, %s", name)
} else if *qo.Count > 1 {
return nil, fmt.Errorf("Expected only 1 item returned from Dynamo, got %d", *qo.Count)
}
} else {
return nil, errors.New("Count not returned")
}
if len(qo.Items) < 1 || qo.Items[0] == nil {
return nil, errors.New("No item returned, count is invalid.")
}
n := ""
c := int64(0)
for index, element := range qo.Items[0] {
if index == "Name" {
n = *element.S
}
if index == "Created" {
c, _ = strconv.ParseInt(*element.N, 10, 0)
}
}
if n == "" || c == 0 {
return nil, errors.New("The Name and Created keys were not found in the Dynamo result")
}
i := &models.Item{
Name: n,
Created: c,
}
return i, nil
}
func (db *database) Delete(name string) error {
k := map[string]*dynamodb.AttributeValue{
"Name": {
S: aws.String(name),
},
}
dii := &dynamodb.DeleteItemInput{
TableName: aws.String(db.tableName),
Key: k,
}
_, err := db.client.DeleteItem(dii)
return err
}