Ë
    Ü_ËiD  ã                  ó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 )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)Úmax_stalenessÚheartbeat_frequencys     úb/var/www/pod-logistic/pod-api/venv/lib/python3.12/site-packages/pymongo/max_staleness_selectors.pyÚ_validate_max_stalenessr   .   sk   € àÐ*Ô->Ñ>Ò>Ü ð2ô ! -Ð1DÀtÑ1KÐLñMó
ð 	
ð Ô-Ò-Ü ð0Ü3IÈ=Ð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Ù€Nˆ7Ø
€Cà×*Ñ*ò ˆØ�=‰=œK×3Ñ3Ò3à×$Ò$¨×)@Ò)@Ð@Ð@à×#Ñ# a×&7Ñ&7Ñ7Ø×+Ñ+¨g×.EÑ.EÑEñGà×/Ñ/ñ0ð ð ˜MÓ)Ø—
‘
˜1•à�J‰J�q�Mðð ×-Ñ-¨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Ó9€DÙà×1Ñ1°"Ó5Ð5à
€Cà×*Ñ*ò 	ˆØ�=‰=œK×3Ñ3Ò3à×'Ò'¨A×,=Ò,=Ð=Ð=Ø×,Ñ,¨q×/@Ñ/@Ñ@À9×C`ÑC`Ñ`ˆIà˜MÓ)Ø—
‘
˜1•à�J‰J�q�Mð	ð ×-Ñ-¨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   € à˜ÒØÐô
 ˜M¨9×+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ð
 Ð ØÐ ó
ó"3ó03ô05r   