-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
349 lines (281 loc) · 7.97 KB
/
app.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#app.py
from flask import Flask
from datetime import datetime
from flask import request
from flask import jsonify
import time
import pickle
app = Flask(__name__)
#Global variable
table_of_string = {}
table_of_list = {}
table_of_set = {}
class StringExpire:
def __init__(self, data):
self.data = data
self.timeout = 0
self.timestamp = 0
def __init__(self, data, timeout, timestamp):
self.data = data
self.timeout = timeout
self.timestamp = timestamp
def __repr__(self):
return self.data + str(self.timeout) + str(self.timestamp)
def __str__(self):
return self.data
def getData(self):
return self.data
def getTimeout(self):
return self.timeout
def getTimestamp(self):
return self.timestamp
def setData(self, data):
self.data = data
def setTimeout(self, timeout):
self.timeout = timeout
#calculate timestamp here
ts = time.time()
self.timestamp = int(ts) + timeout
def setTimeoutTimestamp(self, timeout, timestamp):
self.timeout = timeout
self.timestamp = timestamp
#STRING
@app.route('/SET', methods=['POST'])
def string_set():
content = request.get_json()
data = StringExpire(content["value"], 0, 0)
table_of_string[content["key"]] = data
return 'SET OK'
@app.route('/GET', methods=['POST'])
def string_get():
content = request.get_json()
if content["key"] in table_of_string:
return str(table_of_string[content["key"]])
else :
return "ERROR: KEY NOT FOUND"
#LIST
@app.route('/LLEN', methods=['POST'])
def list_len():
content = request.get_json()
if content["key"] in table_of_list:
return str(len(table_of_list[content["key"]]))
return "ERROR: KEY NOT FOUND"
@app.route('/RPUSH', methods=['POST'])
def list_rpush():
content = request.get_json()
list_name = content["key"]
list_new_data = content["value"].split()
if list_name in table_of_list:
raw_list = table_of_list[list_name]
for value in list_new_data:
raw_list.append(value)
table_of_list[list_name] = raw_list
else :
new_list = []
for value in list_new_data:
new_list.append(value)
table_of_list[list_name] = new_list
return str(len(table_of_list[list_name]))
@app.route('/LPOP', methods=['POST'])
def list_pop():
content = request.get_json()
list_name = content["key"]
if list_name in table_of_list:
list_data = table_of_list[list_name]
if len(list_data) > 0:
result = list_data.pop(0)
return str(result)
else:
return "ERROR: LIST EMPTY"
else:
return "ERROR: LIST NOT FOUND"
@app.route('/RPOP', methods=['POST'])
def list_rpop():
content = request.get_json()
list_name = content["key"]
if list_name in table_of_list:
list_data = table_of_list[list_name]
if len(list_data) > 0:
last_item = list_data[-1]
del list_data[-1]
return str(last_item)
else:
return "ERROR: LIST EMPTY"
else:
return "ERROR: LIST NOT FOUND"
@app.route('/LRANGE', methods=['POST'])
def list_lrange():
content = request.get_json()
list_name = content["key"]
list_index = content["value"].split()
start = list_index[0]
stop = list_index[1]
if list_name in table_of_list:
list_value = table_of_list[list_name]
new_list = list_value[int(start): int(stop) + 1]
return str(new_list)
else:
return "ERROR: LIST NOT FOUND"
#SET
@app.route('/SADD', methods=['POST'])
def set_add():
content = request.get_json()
set_name = content["key"]
list_value = content["value"].split()
if set_name in table_of_set:
set_data = table_of_set[set_name]
for value in list_value:
set_data.add(value)
else:
set_data = set()
for value in list_value:
set_data.add(value)
table_of_set[set_name] = set_data
return "SUCCESSED"
@app.route('/SCARD', methods=['POST'])
def set_card():
content = request.get_json()
set_name = content["key"]
if set_name in table_of_set:
return str(len(table_of_set[set_name]))
else :
return "ERROR: SET NOT FOUND"
@app.route('/SMEMBERS', methods=['POST'])
def set_member():
content = request.get_json()
set_name = content["key"]
if set_name in table_of_set:
return str(table_of_set[set_name])
else:
return "ERROR: SET NOT FOUND"
@app.route('/SREM', methods=['POST'])
def set_rem():
content = request.get_json()
set_name = content["key"]
list_value = content["value"].split()
if set_name in table_of_set:
set_data = table_of_set[set_name]
for value in list_value:
set_data.remove(value)
table_of_set[set_name] = set_data
return "'DELETE SUCCESSED"
else:
return "ERROR: SET NOT FOUND"
@app.route('/SINTER', methods=['POST'])
def set_inter():
content = request.get_json()
list_set_name = content["listKey"].split()
if list_set_name[0] in table_of_set:
first_set = table_of_set[list_set_name[0]]
for name in list_set_name:
if not name in table_of_set:
return "ERROR: SET " + name + " NOT FOUND"
else:
first_set = set.intersection(first_set, table_of_set[name])
if len(first_set) == 0:
return "EMPTY RESULT"
return str(first_set)
#DATA EXPIRATION
@app.route('/KEYS', methods=['POST'])
def set_keys():
list1 = table_of_string.keys()
list2 = table_of_list.keys()
list3 = table_of_set.keys()
joined_list = [*list1, *list2, *list3]
if len(joined_list) == 0:
return "ERROR: NO KEY EXIST"
result = '\n'.join(joined_list)
return result
@app.route('/DEL', methods=['POST'])
def set_del_key():
content = request.get_json()
key_name = content["key"]
table_of_string.pop(key_name, None)
return "'DELETE SUCCESSED"
@app.route('/FLUSHDB', methods=['POST'])
def set_flushdb():
table_of_set.clear()
table_of_string.clear()
table_of_list.clear()
return "'DELETED ALL KEY"
@app.route('/EXPIRE', methods=['POST'])
def set_expire():
content = request.get_json()
key_name = content["key"]
key_timeout = content["timeout"]
if key_name in table_of_string:
data = table_of_string[key_name]
data.setTimeout(int(key_timeout))
table_of_string[key_name] = data
return key_timeout
else:
return "ERROR: KEY NOT FOUND"
@app.route('/TTL', methods=['POST'])
def set_ttl():
content = request.get_json()
key_name = content["key"]
if key_name in table_of_string:
data = table_of_string[key_name]
timeEnd = data.getTimestamp()
curTime = int( time.time())
return str(timeEnd - curTime)
else:
return "ERROR: KEY NOT FOUND"
##### SNAPSHOT#####
@app.route('/SAVE', methods=['POST'])
def set_save():
saveFile = open('data.txt', 'w')
numberString = len(table_of_string)
saveFile.write("%s\n" % str(numberString))
for item in table_of_string:
data = table_of_string[item]
text = item + ' ' + data.getData() + ' ' + str(data.getTimeout()) + ' ' + str(data.getTimestamp())
saveFile.write("%s\n" % text)
saveFile.write("%s\n" % str(len(table_of_list)))
for item in table_of_list:
data = table_of_list[item]
s = ' '.join(data)
item = item + ' ' + s
saveFile.write("%s\n" % item)
saveFile.write("%s\n" % str(len(table_of_set)))
for item in table_of_set:
data = table_of_set[item]
s = ' '.join(data)
item = item + ' ' + s
saveFile.write("%s\n" % item)
return "SAVE OK"
@app.route('/RESTORE', methods=['POST'])
def set_restore():
saveFile = open('data.txt', 'r')
line = saveFile.readline()
if int(line) != 0:
for i in range(0, int(line)):
raw_data = saveFile.readline()
list_data = raw_data.split()
value = StringExpire(list_data[1], list_data[2], list_data[3])
table_of_string[list_data[0]] = value
line = saveFile.readline()
if int(line) != 0:
for i in range(0, int(line)):
data = saveFile.readline()
list_new_data = data.split()
list_name = list_new_data[0]
table_of_list[list_name] = list_new_data[1:]
line = saveFile.readline()
if int(line) != 0:
for i in range(0, int(line)):
data = saveFile.readline()
list_new_data = data.split()
set_name = list_new_data[0]
table_of_set[set_name] = list_new_data[1:]
return "restore"
@app.route('/')
def homepage():
the_time = datetime.now().strftime("%A, %d %b %Y %l:%M %p")
return """
<h1>Hello heroku</h1>
<p>It is currently {time}.</p>
<img src="http://loremflickr.com/600/400">
""".format(time=the_time)
if __name__ == '__main__':
app.run(debug=True, use_reloader=True)