-
Notifications
You must be signed in to change notification settings - Fork 10
/
code.py
89 lines (79 loc) · 3.24 KB
/
code.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
import threading
#this is for python 3.0 and above. use import thread for python2.0 versions
from threading import*
import time
d={} #'d' is the dictionary in which we store data
#for create operation
#use syntax "create(key_name,value,timeout_value)" timeout is optional you can continue by passing two arguments without timeout
def create(key,value,timeout=0):
if key in d:
print("error: this key already exists") #error message1
else:
if(key.isalpha()):
if len(d)<(1024*1020*1024) and value<=(16*1024*1024): #constraints for file size less than 1GB and Jasonobject value less than 16KB
if timeout==0:
l=[value,timeout]
else:
l=[value,time.time()+timeout]
if len(key)<=32: #constraints for input key_name capped at 32chars
d[key]=l
else:
print("error: Memory limit exceeded!! ")#error message2
else:
print("error: Invalind key_name!! key_name must contain only alphabets and no special characters or numbers")#error message3
#for read operation
#use syntax "read(key_name)"
def read(key):
if key not in d:
print("error: given key does not exist in database. Please enter a valid key") #error message4
else:
b=d[key]
if b[1]!=0:
if time.time()<b[1]: #comparing the present time with expiry time
stri=str(key)+":"+str(b[0]) #to return the value in the format of JasonObject i.e.,"key_name:value"
return stri
else:
print("error: time-to-live of",key,"has expired") #error message5
else:
stri=str(key)+":"+str(b[0])
return stri
#for delete operation
#use syntax "delete(key_name)"
def delete(key):
if key not in d:
print("error: given key does not exist in database. Please enter a valid key") #error message4
else:
b=d[key]
if b[1]!=0:
if time.time()<b[1]: #comparing the current time with expiry time
del d[key]
print("key is successfully deleted")
else:
print("error: time-to-live of",key,"has expired") #error message5
else:
del d[key]
print("key is successfully deleted")
#I have an additional operation of modify in order to change the value of key before its expiry time if provided
#for modify operation
#use syntax "modify(key_name,new_value)"
def modify(key,value):
b=d[key]
if b[1]!=0:
if time.time()<b[1]:
if key not in d:
print("error: given key does not exist in database. Please enter a valid key") #error message6
else:
l=[]
l.append(value)
l.append(b[1])
d[key]=l
else:
print("error: time-to-live of",key,"has expired") #error message5
else:
if key not in d:
print("error: given key does not exist in database. Please enter a valid key") #error message6
else:
l=[]
l.append(value)
l.append(b[1])
d[key]=l