L i3AdZddlmZddlmZddlmZddlmZddlmZddlmZddlm Z dd lm Z dd lm Z dd lm Z dd lm Z d dlmZd dlmZd dlmZd dlmZd dlmZd dlmZd dlmZd dlmZd dlmZd dlmZd dlmZd dlm Z d dl!m"Z"d dl!m#Z#e r`d dl$m%Z%d dl$m&Z&d dl$m'Z'd dl(m)Z)d d l(m*Z*d d!lm+Z+d d"lm,Z,d d#l-m.Z.d d$l/m0Z0d d%lm1Z1d d&lm2Z2d d'lm3Z3d d(l4m5Z5d d)l6m7Z7d d*l8m9Z9d d+l:m;Z;d,d-gZ<e d.e/Z=e>Z?Gd0d1e"Z@Gd2d3e"ZAGd4d-ee=ZBGd5d,e ZCGd6d7eZD d:d8ZEy9);aHorizontal 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 refering 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) ClauseElementShardedSession ShardedQuery_T)boundc$eZdZ ddZy) ShardChoosercyN)selfmapperinstanceclauses e/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sqlalchemy/ext/horizontal_shard.py__call__zShardChooser.__call__Qs N)r7Optional[Mapper[_T]]r8rr9Optional[ClauseElement]returnr__name__ __module__ __qualname__r;r5r<r:r2r2Ps-$(  r<r2c0eZdZ ddZy)IdentityChooserc yr4r5)r6r7 primary_keylazy_loaded_fromexecution_optionsbind_argumentskws r:r;zIdentityChooser.__call__Zsr<Nr7z Mapper[_T]rGrrHOptional[InstanceState[Any]]rIrrJrrKrr?rr@r5r<r:rErEYsK  )  7  6 '    r<rEc,eZdZdZdfd ZddZxZS)r.aQuery 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. ct||i|t|jtsJ|jj |_|jj |_d|_yr4)super__init__ isinstancesessionr-identity_chooserexecute_chooser _shard_id)r6argskwargs __class__s r:rQzShardedQuery.__init__osT $)&)$,,777 $ = =#||;;r<c&|j|S)aReturn 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)rI)r6shard_ids r: set_shardzShardedQuery.set_shardws%%8%<._legacy_identity_choosers4JJv&#,,-=>A"1k22r<z*identity_chooser or id_chooser is requiredzNThe ``query_chooser`` parameter is deprecated; please use ``execute_chooser``.z1.4z>Can't pass query_chooser and execute_chooser at the same time.c(|jSr4) statement) orm_context_query_choosers r:_default_execute_chooserz9ShardedSession.__init__.._default_execute_choosers&k&;&;< 3 !/ 3 3 3%=D ! $4D !##<  *N  2  ''(  =, = = &":  "##>  /=?   .6!9- . r<c |t |||fd|i|} | S|j|||||r t|niD]} t |||f| |d|} | | cSy)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`. Nidentity_token)rHrIrJ)rrH)rP_identity_lookuprTdict) r6r7primary_key_identityrpassiverHrIrJrKobjr\obj2rYs r:rzShardedSession._identity_lookups(  %'*$ . CJ 11$!1"37EtN32 2 w/($,%5   #K "r<c |Dt|}|jr|jd}|J|S|jr |jSt|tsJ|j ||fi|}||_|S)Nr)rkeyrrRrrd)r6r7r8rKstatetokenr\s r:_choose_shard_and_assignz'ShardedSession._choose_shard_and_assign,s  H%Eyy ! ((( %%+++&&)))%4%%fh="=  #+E r<c ,||j||}|jr'|j}|J|j||S|j |||}t |t r|jdi|St |tsJ|S)zaProvide a :class:`_engine.Connection` to use in the unit of work flush process. )r\)r7r\r8r5) rin_transactionget_transaction connectionget_bindrRrconnectr)r6r7r8r\rKtransbinds r:connection_callablez"ShardedSession.connection_callableAs  44VXFH    ((*E$ $$##FX#> >==8!D$'#t||)b))!$ 333 r<)r\r8r9c T||j|||}|J|j|S)N)r8r9)rrz)r6r7r\r8r9rKs r:rzShardedSession.get_bind_sC  44&5H' ''}}X&&r<c"||j|<yr4)rz)r6r\rs r:r{zShardedSession.bind_shardos#' hr<)rdr2rTzOptional[IdentityChooser]rUz4Optional[Callable[[ORMExecuteState], Iterable[Any]]]r|zOptional[Dict[str, Any]]rhzType[Query[_T]]rezrKrr?r')r\r_rzUnion[Engine, OptionEngine]r?r^)rArBrC__annotations__r.rQr PASSIVE_OFFr EMPTY_DICTrrrrr{rarbs@r:r-r-s%%?? 7; +/%1n. IMn.#n.4n. n.)n.#n. n.Gn.n. n.h)-*669=8<37//:/& /  / 7 /6/1// 3/b,  .(,"&.2 $ ,     @04'/3"&*. ',', '  ' ( '' ' '''/J' 'r<c&eZdZdZdZ d ddZy) set_shard_idaa 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 r\propagate_to_loadersc ||_||_y)aHConstruct 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)r6r\rs r:rQzset_shard_id.__init__s! $8!r<N)T)r\r_rbool)rArBrCr` __slots__rQr5r<r:rrus*85IGK9'9?C9r<rcjr j}n'js jr j}nd}j }t |tsJ dfd }jD] }t |ts|j}nZ|r|j |j}n>djvrjd}n djvrjd}nd}|||Sg}|jD]}||}|j||dj |ddS)Nr\ctj}||d<j|j|S)Nr\)r)rJ)rrJupdate_execution_optionsinvoke_statement)r\rJrss r:iter_for_shardz-execute_and_instances..iter_for_shardsDk889%-z",,H,E++>+JJr<r[r)r\r_r?&Union[Result[_T], IteratorResult[_TP]]) is_select load_options is_update is_deleteupdate_delete_optionsrSrRr-_non_compile_orm_optionsrr\_identity_tokenrIrJrUappendmerge)rsactive_optionsrSrorm_optr\partialresult_s` r:rwrws_$11   +"7"7$::!!G g~ .. .K!K /K77 g| ,''H   n<<H%55H {<< <"44^DH ;55 5"11*=HHh''// < $H$X.G NN7 # $ wqz--r<N)rsr(r?r)Fr` __future__rtypingrrrrrr r r r r rrrrormr orm._typingrorm.interfacesr orm.mapperr orm.queryr orm.sessionrrr util.typingrr engine.baserrr engine.resultr r!r"r#orm.bulk_persistencer$ orm.contextr%r&r'r( orm.stater)sqlr* sql._typingr+ sql.elementsr,__all__r/strr_r2rEr.r-rrwr5r<r:rs$# 4&(-!"($*.&* 9*,*-) !, ^ , T8 h =59=@l'Wl'^/99/9d7. 7.+7.r<