o
    'èhD  ã                   @  sx   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d„Zddd„Zddd„Zd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                 C  s>   | |t  k rtdt | |d f ƒ‚| tk rtdt| f ƒ‚d S )NzŒmaxStalenessSeconds 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   © r   ú[/var/www/html/olx_land/venv/lib/python3.10/site-packages/pymongo/max_staleness_selectors.pyÚ_validate_max_staleness.   s   ýÿÿÿÿr   Ú	selectionr   c                 C  s€   |j }|sJ ‚g }|jD ].}|jtjkr5|jr|jsJ ‚|j|j |j|j  |j }|| kr4| |¡ q| |¡ q| 	|¡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Ú	stalenessr   r   r   Ú_with_primary?   s"   


ÿþÿ
€
r   c                 C  s|   |  ¡ }|s| g ¡S g }|jD ](}|jtjkr3|jr|js J ‚|j|j |j }|| kr2| |¡ q| |¡ q| |¡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   r   r   r   Ú_no_primaryW   s   


€
r"   c                 C  s2   | 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   r   r   r   Úselecto   s   

r$   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   r   r   Ú<module>   s   


