-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamodb.py
More file actions
57 lines (51 loc) · 1.76 KB
/
Copy pathdynamodb.py
File metadata and controls
57 lines (51 loc) · 1.76 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
import boto3
from .base import Key, is_none, Map, Optional, UUID
class DynamoMap(Map):
def open(self):
self._db = self._config.dynamodb
table_query = self._db.list_tables(
ExclusiveStartTableName=self._config.table[:-1],
Limit=1
)['TableNames']
table_exists = len(
table_query) == 1 and table_query[0] == self._config.table
if not table_exists:
self._db.create_table(
TableName=self._config.table,
KeySchema=[
{'AttributeName': 'uuid', 'KeyType': 'HASH'},
{'AttributeName': 'time', 'KeyType': 'RANGE'}
],
AttributeDefinitions=[
{'AttributeName': 'uuid', 'AttributeType': 'B'},
{'AttributeName': 'time', 'AttributeType': 'N'}
],
BillingMode='PAY_PER_REQUEST'
)
return self
def _put(self, key: Key, value: bytes) -> Key:
item = {
'uuid': {'B': key.uuid.bytes_le},
'time': {'N': str(key.time)},
'kind': {'S': key.kind}
}
if not is_none(value):
item['value'] = {'B': value}
self._db.put_item(
TableName=self._config.table,
Item=item
)
return key
def _get(self, uuid: UUID, time: int) -> Optional[bytes]:
value = self._db.get_item(
TableName=self._config.table,
Key={
'uuid': {'B': uuid.bytes_le},
'time': {'N': str(time)}
},
ConsistentRead=False
)['Item']
return value['value']['B'] if 'value' in value.keys() else None
def close(self):
del(self._db)
return self