forked from dugancathal/dynago
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest_batch_write_item.go
109 lines (89 loc) · 2.27 KB
/
request_batch_write_item.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
package dynago
type batchWriteItemRequest struct {
RequestItems BatchWriteTableMap
}
type BatchWriteTableMap map[string][]*BatchWriteTableEntry
type BatchWriteTableEntry struct {
DeleteRequest *batchDelete `json:",omitempty"`
PutRequest *batchPut `json:",omitempty"`
}
// Set this table entry as a delete request
func (e *BatchWriteTableEntry) SetDelete(key Document) {
e.DeleteRequest = &batchDelete{key}
}
func (e *BatchWriteTableEntry) SetPut(item Document) {
e.PutRequest = &batchPut{item}
}
type batchDelete struct {
Key Document
}
type batchPut struct {
Item Document
}
type batchAction struct {
next *batchAction
table string
item Document
}
func newBatchWrite(client *Client) *BatchWrite {
return &BatchWrite{
client: client,
}
}
type BatchWrite struct {
client *Client
puts *batchAction
deletes *batchAction
}
/*
Add some number of puts for a table.
*/
func (b BatchWrite) Put(table string, items ...Document) *BatchWrite {
b.addActions(&b.puts, table, items)
return &b
}
/*
Add some number of deletes for a table.
*/
func (b BatchWrite) Delete(table string, keys ...Document) *BatchWrite {
b.addActions(&b.deletes, table, keys)
return &b
}
func (b *BatchWrite) addActions(list **batchAction, table string, items []Document) {
head := *list
for _, item := range items {
head = &batchAction{head, table, item}
}
*list = head
}
func (b *BatchWrite) Execute() (*BatchWriteResult, error) {
return b.client.executor.BatchWriteItem(b)
}
// Build the table map that is represented by this BatchWrite
func (b *BatchWrite) buildTableMap() (m BatchWriteTableMap) {
m = BatchWriteTableMap{}
ensure := func(table string) (r *BatchWriteTableEntry) {
r = &BatchWriteTableEntry{}
m[table] = append(m[table], r)
return
}
for put := b.puts; put != nil; put = put.next {
ensure(put.table).SetPut(put.item)
}
for d := b.deletes; d != nil; d = d.next {
ensure(d.table).SetDelete(d.item)
}
return
}
func (e *awsExecutor) BatchWriteItem(b *BatchWrite) (result *BatchWriteResult, err error) {
req := batchWriteItemRequest{
RequestItems: b.buildTableMap(),
}
result = &BatchWriteResult{}
err = e.makeRequestUnmarshal("BatchWriteItem", req, result)
return
}
type BatchWriteResult struct {
UnprocessedItems BatchWriteTableMap
// TODO ConsumedCapacity
}