m TEc@sdZdkZdkZdkZdkZdkZdklZdklZdk l Z dk l Z dk lZdk lZdk lZd klZdkZd klZd klZd klZd klZlZdklZlZdkl Z l!Z!l"Z"dk#l$Z$l%Z%l&Z&l'Z'l(Z(da)dZ*dee+fdYZ,dfdYZ-dfdYZ.dS(sPDatabase connection support $Id: Connection.py 69549 2006-08-15 23:35:21Z jim $N(stime(s PickleCache(sIPersistentDataManager(s IConnection(sISavepointDataManager(sIDataManagerSavepoint(s ISynchronizer(s implements(sResolvedSerial(s ExportImport(s POSException(sInvalidObjectReferencesConnectionStateError(s ConflictErrorsReadConflictError(s ObjectWriters ObjectReaders myhasattr(sp64su64sz64soid_reprs positive_idicCstd7adS(sCauses all connection caches to be reset as connections are reopened. Zope's refresh feature uses this. When you reload Python modules, instances of classes continue to use the old class definitions. To use the new code immediately, the refresh feature asks ZODB to clear caches by calling resetCaches(). When the instances are loaded by subsequent connections, they will use the new class definitions. iN(tglobal_reset_counter(((t./data/zmath/zope/lib/python/ZODB/Connection.pyt resetCaches/s t ConnectioncBsItZdZeeeeedZdddZ dZ dZ dZ dZ eZd Zed Zd Zd Zd ZdZdZdZdZdZdZdZdZedZdZ dZ!dZ"dZ#dZ$dZ%dZ&dZ'edd Z(d!Z)d"Z*ed#Z+d$Z,d%Z-d&Z.d'Z/d(Z0e0Z1e0Z2d)Z3d*Z4d+Z5d,Z6d-Z7d.Z8d/Z9ed0Z:d1Z;eeeed2Z<d3Z=d4Z>e Z?d5Z@d6ZAd7ZBd8ZCd9ZDd:ZERS(;s3Connection to ZODB for loading and storing objects.iticCsu||_|i|_|_|ii|_d|_d|_|_ |_ t i d|_ f|_d|_||_t|||_}|od|i_ng|_h|_d|_t|_d|_d|_h|_g|_g|_ t!|_"t#i$|_%h|_&t'|_(h|_)d|_*d|_+t,||i|ii-|_.h|ii/|<|_0dS(sCreate a new Connection.sZODB.ConnectionidiN(1tdbtselft_dbt_storaget_normal_storagetnew_oidtNonet_savepoint_storagettransaction_managert_syncht_mvcctloggingt getLoggert_logt _debug_infot_openedtversiont_versiont PickleCachet cache_sizet_cachetcachetcache_drain_resistancet _committedt_addedt_added_during_commitRt_reset_countert _load_countt _store_countt _creatingt _modifiedt_registered_objectstTruet_needs_to_joint threadingtLockt _inv_lockt _invalidatedtFalset_invalidatedCachet _conflictst _txn_timet_importt ObjectReadert classFactoryt_readert database_namet connections(RRRRR((Rt__init__Js>                   cCs |idjotdnt}t|d|}||jot d|n|i djox|i djpt |i i}|_ ||_ |idj o|ii|n|i|||i|t _p_changedt _p_serial(RR<R;RFRG((RRDs"       cCs|iidS(s/Deactivate all unmodified objects in the cache.N(RRtminimize(R((Rt cacheMinimizescCs|iidS(s!Reduce cache size to target size.N(RRtincrgc(R((RtcacheGCscCs1|idjo g|_n|ii|dS(s0Register a callable, f, to be called by close().N(Rt_Connection__onCloseCallbacksR R@tf(RRP((RtonCloseCallbacks cCsV|iptdn|idj o|iin|idj ogxW|iD]L}y |WqUt|d|}|i i d|dt i qUXqUWd|_nf|_ |io|ii|d|_n|o`x5|iiD]$}||j o|itqqW|idj o|ii|qRn d|_dS(sClose the Connection.s1Cannot close a connection joined to a transactiontim_selfsClose callback failed for %stexc_infoN(RR&R7RR RMRORPR:RterrortsysRSRRR tunregisterSynchtprimaryR4tvaluest connectiontcloseR+RRt _returnToPool(RRWRPRY((RRZs6        cCs|iS(s<Returns a handle to the database this connection belongs to.N(RR(R((RRscCs-|idjotdn|iiS(s=Returns True if the storage for this connection is read only.s!The database connection is closedN(RRR R7Rt isReadOnly(R((RR\scCsS|iiz1|idjo ||_n|ii|Wd|ii XdS(s>Notify the Connection that transaction 'tid' invalidated oids.N( RR)tacquireR.R ttidR*tupdatetoidstrelease(RR^R`((Rt invalidates  cCs/|iiz t|_Wd|iiXdS(N(RR)R]R%R,Ra(R((RtinvalidateCache&s   cCs |itS(s Return the database root object.N(RRDtz64(R((Rtroot.scCs'|idjotdn|iS(s3Returns the version this connection is attached to.s!The database connection is closedN(RRR R7R(R((Rt getVersion2sc Cs|ii|}|djoc|ii|id|i d|i d|i d|i }|ii|i|i|_|}n|S(s+Return a Connection for the named database.R tmvccRtsynchN(RR4RDR3RYR Rt databasestopenR RRRtnew_conR_(RR3RYRk((Rtget_connection8s !  cCs<|ii|dp&|idj o|iii|dS(sAre we implicitly adding an object within the current transaction This is used in a check to avoid implicitly adding an object to a database in a multi-database situation. See serialize.ObjectWriter.persistent_id. iN(RR"RDR<R R tcreating(RR<((Rt_implicitlyAddingEscCs|ii|idS(s)Manually update the view on the database.N(RR tabortt _storage_sync(R((RtsyncUs cCs|iS(sSReturns a tuple with different items for debugging the connection. N(RR(R((Rt getDebugInfoZscGs|i||_dS(s@Add the given items to the debug information of this connection.N(RRtargs(RRs((Rt setDebugInfo`scCs3|i|if}|od|_d|_n|S(s0Returns the number of objects loaded and stored.iN(RR R!trestclear(RRvRu((RtgetTransferCountsds   cCs6|i|idj o|in|idS(s+Abort a transaction and forget all changes.N(Rt_abortR R t_abort_savepointt _tpc_cleanup(Rt transaction((RRors  cCsoxh|iD]]}|i}|dj pt||ijo|i|=|`|`q |i i |q WdS(s+Abort a transaction and forget all changes.N( RR$R;R6R<R R?RR>RRb(RR<R;((RRxs    cCsH|ii|ip|int|_g|_|iidS(s@Performs cleanup operations to support tpc_finish and tpc_abort.N( RR-RvRt_flush_invalidationsR%R&R$R"(R((RRzs    cCs|iizH|i}h|_d|_|iot|_|i i i }nWd|ii X|i i ||i idS(N(RR)R]R*t invalidatedR R.R,R+Rt cache_datatcopyRaRbRM(RR}((RR|s      cCs*g|_|ii|ii|dS(s=Begin commit of a transaction, starting the two-phase commit.N(RR#R"RvR t tpc_beginR{(RR{((RRs  cCs<|idj o|i|i|n|i|dS(sCommit changes to an objectN(RR R t savepointt_commit_savepointR{t_commit(RR{((Rtcommits  cCs|io |i||id|_ng|_|io tnx|iD]}|i }|pt ||i jotd|n|i|j ot||in||ijo|itjpt nh|ioZ||ijo6t|dd}|djotd|q5n|ii|nqT|it||qTWx'|iD]}|it||qpWd|_dS(sCommit changes to an objectR8t_p_resolveConflictN(RR/t_importDuringCommitR{R RR,t ConflictErrorR$R;R6R<R?R-tReadConflictErrorR>RBRRJRdRIR*R:tresolveR#R@t_store_objectst ObjectWriter(RR{R;R<R((RRs:          c Cs>x7|D]/}|i}t|dt}|tjo,|ii |ddj}||i |Rz(RR{R;R<((RRvs     cCsq|djo|i}h|_nxG|D]?}|ii|}|dj o|i|=|`|` q*q*WdS(s=Disown any objects newly saved in an uncommitted transaction.N( RmR RR"R<RRDtoR>R6(RRmR<R((RRs     cCsGy|ii}Wntj o dSnX||}|i|dS(s6Verify that a data manager can commit the transaction.N(RRttpc_votetvotetAttributeErrorR{RR(RR{RR((RRs  cs-d}ii||idS(s3Indicate confirmation that the transaction is done.cs,tii}ii||dS(N(tdicttfromkeysRR#tdRRbR^(R^R(R(RtcallbacksN(RRRt tpc_finishR{Rz(RR{R((RRRs cCsd|iit|fS(s1Return a consistent sort key for this connection.s%s:%sN(RRtsortKeytid(R((RRscCsdS(N((Rttxn((RtbeforeCompletionscGs5t|idd}|o |n|idS(NRqi(R:RRRqR|(RtignoredRq((RRps cCs?|i|jpt|ii|i|}|i i |S(s;Return copy of 'obj' that was written by transaction 'tid'.N( R;R>RR?Rt loadSerialR6R^RFR2tgetState(RR;R^RF((RtoldstatescCs|i}|idjo0dt|}|ii |t |ny|i |WnDt j o n0|ii dt|dt inXdS(sSTurns the ghost 'obj' into a real object by loading it's from the database.s9Shouldn't load state for %s when the connection is closedsCouldn't load state for %sRSN(R;R6R<RRR toid_reprtmsgRRTR7t _setstateRRURS(RR;R<R((Rtsetstates cCs|io tn|i|ijo&t|d o|i|dSn|ii |i|i \}}|i d7_ |iiz|i|ij}Wd|iiX|o6t|do|i|q|i|dSn|ii||||_dS(Nt_p_independenti(RR,RR;R6R*t myhasattrt_load_before_or_conflictRRERRFRGR R)R]tinvalidRat_handle_independentR2t setGhostStateRJ(RR;RRFRG((RRs&  $ !  cCsN|io |i|p0|i|t|i|iRR?R6R t ValueErrorRRA(RR;((Rtregister\scCsR|io#|iii|t|_n|dj o|ii |ndS(N( RR&R RDtjoinR+R;R R$R@(RR;((RRAps    cCsG|ii}|ii}x|D]\}}||=q"W|i|S(N(RRt lru_itemstitemsR~t everythingtktv(RRRRR((Rt _cache_itemss    cCst|_||_|o|i |_|djo t i }n||_|i t jo|in |i|o|i|n|idj o|iin|oExB|iiD]-}||j o|i|||tqqWndS(sRegister odb, the DB that this Connection uses. This method is called by the DB every time a Connection is opened. Any invalidations received while the Connection was closed will be processed. If the global module function resetCaches() was called, the cache will be cleared. Parameters: odb: database that owns the Connection mvcc: boolean indicating whether MVCC is enabled transaction_manager: transaction manager to use. None means use the default transaction manager. synch: boolean indicating whether Connection should register for afterCompletion() calls. N(ttimeRRRhRRgRRR R R{tmanagerRRt _resetCacheR|t registerSynchRRMtdelegateR4RXRYRjR+(RR RgRhRRY((RRjs&       cCsEt|_|iit|_|ii}t |||_}dS(spCreates a new cache, discarding the old one. See the docstring for the resetCaches() function. N( RRRR*RvR+R,RRRR(RRR((RRs     cCs6|iod|i }nd}dt||fS(Ns (in version %s)Rs(RRtvert positive_id(RR((Rt__repr__s cCs8y|ii|SWntj o|iSnXdS(sReturns the version the object with the given oid was modified in. If it wasn't modified in a version, the current version of this connection is returned. N(RRtmodifiedInVersionR<RRf(RR<((RRs cCsB|i}||_||_d|_|i|||i|RIRAR(RRRR<((Rtexchanges      cCs|idjo(t|i|i|_|i|_n|ii|i d|ii i |i|iig|_ |ii |iiif}t||}|i|S(N(RR R tTmpStoreRR RR"RvRRmR_R$tpositiontindexRtstatet SavepointtresultRN(RRR((RRs      cCs@|ig|_|i}|ii|i|i|dS(N( RRxR$RtsrcRRbRtresetR(RRR((Rt _rollbacks    cCs|i}|i|_d|_|iid|i|i i }|i i ||ii|ix]|D]U}|i||\}}|ii||||i|}|i||dtqpW|idS(sLCommit all changes made in subtransactions and begin 2-phase commit sCommiting savepoints of size %sRN(RR RR RR RtdebugtgetSizeRtkeysR`R#textendR"R_RmR<RERRGRRR{RRR+RZ(RR{RR<R`RRGR((RRs   cCsO|i}|i|_d|_|ii|i|i |i |i dS(s Discard all subtransaction data.N( RR RR RR RRbRRRmRZ(RR((RRy5s   (Ft__name__t __module__t__doc__t implementst IConnectiontISavepointDataManagertIPersistentDataManagert ISynchronizert_code_timestampR5RCRDRLRNR RORQR%RZRR\RbRcReRfRlRnRqRrRtR+RwRoRxRzR|RRRRRRRRRRRRRptafterCompletiontnewTransactionRRRRRRRRARRjRRt __getitem__RRRRRRy(((RR<s ^      +            ! -  1 ' !      5      4      RcBs$tZeedZdZRS(NcCs||_||_dS(N(t datamanagerRR(RRR((RR5Vs cCs|ii|idS(N(RRRR(R((RtrollbackZs(RRRtIDataManagerSavepointR5R(((RRRs  RcBsDtZdZdZdZdZdZdZdZRS(s+A storage-like thing to support savepoints.cCsj||_x'd D]}t||t||qW||_ti |_ d |_ h|_ h|_ dS( NtgetNameR RRtundoLogt versionEmptyRRl(Rsnew_oidsmodifiedInVersionsgetSizeRRssortKeys loadBefore(tstorageRRtmethodtsetattrR:t base_versiont _base_versionttempfilet TemporaryFilet_fileRRRm(RRRR((RR5`s    cCs t|iS(N(tlenRR(R((Rt__len__qscCs|iidS(N(RRRZ(R((RRZtsc Cs|ii|}|djo|ii||iSn|i i ||i i d}t |}|i i |}||jotidn|i i d}t |d}|d }|i i ||fS(NisBad temporary storagei(RRRDR<tposR RRERRtseektreadthRtoidlentread_oidt POSExceptiontStorageSystemErrortsizeRG( RR<RRRRRRGR((RREws    cCs||ijpt|ii|it|}|djo t }nt t|||t |}|ii||ii||i|i|<|i|t|7_|S(N(RRRR?RRRRRtlRGR Rdtp64R<theadertwriteR(RR<RGRRR{RR ((RRs   $cCs,|ii|||_|i|_dS(N(RRttruncateRRR(RRR((RRs ( RRRR5RRZRERR(((RR]s      (/RRRURR'twarningsRt persistentRtpersistent.interfacesRtZODB.interfacesRttransaction.interfacesRRRtzope.interfaceRR{tZODB.ConflictResolutionRtZODB.ExportImportt ExportImporttZODBRtZODB.POSExceptionRBR7RRtZODB.serializeRR0Rt ZODB.utilsR RRdRRRRR8RRR(!RR RRRRRRRRBRRdRR RRRRURRR0RRR{RRR'RRR7RRR((Rt?s<                 %