
    +hV                     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 Zy)ProfileStatszB
    ProfileStats, runtime execution statistics of operation.
    c                      || _         || _        y N)records_producedexecution_time)selfr   r   s      V/var/www/html/venv/lib/python3.12/site-packages/redis/commands/graph/execution_plan.py__init__zProfileStats.__init__	   s     0,    N)__name__
__module____qualname____doc__r    r   r
   r   r      s    -r   r   c                   @    e Zd ZdZd
dZd Zd ZdedefdZ	de
fd	Zy)	Operationz<
    Operation, single operation within execution plan.
    Nc                 <    || _         || _        || _        g | _        y)z
        Create a new operation.

        Args:
            name: string that represents the name of the operation
            args: operation arguments
            profile_stats: profile statistics
        N)nameargsprofile_statschildren)r	   r   r   r   s       r
   r   zOperation.__init__   s!     		*r   c                 z    t        |t              r| |u rt        d      | j                  j	                  |       | S )Nzchild must be Operation)
isinstancer   	Exceptionr   append)r	   childs     r
   append_childzOperation.append_child!   s5    %+tu}566U#r   c                 ,    t        | j                        S r   )lenr   )r	   s    r
   child_countzOperation.child_count(   s    4==!!r   oreturnc                     t        |t              sy| j                  |j                  k(  xr | j                  |j                  k(  S )NF)r   r   r   r   )r	   r"   s     r
   __eq__zOperation.__eq__+   s4    !Y'yyAFF":tyyAFF'::r   c                 \    | j                   dnd| j                   z   }| j                   | S )N z | )r   r   )r	   args_strs     r
   __str__zOperation.__str__1   s.    *2		0A))XJ''r   )NN)r   r   r   r   r   r   r!   objectboolr%   strr)   r   r   r
   r   r      s5    "; ;4 ;( (r   r   c                   D    e Zd ZdZd Zd ZdefdZdede	fdZ
d Zd	 Zy
)ExecutionPlanz2
    ExecutionPlan, collection of operations.
    c                     t        |t              st        d      t        |d   t              r|D cg c]  }|j	                          }}|| _        | j                         | _        yc c}w )z
        Create a new execution plan.

        Args:
            plan: array of strings that represents the collection operations
                  the output from GRAPH.EXPLAIN
        zplan must be an arrayr   N)r   listr   bytesdecodeplan_operation_treestructured_plan)r	   r3   bs      r
   r   zExecutionPlan.__init__;   s^     $%344d1gu%(,-1AHHJ-D-	#335 .s   A)c                     ||k7  ry|j                         |j                         k7  ryt        |j                               D ]0  }| j                  |j                  |   |j                  |         r0 y y)z{
        Compare execution plan operation tree

        Return: True if operation trees are equal, False otherwise
        FT)r!   range_compare_operationsr   )r	   root_aroot_bis       r
   r9   z!ExecutionPlan._compare_operationsL   sx     V 6#5#5#77 v))+, 	A++FOOA,>PQ@RS	 r   r#   c                 R    d }d }| j                  | j                  t        ||      S )Nc           	          dj                  | D cg c]  }|j                         D ]  }d|z   	  c}}      S c c}}w )N
    )join
splitlines)str_children	str_childlines      r
   aggraget_strz+ExecutionPlan.__str__.<locals>.aggraget_strc   sR    99 &2! ) 4 4 6  TM! s   !:
c                     |  d| S )Nr?   r   )xys     r
   combine_strz*ExecutionPlan.__str__.<locals>.combine_strl   s    S1#;r   )_operation_traverser5   r,   )r	   rF   rJ   s      r
   r)   zExecutionPlan.__str__b   s0    		 ''  #|[
 	
r   r"   c                 x    t        |t              sy| j                  }|j                  }| j                  ||      S )zfCompares two execution plans

        Return: True if the two plans are equal False otherwise
        F)r   r.   r5   r9   )r	   r"   r:   r;   s       r
   r%   zExecutionPlan.__eq__s   s>     !]+ %%"" ''77r   c           	           ||      }t        |j                        dk(  r|S |j                  D cg c]  }| j                  ||||       }} || ||            S c c}w )aq  
        Traverse 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   rK   )r	   opop_faggregate_f	combine_fop_resr   r   s           r
   rK   z!ExecutionPlan._operation_traverse   sp     br{{q M
  [[ ((k9MH 
 V[%:;;s   Ac                 n   d}d}g }d}d }|t        | j                        k  r| j                  |   }|j                  d      }||k(  rB ||j                  d            }|r!|j	                         }|j                  |       |}|dz  }n||dz   k(  rF ||j                  d            }|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 )z7Build the operation tree from the string representationr   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    intresearchgroupfloatr   r   )r   r   r   r   r   s        r
   _create_operationz8ExecutionPlan._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   r@   |rV   zcorrupted plan)	r    r3   countsplitrX   r   r   r8   r   )r	   r<   levelstackcurrentr^   
current_opop_levelr   levels_back_s              r
   r4   zExecutionPlan._operation_tree   sT    	$ #dii. 1J!''/H5  ***:*:3*?@#iikG((/QUQY& ***:*:3*?@$$U+W%
QE! $h.2{+ *A#iikG*$ 011; #dii. < Qxr   N)r   r   r   r   r   r9   r,   r)   r*   r+   r%   rK   r4   r   r   r
   r.   r.   6   s:    6",
 
"8 84 8 <.9r   r.   )rZ   r   r   r.   r   r   r
   <module>ri      s)    	- -%( %(P] ]r   