o
    Š¨ÊhV  ã                   @   s6   d dl Z G dd„ dƒZG dd„ dƒZG dd„ dƒZdS )é    Nc                   @   s   e Zd ZdZdd„ ZdS )ÚProfileStatszB
    ProfileStats, runtime execution statistics of operation.
    c                 C   s   || _ || _d S ©N)Úrecords_producedÚexecution_time)Úselfr   r   © r   úU/var/www/html/env/lib/python3.10/site-packages/redis/commands/graph/execution_plan.pyÚ__init__	   s   
zProfileStats.__init__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r	   r   r   r   r   r      s    r   c                   @   sJ   e Zd ZdZddd„Zdd„ Zdd„ Zd	ed
efdd„Z	d
e
fdd„ZdS )Ú	Operationz<
    Operation, single operation within execution plan.
    Nc                 C   s   || _ || _|| _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   r   r   r   r	      s   	
zOperation.__init__c                 C   s*   t |tƒr	| |u rtdƒ‚| j |¡ | S )Nzchild must be Operation)Ú
isinstancer   Ú	Exceptionr   Úappend)r   Úchildr   r   r   Úappend_child!   s   zOperation.append_childc                 C   s
   t | jƒS r   ©Úlenr   )r   r   r   r   Úchild_count(   s   
zOperation.child_countÚoÚreturnc                 C   s&   t |tƒsdS | j|jko| j|jkS )NF)r   r   r   r   )r   r   r   r   r   Ú__eq__+   s   
zOperation.__eq__c                 C   s&   | j d u rdnd| j  }| j› |› �S )NÚ z | )r   r   )r   Úargs_strr   r   r   Ú__str__1   s   zOperation.__str__)NN)r
   r   r   r   r	   r   r   ÚobjectÚboolr   Ústrr    r   r   r   r   r      s    
r   c                   @   sP   e Zd ZdZdd„ Zdd„ Zdefdd„Zd	ede	fd
d„Z
dd„ Zdd„ ZdS )ÚExecutionPlanz2
    ExecutionPlan, collection of operations.
    c                 C   sB   t |tƒs	tdƒ‚t |d tƒrd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                 S   s   g | ]}|  ¡ ‘qS r   )Údecode)Ú.0Úbr   r   r   Ú
<listcomp>G   s    z*ExecutionPlan.__init__.<locals>.<listcomp>N)r   Úlistr   ÚbytesÚplanÚ_operation_treeÚstructured_plan)r   r+   r   r   r   r	   ;   s   
zExecutionPlan.__init__c                 C   sT   ||krdS |  ¡ |  ¡ krdS t|  ¡ ƒD ]}|  |j| |j| ¡s' dS qd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Úir   r   r   r/   L   s   ÿz!ExecutionPlan._compare_operationsr   c                 C   s"   dd„ }dd„ }|   | jt||¡S )Nc                 S   s   d  dd„ | D ƒ¡S )NÚ
c                 S   s"   g | ]}|  ¡ D ]}d | ‘qqS )ú    )Ú
splitlines)r&   Ú	str_childÚliner   r   r   r(   e   s    ýþÿz?ExecutionPlan.__str__.<locals>.aggraget_str.<locals>.<listcomp>)Újoin)Ústr_childrenr   r   r   Úaggraget_strc   s
   þÿz+ExecutionPlan.__str__.<locals>.aggraget_strc                 S   s   | › d|› �S )Nr3   r   )ÚxÚyr   r   r   Úcombine_strl   s   z*ExecutionPlan.__str__.<locals>.combine_str)Ú_operation_traverser-   r#   )r   r:   r=   r   r   r   r    b   s
   	
ÿzExecutionPlan.__str__r   c                 C   s&   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   r0   r1   r   r   r   r   s   s
   
zExecutionPlan.__eq__c                    sB   ˆ|ƒ}t |jƒdkr|S ‡ ‡‡‡fd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                    s   g | ]
}ˆ  |ˆˆ ˆ¡‘qS r   )r>   )r&   r   ©Úaggregate_fÚ	combine_fÚop_fr   r   r   r(   “   s    ÿÿz5ExecutionPlan._operation_traverse.<locals>.<listcomp>r   )r   ÚoprB   r@   rA   Úop_resr   r   r?   r   r>   ƒ   s   þz!ExecutionPlan._operation_traversec                 C   s
  d}d}g }d}dd„ }|t | jƒk r�| j| }| d¡}||kr:|| d¡ƒ}|r3| ¡ }| |¡ |}|d7 }n@||d kr\|| d¡ƒ}| |¡ | |¡ |}|d7 }|d7 }n||k rv|| d }	t|	ƒD ]}
| ¡ }qj||	8 }ntdƒ‚|t | jƒk s|d S )	z7Build the operation tree from the string representationr   Nc                 S   s¢   d }| d   ¡ }|  d¡ t| ƒdkr=d| d v r=tt d| d ¡ d¡ƒ}tt d| d ¡ d¡ƒ}t||ƒ}|  d¡ t	|t| ƒdkrId |ƒS | d   ¡ |ƒS )Nr   zRecords producedéÿÿÿÿzRecords produced: (\d+)é   zExecution time: (\d+.\d+) ms)
ÚstripÚpopr   ÚintÚreÚsearchÚgroupÚfloatr   r   )r   r   r   r   r   r   r   r   Ú_create_operation£   s"   
ÿÿ

ÿÿz8ExecutionPlan._operation_tree.<locals>._create_operationr4   ú|rF   zcorrupted plan)	r   r+   ÚcountÚsplitrH   r   r   r.   r   )r   r2   ÚlevelÚstackÚcurrentrN   Ú
current_opÚop_levelr   Úlevels_backÚ_r   r   r   r,   š   s<   








ãzExecutionPlan._operation_treeN)r
   r   r   r   r	   r/   r#   r    r!   r"   r   r>   r,   r   r   r   r   r$   6   s    r$   )rJ   r   r   r$   r   r   r   r   Ú<module>   s    
(