o
    ?8¾\n@  ã                   @   s  d Z ddlmZmZ ddlmZ ddlmZ ddlm	Z	m
Z
 dZdZdZdZd	Zd
Zdd„ Zdd„ Zdd„ ZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZeeeeefZd(dd„Zd ZG d!d"„ d"eƒZd#d$„ ZG d%d&„ d&eƒZ d'S ))zBUtilities for choosing which member of a replica set to read from.é    )ÚabcÚinteger_types)Úmax_staleness_selectors)ÚConfigurationError)Ú member_with_tags_server_selectorÚ#secondary_with_tags_server_selectoré   é   é   é   )ÚprimaryÚprimaryPreferredÚ	secondaryÚsecondaryPreferredÚnearestc                 C   sf   | du r| S t | tƒstd| f ƒ‚t| ƒdkrtd| f ƒ‚| D ]}t |tjƒs0td|f ƒ‚q!| S )z3Validate tag sets for a MongoReplicaSetClient.
    Nz#Tag sets %r invalid, must be a listr   zETag sets %r invalid, must be None or contain at least one set of tagszqTag set %r invalid, must be an instance of dict, bson.son.SON or other type that inherits from collection.Mapping)Ú
isinstanceÚlistÚ	TypeErrorÚlenÚ
ValueErrorr   ÚMapping)Útag_setsÚtags© r   úJ/var/www/html/env/lib/python3.10/site-packages/pymongo/read_preferences.pyÚ_validate_tag_sets(   s(   
ÿþþÿÿr   c                 C   s   d|  S )Nz6maxStalenessSeconds must be a positive integer, not %sr   ©Úmax_stalenessr   r   r   Ú_invalid_max_staleness_msg@   s   ÿr   c                 C   s:   | 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   r   r   r   Ú_validate_max_stalenessF   s   
r    c                   @   sš   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dd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS ) Ú_ServerModez)Base class for all read preferences.
    )Ú__mongos_modeÚ__modeÚ
__tag_setsÚ__max_stalenessNr   c                 C   s(   t | | _|| _t|ƒ| _t|ƒ| _d S ©N)Ú_MONGOS_MODESÚ_ServerMode__mongos_modeÚ_ServerMode__moder   Ú_ServerMode__tag_setsr    Ú_ServerMode__max_staleness)ÚselfÚmoder   r   r   r   r   Ú__init__Z   s   

z_ServerMode.__init__c                 C   s   | j jS )z*The name of this read preference.
        )Ú	__class__Ú__name__©r,   r   r   r   Úname`   s   z_ServerMode.namec                 C   ó   | j S )z1The mongos mode of this read preference.
        )r(   r1   r   r   r   Úmongos_modef   ó   z_ServerMode.mongos_modec                 C   s<   d| j i}| jdi gfvr| j|d< | jdkr| j|d< |S )z'Read preference as a document.
        r-   Nr   r   ÚmaxStalenessSeconds)r(   r*   r+   )r,   Údocr   r   r   Údocumentl   s   



z_ServerMode.documentc                 C   r3   )z3The mode of this read preference instance.
        )r)   r1   r   r   r   r-   w   r5   z_ServerMode.modec                 C   s   | j rt| j ƒS i gS )a{  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." MongoReplicaSetClient tries each set of tags in turn
        until it finds a set of tags with at least one matching member.

           .. seealso:: `Data-Center Awareness
               <http://www.mongodb.org/display/DOCS/Data+Center+Awareness>`_
        )r*   r   r1   r   r   r   r   }   s   z_ServerMode.tag_setsc                 C   r3   )zÏThe 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.©r+   r1   r   r   r   r   Œ   s   z_ServerMode.max_stalenessc                 C   s   | j dkrdS 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   é   r9   r1   r   r   r   Úmin_wire_version“   s   z_ServerMode.min_wire_versionc                 C   s   d| j | j| jf S )Nz!%s(tag_sets=%r, max_staleness=%r))r2   r*   r+   r1   r   r   r   Ú__repr__    s   ÿz_ServerMode.__repr__c                 C   s2   t |tƒr| j|jko| j|jko| j|jkS tS r&   )r   r!   r-   r   r   ÚNotImplemented©r,   Úotherr   r   r   Ú__eq__¤   s   

ÿ
þz_ServerMode.__eq__c                 C   s
   | |k S r&   r   r>   r   r   r   Ú__ne__«   ó   
z_ServerMode.__ne__c                 C   s   | j | j| jdœS )zeReturn value of object for pickling.

        Needed explicitly because __slots__() defined.
        ©r-   r   r   )r)   r*   r+   r1   r   r   r   Ú__getstate__®   s   þz_ServerMode.__getstate__c                 C   s6   |d | _ t| j  | _t|d ƒ| _t|d ƒ| _dS )zRestore from pickling.r-   r   r   N)r)   r'   r(   r   r*   r    r+   )r,   Úvaluer   r   r   Ú__setstate__·   s   
z_ServerMode.__setstate__©Nr   )r0   Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__r.   Úpropertyr2   r4   r8   r-   r   r   r;   r<   r@   rA   rD   rF   r   r   r   r   r!   T   s.    








	r!   c                       s<   e Zd ZdZdZ‡ fdd„Zdd„ Zdd„ Zd	d
„ Z‡  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   c                    s   t t| ƒ t¡ d S r&   )ÚsuperrM   r.   Ú_PRIMARYr1   ©r/   r   r   r.   Ë   s   zPrimary.__init__c                 C   s   |j S )z*Apply this read preference to a Selection.)Úprimary_selection©r,   Ú	selectionr   r   r   Ú__call__Î   ó   zPrimary.__call__c                 C   s   dS )Nz	Primary()r   r1   r   r   r   r<   Ò   s   zPrimary.__repr__c                 C   s   t |tƒr
|jtkS tS r&   )r   r!   r-   rO   r=   r>   r   r   r   r@   Õ   s   

zPrimary.__eq__)
r0   rH   rI   rJ   rK   r.   rT   r<   r@   Ú__classcell__r   r   rP   r   rM   ¿   s    	rM   c                       ó.   e Zd ZdZdZd	‡ fdd„	Zdd„ Z‡  ZS )
ÚPrimaryPreferredaQ  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.

    :Parameters:
      - `tag_sets`: The :attr:`~tag_sets` to use if the primary is not
        available.
      - `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.
    r   Nr   c                    ó   t t| ƒ t||¡ d S r&   )rN   rX   r.   Ú_PRIMARY_PREFERRED©r,   r   r   rP   r   r   r.   ñ   ó   þzPrimaryPreferred.__init__c                 C   s"   |j r|jS t| jt | j|¡ƒS ©z(Apply this read preference to Selection.)r   rQ   r   r   r   Úselectr   rR   r   r   r   rT   ö   s   ÿþzPrimaryPreferred.__call__rG   ©r0   rH   rI   rJ   rK   r.   rT   rV   r   r   rP   r   rX   Û   s
    rX   c                       rW   )
Ú	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.

    :Parameters:
      - `tag_sets`: The :attr:`~tag_sets` for this read preference.
      - `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.
    r   Nr   c                    rY   r&   )rN   r`   r.   Ú
_SECONDARYr[   rP   r   r   r.     ó   zSecondary.__init__c                 C   ó   t | jt | j|¡ƒS r]   )r   r   r   r^   r   rR   r   r   r   rT     ó   ÿþzSecondary.__call__rG   r_   r   r   rP   r   r`     ó
    r`   c                       rW   )
Ú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.

    :Parameters:
      - `tag_sets`: The :attr:`~tag_sets` for this read preference.
      - `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.
    r   Nr   c                    rY   r&   )rN   rf   r.   Ú_SECONDARY_PREFERREDr[   rP   r   r   r.   6  r\   zSecondaryPreferred.__init__c                 C   s$   t | jt | j|¡ƒ}|r|S |jS r]   )r   r   r   r^   r   rQ   )r,   rS   Úsecondariesr   r   r   rT   ;  s   ÿþzSecondaryPreferred.__call__rG   r_   r   r   rP   r   rf   !  s
    rf   c                       rW   )
ÚNearestaø  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.

    :Parameters:
      - `tag_sets`: The :attr:`~tag_sets` for this read preference.
      - `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.
    r   Nr   c                    rY   r&   )rN   ri   r.   Ú_NEARESTr[   rP   r   r   r.   ]  rb   zNearest.__init__c                 C   rc   r]   )r   r   r   r^   r   rR   r   r   r   rT   `  rd   zNearest.__call__rG   r_   r   r   rP   r   ri   H  re   ri   r   c                 C   sB   | t kr|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)rO   r   rM   Ú_ALL_READ_PREFERENCESrC   r   r   r   Úmake_read_preferencel  s   rl   )ÚPRIMARYÚPRIMARY_PREFERREDÚ	SECONDARYÚSECONDARY_PREFERREDÚNEARESTc                   @   s.   e Zd ZdZeƒ Zeƒ Zeƒ Z	e
ƒ Zeƒ ZdS )ÚReadPreferenceaD  An enum that defines the read preference modes supported by PyMongo.

    See :doc:`/examples/high_availability` 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)r0   rH   rI   rJ   rM   rm   rX   rn   r`   ro   rf   rp   ri   rq   r   r   r   r   rr   �  s    1
rr   c                 C   s
   t  | ¡S )z7Get the read preference mode from mongos/uri name.
    )r'   Úindex)r2   r   r   r   Úread_pref_mode_from_nameº  s   
rt   c                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚMovingAveragez0Tracks an exponentially-weighted moving average.c                 C   ó
   d | _ d S r&   ©Úaverager1   r   r   r   r.   Â  rB   zMovingAverage.__init__c                 C   s8   |dk rd S | j d u r|| _ d S d| j  d|  | _ d S )Nr   gš™™™™™é?gš™™™™™É?rw   )r,   Úsampler   r   r   Ú
add_sampleÅ  s
   

zMovingAverage.add_samplec                 C   r3   )z6Get the calculated average, or None if no samples yet.rw   r1   r   r   r   ÚgetÒ  rU   zMovingAverage.getc                 C   rv   r&   rw   r1   r   r   r   ÚresetÖ  rB   zMovingAverage.resetN)r0   rH   rI   rJ   r.   rz   r{   r|   r   r   r   r   ru   À  s    ru   N)r   )!rJ   Úbson.py3compatr   r   Úpymongor   Úpymongo.errorsr   Úpymongo.server_selectorsr   r   rO   rZ   ra   rg   rj   r'   r   r   r    Úobjectr!   rM   rX   r`   rf   ri   rk   rl   Ú_MODESrr   rt   ru   r   r   r   r   Ú<module>   s8   	k& ' ÿ
	9