-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput_reader.py
70 lines (56 loc) · 2.62 KB
/
input_reader.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
import operations
import transactions
class InputReader:
@staticmethod
def parse_operations_string(input_elements, schema):
transactions_parsed = []
for i in range(len(input_elements)):
match input_elements[i]:
case "r":
new_transaction = InputReader._parse_read(input_elements, i, schema)
transactions_parsed.append(new_transaction)
case "w":
new_transaction = InputReader._parse_write(input_elements, i, schema)
transactions_parsed.append(new_transaction)
case "c":
new_transaction = InputReader._parse_commit(input_elements, i)
transactions_parsed.append(new_transaction)
case "u":
new_transaction = InputReader._parse_update(input_elements, i, schema)
transactions_parsed.append(new_transaction)
return transactions_parsed
@staticmethod
def _parse_read(input_elements, index, schema):
parsed_elements = [operations.Operation("r"), transactions.Transaction(input_elements[index + 1])]
aux = InputReader._parse_aux(input_elements, index)
parsed_elements.append(schema["".join(aux)])
return parsed_elements
@staticmethod
def _parse_write(input_elements, index, schema):
parsed_elements = [operations.Operation("w"), transactions.Transaction(input_elements[index + 1])]
aux = InputReader._parse_aux(input_elements, index)
parsed_elements.append(schema["".join(aux)])
return parsed_elements
@staticmethod
def _parse_commit(input_elements, index):
parsed_elements = [operations.Operation("c"), transactions.Transaction(input_elements[index + 1])]
return parsed_elements
@staticmethod
def _parse_update(input_elements, index, schema):
parsed_elements = [operations.Operation("u"), transactions.Transaction(input_elements[index + 1])]
aux = InputReader._parse_aux(input_elements, index)
parsed_elements.append(schema["".join(aux)])
return parsed_elements
@staticmethod
def _parse_aux(input_elements, index):
aux = []
if input_elements[index + 3] == "B":
aux.append(input_elements[index + 3])
aux.append(input_elements[index + 4])
else:
aux.append(input_elements[index + 3])
aux.append(input_elements[index + 4])
aux.append(input_elements[index + 5])
if input_elements[index + 6] != ")":
aux.append(input_elements[index + 6])
return aux