
    Rǻi                     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	  ed      Z
 G d d	e      Z G d
 d      Zy)    )
MutableSet)suppress)	getLogger)	monotonic   )Addressz
neo4j.poolc                   `    e Zd ZddZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd ZddZddZy)
OrderedSetc                 F    t         j                  |      | _        d | _        y N)dictfromkeys	_elements_currentselfelementss     L/opt/lhia/marcimex/agent/venv/lib/python3.12/site-packages/neo4j/_routing.py__init__zOrderedSet.__init__   s    x0    c                     | j                   j                   ddj                  t        t        | j
                               dS )Nz((, z)))	__class____name__joinmapreprr   r   s    r   __repr__zOrderedSet.__repr__"   s;    ~~&&'ryyT4>>234	
r   c                 \    ddj                  t        t        | j                               dS )N{r   })r   r   r   r   r   s    r   __str__zOrderedSet.__str__)   s&    DIIc$789<<r   c                     || j                   v S r   r   r   elements     r   __contains__zOrderedSet.__contains__,   s    $..((r   c                 ,    t        | j                        S r   )iterr   r   s    r   __iter__zOrderedSet.__iter__/   s    DNN##r   c                 ,    t        | j                        S r   )lenr   r   s    r   __len__zOrderedSet.__len__2   s    4>>""r   c                 N    t        | j                  j                               |   S r   )listr   keys)r   indexs     r   __getitem__zOrderedSet.__getitem__5   s    DNN'')*511r   c                 "    d | j                   |<   y r   r%   r&   s     r   addzOrderedSet.add8   s    "&wr   c                 8    | j                   j                          y r   )r   clearr   s    r   r7   zOrderedSet.clear;   s    r   c                 f    t        t              5  | j                  |= d d d        y # 1 sw Y   y xY wr   )r   KeyErrorr   r&   s     r   discardzOrderedSet.discard>   s+    h 	(w'	( 	( 	(s   '0c                 R    	 | j                   |= y # t        $ r t        |      d w xY wr   )r   r9   
ValueErrorr&   s     r   removezOrderedSet.removeB   s/    	0w' 	0W%4/	0s    &c                 `    | j                   j                  t        j                  |             y r   )r   updater   r   r   s     r   r?   zOrderedSet.updateH   s    dmmH56r   c                     | j                   }|j                          |j                  t        j	                  |             y r   )r   r7   r?   r   r   )r   r   es      r   replacezOrderedSet.replaceK   s)    NN			x()r   N) )r   
__module____qualname__r   r   r#   r(   r+   r.   r3   r5   r7   r:   r=   r?   rB   rC   r   r   r
   r
      sC    

=)$#2'(07*r   r
   c                   Z    e Zd Zed        ZddddddZd Zd ZddZd	 Z	d
 Z
d Zd Zy)RoutingTablec                l   g }g }g }	 |D ]s  }|d   }|d   D 	cg c]  }	t        j                  |	d       }
}	|dk(  r|j                  |
       F|dk(  r|j                  |
       ]|dk(  sc|j                  |
       u 	  | |||||      S c c}	w # t        t        f$ r}t        d	      |d
}~ww xY w)z
        Construct a new RoutingTable instance from the given routing info.

        The routing table info is returned from the procedure or BOLT message.
        role	addressesi  )default_portROUTEREADWRITE)databaseroutersreaderswritersttlzCannot parse routing infoN)r   parseextendr9   	TypeErrorr<   )clsrO   serversrS   rP   rQ   rR   serverrI   addressrJ   excs               r   parse_routing_infozRoutingTable.parse_routing_infoR   s     	! .f~ $*+#6 MM'=	  7?NN9-V^NN9-W_NN9-. !  )$ 	C89sB	Cs-   B B5B -B B B3"B..B3rC   r   )rP   rQ   rR   rS   c                    t        |      | _        t        |      | _        t        |      | _        t        |      | _        | j                   | _        t               | _        || _        || _	        y r   )
r
   initial_routersrP   rQ   rR   initialized_without_writersr   last_updated_timerS   rO   )r   rO   rP   rQ   rR   rS   s         r   r   zRoutingTable.__init__t   sZ    )'2!'*!'*!'*/3||+;(!* r   c                     d| j                   dt        | j                        dt        | j                        dt        | j                        d| j
                  d| j                  dS )NzRoutingTable(database=z
, routers=z
, readers=z
, writers=z, last_updated_time=z, ttl=))rO   tuplerP   rQ   rR   r`   rS   r   s    r   r   zRoutingTable.__repr__~   sy    ( )T\\*- .T\\*- .T\\*- .!!%!7!7 : ;88,		
r   c                 ^    || j                   v xs || j                  v xs || j                  v S r   )rP   rQ   rR   )r   rZ   s     r   r(   zRoutingTable.__contains__   s5    t||# '$,,&'$,,&	
r   c                 >   t        |t              sJ | j                  | j                  z   t	               k  }|rt        | j
                        }nt        | j                        }| xr | j                  xr |}t        j                  d|||| j                  |       |S )z5Indicate whether routing information is still usable.z~[#0000]  _: <ROUTING> checking table freshness (readonly=%r): table expired=%r, has_server_for_mode=%r, table routers=%r => %r)

isinstanceboolr`   rS   r   rQ   rR   rP   logdebug)r   readonlyexpiredhas_server_for_moderess        r   is_freshzRoutingTable.is_fresh   s    (D)))((4883y{B"&t||"4"&t||"4kBdllB/B		= LL		
 
r   c                     ddl m} t               }| j                  | j                  z   |j
                  z   }||k  }t        j                  d| j                  | j                  ||       |S )a  
        Check if routing table needs to be purged from memory.

        This is the case if the routing table is stale and has not been used
        for a long time.

        :returns: Returns true if it is old and not used for a while.
        :rtype: bool
        r   )RoutingConfigzS[#0000]  _: <ROUTING> purge check: last_updated_time=%r, ttl=%r, perf_time=%r => %r)_confrp   r   r`   rS   routing_table_purge_delayrh   ri   )r   rp   	perf_timevalid_untilshould_be_purgeds        r   should_be_purged_from_memoryz)RoutingTable.should_be_purged_from_memory   sr     	)K	""hh556 	
 ')3		?""HH	
  r   c                 r   | j                   j                  |j                          | j                  j                  |j                         | j                  j                  |j                         | j                   | _        t               | _        |j                  | _        t        j                  d|        y)z6Update the routing table with new routing information.z&[#0000]  _: <ROUTING> updated table=%rN)
rP   rB   rQ   rR   r_   r   r`   rS   rh   ri   )r   new_routing_tables     r   r?   zRoutingTable.update   s    .667.667.667/3||+;(!*$((		:DAr   c                     t        | j                        t        | j                        z  t        | j                        z  S r   )setrP   rR   rQ   r   s    r   rX   zRoutingTable.servers   s-    4<< 3t||#44s4<<7HHHr   c                 8   t        |t              st        S | j                  |j                  k(  xrj | j                  |j                  k(  xrO | j
                  |j
                  k(  xr4 | j                  |j                  k(  xr | j                  |j                  k(  S r   )rf   rG   NotImplementedrO   rP   rQ   rR   rS   )r   others     r   __eq__zRoutingTable.__eq__   s    %.!!MMU^^+ &-&-& -& EII%	
r   N)F)r   rD   rE   classmethodr\   r   r   r(   rn   rv   r?   rX   r~   rC   r   r   rG   rG   Q   sJ     B -/BA !


* :BI	
r   rG   N)collections.abcr   
contextlibr   loggingr   timer   _addressingr   rh   r
   rG   rC   r   r   <module>r      s;   " '      2* 2*jH
 H
r   