a
    •˜ëj5A  ã                   @  sv  d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm	Z	 dd	lm
Z
 dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm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dl$m&Z& ddl$m'Z' ddl(m)Z) dd l(m*Z* dd!lm+Z+ dd"lm,Z, dd#l-m.Z. dd$l/m0Z0 dd%lm1Z1 dd&lm2Z2 dd'lm3Z3 dd(l4m5Z5 dd)l6m7Z7 dd*l8m9Z9 dd+l:m;Z; d,d-gZ<ed.ed/�Z=e>Z?G d0d1„ d1e"ƒZ@G d2d3„ d3e"ƒZAG d4d-„ d-ee= ƒZBG d5d,„ d,e ƒZCG d6d7„ d7eƒZDd8d9d:œd;d<„ZEd=S )>aÌ  Horizontal sharding support.

Defines a rudimental 'horizontal sharding' system which allows a Session to
distribute queries and persistence operations across multiple databases.

For a usage example, see the :ref:`examples_sharding` example included in
the source distribution.

.. deepalchemy:: The horizontal sharding extension is an advanced feature,
   involving a complex statement -> database interaction as well as
   use of semi-public APIs for non-trivial cases.   Simpler approaches to
   referring to multiple database "shards", most commonly using a distinct
   :class:`_orm.Session` per "shard", should always be considered first
   before using this more complex and less-production-tested system.



é    )Úannotations)ÚAny)ÚCallable)ÚDict)ÚIterable)ÚOptional)ÚTuple)ÚType)ÚTYPE_CHECKING)ÚTypeVar)ÚUnioné   )Úevent)Úexc)Úinspect)Úutil)ÚPassiveFlag)ÚOrmExecuteOptionsParameter)Ú	ORMOption)ÚMapper)ÚQuery)Ú_BindArguments)Ú_PKIdentityArgument)ÚSession)ÚProtocol)ÚSelf)Ú
Connection)ÚEngine)ÚOptionEngine)ÚIteratorResult)ÚResult)ÚLoaderCallableStatus)Ú_O)ÚBulkUDCompileState)ÚQueryContext)Ú_EntityBindKey)Ú_SessionBind)ÚORMExecuteState)ÚInstanceState)Ú
Executable)Ú_TP)ÚClauseElementÚShardedSessionÚShardedQueryÚ_T)Úboundc                   @  s    e Zd Zdddddœdd„ZdS )ÚShardChooserúOptional[Mapper[_T]]r   úOptional[ClauseElement])ÚmapperÚinstanceÚclauseÚreturnc                 C  s   d S ©N© )Úselfr3   r4   r5   r8   r8   ú^/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/sqlalchemy/ext/horizontal_shard.pyÚ__call__R   s    zShardChooser.__call__N©Ú__name__Ú
__module__Ú__qualname__r;   r8   r8   r8   r:   r0   Q   s   r0   c                   @  s&   e Zd Zddddddddœdd	„Zd
S )ÚIdentityChooserú
Mapper[_T]r   úOptional[InstanceState[Any]]r   r   r   ©r3   Úprimary_keyÚlazy_loaded_fromÚexecution_optionsÚbind_argumentsÚkwr6   c                K  s   d S r7   r8   )r9   r3   rD   rE   rF   rG   rH   r8   r8   r:   r;   [   s    	zIdentityChooser.__call__Nr<   r8   r8   r8   r:   r@   Z   s   r@   c                      s:   e Zd ZdZddddœ‡ fdd„Zddd	œd
d„Z‡  ZS )r-   a  Query class used with :class:`.ShardedSession`.

    .. legacy:: The :class:`.ShardedQuery` is a subclass of the legacy
       :class:`.Query` class.   The :class:`.ShardedSession` now supports
       2.0 style execution via the :meth:`.ShardedSession.execute` method.

    r   ÚNone)ÚargsÚkwargsr6   c                   s@   t ƒ j|i |¤Ž t| jtƒs"J ‚| jj| _| jj| _d | _d S r7   )ÚsuperÚ__init__Ú
isinstanceÚsessionr,   Úidentity_chooserÚexecute_chooserZ	_shard_id)r9   rJ   rK   ©Ú	__class__r8   r:   rM   p   s
    

zShardedQuery.__init__ÚShardIdentifierr   ©Úshard_idr6   c                 C  s   | j |d�S )a•  Return a new query, limited to a single shard ID.

        All subsequent operations with the returned query will
        be against the single shard regardless of other state.

        The shard_id can be passed for a 2.0 style execution to the
        bind_arguments dictionary of :meth:`.Session.execute`::

            results = session.execute(stmt, bind_arguments={"shard_id": "my_shard"})

        )Ú_sa_shard_id)rF   )r9   rV   r8   r8   r:   Ú	set_shardx   s    zShardedQuery.set_shard)r=   r>   r?   Ú__doc__rM   rX   Ú__classcell__r8   r8   rR   r:   r-   g   s   c                      sò   e Zd ZU ded< ded< ded< dddefdddœdd	d
dddddddœ	‡ fdd„Zdejdej	dfddddddddddœ	‡ fdd„Z
ddddd œd!d"„Zd4d#dd$dd%d&œd'd(„Zd5dddd)œdd$dd*dd+d,œd-d.„Zd/d0dd1œd2d3„Z‡  ZS )6r,   r0   Úshard_chooserr@   rP   z*Callable[[ORMExecuteState], Iterable[Any]]rQ   N)Ú
id_chooserÚquery_chooserzOptional[IdentityChooser]z4Optional[Callable[[ORMExecuteState], Iterable[Any]]]zOptional[Dict[str, Any]]zType[Query[_T]]z<Optional[Callable[[Query[_T], Iterable[_T]], Iterable[Any]]]z/Optional[Callable[[Executable], Iterable[Any]]]r   rI   )	r[   rP   rQ   ÚshardsÚ	query_clsr\   r]   rK   r6   c                  s  t ƒ jf d|i|¤Ž tjˆdtdd� |ˆ_|rj|‰ t dd¡ ddd	d
ddddœ‡ ‡fdd„}	|	ˆ_n|rv|ˆ_n
t	 
d¡‚|rÂ|‰t dd¡ |r¢t	 
d¡‚dddœ‡fdd„}
|du rÂ|
}|du rÔt	 
d¡‚|ˆ_i ˆ_|du�r|D ]}ˆ ||| ¡ qîdS )aî  Construct a ShardedSession.

        :param shard_chooser: A callable which, passed a Mapper, a mapped
          instance, and possibly a SQL clause, returns a shard ID.  This id
          may be based off of the attributes present within the object, or on
          some round-robin scheme. If the scheme is based on a selection, it
          should set whatever state on the instance to mark it in the future as
          participating in that shard.

        :param identity_chooser: A callable, passed a Mapper and primary key
         argument, which should return a list of shard ids where this
         primary key might reside.

          .. versionchanged:: 2.0  The ``identity_chooser`` parameter
             supersedes the ``id_chooser`` parameter.

        :param execute_chooser: For a given :class:`.ORMExecuteState`,
          returns the list of shard_ids
          where the query should be issued.  Results from all shards returned
          will be combined together into a single listing.

          .. versionchanged:: 1.4  The ``execute_chooser`` parameter
             supersedes the ``query_chooser`` parameter.

        :param shards: A dictionary of string shard names
          to :class:`~sqlalchemy.engine.Engine` objects.

        r_   Zdo_orm_executeT)ÚretvalzLThe ``id_chooser`` parameter is deprecated; please use ``identity_chooser``.z2.0rA   r   rB   r   r   r   rC   c                  s"   ˆ  | ¡}|r| |¡}ˆ ||ƒS r7   )ÚqueryZ_set_lazyload_from)r3   rD   rE   rF   rG   rH   Úq)Ú_id_chooserr9   r8   r:   Ú_legacy_identity_chooserÇ   s    	

z9ShardedSession.__init__.<locals>._legacy_identity_chooserz*identity_chooser or id_chooser is requiredzNThe ``query_chooser`` parameter is deprecated; please use ``execute_chooser``.z1.4z>Can't pass query_chooser and execute_chooser at the same time.r'   zIterable[Any]©Úorm_contextr6   c                   s
   ˆ | j ƒS r7   )Z	statement©rf   )Ú_query_chooserr8   r:   Ú_default_execute_chooserê   s    z9ShardedSession.__init__.<locals>._default_execute_chooserNz,execute_chooser or query_chooser is required)rL   rM   r   ÚlistenÚexecute_and_instancesr[   r   Zwarn_deprecatedrP   r   ÚArgumentErrorrQ   Ú_ShardedSession__shardsÚ
bind_shard)r9   r[   rP   rQ   r^   r_   r\   r]   rK   rd   ri   ÚkrR   )rc   rh   r9   r:   rM   Œ   sP    ,ÿý ÿýÿÿ
zShardedSession.__init__z
Mapper[_O]zUnion[Any, Tuple[Any, ...]]zOptional[Any]r   rB   r   zOptional[_BindArguments]z)Union[Optional[_O], LoaderCallableStatus])	r3   Úprimary_key_identityÚidentity_tokenÚpassiverE   rF   rG   rH   r6   c                   s€   |dur&t ƒ j||fd|i|¤Ž}	|	S | j|||||r>t|ƒni d�D ]0}
t ƒ j||f|
|dœ|¤Ž}|durF|  S qFdS dS )a_  override the default :meth:`.Session._identity_lookup` method so
        that we search for a given non-token primary key identity across all
        possible identity tokens (e.g. shard ids).

        .. versionchanged:: 1.4  Moved :meth:`.Session._identity_lookup` from
           the :class:`_query.Query` object to the :class:`.Session`.

        Nrq   )rE   rF   rG   )rq   rE   )rL   Ú_identity_lookuprP   Údict)r9   r3   rp   rq   rr   rE   rF   rG   rH   ÚobjrV   Úobj2rR   r8   r:   rs   ü   s:    þýüû
þüû
zShardedSession._identity_lookupzOptional[_EntityBindKey[_O]])r3   r4   rH   r6   c                 K  sp   |d ur<t |ƒ}|jr0|jd }|d us,J ‚|S |jr<|jS t|tƒsJJ ‚| j||fi |¤Ž}|d url||_|S )Nr   )r   Úkeyrq   rN   r   r[   )r9   r3   r4   rH   ÚstateÚtokenrV   r8   r8   r:   Ú_choose_shard_and_assign-  s    
z'ShardedSession._choose_shard_and_assignr1   zOptional[ShardIdentifier]r   )r3   r4   rV   rH   r6   c                 K  s~   |du r|   ||¡}|  ¡ r>|  ¡ }|dus0J ‚|j||d�S | j|||d�}t|tƒrh|jf i |¤ŽS t|tƒsvJ ‚|S dS )zaProvide a :class:`_engine.Connection` to use in the unit of work
        flush process.

        N)rV   )r3   rV   r4   )	rz   Zin_transactionZget_transactionÚ
connectionÚget_bindrN   r   Úconnectr   )r9   r3   r4   rV   rH   ZtransÚbindr8   r8   r:   Úconnection_callableB  s    ÿ
z"ShardedSession.connection_callable)rV   r4   r5   r2   r&   )r3   rV   r4   r5   rH   r6   c                K  s.   |d u r$| j |||d�}|d us$J ‚| j| S )N)r4   r5   )rz   rm   )r9   r3   rV   r4   r5   rH   r8   r8   r:   r|   `  s    	ÿzShardedSession.get_bindrT   zUnion[Engine, OptionEngine])rV   r~   r6   c                 C  s   || j |< d S r7   )rm   )r9   rV   r~   r8   r8   r:   rn   p  s    zShardedSession.bind_shard)NNN)N)r=   r>   r?   Ú__annotations__r-   rM   r   ZPASSIVE_OFFr   Ú
EMPTY_DICTrs   rz   r   r|   rn   rZ   r8   r8   rR   r:   r,   ‡   s:   
øó&tø$1   ü  þúc                   @  s&   e Zd ZdZdZd	dddœdd„ZdS )
Úset_shard_ida  a loader option for statements to apply a specific shard id to the
    primary query as well as for additional relationship and column
    loaders.

    The :class:`_horizontal.set_shard_id` option may be applied using
    the :meth:`_sql.Executable.options` method of any executable statement::

        stmt = (
            select(MyObject)
            .where(MyObject.name == "some name")
            .options(set_shard_id("shard1"))
        )

    Above, the statement when invoked will limit to the "shard1" shard
    identifier for the primary query as well as for all relationship and
    column loading strategies, including eager loaders such as
    :func:`_orm.selectinload`, deferred column loaders like :func:`_orm.defer`,
    and the lazy relationship loader :func:`_orm.lazyload`.

    In this way, the :class:`_horizontal.set_shard_id` option has much wider
    scope than using the "shard_id" argument within the
    :paramref:`_orm.Session.execute.bind_arguments` dictionary.


    .. versionadded:: 2.0.0

    ©rV   Úpropagate_to_loadersTrT   Úboolc                 C  s   || _ || _dS )aH  Construct a :class:`_horizontal.set_shard_id` option.

        :param shard_id: shard identifier
        :param propagate_to_loaders: if left at its default of ``True``, the
         shard option will take place for lazy loaders such as
         :func:`_orm.lazyload` and :func:`_orm.defer`; if False, the option
         will not be propagated to loaded objects. Note that :func:`_orm.defer`
         always limits to the shard_id of the parent row in any case, so the
         parameter only has a net effect on the behavior of the
         :func:`_orm.lazyload` strategy.

        Nrƒ   )r9   rV   r„   r8   r8   r:   rM   •  s    zset_shard_id.__init__N)T)r=   r>   r?   rY   Ú	__slots__rM   r8   r8   r8   r:   r‚   v  s    ÿr‚   r'   ú&Union[Result[_T], IteratorResult[_TP]]re   c                   s  ˆ j rˆ j}nˆ jsˆ jr"ˆ j}nd }ˆ j}t|tƒs:J ‚dddœ‡ fdd„}ˆ jD ]}t|t	ƒrT|j
} q´qT|r„|jd ur„|j}n0dˆ jv ršˆ jd }ndˆ jv r°ˆ jd }nd }|d urÄ||ƒS g }| ˆ ¡D ]}||ƒ}| |¡ qÒ|d j|d	d … Ž S d S )
NrT   r‡   rU   c                   s*   t ˆ jƒ}| |d< ˆ j| d� ˆ j|d�S )NrV   )rq   )rG   )rt   rG   Zupdate_execution_optionsZinvoke_statement)rV   rG   rg   r8   r:   Úiter_for_shard¾  s    
z-execute_and_instances.<locals>.iter_for_shardrW   rV   r   é   )Z	is_selectZload_optionsZ	is_updateZ	is_deleteZupdate_delete_optionsrO   rN   r,   Z_non_compile_orm_optionsr‚   rV   Z_identity_tokenrF   rG   rQ   ÚappendÚmerge)rf   Zactive_optionsrO   rˆ   Zorm_optrV   ÚpartialZresult_r8   rg   r:   rk   ¨  s4    	



rk   N)FrY   Ú
__future__r   Útypingr   r   r   r   r   r   r	   r
   r   r   Ú r   r   r   r   Zormr   Zorm._typingr   Zorm.interfacesr   Z
orm.mapperr   Z	orm.queryr   Zorm.sessionr   r   r   Zutil.typingr   r   Zengine.baser   r   r   Zengine.resultr   r    r!   r"   Zorm.bulk_persistencer#   Zorm.contextr$   r%   r&   r'   Z	orm.stater(   Zsqlr)   Zsql._typingr*   Zsql.elementsr+   Ú__all__r.   ÚstrrT   r0   r@   r-   r,   r‚   rk   r8   r8   r8   r:   Ú<module>   sh   	  p2