
    nhD                    p    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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.
    )annotations)TYPE_CHECKING)ConfigurationError)SERVER_TYPE)	Selection
   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     /var/www/html/SchoolMeal/SchoolMeal/pds_admin_SchoolMeal/Backend/venv/lib/python3.12/site-packages/pymongo/max_staleness_selectors.py_validate_max_stalenessr   .   sk    *->>> 2 !-1Dt1KLM
 	
 -- 03I=2YZ
 	
 .    c                   |j                   }|sJ g }|j                  D ]  }|j                  t        j                  k(  rr|j
                  r|j
                  sJ |j                  |j
                  z
  |j                  |j
                  z
  z
  |j                  z   }|| k  s|j                  |       |j                  |        |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   	selectionr   sdss	stalenesss         r   _with_primaryr   ?   s    GN7
C** ==K333$$)@)@@@##a&7&77++g.E.EEG//0  M)

1JJqM --c22r   c                   |j                         }|s|j                  g       S g }|j                  D ]  }|j                  t        j
                  k(  rX|j                  r|j                  sJ |j                  |j                  z
  |j                  z   }|| k  sf|j                  |       x|j                  |        |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#   W   s     779D11"55
C** 	==K333''A,=,===,,q/@/@@9C`C``I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&   o   sE    
 M9+H+HI]I66=)44r   N)r   intr   r'   returnNone)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@    #   - +2
   
"30305r   