
    _i                    r    d Z ddlmZ ddlmZ ddlmZ ddlmZ erddl	m
Z
 dZdZddZddZddZddZdS )a  Criteria to select ServerDescriptions based on maxStalenessSeconds.

The Max Staleness Spec says: When there is a known primary P,
a secondary S's staleness is estimated with this formula:

  (S.lastUpdateTime - S.lastWriteDate) - (P.lastUpdateTime - P.lastWriteDate)
  + heartbeatFrequencyMS

When there is no known primary, a secondary S's staleness is estimated with:

  SMax.lastWriteDate - S.lastWriteDate + heartbeatFrequencyMS

where "SMax" is the secondary with the greatest lastWriteDate.
    )annotations)TYPE_CHECKING)ConfigurationError)SERVER_TYPE)	Selection
   Z   max_stalenessintheartbeat_frequencyreturnNonec                    | |t           z   k     rt          dt           | |dz  fz            | t          k     rt          dt          | fz            d S )NzmaxStalenessSeconds must be at least heartbeatFrequencyMS + %d seconds. maxStalenessSeconds is set to %d, heartbeatFrequencyMS is set to %d.i  zJmaxStalenessSeconds must be at least %d. maxStalenessSeconds is set to %d.)IDLE_WRITE_PERIODr   SMALLEST_MAX_STALENESS)r
   r   s     BD:\Nbitz\venv\Lib\site-packages\pymongo/max_staleness_selectors.py_validate_max_stalenessr   .   s~    *->>>> 2 !-1Dt1KLM
 
 	
 --- 03I=2YZ
 
 	
 .-    	selectionr   c                T   |j         }|sJ g }|j        D ]}|j        t          j        k    rS|j        r|j        sJ |j        |j        z
  |j        |j        z
  z
  |j        z   }|| k    r|                    |           j|                    |           |	                    |          S )zEApply max_staleness, in seconds, to a Selection with a known primary.)
primaryserver_descriptionsserver_typer   RSSecondarylast_write_datelast_update_timer   appendwith_server_descriptions)r
   r   r   sdss	stalenesss         r   _with_primaryr"   ?   s    GNN7
C*  =K333$@)@@@@#a&77+g.EEG/0  M))

1JJqMMMM--c222r   c                t   |                                 }|s|                    g           S g }|j        D ]o}|j        t          j        k    rC|j        r|j        sJ |j        |j        z
  |j        z   }|| k    r|                    |           Z|                    |           p|                    |          S )zFApply max_staleness, in seconds, to a Selection with no known primary.)	"secondary_with_max_last_write_dater   r   r   r   r   r   r   r   )r
   r   smaxr   r    r!   s         r   _no_primaryr&   W   s     7799D 611"555
C* 	 	=K333'=A,====,q/@@9C``IM))

1JJqMMMM--c222r   c                    | dk    r|S t          | |j                   |j        rt          | |          S t	          | |          S )z0Apply max_staleness, in seconds, to a Selection.)r   r   r   r"   r&   )r
   r   s     r   selectr)   o   sS    
 M9+HIII 5]I666=)444r   N)r
   r   r   r   r   r   )r
   r   r   r   r   r   )__doc__
__future__r   typingr   pymongo.errorsr   pymongo.server_typer   pymongo.server_selectorsr   r   r   r   r"   r&   r)    r   r   <module>r1      s     # " " " " "             - - - - - - + + + + + + 3222222
   
 
 
 
"3 3 3 303 3 3 305 5 5 5 5 5r   