
    h                     @    d Z ddlmZ ddlmZ dZdZd Zd Zd Z	d	 Z
y
)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.
    )ConfigurationError)SERVER_TYPE
   Z   c                     | |t         z   k  rt        dt         | |dz  fz        | t        k  rt        dt        | fz        y )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)max_stalenessheartbeat_frequencys     b/var/www/html/ranktracker/api/venv/lib/python3.12/site-packages/pymongo/max_staleness_selectors.py_validate_max_stalenessr   (   ss     *->>> 2 "=2E2L5NNO 	O -- 0&3778 	8 .    c                 n   |j                   }g }|j                  D ]  }|j                  t        j                  k(  rX|j
                  |j                  z
  |j
                  |j                  z
  z
  |j                  z   }|| k  sf|j                  |       x|j                  |        |j                  |      S )zEApply max_staleness, in seconds, to a Selection with a known primary.)
primaryserver_descriptionsserver_typer   RSSecondarylast_update_timelast_write_dater   appendwith_server_descriptions)r
   	selectionr   sdss	stalenesss         r   _with_primaryr   9   s    G
C** ==K333 ##a&7&77))G,C,CCE--. 
 M)

1JJqM --c22r   c                 h   |j                         }|s|j                  g       S g }|j                  D ]n  }|j                  t        j
                  k(  r>|j                  |j                  z
  |j                  z   }|| k  sL|j                  |       ^|j                  |       p |j                  |      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    N   s     779D11"55
C** 
==K333--**+"667I M)

1JJqM
 --c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#   g   sE    
 M9+H+HI]I66=)44r   N)__doc__pymongo.errorsr   pymongo.server_typer   r   r	   r   r   r    r#    r   r   <module>r(      s4    . +
   8"3*325r   