-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
executable file
·104 lines (73 loc) · 2.47 KB
/
main.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
#!/bin/python
#######################################
#
#
# Advanced Database Systems
# Final Project
#
# Sanjar Ahmadov [sa5640]
# Ashwin Prakash Nalwade [apn308]
#
#
#######################################
import argparse
from transaction_manager import *
time_tick = 0
class Database:
TM = None
def __init__(self, debug):
self.TM = TransactionManager(debug)
def listenAndHandleRequests(self, file_path):
if file_path is None or len(file_path) == 0:
# read from stdin
pass
else:
with open(file_path, 'r') as f:
for line in f:
request = self.getRequestFromLine(line)
if request is None:
continue
self.TM.handleRequest(request)
def getRequestFromLine(self, line):
global time_tick
if (line == "\n"):
time_tick += 1
splitted = line.split(")")
if (len(splitted) != 2):
return None
line = splitted[0]
splitted = line.split('(')
# Not valid line to parse
if (len(splitted) != 2):
return None
requestType, params = splitted
# Not valid request type
if not all(c.isalpha() for c in requestType):
return None
splitted = params.split(",")
param1 = param2 = param3 = None
if len(splitted) == 1:
if len(splitted[0]) > 0: param1 = splitted[0].strip()
elif len(splitted) == 2:
if len(splitted[0]) > 0: param1 = splitted[0].strip()
if len(splitted[1]) > 0: param2 = int(splitted[1].strip()[1:])
elif len(splitted) == 3:
if len(splitted[0]) > 0: param1 = splitted[0].strip()
if len(splitted[1]) > 0: param2 = int(splitted[1].strip()[1:])
if len(splitted[2]) > 0: param3 = int(splitted[2].strip())
else:
return None
return Request(requestType, param1, param2, param3)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--test', help='test input source', default='')
parser.add_argument('--debug', help='test debug option', default='False')
args = parser.parse_args()
test_source = args.test
debug = args.debug == "True"
print("Running", test_source)
DB = Database(debug)
DB.listenAndHandleRequests(test_source)
print("Done with", test_source)
print("")
print("")