
    PfV                     \    d dl Z  G d d          Z G d d          Z G d d          ZdS )    Nc                       e Zd ZdZd ZdS )ProfileStatszB
    ProfileStats, runtime execution statistics of operation.
    c                 "    || _         || _        d S N)records_producedexecution_time)selfr   r   s      U/var/www/html/env/lib/python3.11/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                   B    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dS )	Operationz<
    Operation, single operation within execution plan.
    Nc                 >    || _         || _        || _        g | _        dS )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                     t          |t                    r| |u rt          d          | j                            |           | S )Nzchild must be Operation)
isinstancer   	Exceptionr   append)r	   childs     r
   append_childzOperation.append_child!   sF    %++ 	7tu}}5666U###r   c                 *    t          | j                  S r   lenr   )r	   s    r
   child_countzOperation.child_count(   s    4=!!!r   oreturnc                 p    t          |t                    sdS | j        |j        k    o| j        |j        k    S )NF)r   r   r   r   )r	   r#   s     r
   __eq__zOperation.__eq__+   s6    !Y'' 	5yAF":tyAF'::r   c                 >    | j         dn	d| j         z   }| j         | S )N z | )r   r   )r	   args_strs     r
   __str__zOperation.__str__1   s,    *22	0A)'X'''r   )NN)r   r   r   r   r   r   r"   objectboolr&   strr*   r   r   r
   r   r      s              " " "; ;4 ; ; ; ;( ( ( ( ( ( (r   r   c                   F    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d
S )ExecutionPlanz2
    ExecutionPlan, collection of operations.
    c                     t          |t                    st          d          t          |d         t                    rd |D             }|| _        |                                 | _        dS )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   c                 6    g | ]}|                                 S r   )decode).0bs     r
   
<listcomp>z*ExecutionPlan.__init__.<locals>.<listcomp>G   s     ---1AHHJJ---r   N)r   listr   bytesplan_operation_treestructured_plan)r	   r8   s     r
   r   zExecutionPlan.__init__;   so     $%% 	53444d1gu%% 	.-----D	#3355r   c                    ||k    rdS |                                 |                                 k    rdS t          |                                           D ]1}|                     |j        |         |j        |                   s dS 2dS )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
   r=   z!ExecutionPlan._compare_operationsL   s     V5 6#5#5#7#7775 v))++,, 	 	A++FOA,>PQ@RSS uu tr   r$   c                 R    d }d }|                      | j        t          ||          S )Nc                 @    d                     d | D                       S )N
c                 F    g | ]}|                                 D ]}d |z   S )    )
splitlines)r3   	str_childlines      r
   r5   z?ExecutionPlan.__str__.<locals>.aggraget_str.<locals>.<listcomp>e   sO       ! ) 4 4 6 6   TM   r   )join)str_childrens    r
   aggraget_strz+ExecutionPlan.__str__.<locals>.aggraget_strc   s4    99 %1    r   c                     |  d| S )NrC   r   )xys     r
   combine_strz*ExecutionPlan.__str__.<locals>.combine_strl   s    ;;1;;r   )_operation_traverser:   r-   )r	   rK   rO   s      r
   r*   zExecutionPlan.__str__b   sF    	 	 		 	 	 '' #|[
 
 	
r   r#   c                 x    t          |t                    sdS | j        }|j        }|                     ||          S )zfCompares two execution plans

        Return: True if the two plans are equal False otherwise
        F)r   r/   r:   r=   )r	   r#   r>   r?   s       r
   r&   zExecutionPlan.__eq__s   sE     !]++ 	5 %" ''777r   c                       |          }t          |j                  dk    r|S  fd|j        D             } | |                    S )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   c                 @    g | ]}                     |          S r   )rP   )r3   r   aggregate_f	combine_fop_fr	   s     r
   r5   z5ExecutionPlan._operation_traverse.<locals>.<listcomp>   s=        ((k9MM  r   r    )r	   oprV   rT   rU   op_resr   s   ` ```  r
   rP   z!ExecutionPlan._operation_traverse   s     br{q  M      [  H
 9V[[%:%:;;;r   c                    d}d}g }d}d }|t          | j                  k     r9| j        |         }|                    d          }||k    rQ ||                    d                    }|r)|                                }|                    |           |}|dz  }n||dz   k    rU ||                    d                    }|                    |           |                    |           |}|dz  }|dz  }nI||k     r4||z
  dz   }	t          |	          D ]}
|                                }||	z  }nt          d          |t          | j                  k     9|d         S )z7Build the operation tree from the string representationr   Nc                 P   d }| d                                          }|                     d           t          |           dk    rd| d         v rt          t	          j        d| d                                       d                    }t          t	          j        d| d                                       d                    }t          ||          }|                     d           t          |t          |           dk    rd n| d                                          |          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HHQKKK4yy1}}!3tBx!?!?#&I8$r(CCII!LL$ $  "'I>RIIOOPQRR" " !--=~ N Nc$ii1nndd$q'--//=  r   rE   |r\   zcorrupted plan)	r!   r8   countsplitr^   r   r   r<   r   )r	   r@   levelstackcurrentrd   
current_opop_levelr   levels_back_s              r
   r9   zExecutionPlan._operation_tree   s    	 	 	$ #di..  1J!''//H5   *)**:*:3*?*?@@ 0#iikkG((///QUQY&& *)**:*:3*?*?@@$$U+++W%%%
QE!! $h.2{++ * *A#iikkGG$ 0111; #di..  < Qxr   N)r   r   r   r   r   r=   r-   r*   r+   r,   r&   rP   r9   r   r   r
   r/   r/   6   s         6 6 6"  ,
 
 
 
 
"8 84 8 8 8 8 < < <.9 9 9 9 9r   r/   )r`   r   r   r/   r   r   r
   <module>ro      s    				- - - - - - - -%( %( %( %( %( %( %( %(P] ] ] ] ] ] ] ] ] ]r   