
    mBNj$f                         d dl mZmZmZmZ d dlmZ d dlmZ d dl	m
Z
 ddlmZ ddlmZ dZd	Zd
ZdZdZdZdZdZdZ G d de
      Zy)    )AnyDictListOptional)SchemaVersionMismatchException)ExecutionPlan)Graph   )GraphSchema)QueryResultz
DB.INDEXESzDB.CONSTRAINTSz
GRAPH.COPYzGRAPH.QUERYzGRAPH.DELETEzGRAPH.EXPLAINzGRAPH.SLOWLOGzGRAPH.PROFILEzGRAPH.RO_QUERYc                   *    e Zd ZdZdef fdZ	 	 	 d@dedeeeef      dee	   de
d	ef
d
Z	 	 dAdedeeeef      dee	   d	efdZ	 	 dAdedeeeef      dee	   d	efdZdefdZdBdZd Zd ZdCded	efdZdCded	efdZ	 	 	 dDdede
dee   deee      d	ef
dZdedededed	ef
dZdeded	efdZdeded	efdZdeded	efd Zdeded	efd!Zdeded	efd"Zdeded	efd#Zd	efd$Zdd%dededed&ed	ef
d'Z ded	efd(Z!ded	efd)Z"d*d+d,ded-e	d.ed	efd/Z#d0ed	efd1Z$d0ed	efd2Z%d*d+d,d0ed-e	d.ed	efd3Z&d4ededefd5Z'defd6Z(d0efd7Z)defd8Z*d0efd9Z+d4ededefd:Z,defd;Z-d0efd<Z.defd=Z/d0efd>Z0d	eeeef      fd?Z1 xZ2S )E
AsyncGraphz/
    Graph, collection of nodes and edges.
    namec                 F    t         |   ||       t        |       | _        y)z}
        Create a new graph.

        Args:
            client: The client object.
            name (str): Graph ID

        N)super__init__r   schema)selfclientr   	__class__s      g/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/falkordb/asyncio/graph.pyr   zAsyncGraph.__init__   s      	&!$'    Nqparamstimeout	read_onlyreturnc                   K   |}| j                  |      |z   }|rt        nt        }|| j                  |dg}t	        |t
              r|j                  d|g       n|t        d      	  | j                  |  d{   }t        |       }	|	j                  |       d{    |	S 7 *7 # t        $ r,}
| j                  j                  |
j                         |
d}
~
ww xY ww)a  
        Executes a query asynchronously against the graph.
        See: https://docs.falkordb.com/commands/graph.query.html

        Args:
            q (str): The query.
            params (dict): Query parameters.
            timeout (int): Maximum query runtime in milliseconds.
            read_only (bool): Whether the query is read-only.

        Returns:
            QueryResult: query result set.

        z	--compactr   Nz+Timeout argument must be a positive integer)_build_params_headerRO_QUERY_CMD	QUERY_CMDr   
isinstanceintextend	Exceptionexecute_commandr   parser   r   refreshversion)r   r   r   r   r   querycmdcommandresponsequery_resultes              r   _queryzAsyncGraph._query*   s     .  ))&1E9
 (lY!499e[A gs#NNIw/0 IJJ		1T117;;H&t,L$$X... <.- 	 KK		*G		sN   A&C")B* ;B&<#B* B( B* %C"&B* (B* *	C3'CCC"c                 H   K   | j                  |||d       d{   S 7 w)aa  
        Executes a query asynchronously against the graph.
        See: https://docs.falkordb.com/commands/graph.query.html

        Args:
            q (str): The query.
            params (dict): Query parameters.
            timeout (int): Maximum query runtime in milliseconds.

        Returns:
            QueryResult: query result set.

        Fr   r   r   Nr0   r   r   r   r   s       r   r*   zAsyncGraph.query^   s%     ( [[67e[TTTT   " "c                 H   K   | j                  |||d       d{   S 7 w)a_  
        Executes a read-only query against the graph.
        See: https://docs.falkordb.com/commands/graph.ro_query.html

        Args:
            q (str): The query.
            params (dict): Query parameters.
            timeout (int): Maximum query runtime in milliseconds.

        Returns:
            QueryResult: query result set.

        Tr2   Nr3   r4   s       r   ro_queryzAsyncGraph.ro_queryt   s%     ( [[67d[SSSSr5   clonec                    K   | j                  t        | j                  |       d{    t        | j                  |      S 7 w)z
        Creates a copy of graph

        Args:
            clone (str): Name of cloned graph

        Returns:
            AsyncGraph: the cloned graph
        N)r&   COPY_CMD_namer   r   )r   r8   s     r   copyzAsyncGraph.copy   s;      ""8TZZ???$++u-- 	@s   %AAAc                    K   | j                   j                          | j                  t        | j                         d{   S 7 w)z
        Deletes the graph.
        See: https://docs.falkordb.com/commands/graph.delete.html

        Returns:
            None

        N)r   clearr&   
DELETE_CMDr;   r   s    r   deletezAsyncGraph.delete   s5      	))*djjAAAAs   >A AAc                 ^   K   | j                  t        | j                         d{   S 7 w)a  
        Get a list containing up to 10 of the slowest queries issued
        against the graph.

        Each item in the list has the following structure:
        1. a unix timestamp at which the log entry was processed
        2. the issued command
        3. the issued query
        4. the amount of time needed for its execution, in milliseconds.

        See: https://docs.falkordb.com/commands/graph.slowlog.html

        Returns:
            List: List of slow log entries.

        Nr&   SLOWLOG_CMDr;   r@   s    r   slowlogzAsyncGraph.slowlog   s%     $ ))+tzzBBBBs   $-+-c                 b   K   | j                  t        | j                  d       d{    y7 w)z
        Reset the slowlog.
        See: https://docs.falkordb.com/commands/graph.slowlog.html

        Returns:
            None

        RESETNrC   r@   s    r   slowlog_resetzAsyncGraph.slowlog_reset   s$      "";

GDDDs   %/-/r*   c                    K   | j                  |      |z   }| j                  t        | j                  |       d{   }t	        |      S 7 w)a  
        Execute a query and produce an execution plan augmented with metrics
        for each operation's execution. Return an execution plan,
        with details on results produced by and time spent in each operation.
        See: https://docs.falkordb.com/commands/graph.profile.html

        Args:
            query (str): The query to profile.
            params (dict): Query parameters.

        Returns:
            ExecutionPlan: The profile information.

        N)r   r&   PROFILE_CMDr;   r   r   r*   r   plans       r   profilezAsyncGraph.profile   sI       ))&1E9))+tzz5IIT"" J   9AAAc                    K   | j                  |      |z   }| j                  t        | j                  |       d{   }t	        |      S 7 w)a{  
        Get the execution plan for a given query.
        GRAPH.EXPLAIN returns an ExecutionPlan object.
        See: https://docs.falkordb.com/commands/graph.explain.html

        Args:
            query (str): The query for which to get the execution plan.
            params (dict): Query parameters.

        Returns:
            ExecutionPlan: The execution plan.

        N)r   r&   EXPLAIN_CMDr;   r   rK   s       r   explainzAsyncGraph.explain   sI      ))&1E9))+tzz5IIT"" JrN   	procedureargsemitc                 >  K   |xs g }d}t        |      dkD  r'i }t        |      D ]  \  }}d| }|||<   d|z   ||<    d| ddj                  |       d}	|%t        |      dkD  r|	d	dj                  |       z  }	| j                  |	||
       d{   S 7 w)a@  
        Call a procedure.

        Args:
            procedure (str): The procedure to call.
            read_only (bool): Whether the procedure is read-only.
            args: Procedure arguments.
            emit: Procedure yield.

        Returns:
            QueryResult: The result of the procedure call.

        Nr   param$zCALL (,)zYIELD )r   r   )len	enumeratejoinr0   )
r   rR   r   rS   rT   r   iarg
param_namer   s
             r   call_procedurezAsyncGraph.call_procedure   s     , zr t9q=F $D/3$QC[
%(z"
*Q *
 I;a/q1D	A6#((4.)**A[[6Y[GGGGs   BBBBidx_typeentity_typelabel	attributec                    K   |dk(  rd| d}n|dk(  rd| d}nt        d      |dk(  r
d	| d
| d}n)|dk(  r
d| d
| d}n|dk(  r
d| d
| d}nt        d      | j                  |       d{   S 7 w)a  Drop a graph index.

        Args:
            idx_type (str): The type of index ("RANGE", "FULLTEXT", "VECTOR").
            entity_type (str): The type of entity ("NODE" or "EDGE").
            label (str): The label of the node or edge.
            attribute (str): The attribute to drop the index on.

        Returns:
            Any: The result of the index dropping query.
        NODE(e:rZ   EDGE()-[e:]->()Invalid entity typeRANGEzDROP INDEX FOR z ON (e.VECTORzDROP VECTOR INDEX FOR FULLTEXTzDROP FULLTEXT INDEX FOR zInvalid index typeN)
ValueErrorr*   )r   rb   rc   rd   re   patternr   s          r   _drop_indexzAsyncGraph._drop_index  s     & & E7!nGF"ugU+G233 w!'')A>A!(	1EA#*7)79+QGA122ZZ]"""s   A0A92A73A9c                 F   K   | j                  dd||       d{   S 7 w)aV  Drop a range index for a node.
        See: https://docs.falkordb.com/commands/graph.query.html#deleting-an-index-for-a-node-label

        Args:
            label (str): The label of the node.
            attribute (str): The attribute to drop the index on.

        Returns:
            Any: The result of the index dropping query.
        rm   rg   Nrr   r   rd   re   s      r   drop_node_range_indexz AsyncGraph.drop_node_range_index?  %      %%gvuiHHHH   !!c                 F   K   | j                  dd||       d{   S 7 w)aZ  Drop a full-text index for a node.
        See: https://docs.falkordb.com/commands/graph.query.html#deleting-an-index-for-a-node-label

        Args:
            label (str): The label of the node.
            attribute (str): The attribute to drop the index on.

        Returns:
            Any: The result of the index dropping query.
        ro   rg   Nrt   ru   s      r   drop_node_fulltext_indexz#AsyncGraph.drop_node_fulltext_indexL  %      %%j&%KKKKrx   c                 F   K   | j                  dd||       d{   S 7 w)aW  Drop a vector index for a node.
        See: https://docs.falkordb.com/commands/graph.query.html#deleting-an-index-for-a-node-label

        Args:
            label (str): The label of the node.
            attribute (str): The attribute to drop the index on.

        Returns:
            Any: The result of the index dropping query.
        rn   rg   Nrt   ru   s      r   drop_node_vector_indexz!AsyncGraph.drop_node_vector_indexY  %      %%hyIIIIrx   c                 F   K   | j                  dd||       d{   S 7 w)a^  Drop a range index for an edge.
        See: https://docs.falkordb.com/commands/graph.query.html#deleting-an-index-for-a-relationship-type

        Args:
            label (str): The label of the edge.
            attribute (str): The attribute to drop the index on.

        Returns:
            Any: The result of the index dropping query.
        rm   ri   Nrt   ru   s      r   drop_edge_range_indexz AsyncGraph.drop_edge_range_indexf  rw   rx   c                 F   K   | j                  dd||       d{   S 7 w)ab  Drop a full-text index for an edge.
        See: https://docs.falkordb.com/commands/graph.query.html#deleting-an-index-for-a-relationship-type

        Args:
            label (str): The label of the edge.
            attribute (str): The attribute to drop the index on.

        Returns:
            Any: The result of the index dropping query.
        ro   ri   Nrt   ru   s      r   drop_edge_fulltext_indexz#AsyncGraph.drop_edge_fulltext_indexs  r{   rx   c                 F   K   | j                  dd||       d{   S 7 w)a_  Drop a vector index for an edge.
        See: https://docs.falkordb.com/commands/graph.query.html#deleting-an-index-for-a-relationship-type

        Args:
            label (str): The label of the edge.
            attribute (str): The attribute to drop the index on.

        Returns:
            Any: The result of the index dropping query.
        rn   ri   Nrt   ru   s      r   drop_edge_vector_indexz!AsyncGraph.drop_edge_vector_index  r~   rx   c                 H   K   | j                  t               d{   S 7 w)zRetrieve a list of graph indices.
        See: https://docs.falkordb.com/commands/graph.query.html#procedures

        Returns:
            list: List of graph indices.
        N)ra   GRAPH_INDEXESr@   s    r   list_indiceszAsyncGraph.list_indices  s      ((7777r5   )options
propertiesc                  K   |dk(  rd| d}n|dk(  rd| d}nt        d      |dk(  rd	}d
| d| d}|dj                  t        dj                  |            z  }|dz  }|`d}|j	                         D ];  \  }	}
t        |
t              r||	dz   |
z   dz   z  }%||	dz   t        |
      z   dz   z  }= |dd dz   }|d| z  }| j                  |       d{   S 7 w)a  Create a typed index for nodes or edges.

        Args:
            idx_type (str): The type of index ("RANGE", "FULLTEXT", "VECTOR").
            entity_type (str): The type of entity ("NODE" or "EDGE").
            label (str): The label of the node or edge.
            properties: Variable number of property names to be indexed.
            options (dict, optional): Additional options for the index.

        Returns:
            Any: The result of the index creation query.
        rg   rh   rZ   ri   rj   rk   rl   rm    zCREATE z INDEX FOR z ON (rY   ze.{0}N{z:'z',:}z	 OPTIONS )rp   r]   mapformatitemsr"   strr*   )r   rb   rc   rd   r   r   rq   r   options_mapkeyvalues              r   _create_typed_indexzAsyncGraph._create_typed_index  s    ( & E7!nGF"ugU+G233wHhZ{7)59	SXXc'..*566	SK%mmo
UeS)3:#5#<<K39s5z#9C#??K	 .
 &cr*S0K9[M**AZZ]"""s   CC CC c                 L   K    | j                   dd|g|  d{   }|S 7 w)a`  Create a range index for a node.
        See: https://docs.falkordb.com/commands/graph.query.html#creating-an-index-for-a-node-label

        Args:
            label (str): The label of the node.
            properties: Variable number of property names to be indexed.

        Returns:
            Any: The result of the index creation query.
        rm   rg   Nr   r   rd   r   ress       r   create_node_range_indexz"AsyncGraph.create_node_range_index  s1      -D,,WfeQjQQ
 R   $"$c                 L   K    | j                   dd|g|  d{   }|S 7 w)am  Create a full-text index for a node.
        See: https://docs.falkordb.com/commands/graph.query.html#creating-a-full-text-index-for-a-node-label

        Args:
            label (str): The label of the node.
            properties: Variable number of property names to be indexed.

        Returns:
            Any: The result of the index creation query.
        ro   rg   Nr   r   s       r   create_node_fulltext_indexz%AsyncGraph.create_node_fulltext_index  s1      -D,,ZTTT
 Ur   r   	euclidean)dimsimilarity_functionr   r   c                \   K   ||d} | j                   dd|g|d|i d{   }|S 7 w)a  Create a vector index for a node.
        See: https://docs.falkordb.com/commands/graph.query.html#vector-indexing

        Args:
            label (str): The label of the node.
            properties: Variable number of property names to be indexed.
            dim (int, optional): The dimension of the vector.
            similarity_function (str, optional): The similarity function for the vector.

        Returns:
            Any: The result of the index creation query.
        	dimensionsimilarityFunctionrn   rg   r   Nr   )r   rd   r   r   r   r   r   s          r   create_node_vector_indexz#AsyncGraph.create_node_vector_index  sM     & !$;NO,D,,fe
&0
:A
 
 

   !,*,relationc                 L   K    | j                   dd|g|  d{   }|S 7 w)an  Create a range index for an edge.
        See: https://docs.falkordb.com/commands/graph.query.html#creating-an-index-for-a-relationship-type

        Args:
            relation (str): The relation of the edge.
            properties: Variable number of property names to be indexed.

        Returns:
            Any: The result of the index creation query.
        rm   ri   Nr   r   r   r   r   s       r   create_edge_range_indexz"AsyncGraph.create_edge_range_index  s1      -D,,WfhTTT
 Ur   c                 L   K    | j                   dd|g|  d{   }|S 7 w)a[  Create a full-text index for an edge.
        See: https://docs.falkordb.com/commands/graph.query.html#full-text-indexing

        Args:
            relation (str): The relation of the edge.
            properties: Variable number of property names to be indexed.

        Returns:
            Any: The result of the index creation query.
        ro   ri   Nr   r   s       r   create_edge_fulltext_indexz%AsyncGraph.create_edge_fulltext_index  s1      -D,,ZWJWW
 Xr   c                \   K   ||d} | j                   dd|g|d|i d{   }|S 7 w)a  Create a vector index for an edge.
        See: https://docs.falkordb.com/commands/graph.query.html#vector-indexing

        Args:
            relation (str): The relation of the edge.
            properties: Variable number of property names to be indexed.
            dim (int, optional): The dimension of the vector.
            similarity_function (str, optional): The similarity function for the vector.

        Returns:
            Any: The result of the index creation query.
        r   rn   ri   r   Nr   )r   r   r   r   r   r   r   s          r   create_edge_vector_indexz#AsyncGraph.create_edge_vector_index  sM     & !$;NO,D,,fh
)3
=D
 
 

r   constraint_typec                 x   K    | j                   dd| j                  |||dt        |      g|  d{   S 7 w)z%
        Create a constraint
        GRAPH.CONSTRAINTCREATE
PROPERTIESNr&   r   r[   r   r   rc   rd   r   s        r   _create_constraintzAsyncGraph._create_constraint1  sQ      *T))II
O

 

 

 
	
 

   1:8:c                    K   	  | j                   |g|  d{     | j                  dd|g|  d{   S 7 !# t        $ r Y *w xY w7 w)a0  
        Create node unique constraint
        See: https://docs.falkordb.com/commands/graph.constraint-create.html

        The constraint is created asynchronously, use list constraints to pull on
        constraint creation status

        Note: unique constraints require the existence of a range
        index over the constraint properties, this function will
        create any missing range indices

        Args:
            label (str): Node label to apply constraint to
            properties: Variable number of property names to constrain
        NUNIQUErg   )r   r%   r   r   rd   r   s      r   create_node_unique_constraintz(AsyncGraph.create_node_unique_constraintG  se     $	.$..uBzBBB
 -T,,XvuRzRRR C 		 S;   A> <> AAA> 	A
A	A

Ac                    K   	  | j                   |g|  d{     | j                  dd|g|  d{   S 7 !# t        $ r Y *w xY w7 w)a?  
        Create edge unique constraint
        See: https://docs.falkordb.com/commands/graph.constraint-create.html

        The constraint is created asynchronously, use list constraints to pull on
        constraint creation status

        Note: unique constraints require the existence of a range
        index over the constraint properties, this function will
        create any missing range indices

        Args:
            relation (str): Edge relationship-type to apply constraint to
            properties: Variable number of property names to constrain
        Nr   RELATIONSHIP)r   r%   r   r   r   r   s      r   create_edge_unique_constraintz(AsyncGraph.create_edge_unique_constrainta  sn     $	.$..xE*EEE -T,,nh
1;
 
 	
	 F 		
r   c                 H   K    | j                   dd|g|  d{   S 7 w)a  
        Create node mandatory constraint
        See: https://docs.falkordb.com/commands/graph.constraint-create.html

        The constraint is created asynchronously, use list constraints to pull on
        constraint creation status

        Args:
            label (str): Node label to apply constraint to
            properties: Variable number of property names to constrain
        	MANDATORYrg   Nr   r   s      r    create_node_mandatory_constraintz+AsyncGraph.create_node_mandatory_constraint|  s*      -T,,[&%U*UUUUr5   c                 H   K    | j                   dd|g|  d{   S 7 w)a  
        Create edge mandatory constraint
        See: https://docs.falkordb.com/commands/graph.constraint-create.html

        The constraint is created asynchronously, use list constraints to pull on
        constraint creation status

        Args:
            relation (str): Edge relationship-type to apply constraint to
            properties: Variable number of property names to constrain
        r   r   Nr   r   s      r    create_edge_mandatory_constraintz+AsyncGraph.create_edge_mandatory_constraint  s6      -T,,
4>
 
 	
 
r5   c                 x   K    | j                   dd| j                  |||dt        |      g|  d{   S 7 w)a)  
        Drops a constraint

        Args:
        constraint_type (str): Type of constraint to drop
        entity_type (str): Type of entity to drop constraint from
        label (str): entity's label / relationship-type
        properties: entity's properties to remove constraint from
        r   DROPr   Nr   r   s        r   _drop_constraintzAsyncGraph._drop_constraint  sQ      *T))II
O

 

 

 
	
 

r   c                 H   K    | j                   dd|g|  d{   S 7 w)aL  
        Drop node unique constraint
        See: https://docs.falkordb.com/commands/graph.constraint-create.html

        Note: the constraint supporting range index is not removed

        Args:
            label (str): Node label to remove the constraint from
            properties: properties to remove constraint from
        r   rg   Nr   r   s      r   drop_node_unique_constraintz&AsyncGraph.drop_node_unique_constraint  s*      +T**8VUPZPPPPr5   c                 H   K    | j                   dd|g|  d{   S 7 w)aX  
        Drop edge unique constraint
        See: https://docs.falkordb.com/commands/graph.constraint-create.html

        Note: the constraint supporting range index is not removed

        Args:
            label (str): Edge relationship-type to remove the constraint from
            properties: properties to remove constraint from
        r   r   Nr   r   s      r   drop_edge_unique_constraintz&AsyncGraph.drop_edge_unique_constraint  s6      +T**nh
1;
 
 	
 
r5   c                 H   K    | j                   dd|g|  d{   S 7 w)a  
        Drop node mandatory constraint
        See: https://docs.falkordb.com/commands/graph.constraint-create.html

        Args:
            label (str): Node label to remove the constraint from
            properties: properties to remove constraint from
        r   rg   Nr   r   s      r   drop_node_mandatory_constraintz)AsyncGraph.drop_node_mandatory_constraint  s*      +T**;S
SSSSr5   c                 H   K    | j                   dd|g|  d{   S 7 w)a  
        Drop edge mandatory constraint
        See: https://docs.falkordb.com/commands/graph.constraint-create.html

        Args:
            label (str): Edge relationship-type to remove the constraint from
            properties: properties to remove constraint from
        r   r   Nr   r   s      r   drop_edge_mandatory_constraintz)AsyncGraph.drop_edge_mandatory_constraint  s6      +T**
4>
 
 	
 
r5   c           	         K   | j                  t               d{   j                  }g }|D ](  }|j                  |d   |d   |d   |d   |d   d       * |S 7 ?w)z
        Lists graph's constraints

        See: https://docs.falkordb.com/commands/graph.constraint-create.html#listing-constraints

        Returns:
            [Dict[str, object]]: list of constraints
        Nr   r
            )typerd   r   
entitytypestatus)ra   GRAPH_LIST_CONSTRAINTS
result_setappend)r   resultconstraintsrows       r   list_constraintszAsyncGraph.list_constraints  su      ++,BCCOOCF V"%a&"%a&!!f   Ds   AAA A)NNF)NN)r   N)N)TNN)3__name__
__module____qualname____doc__r   r   r   r   objectr#   boolr   r0   r*   r7   r<   rA   rE   rH   r   rM   rQ   r   ra   rr   rv   rz   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__)r   s   @r   r   r      s4   (S (  /3!%22 c6k*+2 #	2
 2 
2n /3!%	UU c6k*+U #	U
 
U2 /3!%	TT c6k*+T #	T
 
T,. .BC(	E#3 # #(#3 # #0 #$((H(H (H tn	(H
 tCy!(H 
(HX$#$# $# 	$#
 $# 
$#LI I I ILC LC LK LJ# J# J+ JI I I ILC LC LK LJ# J# J+ J8K 8 -#-# -# 	-#
 -# 
-#^3  c ; $ #. 	
 ! 
2c ; 	( #. 	
 ! 
2
"
14
=@
,S S4
C 
6VC V
s 
 
"
14
=@
2Qs Q
# 
 
T# 
T
S 
T#v+->(? r   r   N)typingr   r   r   r   falkordb.exceptionsr   falkordb.execution_planr   falkordb.graphr	   graph_schemar   r.   r   r   r   r:   r!   r?   rP   rD   rJ   r    r    r   r   <module>r      sZ    , , > 1   % % )  	
k kr   