mò
a&Gc           @   s   d  Z  d k l Z d k l Z d k l Z d k Z d f  d     YZ d e f d     YZ	 e   Z
 e
 i Z e
 i Z e
 i Z e
 i Z d S(	   sæ  
TMRegistry and ProxyTM

Use case
--------

Persistent objects (ExtFiles) need to participate in ZODB transactions.
ExtFiles perform all operations using temporary files which are saved on
commit or deleted on abort.

Constraints
-----------

- TransactionManagers (TM) must not be persistent themselves, i.e. must
  not have a _p_jar attribute.

- We have to make sure the ExtFile's _abort is called *before* the ZODB
  destroys the attributes of the persistent object.

Solution
--------

- ProxyTM is a subclass of TM.TM that keeps a (hard) reference to the
  (wrapped) persistent object it manages. Calls to _begin, _finish, and
  _abort are forwarded to the target object.

- TMRegistry is a module-level container for ProxyTMs. It creates and
  holds ProxyTMs keyed by (target_id, thread_id).

- ExtFiles implement _finish and _abort and register with the machinery
  by calling TM.register(self).

- On commit (or abort) the ProxyTM notifies its target object and removes
  itself from the registry.

Hacks
-----

- We manipulate the transaction's _resources attribute directly. This is
  to guarantee the ProxyTM is processed before other resources. There may
  be a way to achieve this using official APIs only, but I can't seem to
  find one.

(   s   TM(   s   aq_base(   s	   get_identNt
   TMRegistryc           B   sP   t  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 RS(	   Nc         C   s   h  |  _ d  S(   N(   t   selft   _tms(   R   (    (    t9   /home2/kennisonb/webapps/zope/Zope/Products/ExtFile/TM.pyt   __init__6   s    c         C   s   t  t |   t   f S(   N(   t   idt   aq_baset   targett	   get_ident(   R   R   (    (    R   t   getid9   s    c         C   sN   |  i |  p: |  i |  } t |  } | i   | |  i | <d Sn d S(   Ni   i    (	   R   t   containsR   R	   R   t   ProxyTMt   tmt	   _registerR   (   R   R   R   R   (    (    R   t   register<   s    
c         C   s5   |  i |  o! |  i |  } |  i | =d Sn d S(   Ni   i    (   R   R
   R   R	   R   R   (   R   R   R   (    (    R   t   removeE   s
    
c         C   s   |  i |  } |  i i |  S(   N(   R   R	   R   R   R   t   has_key(   R   R   R   (    (    R   R
   L   s    c         C   s   |  i |  } |  i i |  S(   N(   R   R	   R   R   R   t   get(   R   R   R   (    (    R   R   P   s    c         C   s   t  |  i  S(   N(   t   lenR   R   (   R   (    (    R   t   __len__T   s    c         C   s
   t  |   S(   N(   R   R   (   R   (    (    R   t   countW   s    (
   t   __name__t
   __module__R   R	   R   R   R
   R   R   R   (    (    (    R   R    4   s   								R   c           B   s5   t  Z d   Z d   Z d   Z d   Z d   Z RS(   Nc         C   s   | |  _ d  S(   N(   R   R   t   _target(   R   R   (    (    R   R   ]   s    c         C   sS   t  i |   t i   } t | d  o& | i i   } | i i
 d |  n d  S(   Nt
   _resourcesi    (   t   TMR   R   t   transactionR   t   tt   hasattrR   t   popt   rt   insert(   R   R   R   (    (    R   R   `   s
    c         C   s   |  i i   S(   N(   R   R   t   _begin(   R   (    (    R   R    i   s    c         C   s(   t  i |  i  o |  i i   Sn d  S(   N(   t   registryR   R   R   t   _finish(   R   (    (    R   R"   l   s    c         C   s(   t  i |  i  o |  i i   Sn d  S(   N(   R!   R   R   R   t   _abort(   R   (    (    R   R#   p   s    (   R   R   R   R   R    R"   R#   (    (    (    R   R   [   s
   					(   t   __doc__t   Shared.DC.ZRDB.TMR   t   AcquisitionR   t   threadR   R   R    R   R!   R   R   R
   R   (   R   R   R   R   R   R   R   R   R!   R
   R    (    (    R   t   ?,   s   	'				