
    2i(                     H    d dl Z  G d d      Z G d d      Z G d d      Zy)    Nc                        e Zd ZdZdedefdZy)ProfileStatsz
    ProfileStats Class for representing runtime execution statistics of an operation.

    Attributes:
        records_produced (int): The number of records produced.
        execution_time (float): The execution time in milliseconds.
    records_producedexecution_timec                      || _         || _        y)a  
        Initializes a new ProfileStats instance with the given records_produced and execution_time.

        Args:
            records_produced (int): The number of records produced.
            execution_time (float): The execution time in milliseconds.
        N)r   r   )selfr   r   s      a/mnt/e/genesis-system/.venvs/voice-bridge/lib/python3.12/site-packages/falkordb/execution_plan.py__init__zProfileStats.__init__   s     !/ 0    N)__name__
__module____qualname____doc__intfloatr
    r   r	   r   r      s    	1 	1e 	1r   r   c                   |    e Zd ZdZddedefdZedefd       Z	edefd       Z
d	 Zdefd
ZdedefdZdefdZy)	Operationa?  
    Operation Class for representing a single operation within an execution plan.

    Attributes:
        name (str): The name of the operation.
        args (str): Operation arguments.
        children (list): List of child operations.
        profile_stats (ProfileStats): Profile statistics for the operation.
    Nnameprofile_statsc                 <    || _         || _        g | _        || _        y)a  
        Creates a new Operation instance.

        Args:
            name (str): The name of the operation.
            args (str, optional): Operation arguments.
            profile_stats (ProfileStats, optional): Profile statistics for the operation.
        N)r   argschildrenr   )r   r   r   r   s       r	   r
   zOperation.__init__$   s!     "	!	*r   returnc                 .    | j                   j                  S )z:
        returns operation's execution time in ms
        )r   r   r   s    r	   r   zOperation.execution_time2   s    
 !!000r   c                 .    | j                   j                  S )zB
        returns number of records produced by operation.
        )r   r   r   s    r	   r   zOperation.records_produced9   s    
 !!222r   c                 r    t        |t              st        d      | j                  j	                  |       | S )z
        Appends a child operation to the current operation.

        Args:
            child (Operation): The child operation to append.

        Returns:
            Operation: The updated operation instance.
        zchild must be Operation)
isinstancer   	Exceptionr   append)r   childs     r	   append_childzOperation.append_child@   s1     %+566U#r   c                 ,    t        | j                        S )zx
        Returns the number of child operations.

        Returns:
            int: Number of child operations.
        )lenr   r   s    r	   child_countzOperation.child_countP   s     4==!!r   oc                     t        |t              sy| j                  |j                  k(  xr | j                  |j                  k(  S )a  
        Compares two Operation instances for equality based on their name and arguments.

        Args:
            o (object): Another Operation instance for comparison.

        Returns:
            bool: True if the operations are equal, False otherwise.
        F)r   r   r   r   )r   r'   s     r	   __eq__zOperation.__eq__Y   s6     !Y'yyAFF":tyyAFF'::r   c                 \    | j                   dnd| j                   z   }| j                   | S )z
        Returns a string representation of the operation.

        Returns:
            str: String representation of the operation.
         z | )r   r   )r   args_strs     r	   __str__zOperation.__str__h   s0     *2		0A))XJ''r   )NN)r   r   r   r   strboolr
   propertyr   r   r   r#   r&   objectr)   r-   r   r   r	   r   r      s    +S +D + 1 1 1 3# 3 3 "S "; ;4 ;( (r   r   c                   V    e Zd ZdZd Zd ZdefdZdefdZ	de
defdZd	 Zd
 Zd Zy)ExecutionPlanz
    ExecutionPlan Class for representing a collection of operations.

    Attributes:
        plan (list): List of strings representing the collection of operations.
        structured_plan (Operation): Root of the structured operation tree.
    c                 F   t        |t              st        d      t        |d   t              r|D cg c]  }|j	                          }}|| _        i | _        | j                         | _        | j                  D ]  }| j                  |   j                          ! yc c}w )z
        Creates a new ExecutionPlan instance.

        Args:
            plan (list): List of strings representing the collection of operations.
        zplan must be an arrayr   N)
r   listr    bytesdecodeplan
operations_operation_treestructured_planreverse)r   r8   bkeys       r	   r
   zExecutionPlan.__init__|   s     $%344d1gu%(,-1AHHJ-D-	#335?? 	+COOC ((*	+ .s   Bc                 @    || j                   v r| j                   |   S g S )z
        Collects all operations with specified name from plan

        Args:
            op_name (string): Name of operation to collect

        Returns:
            List[Operation]: All operations with the specified name
        )r9   r   r!   )r   op_nameopsops       r	   collect_operationsz ExecutionPlan.collect_operations   s$     doo%??7++	r   r   c                     ||k7  ry|j                         |j                         k7  ryt        |j                               D ]0  }| j                  |j                  |   |j                  |         r0 y y)z
        Compares execution plan operation trees.

        Returns:
            bool: True if operation trees are equal, False otherwise.
        FT)r&   range"_ExecutionPlan__compare_operationsr   )r   root_aroot_bis       r	   __compare_operationsz"ExecutionPlan.__compare_operations   sx     V 6#5#5#77 v))+, 	A,,V__Q-?QRAST	 r   c                 R    d }d }| j                  | j                  t        ||      S )z
        Returns a string representation of the execution plan.

        Returns:
            str: String representation of the execution plan.
        c           	          dj                  | D cg c]  }|j                         D ]  }d|z   	  c}}      S c c}}w )N
    )join
splitlines)str_children	str_childlines      r	   aggregate_strz,ExecutionPlan.__str__.<locals>.aggregate_str   sR    99 &2! ) 4 4 6  TM! s   !:
c                     |  d| S )NrM   r   )xys     r	   combine_strz*ExecutionPlan.__str__.<locals>.combine_str   s    S1#;r   )_operation_traverser;   r.   )r   rT   rX   s      r	   r-   zExecutionPlan.__str__   s0    		 ''  #}k
 	
r   r'   c                 x    t        |t              sy| j                  }|j                  }| j                  ||      S )z
        Compares two execution plans.

        Returns:
            bool: True if the two plans are equal, False otherwise.
        F)r   r3   r;   rF   )r   r'   rG   rH   s       r	   r)   zExecutionPlan.__eq__   s>     !]+ %%"" ((88r   c                 ,    t        | j                        S )N)iterr9   r   s    r	   __iter__zExecutionPlan.__iter__   s    DOO$$r   c           	           ||      }t        |j                        dk(  r|S |j                  D cg c]  }| j                  ||||       }} || ||            S c c}w )a{  
        Traverses the operation tree recursively applying functions.

        Args:
            op: Operation to traverse.
            op_f: Function applied for each operation.
            aggregate_f: Aggregation function applied for all children of a single operation.
            combine_f: Combine function applied for the operation result and the children result.
        r   )r%   r   rY   )r   rB   op_faggregate_f	combine_fop_resr"   r   s           r	   rY   z!ExecutionPlan._operation_traverse   sp     br{{q M
 
 $$UD+yI
 

 X!677
s   Ac                    d}d}g }d}d }|t        | j                        k  r| j                  |   }|j                  d      }||k(  r ||j                  d            }|j                  | j
                  vrg | j
                  |j                  <   | j
                  |j                     j                  |       |r!|j                         }|j                  |       |}|dz  }|j                  |       n||dz   k(  r ||j                  d            }|j                  | j
                  vrg | j
                  |j                  <   | j
                  |j                     j                  |       |j                  |       |j                  |       |}|dz  }|dz  }n>||k  r.||z
  dz   }	t        |	      D ]  }
|j                         } ||	z  }nt        d      |t        | j                        k  r|d   S )z
        Builds the operation tree from the string representation.

        Returns:
            Operation: Root of the structured operation tree.
        r   Nc                    d }| d   j                         }| j                  d       t        |       dkD  rd| d   v rt        t	        j
                  d| d         j                  d            }t        t	        j
                  d| d         j                  d            }t        ||      }| j                  d       t        |t        |       dk(  rd |      S | d   j                         |      S )Nr   zRecords producedzRecords produced: (\d+)   zExecution time: (\d+.\d+) ms)
strippopr%   r   researchgroupr   r   r   )r   r   r   r   r   s        r	   create_operationz7ExecutionPlan._operation_tree.<locals>.create_operation  s     M7==?DHHQK4y1}!3tBx!?#&II8$r(CII!L$  "'II>RIOOPQR" !--=~ Nc$i1nd= 26q'--/= r   rN   |rf   zcorrupted plan)r%   r8   countsplitr   r9   r!   rh   r#   rE   r    )r   rI   levelstackcurrentrl   
current_opop_levelr"   levels_back_s              r	   r:   zExecutionPlan._operation_tree   s    	$ #dii. 1J!''/H5  ))9)9#)>?::T__424DOOEJJ/

+2259#iikG((/QU#UQY& ))9)9#)>?::T__424DOOEJJ/

+2259$$U+W%
QE! $h.2{+ *A#iikG*$ 011M #dii. N Qxr   N)r   r   r   r   r
   rC   r/   rF   r.   r-   r1   r)   r]   rY   r:   r   r   r	   r3   r3   s   sL    +&,d ,
 
.9 94 9$%8.Fr   r3   )ri   r   r   r3   r   r   r	   <module>rw      s+    	1 1*W( W(tQ Qr   