id#dZddlmZddlmZddlmZddlmZddgZGddeZ Gd d e Z Gd deZ d S) 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. )inspect)util)Query)SessionShardedSession ShardedQuerycLeZdZfdZdZdZdZ dfd Zd fd ZxZ S) rctt|j|i||jj|_|jj|_d|_dSN)superr__init__session id_chooser query_chooser _shard_id)selfargskwargs __class__s T/opt/cloudlinux/venv/lib/python3.11/site-packages/sqlalchemy/ext/horizontal_shard.pyr zShardedQuery.__init__sI*lD!!*D;F;;;,1!\7c<|}||_|S)zReturn 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. )_cloner)rshard_idqs r set_shardzShardedQuery.set_shard!s KKMM rcfd}j|jSj|jSg}D] }|||!t |S)Nc|xjd<_|jj}|S)Nr)mapperr) attributesidentity_token_connection_from_session _bind_mapperexecute statement_params instances)rresultcontextrs riter_for_shardz;ShardedQuery._execute_and_instances..iter_for_shard.spFN NG z *W-C22((**X3gg'66 >>&'22 2r)r!rrextenditer)rr)r*partialrs`` r_execute_and_instancesz#ShardedQuery._execute_and_instances-s 3 3 3 3 3 3  ! -!>'"899 9 ^ '!>$.11 1G ..t44 9 9~~h778888== rcfd}j|jSd}g}D],}||}||jz }||-t ||S)Ncp|d}|j}|S)NT)rrclauseclose_with_result)r"r$r&)rconnr(rrstmts rexec_for_shardz2ShardedQuery._execute_crud..exec_for_shardCsE00!"& 1D \\$ 55FMr)rrrowcountappend ShardedResult)rr4rr5r7resultsrr(s``` r _execute_crudzShardedQuery._execute_crudBs        > %!>$.11 1HG ..t44 ' ''11FO+v&&&& (33 3rNc <|$tt|j||fd|i|S|j|}|r||}|||D],}tt|j||fd|i|}||cS-dS)zOverride the default Query._identity_lookup method so that we search for a given non-token primary key identity across all possible identity tokens (e.g. shard ids). Nr!)r r_identity_lookuprquery_set_lazyload_fromr) rrprimary_key_identityr!lazy_loaded_fromkwrrobjrs rr=zShardedQuery._identity_lookupYs  %=5t,,=$ .   ""6**A ;(()9:: OOA/CDD  @eL$//@0AIMO?JJJ#4rcfd}|jj}tt|||S)zOverride the default Query._get_impl() method so that we emit a query to the DB for each possible identity token, if we don't have one already. cj |Stj|}|D])}|}||}||cS*dSr )rrto_listrr)r>r@identrro db_load_fnrs r _db_load_fnz+ShardedQuery._get_impl.._db_load_fns~)!z$(<=== %9::!%e < <  Hx00A" 1e,,A} % 4rN)r!)rr r _get_impl)rr@rIr!rJrs` ` rrKzShardedQuery._get_impl{sg      $  !dn&@!^N\4((22 +n3   r)NNr ) __name__ __module__ __qualname__r rr.r;r=rK __classcell__rs@rrrs   !!!*4446      D          rc2eZdZdZdZdZedZdS)r9aA value object that represents multiple :class:`_engine.ResultProxy` objects. This is used by the :meth:`.ShardedQuery._execute_crud` hook to return an object that takes the place of the single :class:`_engine.ResultProxy`. Attribute include ``result_proxies``, which is a sequence of the actual :class:`_engine.ResultProxy` objects, as well as ``aggregate_rowcount`` or ``rowcount``, which is the sum of all the individual rowcount values. .. versionadded:: 1.3 result_proxiesaggregate_rowcountc"||_||_dSr rR)rrSrTs rr zShardedResult.__init__s,"4rc|jSr )rT)rs rr7zShardedResult.rowcounts &&rN)rLrMrN__doc__ __slots__r propertyr7rrr9r9sO  9I555''X'''rr9cBeZdZdeffd ZdZddZ ddZdZxZ S)rNc tt|jdd|i|||_||_||_i|_|j|_|!|D] }| |||dSdS)aConstruct 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 id_chooser: A callable, passed a query and a tuple of identity values, which should return a list of shard ids where the ID might reside. The databases will be queried in the order of this listing. :param query_chooser: For a given Query, 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. :param shards: A dictionary of string shard names to :class:`~sqlalchemy.engine.Engine` objects. query_clsNrZ) r rr shard_chooserrr_ShardedSession__binds connectionconnection_callable bind_shard) rr^rrshardsr]rkrs rr zShardedSession.__init__s< -nd##,KKyKFKKK*$* #'?   . .6!9----   . .rc |7t|}|jr|jd}|J|S|jr|jS|j||fi|}|||_|S)Nr)rkeyr!r^)rrinstancerBstatetokenrs r_choose_shard_and_assignz'ShardedSession._choose_shard_and_assigns~  H%%Ey , ! ((( % ,++%4%fh=="==  #+E rc ||||}|j|j||S||||jdi|S)N)r)rrgrZ)rj transactionr`get_bind_contextual_connect)rrrgrrs rr`zShardedSession.connections  44VXFFH   '#..v.II I"4==H!!,,$*,, ,rc P|||||}|j|S)N)r1)rjr_)rrrrgr1rBs rrmzShardedSession.get_binds:  445H|H%%rc||j|<dSr )r_)rrbinds rrbzShardedSession.bind_shards!% Xr)NNN) rLrMrNrr rjr`rmrbrOrPs@rrrs  &.&.&.&.&.&.P    , , , ,<@&&&&&&&&&&&rN) rWrr orm.queryr orm.sessionr__all__robjectr9rrZrrrws!!!!!! ^ ,     5   D'''''F'''6M&M&M&M&M&WM&M&M&M&M&r