
    i)i:W                       d Z ddlmZ ddlZddlmZ ddlmZmZm	Z	m
Z
mZ ddlmZ ddlmZ ddlmZmZ erdd	lmZ dd
lmZ dZdZdZdZdZdZe	eef         Zee	eef                  Zd5dZd6dZ d7dZ!d8dZ" G d d          Z# G d d e#          Z$ G d! d"e#          Z% G d# d$e#          Z& G d% d&e#          Z' G d' d(e#          Z( G d) d*          Z)e$e%e&e'e(fZ*	 d9d:d-Z+d.Z, G d/ d0          Z-d;d2Z. G d3 d4          Z/dS )<zUtilities for choosing which member of a replica set to read from.

.. seealso:: This module is compatible with both the synchronous and asynchronous PyMongo APIs.
    )annotationsN)abc)TYPE_CHECKINGAnyMappingOptionalSequence)max_staleness_selectors)ConfigurationError) member_with_tags_server_selector#secondary_with_tags_server_selector)	Selection)TopologyDescription            )primaryprimaryPreferred	secondarysecondaryPreferrednearesttag_setsOptional[_TagSets]returnc                6   | | S t          | t          t          f          st          d| d          t	          |           dk    rt          d| d          | D ]/}t          |t          j                  st          d|d          0t          |           S )z$Validate tag sets for a MongoClient.Nz	Tag sets z invalid, must be a sequencer   z: invalid, must be None or contain at least one set of tagszTag set zg invalid, must be an instance of dict, bson.son.SON or other type that inherits from collection.Mapping)
isinstancelisttuple	TypeErrorlen
ValueErrorr   r   )r   tagss     j/Users/user/workspace/sujinbaek/cqa-test-app/venv/lib/python3.11/site-packages/pymongo/read_preferences.py_validate_tag_setsr%   9   s    hu.. NLHLLLMMM
8}}^^^^
 
 	
   $,, 	%4 % % %  	 >>    max_stalenessr   strc                    d| z  S )Nz6maxStalenessSeconds must be a positive integer, not %s r'   s    r$   _invalid_max_staleness_msgr,   P   s    CmSSr&   intc                    | dk    rdS t          | t                    st          t          |                     | dk    rt	          t          |                     | S )zValidate max_staleness.r   )r   r-   r    r,   r"   r+   s    r$   _validate_max_stalenessr0   U   sd    rmS)) C2=AABBB3MBBCCCr&   hedgeOptional[_Hedge]c                    | dS t          | t                    st          d|           t          j        dt
          d           | S )zValidate hedge.Nz hedge must be a dictionary, not The read preference 'hedge' option is deprecated in PyMongo 4.12+ because hedged reads are deprecated in MongoDB version 8.0+. Support for 'hedge' will be removed in PyMongo 5.0.r   
stacklevel)r   dictr    warningswarnDeprecationWarning)r1   s    r$   _validate_hedger;   c   sb    }teT"" FD5DDEEEM 	}   
 Lr&   c                     e Zd ZdZdZ	 	 	 d'd(dZed)d            Zed)d            Zed*d            Z	ed+d            Z
ed,d            Zed+d            Zed-d            Zed+d            Zd)dZd.dZd.dZd*d Zd/d#Zd0d&ZdS )1_ServerModez$Base class for all read preferences.)__mongos_mode__mode
__tag_sets__max_staleness__hedgeNr/   moder-   r   r   r'   r1   r2   r   Nonec                    t           |         | _        || _        t          |          | _        t          |          | _        t          |          | _        d S N)	_MONGOS_MODES_ServerMode__mongos_mode_ServerMode__moder%   _ServerMode__tag_setsr0   _ServerMode__max_stalenessr;   _ServerMode__hedge)selfrC   r   r'   r1   s        r$   __init__z_ServerMode.__init__x   sJ     +40,X666}EE&u--r&   r(   c                    | j         j        S )z!The name of this read preference.)	__class____name__rM   s    r$   namez_ServerMode.name   s     ~&&r&   c                    | j         S )z(The mongos mode of this read preference.)rH   rR   s    r$   mongos_modez_ServerMode.mongos_mode   s     !!r&   dict[str, Any]c                    d| j         i}| j        di gfvr
| j        |d<   | j        dk    r
| j        |d<   | j        di fvr
| j        |d<   |S )zRead preference as a document.rC   Nr#   r/   maxStalenessSecondsr1   )rH   rJ   rK   rL   )rM   docs     r$   documentz_ServerMode.document   sl      &t'9:?4",../CK2%%)-)=C%&<bz))<CL
r&   c                    | j         S )z*The mode of this read preference instance.)rI   rR   s    r$   rC   z_ServerMode.mode   s     {r&   _TagSetsc                >    | j         rt          | j                   ni gS )aW  Set ``tag_sets`` to a list of dictionaries like [{'dc': 'ny'}] to
        read only from members whose ``dc`` tag has the value ``"ny"``.
        To specify a priority-order for tag sets, provide a list of
        tag sets: ``[{'dc': 'ny'}, {'dc': 'la'}, {}]``. A final, empty tag
        set, ``{}``, means "read from any member that matches the mode,
        ignoring tags." MongoClient tries each set of tags in turn
        until it finds a set of tags with at least one matching member.
        For example, to only send a query to an analytic node::

           Nearest(tag_sets=[{"node":"analytics"}])

        Or using :class:`SecondaryPreferred`::

           SecondaryPreferred(tag_sets=[{"node":"analytics"}])

           .. seealso:: `Data-Center Awareness
               <https://www.mongodb.com/docs/manual/data-center-awareness/>`_
        )rJ   r   rR   s    r$   r   z_ServerMode.tag_sets   s"    ( )-AtDO$$$bTAr&   c                    | j         S )zThe maximum estimated length of time (in seconds) a replica set
        secondary can fall behind the primary in replication before it will
        no longer be selected for operations, or -1 for no maximum.
        rK   rR   s    r$   r'   z_ServerMode.max_staleness   s     ##r&   c                V    | j         t          j        dt          d           | j         S )am  **DEPRECATED** - The read preference 'hedge' option is deprecated in PyMongo 4.12+ because hedged reads are deprecated in MongoDB version 8.0+. Support for 'hedge' will be removed in PyMongo 5.0.

        The read preference ``hedge`` parameter.

        A dictionary that configures how the server will perform hedged reads.
        It consists of the following keys:

        - ``enabled``: Enables or disables hedged reads in sharded clusters.

        Hedged reads are automatically enabled in MongoDB 4.4+ when using a
        ``nearest`` read preference. To explicitly enable hedged reads, set
        the ``enabled`` key  to ``true``::

            >>> Nearest(hedge={'enabled': True})

        To explicitly disable hedged reads, set the ``enabled`` key  to
        ``False``::

            >>> Nearest(hedge={'enabled': False})

        .. versionadded:: 3.11
        Nr4   r   r5   )rL   r8   r9   r:   rR   s    r$   r1   z_ServerMode.hedge   s;    0 <#M E"   
 |r&   c                     | j         dk    rdndS )a  The wire protocol version the server must support.

        Some read preferences impose version requirements on all servers (e.g.
        maxStalenessSeconds requires MongoDB 3.4 / maxWireVersion 5).

        All servers' maxWireVersion must be at least this read preference's
        `min_wire_version`, or the driver raises
        :exc:`~pymongo.errors.ConfigurationError`.
        r/   r      r_   rR   s    r$   min_wire_versionz_ServerMode.min_wire_version   s     (B..qqA5r&   c                Z    d                     | j        | j        | j        | j                  S )Nz1{}(tag_sets={!r}, max_staleness={!r}, hedge={!r}))formatrS   rJ   rK   rL   rR   s    r$   __repr__z_ServerMode.__repr__   s/    BIIIO L	
 
 	
r&   otherr   boolc                    t          |t                    r@| j        |j        k    o/| j        |j        k    o| j        |j        k    o| j        |j        k    S t          S rF   )r   r=   rC   r   r'   r1   NotImplementedrM   rg   s     r$   __eq__z_ServerMode.__eq__   sd    e[)) 		UZ' .MU^3.&%*==. J%+-	 r&   c                    | |k     S rF   r*   rk   s     r$   __ne__z_ServerMode.__ne__   s    5=  r&   c                8    | j         | j        | j        | j        dS )zeReturn value of object for pickling.

        Needed explicitly because __slots__() defined.
        )rC   r   r'   r1   )rI   rJ   rK   rL   rR   s    r$   __getstate__z_ServerMode.__getstate__   s(     K!1\	
 
 	
r&   valueMapping[str, Any]c                    |d         | _         t          | j                  | _        t          |d                   | _        t          |d                   | _        t          |d                   | _        dS )zRestore from pickling.rC   r   r'   r1   N)	rI   rG   rH   r%   rJ   r0   rK   r;   rL   )rM   rq   s     r$   __setstate__z_ServerMode.__setstate__  s\    Fm*4;7,U:->??6u_7MNN&uW~66r&   	selectionr   c                    |S rF   r*   rM   ru   s     r$   __call__z_ServerMode.__call__  s    r&   Nr/   N)
rC   r-   r   r   r'   r-   r1   r2   r   rD   r   r(   )r   rV   )r   r-   )r   r\   )r   r2   rg   r   r   rh   )rq   rr   r   rD   ru   r   r   r   )rQ   
__module____qualname____doc__	__slots__rN   propertyrS   rU   rZ   rC   r   r'   r1   rc   rf   rl   rn   rp   rt   rx   r*   r&   r$   r=   r=   s   s       ..WI
 (,"&. . . . . ' ' ' X' " " " X" 	 	 	 X	    X B B B XB* $ $ $ X$    X> 
6 
6 
6 X
6
 
 
 
   ! ! ! !

 

 

 

7 7 7 7     r&   r=   c                  @     e Zd ZdZdZd fdZddZdd
ZddZ xZ	S )PrimaryaG  Primary read preference.

    * When directly connected to one mongod queries are allowed if the server
      is standalone or a replica set primary.
    * When connected to a mongos queries are sent to the primary of a shard.
    * When connected to a replica set queries are sent to the primary of
      the replica set.
    r*   r   rD   c                T    t                                          t                     d S rF   )superrN   _PRIMARY)rM   rP   s    r$   rN   zPrimary.__init__$  s!    """""r&   ru   r   c                    |j         S z*Apply this read preference to a Selection.)primary_selectionrw   s     r$   rx   zPrimary.__call__'  s    **r&   r(   c                    dS )Nz	Primary()r*   rR   s    r$   rf   zPrimary.__repr__+  s    {r&   rg   r   rh   c                Z    t          |t                    r|j        t          k    S t          S rF   )r   r=   rC   r   rj   rk   s     r$   rl   zPrimary.__eq__.  s'    e[)) 	*:))r&   r   rD   r|   rz   r{   )
rQ   r}   r~   r   r   rN   rx   rf   rl   __classcell__rP   s   @r$   r   r     s          I# # # # # #+ + + +          r&   r   c                  8     e Zd ZdZdZ	 	 	 dd fdZddZ xZS )PrimaryPreferreda  PrimaryPreferred read preference.

    * When directly connected to one mongod queries are allowed to standalone
      servers, to a replica set primary, or to replica set secondaries.
    * When connected to a mongos queries are sent to the primary of a shard if
      available, otherwise a shard secondary.
    * When connected to a replica set queries are sent to the primary if
      available, otherwise a secondary.

    .. note:: When a :class:`~pymongo.mongo_client.MongoClient` is first
      created reads will be routed to an available secondary until the
      primary of the replica set is discovered.

    :param tag_sets: The :attr:`~tag_sets` to use if the primary is not
        available.
    :param max_staleness: (integer, in seconds) The maximum estimated
        length of time a replica set secondary can fall behind the primary in
        replication before it will no longer be selected for operations.
        Default -1, meaning no maximum. If it is set, it must be at least
        90 seconds.
    :param hedge: **DEPRECATED** - The :attr:`~hedge` for this read preference.

    .. versionchanged:: 3.11
       Added ``hedge`` parameter.
    r*   Nr/   r   r   r'   r-   r1   r2   r   rD   c                Z    t                                          t          |||           d S rF   )r   rN   _PRIMARY_PREFERREDrM   r   r'   r1   rP   s       r$   rN   zPrimaryPreferred.__init__Q  s*     	+X}eLLLLLr&   ru   r   c                x    |j         r|j        S t          | j        t	          j        | j        |                    S z(Apply this read preference to Selection.)r   r   r   r   r
   selectr'   rw   s     r$   rx   zPrimaryPreferred.__call__Y  sA     	..66=d>PR[\\  r&   ry   r   r   r'   r-   r1   r2   r   rD   r|   rQ   r}   r~   r   r   rN   rx   r   r   s   @r$   r   r   4  sy         4 I (,"&	M M M M M M M       r&   r   c                  8     e Zd ZdZdZ	 	 	 dd fdZddZ xZS )	Secondarya  Secondary read preference.

    * When directly connected to one mongod queries are allowed to standalone
      servers, to a replica set primary, or to replica set secondaries.
    * When connected to a mongos queries are distributed among shard
      secondaries. An error is raised if no secondaries are available.
    * When connected to a replica set queries are distributed among
      secondaries. An error is raised if no secondaries are available.

    :param tag_sets: The :attr:`~tag_sets` for this read preference.
    :param max_staleness: (integer, in seconds) The maximum estimated
        length of time a replica set secondary can fall behind the primary in
        replication before it will no longer be selected for operations.
        Default -1, meaning no maximum. If it is set, it must be at least
        90 seconds.
    :param hedge: **DEPRECATED** - The :attr:`~hedge` for this read preference.

    .. versionchanged:: 3.11
       Added ``hedge`` parameter.
    r*   Nr/   r   r   r'   r-   r1   r2   r   rD   c                Z    t                                          t          |||           d S rF   )r   rN   
_SECONDARYr   s       r$   rN   zSecondary.__init__{  s)     	X}eDDDDDr&   ru   r   c                \    t          | j        t          j        | j        |                    S r   )r   r   r
   r   r'   rw   s     r$   rx   zSecondary.__call__  s,    2M29$:LiXX
 
 	
r&   ry   r   r|   r   r   s   @r$   r   r   c  sy         * I (,"&	E E E E E E E
 
 
 
 
 
 
 
r&   r   c                  8     e Zd ZdZdZ	 	 	 dd fdZddZ xZS )SecondaryPreferreda  SecondaryPreferred read preference.

    * When directly connected to one mongod queries are allowed to standalone
      servers, to a replica set primary, or to replica set secondaries.
    * When connected to a mongos queries are distributed among shard
      secondaries, or the shard primary if no secondary is available.
    * When connected to a replica set queries are distributed among
      secondaries, or the primary if no secondary is available.

    .. note:: When a :class:`~pymongo.mongo_client.MongoClient` is first
      created reads will be routed to the primary of the replica set until
      an available secondary is discovered.

    :param tag_sets: The :attr:`~tag_sets` for this read preference.
    :param max_staleness: (integer, in seconds) The maximum estimated
        length of time a replica set secondary can fall behind the primary in
        replication before it will no longer be selected for operations.
        Default -1, meaning no maximum. If it is set, it must be at least
        90 seconds.
    :param hedge: **DEPRECATED** - The :attr:`~hedge` for this read preference.

    .. versionchanged:: 3.11
       Added ``hedge`` parameter.
    r*   Nr/   r   r   r'   r-   r1   r2   r   rD   c                Z    t                                          t          |||           d S rF   )r   rN   _SECONDARY_PREFERREDr   s       r$   rN   zSecondaryPreferred.__init__  s*     	-xNNNNNr&   ru   r   c                r    t          | j        t          j        | j        |                    }|r|S |j        S r   )r   r   r
   r   r'   r   )rM   ru   secondariess      r$   rx   zSecondaryPreferred.__call__  sB    9M29$:LiXX
 
  	/..r&   ry   r   r|   r   r   s   @r$   r   r     sy         2 I (,"&	O O O O O O O	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/r&   r   c                  8     e Zd ZdZdZ	 	 	 dd fdZddZ xZS )Nearestay  Nearest read preference.

    * When directly connected to one mongod queries are allowed to standalone
      servers, to a replica set primary, or to replica set secondaries.
    * When connected to a mongos queries are distributed among all members of
      a shard.
    * When connected to a replica set queries are distributed among all
      members.

    :param tag_sets: The :attr:`~tag_sets` for this read preference.
    :param max_staleness: (integer, in seconds) The maximum estimated
        length of time a replica set secondary can fall behind the primary in
        replication before it will no longer be selected for operations.
        Default -1, meaning no maximum. If it is set, it must be at least
        90 seconds.
    :param hedge: **DEPRECATED** - The :attr:`~hedge` for this read preference.

    .. versionchanged:: 3.11
       Added ``hedge`` parameter.
    r*   Nr/   r   r   r'   r-   r1   r2   r   rD   c                Z    t                                          t          |||           d S rF   )r   rN   _NEARESTr   s       r$   rN   zNearest.__init__  s)     	8]EBBBBBr&   ru   r   c                \    t          | j        t          j        | j        |                    S r   )r   r   r
   r   r'   rw   s     r$   rx   zNearest.__call__  s,    /M29$:LiXX
 
 	
r&   ry   r   r|   r   r   s   @r$   r   r     sy         * I (,"&	C C C C C C C
 
 
 
 
 
 
 
r&   r   c                  >    e Zd ZdZdZddZdd
ZddZddZddZ	dS )_AggWritePrefzAgg $out/$merge write preference.

    * If there are readable servers and there is any pre-5.0 server, use
      primary read preference.
    * Otherwise use `pref` read preference.

    :param pref: The read preference to use on MongoDB 5.0+.
    )prefeffective_prefr   r=   c                6    || _         t          j        | _        d S rF   )r   ReadPreferencePRIMARYr   )rM   r   s     r$   rN   z_AggWritePref.__init__  s    	+9+Ar&   topology_descriptionr   r   rD   c                    |j         }|                    t          j                  r|r|dk     rt          j        | _        d S | j        | _        d S )N   )common_wire_versionhas_readable_serverr   PRIMARY_PREFERREDr   r   r   )rM   r   	common_wvs      r$   selection_hookz_AggWritePref.selection_hook  sW    (<	 44^5UVV	,	, B"0"8D"&)Dr&   ru   r   c                ,    |                      |          S r   )r   rw   s     r$   rx   z_AggWritePref.__call__  s    ""9---r&   r(   c                    d| j         dS )Nz_AggWritePref(pref=))r   rR   s    r$   rf   z_AggWritePref.__repr__   s    3TY3333r&   rS   r   c                ,    t          | j        |          S rF   )getattrr   )rM   rS   s     r$   __getattr__z_AggWritePref.__getattr__  s    t*D111r&   N)r   r=   )r   r   r   rD   r|   rz   )rS   r(   r   r   )
rQ   r}   r~   r   r   rN   r   rx   rf   r   r*   r&   r$   r   r     s          +IB B B B	, 	, 	, 	,. . . .4 4 4 4
2 2 2 2 2 2r&   r   r/   rC   c                    | t           k    r9|d i gfvrt          d          |dk    rt          d          t                      S t          |          ||          S )Nz4Read preference primary cannot be combined with tagsr/   zCRead preference primary cannot be combined with maxStalenessSeconds)r   r   r   _ALL_READ_PREFERENCES)rC   r   r'   s      r$   make_read_preferencer     sp     xD2$<''$%[\\\B$U   yy &x???r&   )r   r   	SECONDARYSECONDARY_PREFERREDNEARESTc                  v    e Zd ZdZ e            Z e            Z e            Z	 e
            Z e            ZdS )r   a  An enum that defines some commonly used read preference modes.

    Apps can also create a custom read preference, for example::

       Nearest(tag_sets=[{"node":"analytics"}])

    See `Read and Write Settings <https://www.mongodb.com/docs/languages/python/pymongo-driver/current/crud/configure/#read-and-write-settings>`_ for code examples.

    A read preference is used in three cases:

    :class:`~pymongo.mongo_client.MongoClient` connected to a single mongod:

    - ``PRIMARY``: Queries are allowed if the server is standalone or a replica
      set primary.
    - All other modes allow queries to standalone servers, to a replica set
      primary, or to replica set secondaries.

    :class:`~pymongo.mongo_client.MongoClient` initialized with the
    ``replicaSet`` option:

    - ``PRIMARY``: Read from the primary. This is the default, and provides the
      strongest consistency. If no primary is available, raise
      :class:`~pymongo.errors.AutoReconnect`.

    - ``PRIMARY_PREFERRED``: Read from the primary if available, or if there is
      none, read from a secondary.

    - ``SECONDARY``: Read from a secondary. If no secondary is available,
      raise :class:`~pymongo.errors.AutoReconnect`.

    - ``SECONDARY_PREFERRED``: Read from a secondary if available, otherwise
      from the primary.

    - ``NEAREST``: Read from any member.

    :class:`~pymongo.mongo_client.MongoClient` connected to a mongos, with a
    sharded cluster of replica sets:

    - ``PRIMARY``: Read from the primary of the shard, or raise
      :class:`~pymongo.errors.OperationFailure` if there is none.
      This is the default.

    - ``PRIMARY_PREFERRED``: Read from the primary of the shard, or if there is
      none, read from a secondary of the shard.

    - ``SECONDARY``: Read from a secondary of the shard, or raise
      :class:`~pymongo.errors.OperationFailure` if there is none.

    - ``SECONDARY_PREFERRED``: Read from a secondary of the shard if available,
      otherwise from the shard primary.

    - ``NEAREST``: Read from any shard member.
    N)rQ   r}   r~   r   r   r   r   r   r   r   r   r   r   r   r*   r&   r$   r   r   #  sZ        4 4l giiG((**	I,,..giiGGGr&   r   rS   c                6    t                               |           S )z2Get the read preference mode from mongos/uri name.)rG   index)rS   s    r$   read_pref_mode_from_namer   a  s    t$$$r&   c                  >    e Zd ZU dZded<   ddZdd	Zdd
ZddZdS )MovingAveragez0Tracks an exponentially-weighted moving average.Optional[float]averager   rD   c                    d | _         d S rF   r   rR   s    r$   rN   zMovingAverage.__init__k      r&   samplefloatc                    |dk     rt          d|           | j        	|| _        d S d| j        z  d|z  z   | _        d S )Nr   zduration cannot be negative g?g?)r"   r   )rM   r   s     r$   
add_samplezMovingAverage.add_samplen  sR    A::DFDDEEE<!DLLL -f<DLLLr&   c                    | j         S )z6Get the calculated average, or None if no samples yet.r   rR   s    r$   getzMovingAverage.getx  s
    |r&   c                    d | _         d S rF   r   rR   s    r$   resetzMovingAverage.reset|  r   r&   Nr   )r   r   r   rD   )r   r   )	rQ   r}   r~   r   __annotations__rN   r   r   r   r*   r&   r$   r   r   f  sx         ::   = = = =        r&   r   )r   r   r   r   )r'   r   r   r(   )r'   r   r   r-   )r1   r2   r   r2   )r/   )rC   r-   r   r   r'   r-   r   r=   )rS   r(   r   r-   )0r   
__future__r   r8   collectionsr   typingr   r   r   r   r	   pymongor
   pymongo.errorsr   pymongo.server_selectorsr   r   r   pymongo.topology_descriptionr   r   r   r   r   r   rG   r(   _Hedger\   r%   r,   r0   r;   r=   r   r   r   r   r   r   r   r   _MODESr   r   r   r*   r&   r$   <module>r      s_   
 # " " " " "        B B B B B B B B B B B B B B + + + + + + - - - - - -       
  A222222@@@@@@  
  
c	GCH%&   .T T T T
       b b b b b b b bJ    k   8, , , , ,{ , , ,^$
 $
 $
 $
 $
 $
 $
 $
N-/ -/ -/ -/ -/ -/ -/ -/`$
 $
 $
 $
 $
k $
 $
 $
N%2 %2 %2 %2 %2 %2 %2 %2P !"2I?QSZ[  CE@ @ @ @ @
; ; ; ; ; ; ; ;|% % % %
         r&   