B 4]#@spdZddlmZddlmZddlmZddlmZddgZGdddeZ Gd d d e Z Gd ddeZ 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 ShardedQuerycsPeZdZfddZddZddZddZdfd d Zdfd d ZZ S)rcs0tt|j|||jj|_|jj|_d|_dS)N)superr__init__session id_chooser query_chooser _shard_id)selfargskwargs) __class__R/opt/alt/python37/lib64/python3.7/site-packages/sqlalchemy/ext/horizontal_shard.pyr s  zShardedQuery.__init__cCs|}||_|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. )Z_cloner )rshard_idqrrr set_shard"szShardedQuery.set_shardcshfdd}jdk r"|jSjdk r6|jSg}x D]}|||qFWt|SdS)Ncs:|jd<_j|djj}|S)Nr)mapperr)Z attributesidentity_token_connection_from_sessionZ _bind_mapperexecuteZ statement_paramsZ instances)rresult)contextrrriter_for_shard.s  z;ShardedQuery._execute_and_instances..iter_for_shard)rr r extenditer)rrrpartialrr)rrr_execute_and_instances-s    z#ShardedQuery._execute_and_instancescsjfdd}jdk r$|jSd}g}x.D] }||}||j7}||q8Wt||SdS)Ncs$j|dd}|j}|S)NT)rrclauseZclose_with_result)rrr)rZconnr)rrstmtrrexec_for_shardCsz2ShardedQuery._execute_crud..exec_for_shard)r r rowcountappend ShardedResult)rr$rr%r'resultsrrr)rrr$r _execute_crudBs   zShardedQuery._execute_crudNc  s|dk r&tt|j||fd|i|S|j|}|r@||}x>|||D].}tt|j||fd|i|}|dk rN|SqNWdSdS)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)rr_identity_lookupr queryZ_set_lazyload_fromr ) rrprimary_key_identityrZlazy_loaded_fromkwrrobj)rrrr,Ys    zShardedQuery._identity_lookupcs<fdd}|dkr&jdk r&j}ttj|||dS)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. cs\jdk r|St|}x8|D]$}|}||}|dk r,|Sq,WdSdS)N)r rZto_listr r)r-r.Zidentrro) db_load_fnrrr _db_load_fns     z+ShardedQuery._get_impl.._db_load_fnN)r)r rr _get_impl)rr.r2rr3)r)r2rrr4{s  zShardedQuery._get_impl)NN)N) __name__ __module__ __qualname__r rr"r+r,r4 __classcell__rr)rrrs  c@s(eZdZdZdZddZeddZdS)r)aA value object that represents multiple :class:`.ResultProxy` objects. This is used by the :meth:`.ShardedQuery._execute_crud` hook to return an object that takes the place of the single :class:`.ResultProxy`. Attribute include ``result_proxies``, which is a sequence of the actual :class:`.ResultProxy` objects, as well as ``aggregate_rowcount`` or ``rowcount``, which is the sum of all the individual rowcount values. .. versionadded:: 1.3 )result_proxiesaggregate_rowcountcCs||_||_dS)N)r9r:)rr9r:rrrr szShardedResult.__init__cCs|jS)N)r:)rrrrr'szShardedResult.rowcountN)r5r6r7__doc__ __slots__r propertyr'rrrrr)s r)csFeZdZdeffdd ZddZd ddZd dd Zd d ZZ S)rNc sdtt|jfd|i|||_||_||_i|_|j|_|dk r`x|D]}| |||qHWdS)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_clsN) rrr shard_chooserr r _ShardedSession__binds connectionZconnection_callable bind_shard)rr?r r Zshardsr>rk)rrrr s zShardedSession.__init__cKs^|dk rs