-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
164 lines (131 loc) · 4.75 KB
/
utils.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
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
import sys
import os
import re
#
#Possible message types
#
LOCK_REQUEST = 'lock_req'
LOCK_RESPONSE = 'lock_resp'
DB_UPDATE_QUE = 'update'
DB_UPDATE_ACK = 'ack'
#
#Message structure
#
frame = { 'type':None, #one of the four possible message types
'sender': None,
'lock_vars':None, #if type is LOCK_REQ or LOCK_RESPONSE, this is a list of variable names to lock
'response':None, #if type is LOCK_RESP, this is 'YES'/'NO'
'query':None, #if type is UPDATE, this is an expression formated as: 'var1 = value1; var2 = value2; ....'
'ack':None #if type is ACK, then this argument is the expression that has been updated
}
#
#From the give query (transaction), extracts all potential variables
#ex. get_tokens('a = 4; b = a * a; c = d') -> [a,b,c,d]
#
def get_tokens(query):
tokens = re.findall(r'([a-zA-Z0-9]+)',query)
invalid_tokens = []
for token in tokens:
if len(re.findall(r'^[a-zA-Z]',token)) == 0:
invalid_tokens += [token]
for token in invalid_tokens:
tokens.remove(token)
return tokens
#
#Takes a single expression from the transaction, and splits it into a left and right expression according to the '=' sign
#
def get_left_right_expression(query):
m = re.match(r'\s*([a-zA-Z]+\w*)\s*=(.*)$',query)
if m == None:
print 'cannot parse expression, invalid variable name on left side of equation'
return None,None
return m.group(1),m.group(2)
#
#Checks whether the query 'QUERY' is a valid query in the current database
#ex. Database contains: a = 0, b = 0.
# QUERY = 'a = 3; b = c * c' - QUERY uses the variable 'c' before it assigns a value to it -> QUERY is invalid.
#
#ex. QUERY = '12a = 3' - '12a' is an invalid variable name
#
#Always process subqueries from left to right
def check_if_valid(database, QUERY):
db_variables = os.popen('ls '+database).readlines() #reads all variable names from database
for query in QUERY.split(";"):
tokens = get_tokens(query)
for token in tokens[1:]: #checks if all tokens in the right side of the subquery exist in the database
if not (token+'\n' in db_variables):
print "'",token, "' not in database."
return False
left,right = get_left_right_expression(query)
if left == None or right == None:
print query, ' is not a valid expression.'
return False
try:
for token in tokens[1:]:
f = open(database + token,'r')
val = f.read().strip()
f.close()
exec(token + '=' + val)
eval(right)
except Exception as e:
print 'An error occured while parsing the expression: ',QUERY
print 'Error: ',e
return False
db_variables += [left+'\n'] #The variable on the left side of the equation may be created for the first time. Add it to the list of variables, so that the next subqueries that use it are valid.
return True
#
#Set the variable 'var' in database 'database' to the value 'val'
#
def set_var(database,var,val):
f = open(database+var,'w')
f.write(str(val))
f.close()
#
#Execute the query 'QUERY' over the database 'database'
#and return the result as an update string for the other databases: var1=val1; var2 = val2; var3 = val3; ...
#
def execute_local_query(database, QUERY):
update_query_list = []
for query in QUERY.split(";"): #for each subquery
tokens = get_tokens(query) #extract the tokens/variables concerned
for token in tokens[1:]: #for each token
fil = open(database + token, 'r')
exec(token + '=' + fil.read().strip()) #read its value and store it in runtime
fil.close()
left,right = get_left_right_expression(query) #extract the left and right side of the subquery
result = str(eval(right)) #evaluate the right side
exec(left + '=' + result) #store the result in the variable on the left side
for token in tokens: #construct the update query that needs to be sent to other nodes to update their databases
set_var(database,token,eval(token))
update_query_list += [token + '=' + str(eval(token))]
return ';'.join(update_query_list) #the update query looks like: var1=val1; var2 = val2; var3 = val3; ...
#
#If the node's database doesn't exist, create it as a folder with the name <database>
#
def create_local_database(database):
dirs = os.popen("ls -l | grep ^d | awk '{print $9}'").readlines()
for d in dirs:
if database[:-1] in d: return
os.popen('mkdir '+database).close()
#
#Prints the database contents
#
def list_database_contents(database):
dirs = os.popen("ls -l | grep ^d | awk '{print $9}'").readlines()
ok = False
for d in dirs:
if database[:-1] in d:
ok = True
if not ok:
print "Database " ,database ," doesn't exist"
sys.exit(1)
entries = os.popen('ls ' +database).readlines()
PHASE1_LOCKS = {}
for entry in entries:
entry = entry.strip()
f = open(database+entry)
content = f.read()
f.close()
PHASE1_LOCKS[entry] = False
print entry,'=',content
return PHASE1_LOCKS