-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatabase.py
More file actions
130 lines (119 loc) · 3.55 KB
/
database.py
File metadata and controls
130 lines (119 loc) · 3.55 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
from singleton import Singleton
class Database(object):
"""
Represents the In Memory Key Value Store.
This class is a Singleton class. There will be
on Database object at a time/process.
"""
__metaclass__ = Singleton
def __init__(self, **kwargs):
"""
self.store = {
"11111111": {
"key": {
"encoding": "string",
"data": "11111111"
},
"value":{
"encoding": "string",
"data": "abcedefgh"
},
},
"1010101010": {
"key": {
"encoding": "binary",
"data": "1010101010"
},
"value":{
"encoding": "binary",
"data": "1111100000"
},
}
}
"""
self.store = {}
def query(self, **kwargs):
"""
Expects list of keys in kwargs["data"]
data = [{
encoding: 'binary',
'data': ##1111111
},
{
encoding: 'string',
'data': ##222222
}
]
Return = [
{key: {
encoding: 'binary',
'data': ##1111111
},
value: True
},
{key: {
encoding: 'string',
'data': ##12324
},
value: False
}
]
"""
result = []
getAllData = kwargs.get("getAllData")
for key in kwargs.get("data"):
value = self.store.get(key["data"])
if getAllData:
result.append(value)
else:
result.append({
"key": key,
"value": True if value else False
})
return result
def fetch(self, **kwargs):
"""
Returns all the key value pairs from database
"""
data = kwargs.get("data")
if data:
self.query(**{"data": data, "getAllData": True})
return self.store.values()
def set(self, **kwargs):
"""
Creates the given key value pairs if key is not present else
Updates the given key Value pairs.
Expects kwargs['data']
[
{
key: {
encoding: 'binary',
'data': ##1111111
},
value: {
encoding: 'binary',
'data': "1010101010"
}
},
{
key: {
encoding: 'string',
'data': ##12324
},
value: {
encoding: 'string',
'data': 'abcdefg'
}
}
]
"""
keysAdded = 0
keysFailed = []
data = kwargs.get('data')
for keyValuePair in data:
try:
self.store[keyValuePair["key"]["data"]] = keyValuePair
keysAdded += 1
except Exception:
keysFailed.append(keyValuePair)
return keysAdded, keysFailed