-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCRD2.py
More file actions
169 lines (117 loc) · 3.96 KB
/
Copy pathCRD2.py
File metadata and controls
169 lines (117 loc) · 3.96 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
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
#!/usr/bin/env python
# coding: utf-8
# In[6]:
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
t1=Thread(target=(create or read or delete),args=(key_name,value,timeout)) #as per the operation
t1.start()
t1.sleep()
t2=Thread(target=(create or read or delete),args=(key_name,value,timeout)) #as per the operation
t2.start()
t2.sleep()
# In[7]:
#creating data
aa=create("Umraz",55)
ab=create("Hussain",56,300)
ac=create("Khan",57,30) #after 30sec data will be earased
# In[8]:
#reading the data
aa1=read("Umraz")
aa1
# In[10]:
#after 30sec data will be earased
ac1=read("Khan")
ac1
# In[11]:
ab1=read("Hussain")
ab1
# In[12]:
#we can modify the data
aa2=modify("Umraz",60)
# In[13]:
aa3=read("Umraz")
aa3
# In[14]:
#delete operation
aa4=delete("Umraz")
# In[15]:
#if key exist it'll give error
aa5=read("Umraz")
aa5
# In[ ]: