-
Notifications
You must be signed in to change notification settings - Fork 0
/
inMemoryDB.py
189 lines (139 loc) · 5.36 KB
/
inMemoryDB.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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
__author__ = 'kathan'
from uuid import uuid4
DATABASE = {} # main source of storage
COUNT_DB = {} # main source of count values
TRANSACTION_STACK = [] # transaction stack with uuid
UNCOMMITED_DATABASE = {} # uncommited data per transaction
UNCOMMITED_COUNT_DB = {} # uncommited count data per transaction
# sets value in database and updates COUNT_DB
def set_value(key, value, transaction=None):
if transaction:
if key in UNCOMMITED_DATABASE[transaction]:
#get the count of current key and subtract 1
count_key = UNCOMMITED_DATABASE[transaction][key]
if UNCOMMITED_COUNT_DB[transaction][count_key] > 0:
UNCOMMITED_COUNT_DB[transaction][count_key] = UNCOMMITED_COUNT_DB[transaction][count_key] - 1
else:
UNCOMMITED_COUNT_DB[transaction][count_key] = 0
UNCOMMITED_DATABASE[transaction][key] = value
if value in UNCOMMITED_COUNT_DB[transaction]:
UNCOMMITED_COUNT_DB[transaction][value] = UNCOMMITED_COUNT_DB[transaction][value] + 1
else:
UNCOMMITED_COUNT_DB[transaction][value] = 1
else:
UNCOMMITED_DATABASE[transaction][key] = value
if value in UNCOMMITED_COUNT_DB[transaction]:
UNCOMMITED_COUNT_DB[transaction][value] = UNCOMMITED_COUNT_DB[transaction][value] + 1
else:
UNCOMMITED_COUNT_DB[transaction][value] = 1
else:
if key in DATABASE:
#get the count of current key and subtract 1
count_key = DATABASE[key]
COUNT_DB[count_key] = COUNT_DB[count_key] - 1 if COUNT_DB[count_key] > 0 else 0
DATABASE[key] = value
if value in COUNT_DB:
COUNT_DB[value] = COUNT_DB[value] + 1
else:
COUNT_DB[value] = 1
else:
DATABASE[key] = value
if value in COUNT_DB:
COUNT_DB[value] = COUNT_DB[value] + 1
else:
COUNT_DB[value] = 1
# gets value from database if key is present else returns null
def get_value(key, transaction=None):
if transaction:
if key in UNCOMMITED_DATABASE[transaction]:
return UNCOMMITED_DATABASE[transaction][key]
else:
if key in DATABASE:
return DATABASE[key]
else:
return None
else:
if key in DATABASE:
return DATABASE[key]
else:
return None
# deletes key from database if exists and update COUNT_DB
def delete_key(key, transaction=None):
if transaction:
if key in UNCOMMITED_DATABASE[transaction]:
UNCOMMITED_COUNT_DB[transaction][UNCOMMITED_DATABASE[transaction][key]] = UNCOMMITED_COUNT_DB[transaction][UNCOMMITED_DATABASE[transaction][key]] - 1
del UNCOMMITED_DATABASE[transaction][key]
else:
if key in DATABASE:
COUNT_DB[DATABASE[key]] = COUNT_DB[DATABASE[key]] - 1
del DATABASE[key]
# returns the count of the number of occurance of the key
def get_count(value, transaction=None):
if transaction:
total_count = get_count(value)
for trans in TRANSACTION_STACK:
if value in UNCOMMITED_COUNT_DB[trans]:
total_count = total_count + UNCOMMITED_COUNT_DB[trans][value]
if trans == transaction:
break
return total_count
else:
if value in COUNT_DB:
return COUNT_DB[value]
else:
return 0
# starts transaction which one can roll back
def start_transaction():
transaction_hash = uuid4()
TRANSACTION_STACK.append(transaction_hash)
UNCOMMITED_DATABASE[transaction_hash] = {}
UNCOMMITED_COUNT_DB[transaction_hash] = {}
return transaction_hash
def commit_data():
uncommitted_data = {}
uncommited_count_data = {}
for trans in TRANSACTION_STACK:
uncommitted_data.update(UNCOMMITED_DATABASE[trans])
uncommited_count_data.update(UNCOMMITED_COUNT_DB[trans])
DATABASE.update(uncommitted_data)# A.update(B)
COUNT_DB.update(uncommited_count_data)
def rollback_data():
transaction_value = TRANSACTION_STACK.pop()
del UNCOMMITED_COUNT_DB[transaction_value]
del UNCOMMITED_DATABASE[transaction_value]
def test_case_just_get_set_count():
set_value('a', 10)
set_value('b', 10)
print get_count(10)
print get_count(20)
delete_key('a')
print get_count(10)
set_value('b', 30)
print get_count(10)
print get_count(30)
print get_count(20)
def test_case_for_transaction():
# transaction cases
transaction_value_a = start_transaction()
set_value('a', 10, transaction_value_a)
print get_value('a', transaction_value_a)
transaction_value_b = start_transaction()
set_value('a', 20, transaction_value_b)
print get_value('a', transaction_value_b)
rollback_data()
print get_value('a', transaction_value_a)
rollback_data()
print get_value('a')
def test_case_for_transaction_count():
set_value('a', 10)
trans_a = start_transaction()
print get_count(10, trans_a)
trans_b = start_transaction()
delete_key('a', trans_b)
print get_count(10, trans_b)
rollback_data()
print get_count(10, trans_a)
if __name__ == "__main__":
#test_case_just_get_set_count()
#test_case_for_transaction()
test_case_for_transaction_count()