-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathOptimizer.py
More file actions
171 lines (150 loc) · 6.81 KB
/
Copy pathOptimizer.py
File metadata and controls
171 lines (150 loc) · 6.81 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
166
167
168
169
170
171
from Blocks.Block import Block
from Blocks.EndWhileBlock import EndWhileBlock
from Blocks.FlowControlBlock import FlowControlBlock
from Blocks.InsertUpdateBlock import InsertUpdateBlock
class Optimizer:
'''
class that contains the functions that's used for optimizing grail
'''
options={}
blocks=[]
senders=[]
senderIndex=0
def __init__(self,opts,bls,sendlist):
'''
constructor for the optimizer class
@param opts:parser generated options
@type opts: dictionary
@param bls: list of blocks
@type bls: list of strings
@param sendlist: sender list
@type sendlist: list of strings
'''
self.options=opts
self.blocks=bls
self.senders=sendlist
if(len(self.senders)>0):
self.senderIndex=0
def createIdx(self): # @DontTrace
'''
creating an index on the "edge" table depending on the direction of sending messages (src/dest)
'''
dirval=self.options["SendMsgDir"]
for idx in range(0,len(self.blocks)):
if(self.blocks[idx].getStage()=="beginWhile"):
break
if(dirval=="in"):
self.blocks.insert(idx-1,Block("createIdx",0,self.getCreateIdxSQL("dest")))
elif(dirval=="out"):
self.blocks.insert(idx-1,Block("createIdx",0,self.getCreateIdxSQL("src")))
elif(dirval=="all"):
self.blocks.insert(idx-1,Block("createIdx",0,self.getCreateIdxSQL("src")))
self.blocks.insert(idx-1,Block("createIdx",0,self.getCreateIdxSQL("dest")))
def getCreateIdxSQL(self,attr):
'''
returns the sql statement for index creation given the attribute (src/dest)
@param attr: src/dest
@type attr: string
'''
return "CREATE INDEX idx_" + attr + " ON edge(" + attr + ");"
def removeFromList(self,lists,index):
'''
function to remove an index from list
@param lists: any list
@type lists: list
@param index: index in the list to be removed
@type index: int
'''
lists=lists[:index] + lists[index+1 :]
def mergeSendCombineMsg(self):
dropBlocks=[]
for sender in self.senders:
for i in range(0,len(self.blocks)):
if(self.blocks[i].getStage()=="drop"+sender):
if(sender != "cur"):
dropBlocks.append(self.blocks[i])
self.removeFromList(self.blocks,i)
else:
self.blocks[i]=Block("renamecur",self.blocks[i].getIndentLevel(),"ALTER TABLE cur RENAME TO cur_alias;")
dropBlocks.append(Block("dropcuralias",self.blocks[i].getIndentLevel(),"DROP TABLE IF EXISTS cur_alias;"))
break;
self.blocks.insert(0,Block("intdropcuralias", 0, "DROP TABLE IF EXISTS cur_alias;"))
combineMsgBlockIndex = 0
for combineMsgBlockIndex in range(0,len(self.blocks)):
if(self.blocks[combineMsgBlockIndex].getStage()=="combineMsg"):
break;
sendMsgBlockIndex = 0;
combineMsg=self.blocks[combineMsgBlockIndex].getSql()
indent=self.blocks[combineMsgBlockIndex].getIndentLevel()
self.removeFromList(self.blocks,combineMsgBlockIndex)
for sendMsgBlockIndex in range(0,len(self.blocks)):
if(self.blocks[sendMsgBlockIndex].getStage()=="sendMsg"):
break;
self.removeFromList(self.blocks,sendMsgBlockIndex-1)
sendMsg=self.blocks[sendMsgBlockIndex].getSql()
sendMsgBlock=self.blocks[sendMsgBlockIndex]
aggFunc=self.options["aggFunc"].replace("message.val",self.options["contentStr"])
if(self.options["msgDir"]=="all"):
sendMsgBlock.append("GROUP BY id")
sendMsgBlock.append(";")
sendMsg=sendMsgBlock.getSql()
sendMsg = sendMsg.replace("SELECT *", "SELECT id, "+ self.options["aggFunc"].replace("message.val", "val") + " as val ")
sendMsg = sendMsg.replace("GROUP BY src", "")
sendMsg = sendMsg.replace("GROUP BY dest", "")
else:
sendMsg=sendMsg.replace(self.options["contentStr"],aggFunc)
self.blocks[sendMsgBlockIndex].setSql(sendMsg)
if(self.options["isSender"]=="all" and self.options["setValNewVal"]=="cur.val"):
sendMsg=sendMsg.replace("cur","next")
else:
sendMsg=sendMsg.replace("cur","cur_alias")
sendMsg = sendMsg.replace("INTO message", "INTO cur")
setIsFirstBlock = Block("setIsFirst", indent, "isFirst := 0;")
for block in dropBlocks:
sendMsg+=block.getSql()
self.blocks.insert(combineMsgBlockIndex,FlowControlBlock("flowControl",
indent,
"isFirst = 1",
combineMsg + setIsFirstBlock.getSql(),
sendMsg))
i=0
for i in range(0,len(self.blocks)):
if(self.blocks[i].getStage()=="dropmessage"):
self.removeFromList(self.blocks,i)
i=0
for i in range(0,len(self.blocks)):
if(self.blocks[i].getStage()=="declare"):
self.blocks[i].setSql(self.blocks[i].getSql()+"\n"+"isFirst integer := 1;")
i=0
for i in range(0,len(self.blocks)):
if(self.blocks[i].getStage()=="endWhile"):
endWhileBlock = self.blocks[i]
self.blocks[i]=EndWhileBlock(endWhileBlock.getStage(),
endWhileBlock.getIndentLevel(),
endWhileBlock.getEndStr(),
self.senders[self.senderIndex])
self.senderIndex=self.senderIndex+1 #TODO
def allSender(self):
'''
Reduce update when all vertices are senders.
'''
block=self.blocks[0]
idx=0
if(self.options["isSender"]=="all" and self.options["setValNewVal"]=="cur.val"):
for idx in range(0,len(self.blocks)):
if(self.blocks[idx].getStage()=="setVal"):
block=self.blocks[idx]
break
self.removeFromList(self.blocks,idx)
self.blocks.insert(idx,InsertUpdateBlock(block))
for idx in range(0,len(self.blocks)):
if(self.blocks[idx].getStage()=="dropcur"):
self.removeFromList(self.blocks,idx)
break
def run(self):
'''
run the optimizer
'''
self.createIdx()
self.allSender()
self.mergeSendCombineMsg()