
    hQZ              
          d 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dlmZ ddlmZ   ed	g d
       ed       Zej"                  ej$                  fZ G d de      Zej,                  ej$                  ej.                  ej0                  ej2                  ej4                  ej6                  ej4                  ej8                  ej4                  iZd Zd Zd Z d Z!d Z"d Z#y)z*Represent a deployment of MongoDB servers.    )
namedtuple)common)ConfigurationError)ReadPreferenceServerDescription)	Selection)SERVER_TYPETopologyType)SingleReplicaSetNoPrimaryReplicaSetWithPrimaryShardedUnknown   c                      e Zd Zd Zd Zd Zd Zd Zd Ze	d        Z
e	d        Ze	d	        Ze	d
        Ze	d        Ze	d        Ze	d        Ze	d        Ze	d        Ze	d        Ze	d        ZddZej.                  fdZd Zd Zy)TopologyDescriptionc                 ,   || _         || _        || _        || _        || _        || _        d| _        | j                  j                         D ]  }|j                  s|j                  duxr |j                  t        j                  kD  }|j                  duxr |j                  t        j                  k  }	|rAd|j                  d   |j                  d   |j                  t        j                  fz  | _        |	sd|j                  d   |j                  d   |j                  t        j                  t        j                  fz  | _         n | j                   }
|
sd| _        yt%        d |
D              rd| _        yt'        d |
D              | _        y)a  Representation of a deployment of MongoDB servers.

        :Parameters:
          - `topology_type`: initial type
          - `server_descriptions`: dict of (address, ServerDescription) for
            all seeds
          - `replica_set_name`: replica set name or None
          - `max_set_version`: greatest setVersion seen from a primary, or None
          - `max_election_id`: greatest electionId seen from a primary, or None
          - `topology_settings`: a TopologySettings
        Nz]Server at %s:%d requires wire version %d, but this version of PyMongo only supports up to %d.r      zgServer at %s:%d reports wire version %d, but this version of PyMongo requires at least %d (MongoDB %s).c              3   8   K   | ]  }|j                   d u   y wNlogical_session_timeout_minutes.0ss     _/var/www/html/ranktracker/api/venv/lib/python3.12/site-packages/pymongo/topology_description.py	<genexpr>z/TopologyDescription.__init__.<locals>.<genexpr>o   s#      , 22d: ,   c              3   4   K   | ]  }|j                     y wr   r   r   s     r   r   z/TopologyDescription.__init__.<locals>.<genexpr>s   s       +F/0 ,-+L+L +F   )_topology_type_replica_set_name_server_descriptions_max_set_version_max_election_id_topology_settings_incompatible_errvaluesis_server_type_knownmin_wire_versionr   MAX_SUPPORTED_WIRE_VERSIONmax_wire_versionMIN_SUPPORTED_WIRE_VERSIONaddressMIN_SUPPORTED_SERVER_VERSIONreadable_servers_ls_timeout_minutesanymin)selftopology_typeserver_descriptionsreplica_set_namemax_set_versionmax_election_idtopology_settingsr   server_too_newserver_too_oldr1   s              r   __init__zTopologyDescription.__init__%   s   $ ,!1$7! / / #4 "&**113  	A)) ""$. K&&)J)JJ  ""$. K&&)J)JJ 
 Ayy|QYYq\))6+L+LNN &  Lyy|QYYq\))88::<< & A 	R  00'+D$ ,*, ,'+D$'* +F4D+F (FD$    c                 F    | j                   rt        | j                         y)zRaise ConfigurationError if any server is incompatible.

        A server is incompatible if its wire protocol version range does not
        overlap with PyMongo's.
        N)r(   r   r5   s    r   check_compatiblez$TopologyDescription.check_compatiblev   s#     !!$T%;%;<< "r?   c                     || j                   v S r   )r$   )r5   r/   s     r   
has_serverzTopologyDescription.has_server   s    $3333r?   c                 T    | j                   |   j                         }t        | |      S )z;A copy of this description, with one server marked Unknown.)r$   
to_unknownupdated_topology_description)r5   r/   
unknown_sds      r   reset_serverz TopologyDescription.reset_server   s(    ..w7BBD
+D*==r?   c                    | j                   t        j                  k(  rt        j                  }n| j                   }t	        d | j
                  D              }t        ||| j                  | j                  | j                  | j                        S )z<A copy of this description, with all servers marked Unknown.c              3   6   K   | ]  }|t        |      f  y wr   r   )r   r/   s     r   r   z,TopologyDescription.reset.<locals>.<genexpr>   s#      = .w78 =s   )r"   TOPOLOGY_TYPEr   r   dictr$   r   r#   r%   r&   r'   )r5   r6   sdss      r   resetzTopologyDescription.reset   s    -"E"EE)==M //M  ="&";";= = #""!!!!##% 	%r?   c                 6    | j                   j                         S )zRDict of (address,
        :class:`~pymongo.server_description.ServerDescription`).)r$   copyrA   s    r   r7   z'TopologyDescription.server_descriptions   s     ((--//r?   c                     | j                   S )zThe type of this topology.)r"   rA   s    r   r6   z!TopologyDescription.topology_type   s     """r?   c                 <    t         j                  | j                     S )zUThe topology type as a human readable string.

        .. versionadded:: 3.4
        )rL   _fieldsr"   rA   s    r   topology_type_namez&TopologyDescription.topology_type_name   s     $$T%8%899r?   c                     | j                   S )zThe replica set name.)r#   rA   s    r   r8   z$TopologyDescription.replica_set_name   s     %%%r?   c                     | j                   S )z1Greatest setVersion seen from a primary, or None.)r%   rA   s    r   r9   z#TopologyDescription.max_set_version        $$$r?   c                     | j                   S )z1Greatest electionId seen from a primary, or None.)r&   rA   s    r   r:   z#TopologyDescription.max_election_id   rX   r?   c                     | j                   S )z)Minimum logical session timeout, or None.)r2   rA   s    r   r   z3TopologyDescription.logical_session_timeout_minutes   s     '''r?   c                 t    | j                   j                         D cg c]  }|j                  r| c}S c c}w )z)List of Servers of types besides Unknown.)r$   r)   r*   r5   r   s     r   known_serversz!TopologyDescription.known_servers   s;      44;;= +a))  + 	+ +s   5c                 V    t        d | j                  j                         D              S )z7Whether there are any Servers of types besides Unknown.c              3   8   K   | ]  }|j                   r|  y wr   )r*   r   s     r   r   z8TopologyDescription.has_known_servers.<locals>.<genexpr>   s       .,,  .r   )r3   r$   r)   rA   s    r   has_known_serversz%TopologyDescription.has_known_servers   s,      .d77>>@ . . 	.r?   c                 v    | j                   j                         D cg c]  }|j                  s| c}S c c}w )zList of readable Servers.)r$   r)   is_readabler\   s     r   r1   z$TopologyDescription.readable_servers   s-      44;;=OaOOOs   66c                 X    | j                   }|rt        d | j                   D              S y)z3Minimum of all servers' max wire versions, or None.c              3   4   K   | ]  }|j                     y wr   )r-   r   s     r   r   z:TopologyDescription.common_wire_version.<locals>.<genexpr>   s     Faq))Fr!   N)r]   r4   r5   serverss     r   common_wire_versionz'TopologyDescription.common_wire_version   s,     $$F43E3EFFFr?   c                 .    | j                   j                  S r   )r'   heartbeat_frequencyrA   s    r   ri   z'TopologyDescription.heartbeat_frequency   s    &&:::r?   Nc                 0     fd}t        |dd      r8 j                  }|r*||j                  k  rt        d||j                  |fz         j                  t
        j                  k(  r j                  S |r& j                         j                  |      }|r|gS g S  j                  t
        j                  k(  rt        j                         }n |t        j                               }|#|r!|j                   ||j                              } ||      S )Nc                     | sg S j                   }t        d | j                  D              }|j                  dz  }| j                  D cg c]  }|j                  |z
  |k  r| c}S c c}w )Nc              3   4   K   | ]  }|j                     y wr   )round_trip_timer   s     r   r   zTTopologyDescription.apply_selector.<locals>.apply_local_threshold.<locals>.<genexpr>   s      J&'!!Jr!   g     @@)r'   r4   r7   local_threshold_msrm   )	selectionsettingsfastest	thresholdr   r5   s        r   apply_local_thresholdzATopologyDescription.apply_selector.<locals>.apply_local_threshold   s    	..H  J+4+H+HJ JG 33f<I(<< C!))G3	A  C C Cs   A)r+   r   zF%s requires min wire version %d, but topology's min wire version is %d)getattrrg   r+   r   r6   rL   r   r]   r7   getr   r	   from_topology_descriptionwith_server_descriptions)r5   selectorr/   custom_selectorrs   	common_wvdescriptionro   s   `       r   apply_selectorz"TopologyDescription.apply_selector   s   	C 8/300IY)B)BB(*-5-5-F-F-6-889 9 !5!55%%%22488AK$/K=7R7=#8#88!;;DAI !D!DT!JKI &9!::	 = =>@I$Y//r?   c                 d    t        j                  d|       t        | j                  |d            S )a  Does this topology have any readable servers available matching the
        given read preference?

        :Parameters:
          - `read_preference`: an instance of a read preference from
            :mod:`~pymongo.read_preferences`. Defaults to
            :attr:`~pymongo.read_preferences.ReadPreference.PRIMARY`.

        .. note:: When connected directly to a single server this method
          always returns ``True``.

        .. versionadded:: 3.4
        read_preferenceN)r   validate_read_preferencer3   r|   )r5   r~   s     r   has_readable_serverz'TopologyDescription.has_readable_server	  s-     	''(9?K4&&=>>r?   c                 @    | j                  t        j                        S )zDoes this topology have a writable server available?

        .. note:: When connected directly to a single server this method
          always returns ``True``.

        .. versionadded:: 3.4
        )r   r   PRIMARYrA   s    r   has_writable_serverz'TopologyDescription.has_writable_server  s     ''(>(>??r?   c           	          t        | j                  j                         d       }d| j                  j                  d| j
                  j                  d| j                  d|d	S )Nc                     | j                   S r   )r/   )sds    r   <lambda>z.TopologyDescription.__repr__.<locals>.<lambda>'  s
    

 r?   )key<z id: z, topology_type: z, servers: >)sortedr$   r)   	__class____name__r'   _topology_idrU   re   s     r   __repr__zTopologyDescription.__repr__$  sR    2299;24 NN##T%<%<%I%I##W. 	.r?   r   )r   
__module____qualname__r>   rB   rD   rI   rO   r7   propertyr6   rU   r8   r9   r:   r   r]   r`   r1   rg   ri   r|   r   r   r   r   r    r?   r   r   r   $   s#   OFb=4>
%&0
 # # : : & & % % % % ( ( + +
 . .
 P P   ; ;)0V 3A2H2H ?"@.r?   r   c                    |j                   }| j                  }| j                  }| j                  }| j                  }|j
                  }| j                         }|||<   |t        j                  k(  rj|@||j                  k7  r1t        d|d|j                  d      }	|j                  |	      ||<   t        t        j                  ||||| j                        S |t        j                  k(  r|t        j                  k(  rEt!        | j                  j"                        dk(  rt        j                  }n=|j%                  |       n+|t        j                  t        j&                  fvr	t(        |   }|t        j*                  k(  r6|t        j,                  t        j                  fvr|j%                  |       nw|t        j.                  k(  r|t        j                  t        j,                  fv r|j%                  |       n/|t        j0                  k(  rt3        |||||      \  }}}}n|t        j4                  t        j6                  t        j8                  fv rt;        |||      \  }}n|t        j<                  k(  r|t        j                  t        j,                  fv r|j%                  |       t?        |      }nr|t        j0                  k(  rt3        |||||      \  }}}}nJ|t        j4                  t        j6                  t        j8                  fv rtA        |||      }nt?        |      }t        |||||| j                        S )a  Return an updated copy of a TopologyDescription.

    :Parameters:
      - `topology_description`: the current TopologyDescription
      - `server_description`: a new ServerDescription that resulted from
        an ismaster call

    Called after attempting (successfully or not) to call ismaster on the
    server at server_description.address. Does not modify topology_description.
    z8client is configured to connect to a replica set named 'z(' but this node belongs to a set named '')errorr   )!r/   r6   r8   r9   r:   server_typer7   rL   r   r   rF   r   r'   r   r
   
StandalonelenseedspopRSGhost_SERVER_TYPE_TO_TOPOLOGY_TYPEr   Mongosr   	RSPrimary_update_rs_from_primaryRSSecondary	RSArbiterRSOther!_update_rs_no_primary_from_memberr   _check_has_primary#_update_rs_with_primary_from_member)
topology_descriptionserver_descriptionr/   r6   set_namer9   r:   r   rN   r   s
             r   rG   rG   8  s'    !((G )66M#44H*::O*::O$00K 
2
2
4C &CL,,, .???& 0AACDE .88u8ECL"   335 	5 ---+000'::@@AQF - 4 4  !4!4k6I6I JJ9+FM---{11;3F3FGGGGG	-;;	;;11;3E3EFFGGGK111 !88@8J8G8G	!I] ''%%##% % 'HX1'3#M8 
-==	=;11;3E3EFFGGG.s3MK111 !88@8J8G8G	!I] ''%%##% % @X13M
 /s3M }"'..3FFH Hr?   c                    | j                         }t        |j                               t        |      k(  r| S |D ]  }||vst        |      ||<    t	        |j                               D ]  }||vs|j                  |        t        | j                  || j                  | j                  | j                  | j                        S )zReturn an updated copy of a TopologyDescription.

    :Parameters:
      - `topology_description`: the current TopologyDescription
      - `seedlist`: a list of new seeds new ServerDescription that resulted from
        an ismaster call
    )r7   setkeysr   listr   r   r6   r8   r9   r:   r'   )r   seedlistrN   r/   s       r   )_updated_topology_description_srv_pollingr     s     
2
2
4C 388:#h-'##  6#,W5CL6
 
# ("GGG **--,,,,//1 1r?   c                 4   ||j                   }n9||j                   k7  r*| j                  |j                         t        |       |||fS ||f}d|j                  vrKd|vr;||j                  kD  r,|j                         | |j                  <   t        |       |||fS |j                  }|j                  ||j                  |kD  r|j                  }| j                         D ]W  }|j                  t        j                  u s |j                  |j                  k7  s:|j                         | |j                  <    n |j                  D ]  }|| vst        |      | |<    t        |       |j                  z
  D ]  }| j                  |        t        |       |||fS )aj  Update topology description from a primary's ismaster response.

    Pass in a dict of ServerDescriptions, current replica set name, the
    ServerDescription we are processing, and the TopologyDescription's
    max_set_version and max_election_id if any.

    Returns (new topology type, new replica_set_name, new max_set_version,
    new max_election_id).
    N)r8   r   r/   r   election_tuplerF   election_idset_versionr)   r   r
   r   	all_hostsr   r   )	rN   r8   r   r9   r:   max_election_tupleservernew_addressaddrs	            r   r   r     s    ->>	/@@	@ 	"**+"3' ! 	!
 )/9%444**"%7%F%FF /A.K.K.MC"**+&s+$##% %
 -88&&2		 **_<,88 **, +"7"77NN&8&@&@@ #)"3"3"5C  *33 >c!0=C>
 C-777 
 s# r?   c                 
   |J ||j                   k7  r&| j                  |j                         t	        |       S |j                  r4|j                  |j                  k7  r| j                  |j                         t	        |       S )zRS with known primary. Process a response from a non-primary.

    Pass in a dict of ServerDescriptions, current replica set name, and the
    ServerDescription we are processing.

    Returns new topology type.
    )r8   r   r/   mer   )rN   r8   r   s      r   r   r     s     '''->>>"**+ c"" 



$
$(:(=(=
="**+ c""r?   c                 l   t         j                  }||j                  }n.||j                  k7  r| j                  |j                         ||fS |j
                  D ]  }|| vst        |      | |<    |j                  r4|j                  |j                  k7  r| j                  |j                         ||fS )zRS without known primary. Update from a non-primary's response.

    Pass in a dict of ServerDescriptions, current replica set name, and the
    ServerDescription we are processing.

    Returns (new topology type, new replica_set_name).
    )rL   r   r8   r   r/   r   r   r   )rN   r8   r   r6   r/   s        r   r   r   /  s     "55M->>	/@@	@"**+... &// 6#,W5CL6 	&&*<*?*??"**+***r?   c                     | j                         D ]1  }|j                  t        j                  k(  s!t        j
                  c S  t        j                  S )zCurrent topology type is ReplicaSetWithPrimary. Is primary still known?

    Pass in a dict of ServerDescriptions.

    Returns new topology type.
    )r)   r   r
   r   rL   r   r   )rN   r   s     r   r   r   O  sE     ZZ\ 1==K111 6661 000r?   N)$__doc__collectionsr   pymongor   pymongo.errorsr   pymongo.read_preferencesr   pymongo.server_descriptionr   pymongo.server_selectorsr	   pymongo.server_typer
   rangerL   r   r   SRV_POLLING_TOPOLOGIESobjectr   r   r   r   r   r   r   r   r   rG   r   r   r   r   r   r   r?   r   <module>r      s    1 "  - 3 8 . +8
> ,7 89>qC
 (//1F1FG F.& F.X --=>>]>>=<<::! pHf1DHV#.+@1r?   