o Sûbžã@sHdZddlmZddlmZdZdZdd„Zdd „Zd d „Z d d „Z dS)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é éZcCs>||tkrtdt||dfƒ‚|tkrtdt|fƒ‚dS)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_frequency©r úF/tmp/pip-target-onvjaxws/lib/python/pymongo/max_staleness_selectors.pyÚ_validate_max_staleness's  ýÿÿÿÿr cCsh|j}g}|jD]&}|jtjkr)|j|j|j|j|j}||kr(| |¡q| |¡q|  |¡S)zEApply max_staleness, in seconds, to a Selection with a known primary.) ÚprimaryÚserver_descriptionsÚ server_typerÚ RSSecondaryZlast_update_timeÚlast_write_dater ÚappendÚwith_server_descriptions)rÚ selectionr ÚsdsÚsÚ stalenessr r r Ú _with_primary8s    ÿþÿ €  rcCsl| ¡}|s | g¡Sg}|jD] }|jtjkr+|j|j|j}||kr*| |¡q| |¡q| |¡S)zFApply max_staleness, in seconds, to a Selection with no known primary.) Z"secondary_with_max_last_write_daterrrrrrr r)rrZsmaxrrrr r r Ú _no_primaryNs    €  rcCs2|dkr|St||jƒ|jrt||ƒSt||ƒS)z0Apply max_staleness, in seconds, to a Selection.éÿÿÿÿ)r r r rr)rrr r r Úselectes    rN) Ú__doc__Zpymongo.errorsrZpymongo.server_typerrrr rrrr r r r Ús