Skip to content

Commit 412393a

Browse files
committed
document
1 parent ef428b2 commit 412393a

File tree

1 file changed

+33
-13
lines changed

1 file changed

+33
-13
lines changed

redisgraph/execution_plan.py

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def append_child(self, child):
2323
return self
2424

2525
def child_count(self):
26-
return array_len(self.children)
26+
return len(self.children)
2727

2828
def __eq__(self, o: object) -> bool:
2929
if not isinstance(o, Operation):
@@ -38,6 +38,7 @@ def __str__(self) -> str:
3838
args_str = "" if self.args is None else f" | {self.args}"
3939
return f"{self.name}{args_str}"
4040

41+
4142
class ExecutionPlan:
4243
"""
4344
ExecutionPlan, collection of operations.
@@ -60,7 +61,7 @@ def __init__(self, plan):
6061
def _compare_operations(self, root_a, root_b):
6162
"""
6263
Compare execution plan operation tree
63-
64+
6465
Return: True if operation trees are equal, False otherwise
6566
"""
6667

@@ -103,44 +104,63 @@ def __eq__(self, o: object) -> bool:
103104

104105
# compare execution trees
105106
return self._compare_operations(root_a, root_b)
106-
107+
107108
def _operation_traverse(self, op, op_f, aggregate_f, combine_f):
108-
# TODO: document function and its arguments
109-
child_res = op_f(op)
109+
"""
110+
Traverse operation tree recursively applying functions
111+
112+
Args:
113+
op: operation to traverse
114+
op_f: function applied for each operation
115+
aggregate_f: aggregation function applied for all children of a single operation
116+
combine_f: combine function applied for the operation result and the children result
117+
"""
118+
# apply op_f for each operation
119+
op_res = op_f(op)
110120
if len(op.children) == 0:
111-
return child_res
121+
return op_res # no children return
112122
else:
123+
# apply _operation_traverse recursively
113124
children = [self._operation_traverse(child, op_f, aggregate_f, combine_f) for child in op.children]
114-
return combine_f(child_res, aggregate_f(children))
125+
# combine the operation result with the children aggregated result
126+
return combine_f(op_res, aggregate_f(children))
115127

116128
def _operation_tree(self):
117-
# TODO: document!
129+
""" Build the operation tree from the string representation """
130+
131+
# initial state
118132
i = 0
119133
level = 0
120134
stack = []
121135
current = None
122136

137+
# iterate plan operations
123138
while i < len(self.plan):
124-
op = self.plan[i]
125-
op_level = op.count(" ")
139+
current_op = self.plan[i]
140+
op_level = current_op.count(" ")
126141
if op_level == level:
127-
args = op.split("|")
142+
# if the operation level equal to the current level
143+
# set the current operation and move next
144+
args = current_op.split("|")
128145
current = Operation(args[0].strip(), None if len(args) == 1 else args[1].strip())
129146
i += 1
130147
elif op_level == level + 1:
131-
args = op.split("|")
148+
# if the operation is child of the current operation
149+
# add it as child and set as current operation
150+
args = current_op.split("|")
132151
child = Operation(args[0].strip(), None if len(args) == 1 else args[1].strip())
133152
current.append_child(child)
134153
stack.append(current)
135154
current = child
136155
level += 1
137156
i += 1
138157
elif op_level < level:
158+
# if the operation is not child of current operation
159+
# go back to it's parent operation
139160
levels_back = level - op_level + 1
140161
for _ in range(levels_back):
141162
current = stack.pop()
142163
level -= levels_back
143164
else:
144165
raise Exception("corrupted plan")
145166
return stack[0]
146-

0 commit comments

Comments
 (0)