σ ΓΜ4]c@s˜dZddlmZddlmZddlmZddlmZddgZdefd„ƒYZ d e fd „ƒYZ defd „ƒYZ d S( sHorizontal 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. i(tinspect(tutil(tQuery(tSessiontShardedSessiont ShardedQuerycBsGeZd„Zd„Zd„Zd„Zddd„Zdd„ZRS(cOsDtt|ƒj||Ž|jj|_|jj|_d|_dS(N(tsuperRt__init__tsessiont id_choosert query_choosertNonet _shard_id(tselftargstkwargs((sR/opt/alt/python27/lib64/python2.7/site-packages/sqlalchemy/ext/horizontal_shard.pyRscCs|jƒ}||_|S(sΉ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. (t_cloneR (R tshard_idtq((sR/opt/alt/python27/lib64/python2.7/site-packages/sqlalchemy/ext/horizontal_shard.pyt set_shard"s  cs‹‡‡fd†}ˆjdk r.|ˆjƒSˆjdk rJ|ˆjƒSg}x*ˆjˆƒD]}|j||ƒƒq`Wt|ƒSdS(NcsT|ˆjd<ˆ_ˆjdˆjƒd|ƒjˆjˆjƒ}ˆj|ˆƒS(NRtmapper(t attributestidentity_tokent_connection_from_sessiont _bind_mappertexecutet statementt_paramst instances(Rtresult(tcontextR (sR/opt/alt/python27/lib64/python2.7/site-packages/sqlalchemy/ext/horizontal_shard.pytiter_for_shard.s  (RR R R textendtiter(R RRtpartialR((RR sR/opt/alt/python27/lib64/python2.7/site-packages/sqlalchemy/ext/horizontal_shard.pyt_execute_and_instances-s  csއ‡‡fd†}ˆjdk r1|ˆjƒSd}g}x=ˆjˆƒD],}||ƒ}||j7}|j|ƒqMWt||ƒSdS(Nc s=ˆjdˆd|dˆdtƒ}|jˆˆjƒ}|S(NRRtclausetclose_with_result(RtTrueRR(RtconnR(RR tstmt(sR/opt/alt/python27/lib64/python2.7/site-packages/sqlalchemy/ext/horizontal_shard.pytexec_for_shardCs  i(R R R trowcounttappendt ShardedResult(R R(RR)R*tresultsRR((RR R(sR/opt/alt/python27/lib64/python2.7/site-packages/sqlalchemy/ext/horizontal_shard.pyt _execute_crudBs    c Ks±|dk r.tt|ƒj||d||S|jj|ƒ}|rX|j|ƒ}nxN|j||ƒD]:}tt|ƒj||d||}|dk rk|SqkWdSdS(sΏoverride 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). RN(R RRt_identity_lookupRtqueryt_set_lazyload_fromR ( R Rtprimary_key_identityRtlazy_loaded_fromtkwRRtobj((sR/opt/alt/python27/lib64/python2.7/site-packages/sqlalchemy/ext/horizontal_shard.pyR/Ys  csX‡‡fd†}|dkr9ˆjdk r9ˆj}nttˆƒj||d|ƒS(s©Override 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ˆˆ|ƒStj|ƒ}xLˆjˆ|ƒD]4}ˆj|ƒ}ˆ||ƒ}|dk r>|Sq>WdSdS(N(R R Rtto_listR R(R0R2tidentRRto(t db_load_fnR (sR/opt/alt/python27/lib64/python2.7/site-packages/sqlalchemy/ext/horizontal_shard.pyt _db_load_fn‚s  RN(R R RRt _get_impl(R R2R9RR:((R9R sR/opt/alt/python27/lib64/python2.7/site-packages/sqlalchemy/ext/horizontal_shard.pyR;{s  N( t__name__t __module__RRR#R.R R/R;(((sR/opt/alt/python27/lib64/python2.7/site-packages/sqlalchemy/ext/horizontal_shard.pyRs    R,cBs,eZdZdZd„Zed„ƒZRS(sΪA 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 tresult_proxiestaggregate_rowcountcCs||_||_dS(N(R>R?(R R>R?((sR/opt/alt/python27/lib64/python2.7/site-packages/sqlalchemy/ext/horizontal_shard.pyR«s cCs|jS(N(R?(R ((sR/opt/alt/python27/lib64/python2.7/site-packages/sqlalchemy/ext/horizontal_shard.pyR*―s(R>R?(R<R=t__doc__t __slots__RtpropertyR*(((sR/opt/alt/python27/lib64/python2.7/site-packages/sqlalchemy/ext/horizontal_shard.pyR,œs  cBsMeZded„Zd„Zdddd„Zdddd„Zd„ZRS(cKs„tt|ƒjd||||_||_||_i|_|j|_|dk r€x%|D]}|j |||ƒq_WndS(sConstruct 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. t query_clsN( RRRt shard_chooserR R t_ShardedSession__bindst connectiontconnection_callableR t bind_shard(R RDR R tshardsRCRtk((sR/opt/alt/python27/lib64/python2.7/site-packages/sqlalchemy/ext/horizontal_shard.pyR΅s       cKsˆ|dk rWt|ƒ}|jrD|jd}|dk s@t‚|S|jrW|jSn|j|||}|dk r„||_n|S(Ni(R RtkeytAssertionErrorRRD(R RtinstanceR4tstatettokenR((sR/opt/alt/python27/lib64/python2.7/site-packages/sqlalchemy/ext/horizontal_shard.pyt_choose_shard_and_assignέs        cKsl|dkr!|j||ƒ}n|jdk rF|jj|d|ƒS|j|d|d|ƒj|SdS(NRRM(R RPt transactionRFtget_bindt_contextual_connect(R RRMRR((sR/opt/alt/python27/lib64/python2.7/site-packages/sqlalchemy/ext/horizontal_shard.pyRFμs cKs2|dkr'|j||d|ƒ}n|j|S(NR$(R RPRE(R RRRMR$R4((sR/opt/alt/python27/lib64/python2.7/site-packages/sqlalchemy/ext/horizontal_shard.pyRRχs cCs||j|s 