-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmemory.go
129 lines (110 loc) · 3.45 KB
/
memory.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
package dynamodb
import (
"errors"
)
type items map[string]Item
type memory struct {
mapping
tables map[string]items
}
func NewMemoryDB() DynamoDB {
return &memory{mapping: make(mapping)}
}
func (b *memory) BatchGetItem(requestedItems map[string]KeysAndAttributes, options *BatchGetItemOptions) (*BatchGetItemResult, error) {
return nil, errors.New("NYI")
}
func (b *memory) BatchWriteItem(requestedItems map[string]WriteRequest, options *BatchWriteItemOptions) (*BatchWriteItemResult, error) {
return nil, errors.New("NYI")
}
func (b *memory) CreateTable(tableName string, attributeDefinitions []AttributeDefinition, keySchema []KeySchemaElement, ProvisionedThroughput ProvisionedThroughput, options *CreateTableOptions) (*CreateTableResult, error) {
if b.tables == nil {
b.tables = make(map[string]items)
}
b.tables[tableName] = make(items)
td := TableDescription{}
td.TableStatus = "ACTIVE"
return &CreateTableResult{TableDescription: &td}, nil
}
func (m *memory) UpdateItem(tableName string, key Key, options *UpdateItemOptions) (*UpdateItemResult, error) {
return nil, errors.New("NYI")
}
func (db *memory) UpdateTable(tableName string, provisionedThroughput ProvisionedThroughput, options *UpdateTableOptions) (*UpdateTableResult, error) {
return nil, errors.New("NYI")
}
func (db *memory) DescribeTable(tableName string, options *DescribeTableOptions) (*DescribeTableResult, error) {
td := TableDescription{}
td.TableStatus = "ACTIVE"
return &DescribeTableResult{Table: &td}, nil
}
func (db *memory) DeleteTable(tableName string, options *DeleteTableOptions) (*DeleteTableResult, error) {
delete(db.tables, tableName)
td := TableDescription{}
// TODO
return &DeleteTableResult{TableDescription: &td}, nil
}
func (b *memory) PutItem(tableName string, item Item, options *PutItemOptions) (*PutItemResult, error) {
if b.tables == nil {
return nil, errors.New("no tables")
}
t, ok := b.tables[tableName]
if !ok {
return nil, errors.New("no such table")
}
pk := ""
hash := b.mapping[tableName].TableDescription.KeySchema[0]
m := item[hash.AttributeName]
if len(m) == 1 {
for _, v := range m {
pk = v
}
} else {
panic("boo")
}
t[pk] = item
r := PutItemResult{} // TODO
return &r, nil
}
func (b *memory) DeleteItem(tableName string, key Key, options *DeleteItemOptions) (*DeleteItemResult, error) {
return nil, errors.New("NYI")
}
func (b *memory) GetItem(tableName string, key Key, options *GetItemOptions) (*GetItemResult, error) {
if b.tables == nil {
return nil, errors.New("no tables")
}
t, ok := b.tables[tableName]
if !ok {
return nil, errors.New("no such table")
}
pk := ""
hash := b.mapping[tableName].TableDescription.KeySchema[0]
m := key[hash.AttributeName]
if len(m) == 1 {
for _, v := range m {
pk = v
}
} else {
panic("boo")
}
i := t[pk]
return &GetItemResult{Item: &i}, nil
}
func (b *memory) ListTables(options *ListTablesOptions) (*ListTablesResult, error) {
return nil, errors.New("NYI")
}
func (b *memory) Scan(tableName string, options *ScanOptions) (scanResult *ScanResult, err error) {
if b.tables == nil {
return nil, errors.New("no tables")
}
t, ok := b.tables[tableName]
if !ok {
return nil, errors.New("no such table")
}
var items []Item
for _, item := range t {
items = append(items, item)
}
return &ScanResult{Count: len(t), ScannedCount: len(t), Items: items}, nil
}
func (m *memory) Query(tableName string, options *QueryOptions) (*QueryResult, error) {
return nil, errors.New("NYI")
}