-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
75 lines (60 loc) · 1.89 KB
/
db.py
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
import boto3
dynamodb = boto3.resource('dynamodb')
class DB(object):
def __init__(self, table):
self.table = dynamodb.Table(table)
self.params = ''
def set_rainbow_parameters(self, chain_len, hash_f, reduc_f,
alphabet, max_word_len):
self.params = '-'.join([str(chain_len), hash_f, reduc_f,
alphabet, str(max_word_len)])
def construct_key(self, key):
return ':'.join([key, str(self.params)])
def exists(self, key):
response = self.table.get_item(
Key={
'Key': self.construct_key(key)
}
)
return 'Item' in response
def get(self, key):
response = self.table.get_item(
Key={
'Key': self.construct_key(key)
}
)
return response['Item']['val'] if 'Item' in response else None
def set(self, key, value):
self.table.put_item(
Item={
'Key': self.construct_key(key),
'val': value
}
)
def batch_set(self, items_dict):
with self.table.batch_writer() as batch:
for key in items_dict:
batch.put_item(
Item={
'Key': self.construct_key(key),
'val': items_dict[key]
}
)
class TaskDB(object):
def __init__(self, table):
self.table = dynamodb.Table(table)
def get(self, key):
response = self.table.get_item(
Key={
'ID': key
}
)
return response['Item'] if 'Item' in response else None
def set(self, key, status, result=None):
self.table.put_item(
Item={
'ID': key,
'status': status,
'result': result
}
)