
    &e.&                         d dl Z d dlmZmZmZmZmZ d dlmZ d dl	m
Z
mZ d dlmZ d dlmZ d dlmZmZ d dlmZ erd d	lmZ  ee          Zd
edefdZd
edefdZ G d de          ZdS )    N)TYPE_CHECKINGDictListMutableMappingOptional)WeakKeyDictionary)configutil)
get_logger)
ForwardMsg)	CacheStatCacheStatsProvider)HASHLIB_KWARGS)
AppSessionmsgreturnc                 >   | j         dk    r| j        }|                     d           t          j        di t
          }|                    |                                            |                                | _         | j        	                    |           | j         S )ao  Computes and assigns the unique hash for a ForwardMsg.

    If the ForwardMsg already has a hash, this is a no-op.

    Parameters
    ----------
    msg : ForwardMsg

    Returns
    -------
    string
        The message's hash, returned here for convenience. (The hash
        will also be assigned to the ForwardMsg; callers do not need
        to do this.)

     metadata )
hashr   
ClearFieldhashlibmd5r   updateSerializeToString	hexdigestCopyFrom)r   r   hashers      Clib/python3.11/site-packages/streamlit/runtime/forward_msg_cache.pypopulate_hash_if_neededr!      s    " x2~~ <z""" ..~..c++--...##%% 	h'''8O    c                     t                      }t          |           |_        |j                            | j                   |S )a  Create a ForwardMsg that refers to the given message via its hash.

    The reference message will also get a copy of the source message's
    metadata.

    Parameters
    ----------
    msg : ForwardMsg
        The ForwardMsg to create the reference to.

    Returns
    -------
    ForwardMsg
        A new ForwardMsg that "points" to the original message via the
        ref_hash field.

    )r   r!   ref_hashr   r   )r   ref_msgs     r    create_reference_msgr&   A   s;    $ llG.s33Gcl+++Nr"   c                       e Zd ZdZ G d d          Zd ZdefdZdedd	d
e	ddfdZ
dedee         fdZdedd	d
e	defdZddZdd	d
e	ddfdZddZdee         fdZdS )ForwardMsgCachea  A cache of ForwardMsgs.

    Large ForwardMsgs (e.g. those containing big DataFrame payloads) are
    stored in this cache. The server can choose to send a ForwardMsg's hash,
    rather than the message itself, to a client. Clients can then
    request messages from this cache via another endpoint.

    This cache is *not* thread safe. It's intended to only be accessed by
    the server thread.

    c                       e Zd ZdZdee         fdZdefdZddde	dd	fd
Z
dddefdZddde	de	fdZddZdefdZd	S )ForwardMsgCache.EntryzCache entry.

        Stores the cached message, and the set of AppSessions
        that we've sent the cached message to.

        r   c                 :    || _         t                      | _        d S N)r   r   _session_script_run_counts)selfr   s     r    __init__zForwardMsgCache.Entry.__init__n   s"    DH "## +++r"   r   c                 *    t          j        |           S r,   r
   repr_r.   s    r    __repr__zForwardMsgCache.Entry.__repr__t   s    :d###r"   sessionr   script_run_countNc                     | j                             |d          }||k     r#t                              d|d|d           |}|| j         |<   dS )a  Adds a reference to a AppSession that has referenced
            this Entry's message.

            Parameters
            ----------
            session : AppSession
            script_run_count : int
                The session's run count at the time of the call

            r   zNew script_run_count (z) is < prev_run_count (z). This should never happen!N)r-   getLOGGERerror)r.   r5   r6   prev_run_counts       r    add_session_refz%ForwardMsgCache.Entry.add_session_refw   so     "<@@!LLN.003C3C3C^^^U   $2 7GD+G444r"   c                     || j         v S r,   r-   r.   r5   s     r    has_session_refz%ForwardMsgCache.Entry.has_session_ref   s    d===r"   c                 "    || j         |         z
  S )znThe age of the given session's reference to the Entry,
            given a new script_run_count.

            r>   )r.   r5   r6   s      r    get_session_ref_agez)ForwardMsgCache.Entry.get_session_ref_age   s     $d&Eg&NNNr"   c                     | j         |= d S r,   r>   r?   s     r    remove_session_refz(ForwardMsgCache.Entry.remove_session_ref   s    /888r"   c                 2    t          | j                  dk    S )zzTrue if this Entry has references from any AppSession.

            If not, it can be removed from the cache.
            r   )lenr-   r3   s    r    has_refszForwardMsgCache.Entry.has_refs   s    
 t677!;;r"   r5   r   r   N)__name__
__module____qualname____doc__r   r   r/   strr4   intr<   boolr@   rB   rD   rG   r   r"   r    Entryr*   f   s       	 		$ 4 	$ 	$ 	$ 	$	$c 	$ 	$ 	$ 	$	H< 	H3 	HSW 	H 	H 	H 	H(	>< 	>D 	> 	> 	> 	>	O'	O;>	O	O 	O 	O 	O	9 	9 	9 	9	<d 	< 	< 	< 	< 	< 	<r"   rP   c                     i | _         d S r,   )_entriesr3   s    r    r/   zForwardMsgCache.__init__   s    <>r"   r   c                 *    t          j        |           S r,   r1   r3   s    r    r4   zForwardMsgCache.__repr__   s    z$r"   r   r5   r   r6   Nc                 D   t          |           | j                            |j        d          }|Xt	          j        d          rt                              |          }nt                              d          }|| j        |j        <   |                    ||           dS )a  Add a ForwardMsg to the cache.

        The cache will also record a reference to the given AppSession,
        so that it can track which sessions have already received
        each given ForwardMsg.

        Parameters
        ----------
        msg : ForwardMsg
        session : AppSession
        script_run_count : int
            The number of times the session's script has run

        Nz)global.storeCachedForwardMessagesInMemory)	r!   rR   r8   r   r	   
get_optionr(   rP   r<   )r.   r   r5   r6   entrys        r    add_messagezForwardMsgCache.add_message   s    " 	 $$$!!#(D11= !LMM 4'--c22'--d33&+DM#(#g'788888r"   r   c                 N    | j                             |d          }|r|j        ndS )zReturn the message with the given ID if it exists in the cache.

        Parameters
        ----------
        hash : str
            The id of the message to retrieve.

        Returns
        -------
        ForwardMsg | None

        N)rR   r8   r   )r.   r   rV   s      r    get_messagezForwardMsgCache.get_message   s,     !!$--!+uyyt+r"   c                    t          |           | j                            |j        d          }||                    |          sdS |                    ||          }|t          t          j        d                    k    S )z6Return True if a session has a reference to a message.NFglobal.maxCachedMessageAge)	r!   rR   r8   r   r@   rB   rN   r	   rU   )r.   r   r5   r6   rV   ages         r    has_message_referencez%ForwardMsgCache.has_message_reference   s~     	 $$$!!#(D11= 5 5g > >=5 ''1ABBc&+,HIIJJJJr"   c                     | j                                                                         D ]K\  }}|                    |          r|                    |           |                                s| j         |= LdS )zRemove refs for all entries for the given session.

        This should be called when an AppSession is disconnected or closed.

        Parameters
        ----------
        session : AppSession
        N)rR   copyitemsr@   rD   rG   )r.   r5   msg_hashrV   s       r    remove_refs_for_sessionz'ForwardMsgCache.remove_refs_for_session   s      $}113399;; 	, 	,OHe$$W-- 2((111>>## , M(+	, 	,r"   c                    t          j        d          }| j                                                                        D ]\  }}|                    |          s|                    ||          }||k    r[t                              dt          |          ||           |
                    |           |                                s| j        |= dS )a8  Remove any cached messages that have expired from the given session.

        This should be called each time a AppSession finishes executing.

        Parameters
        ----------
        session : AppSession
        script_run_count : int
            The number of times the session's script has run

        r[   z4Removing expired entry [session=%s, hash=%s, age=%s]N)r	   rU   rR   r_   r`   r@   rB   r9   debugidrD   rG   )r.   r5   r6   max_agera   rV   r\   s          r    "remove_expired_entries_for_sessionz2ForwardMsgCache.remove_expired_entries_for_session   s     #$@AA  $}113399;; 	0 	0OHe((11 ++G5EFFCW}}JwKK	   ((111~~'' 0 h/!	0 	0r"   c                 8    | j                                          dS )z!Remove all entries from the cacheN)rR   clearr3   s    r    ri   zForwardMsgCache.clear  s    r"   c           	          g }| j                                         D ]J\  }}|                    t          dd|j        |j                                        nd                     K|S )NForwardMessageCacher   r   )category_name
cache_namebyte_length)rR   r`   appendr   r   ByteSize)r.   stats
entry_hashrV   s       r    	get_statszForwardMsgCache.get_stats  s    !#!%!4!4!6!6 	 	JLL"7!8=	8M	 2 2 4 4 4ST      r"   rH   )r   N)rI   rJ   rK   rL   rP   r/   rM   r4   r   rN   rW   r   rY   rO   r]   rb   rg   ri   r   r   rs   r   r"   r    r(   r(   Y   s|       
 
9< 9< 9< 9< 9< 9< 9< 9<v? ? ? #        99(49HK9	9 9 9 96, ,(< , , , , KK(4KHKK	K K K K, , , ,*"0#"07:"0	"0 "0 "0 "0H   
4	? 
 
 
 
 
 
r"   r(   )r   typingr   r   r   r   r   weakrefr   	streamlitr	   r
   streamlit.loggerr   streamlit.proto.ForwardMsg_pb2r   streamlit.runtime.statsr   r   streamlit.utilr   streamlit.runtime.app_sessionr   rI   r9   rM   r!   r&   r(   r   r"   r    <module>r|      s_    F F F F F F F F F F F F F F % % % % % % " " " " " " " " ' ' ' ' ' ' 5 5 5 5 5 5 A A A A A A A A ) ) ) ) ) ) 9888888	H		     Dj Z    0N N N N N( N N N N Nr"   