-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLockingMechanismChecker.py
More file actions
127 lines (113 loc) · 4.72 KB
/
Copy pathLockingMechanismChecker.py
File metadata and controls
127 lines (113 loc) · 4.72 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
import json
import os
class LockingMechanismChecker:
def __init__(self):
self.schemaDirectory = "AllDatabase"
self.error = "Table is already in locked state"
self.databaseError = "Database does not exists"
self.databaseTableError = "Table does not exists for the schema"
self.noLock = "No lock exists"
def addLock(self, tableName, database):
tablePath = self.schemaDirectory + "/" + database + "/" + tableName
lockFile = self.schemaDirectory + "/" + database + "/" + "lock.json"
if not os.path.exists(tablePath):
print(self.databaseTableError)
else:
if not os.path.exists(lockFile):
file = open(lockFile, "w")
file.write("{\"Lock\":[]}")
file.close()
with open(lockFile, "r+") as file:
lockData = json.load(file)
temp = lockData['Lock']
if len(temp) > 0:
table_found = False
for entry in temp:
if tableName == entry['Locked Table']:
table_found = True
break
else:
table_found = False
if table_found:
print(self.error, "-> ", tableName)
else:
print("\nLock has been added for table: ", tableName)
new_lock_entry = {"Locked Table": tableName}
temp.append(new_lock_entry)
else:
print("\nLock has been added for table: ", tableName)
new_lock_entry = {"Locked Table": tableName}
temp.append(new_lock_entry)
with open(lockFile, 'w') as file:
json.dump(lockData, file, indent=4)
def checkLock(self, tableName, database):
tablePath = self.schemaDirectory + "/" + database + "/" + tableName
lockFile = self.schemaDirectory + "/" + database + "/" + "lock.json"
if not os.path.exists(tablePath):
print(self.databaseTableError)
return False
else:
if not os.path.exists(lockFile):
file = open(lockFile, "w")
file.write("{\"Lock\":[]}")
file.close()
return False
with open(lockFile, "r+") as file:
lockData = json.load(file)
temp = lockData['Lock']
if len(temp) > 0:
table_found = False
for entry in temp:
if tableName == entry['Locked Table']:
table_found = True
break
else:
table_found = False
if table_found:
return True
else:
return False
else:
return False
def removeLock(self, tableName, database):
tablePath = self.schemaDirectory + "/" + database + "/" + tableName
lockFile = self.schemaDirectory + "/" + database + "/" + "lock.json"
if not os.path.exists(tablePath):
print(self.databaseTableError)
return False
else:
if not os.path.exists(lockFile):
file = open(lockFile, "w")
file.write("{\"Lock\":[]}")
file.close()
print(self.noLock)
return False
with open(lockFile, "r+") as file:
lockData = json.load(file)
temp = lockData['Lock']
if len(temp) > 0:
table_found = False
for entry in temp:
if tableName == entry['Locked Table']:
table_found = True
temp.remove(entry)
print("Lock has been removed for table: ",tableName )
with open(lockFile, 'w') as file:
json.dump(lockData, file, indent=4)
break
else:
table_found = False
if table_found:
return True
else:
return False
else:
print(self.noLock)
return False
if __name__ == "__main__":
database = "PersonDatabase"
tableName = "PERSON2"
obj = LockingMechanismChecker()
obj.addLock(tableName, database)
print(obj.checkLock(tableName, database))
print(obj.removeLock(tableName, database))