
    hX              
          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LoadBalanced   c                      e Zd 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j0                  fdZd Zd Zy)TopologyDescriptionc                 N   || _         || _        || _        || _        || _        || _        d| _        | j                   t        j                  k7  r| j                          | 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
        Nc              3   8   K   | ]  }|j                   d u   y wNlogical_session_timeout_minutes.0ss     Z/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/pymongo/topology_description.py	<genexpr>z/TopologyDescription.__init__.<locals>.<genexpr>X   s     Uq22d:Us   c              3   4   K   | ]  }|j                     y wr   r   r   s     r   r   z/TopologyDescription.__init__.<locals>.<genexpr>[   s      +6711+   )_topology_type_replica_set_name_server_descriptions_max_set_version_max_election_id_topology_settings_incompatible_errTOPOLOGY_TYPEr   _init_incompatible_errreadable_servers_ls_timeout_minutesanymin)selftopology_typeserver_descriptionsreplica_set_namemax_set_versionmax_election_idtopology_settingsr*   s           r   __init__zTopologyDescription.__init__,   s    ( ,!1$7! / / #4 "&-"<"<<'')  00'+D$UDTUU'+D$'* +;K+ (D$    c                 <   | 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  | _	         y y)z>Internal compatibility check for non-load balanced topologies.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).)r#   valuesis_server_type_knownmin_wire_versionr   MAX_SUPPORTED_WIRE_VERSIONmax_wire_versionMIN_SUPPORTED_WIRE_VERSIONaddressr'   MIN_SUPPORTED_SERVER_VERSION)r.   r   server_too_newserver_too_olds       r   r)   z*TopologyDescription._init_incompatible_err_   s'   **113 +	A)) ""$. K&&)J)JJ  ""$. K&&)J)JJ  A 		!		!**99	 &  L 		!		!**99;; & W+	r6   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   r.   s    r   check_compatiblez$TopologyDescription.check_compatible   s#     !!$T%;%;<< "r6   c                     || j                   v S r   )r#   )r.   r?   s     r   
has_serverzTopologyDescription.has_server   s    $3333r6   c                 T    | j                   |   j                         }t        | |      S )z;A copy of this description, with one server marked Unknown.)r#   
to_unknownupdated_topology_description)r.   r?   
unknown_sds      r   reset_serverz TopologyDescription.reset_server   s(    ..w7BBD
+D*==r6   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     bWG.w78bs   )r!   r(   r   r   dictr#   r   r"   r$   r%   r&   )r.   r/   sdss      r   resetzTopologyDescription.reset   s{    -"E"EE)==M //M bHaHabb"""!!!!##
 	
r6   c                 6    | j                   j                         S )zRDict of (address,
        :class:`~pymongo.server_description.ServerDescription`).)r#   copyrD   s    r   r0   z'TopologyDescription.server_descriptions   s     ((--//r6   c                     | j                   S )zThe type of this topology.)r!   rD   s    r   r/   z!TopologyDescription.topology_type   s     """r6   c                 <    t         j                  | j                     S )zUThe topology type as a human readable string.

        .. versionadded:: 3.4
        )r(   _fieldsr!   rD   s    r   topology_type_namez&TopologyDescription.topology_type_name   s     $$T%8%899r6   c                     | j                   S )zThe replica set name.)r"   rD   s    r   r1   z$TopologyDescription.replica_set_name   s     %%%r6   c                     | j                   S )z1Greatest setVersion seen from a primary, or None.)r$   rD   s    r   r2   z#TopologyDescription.max_set_version        $$$r6   c                     | j                   S )z1Greatest electionId seen from a primary, or None.)r%   rD   s    r   r3   z#TopologyDescription.max_election_id   rZ   r6   c                     | j                   S )z)Minimum logical session timeout, or None.)r+   rD   s    r   r   z3TopologyDescription.logical_session_timeout_minutes   s     '''r6   c                 v    | j                   j                         D cg c]  }|j                  s| c}S c c}w )z)List of Servers of types besides Unknown.)r#   r9   r:   r.   r   s     r   known_serversz!TopologyDescription.known_servers   s/      44;;=XaAWAWXXX   66c                 V    t        d | j                  j                         D              S )z7Whether there are any Servers of types besides Unknown.c              3   :   K   | ]  }|j                   s|  y wr   )r:   r   s     r   r   z8TopologyDescription.has_known_servers.<locals>.<genexpr>   s     [ADZDZ1[s   )r,   r#   r9   rD   s    r   has_known_serversz%TopologyDescription.has_known_servers   s$     [d77>>@[[[r6   c                 v    | j                   j                         D cg c]  }|j                  s| c}S c c}w )zList of readable Servers.)r#   r9   is_readabler^   s     r   r*   z$TopologyDescription.readable_servers   s-      44;;=OaOOOr`   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_   r-   r.   serverss     r   common_wire_versionz'TopologyDescription.common_wire_version   s,     $$F43E3EFFFr6   c                 .    | j                   j                  S r   )r&   heartbeat_frequencyrD   s    r   rl   z'TopologyDescription.heartbeat_frequency   s    &&:::r6   Nc                 N     fd}t        |dd      r8 j                  }|r*||j                  k  rt        d||j                  |fz         j                  t
        j                  t
        j                  fv 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     S!++Sr    g     @@)r&   r-   r0   local_threshold_msrp   )	selectionsettingsfastest	thresholdr   r.   s        r   apply_local_thresholdzATopologyDescription.apply_selector.<locals>.apply_local_threshold   sy    	..H SY5R5RSSG 33f<I #66%%/I=   s   A)r;   r   zF%s requires min wire version %d, but topology's min wire version is %d)getattrrj   r;   r   r/   r(   r   r   r_   r0   getr   r	   from_topology_descriptionwith_server_descriptions)r.   selectorr?   custom_selectorrv   	common_wvdescriptionrr   s   `       r   apply_selectorz"TopologyDescription.apply_selector   s!   	 8/300IY)B)BB(*-5x7P7PR[,\] 
 -"6"68R8R!SS%%%22488AK$/K=7R7=#8#88!;;DAI !D!DT!JKI &9!::	 = =>I %Y//r6   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_preferencer,   r   )r.   r   s     r   has_readable_serverz'TopologyDescription.has_readable_server   s-     	''(9?K4&&=>>r6   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   PRIMARYrD   s    r   has_writable_serverz'TopologyDescription.has_writable_server1  s     ''(>(>??r6   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
    BJJ r6   )key<z id: z, topology_type: z, servers: >)sortedr#   r9   	__class____name__r&   _topology_idrW   rh   s     r   __repr__zTopologyDescription.__repr__;  sP    2299;AVWNN####00##	
 	
r6   r   )r   
__module____qualname__r5   r)   rE   rG   rL   rQ   r0   propertyr/   rW   r1   r2   r3   r   r_   rc   r*   rj   rl   r   r   r   r   r   r    r6   r   r   r   +   s+   1f-^=4>

&0
 # # : : & & % % % % ( ( Y Y \ \ P P   ; ;*0X 3A2H2H ?"@
r6   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                  t        j                   fv 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        j0                  k(  r|t        j                  t        j.                  fv r|j'                  |       n/|t        j2                  k(  rt5        |||||      \  }}}}n|t        j6                  t        j8                  t        j:                  fv rt=        |||      \  }}n|t        j>                  k(  r|t        j                  t        j.                  fv r|j'                  |       tA        |      }nr|t        j2                  k(  rt5        |||||      \  }}}}nJ|t        j6                  t        j8                  t        j:                  fv rtC        |||      }ntA        |      }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
        a hello call

    Called after attempting (successfully or not) to call hello 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 '')errorr8   )"r?   r/   r1   r2   r3   server_typer0   r(   r   r   rI   r   r&   r   r
   
StandaloneLoadBalancer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?   r/   set_namer2   r3   r   rP   r   s
             r   rJ   rJ   R  s    !((G )66M#44H*::O*::O$00K 
2
2
4C &CL,,,H0B0S0S$S& /@@BE
 .88u8ECL"   33
 	
 ---;11;3K3KLL'::@@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1JaX1?OKG]Ho [44k6K6K[M`M`aa&GX1'#M8 
-==	=;11;3E3EFFGGG.s3MK111JaX1?OKG]Ho [44k6K6K[M`M`aa?XOabM /s3M // r6   c                    | j                   t        v sJ | 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
        a hello call
    )r/   SRV_POLLING_TOPOLOGIESr0   setkeysr   listr   r   r1   r2   r3   r&   )r   seedlistrP   r?   s       r   )_updated_topology_description_srv_pollingr     s      --1GGGG

2
2
4C 388:#h-'##  6#,W5CL6
 
# ("GGG **--,,,,// r6   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 )ag  Update topology description from a primary's hello 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)r1   r   r?   r   election_tuplerI   election_idset_versionr9   r   r
   r   	all_hostsr   r   )	rP   r1   r   r2   r3   max_election_tupleservernew_addressaddrs	            r   r   r     s    ->>	/@@	@ 	"**+"3')9?O\\(/9%444**"%7%F%FF /A.K.K.MC"**+&s+-=P_``,88%%1#5#A#AO#S -88 **, 
+"7"77"4"<"<< #)"3"3"5C 
 *33 >c!0=C>
 C-777 
 s#%5XXr6   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.
    )r1   r   r?   mer   )rP   r1   r   s      r   r   r     s     '''->>>"**+
 c""	 
		#5#=#=ASAVAV#V"**+ c""r6   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).
    )r(   r   r1   r   r?   r   r   r   )rP   r1   r   r/   r?   s        r   r   r   2  s     "55M->>	/@@	@"**+... &// 6#,W5CL6 !3!;!;?Q?T?T!T"**+***r6   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.
    )r9   r   r
   r   r(   r   r   )rP   r   s     r   r   r   N  sE     ZZ\ 1==K111 6661 000r6   N)$__doc__collectionsr   pymongor   pymongo.errorsr   pymongo.read_preferencesr   pymongo.server_descriptionr   pymongo.server_selectorsr	   pymongo.server_typer
   ranger(   r   r   r   objectr   r   r   r   r   r   r   r   r   rJ   r   r   r   r   r   r   r6   r   <module>r      s    1 "  - 3 8 . +


 	8
 (//1F1FG X
& X
| --=>>]>>=<<::! cL!H@YF#&+81r6   