ó
[³XMc           @   sd   d  Z  d d l m Z d d l m Z d d l Z e d ƒ \ Z Z Z	 d e j
 f d „  ƒ  YZ d S(   sæ   Journal using twisted.enterprise.row RDBMS support.

You're going to need the following table in your database::

    | CREATE TABLE journalinfo
    | (
    |   commandIndex int
    | );
    | INSERT INTO journalinfo VALUES (0);

iÿÿÿÿ(   t   nested_scopes(   t   deferNi   t
   RowJournalc           B   sk   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d d d d d „ Z d „  Z	 d „  Z
 d	 „  Z d
 „  Z RS(   sÅ   Journal that stores data 'snapshot' in using twisted.enterprise.row.

    Use this as the reflector instead of the original reflector.

    It may block on creation, if it has to run recovery.
    c         C   s5   | |  _  g  |  _ d |  _ t j j |  | | ƒ d  S(   Ni    (   t	   reflectort   commandst   syncingt   baset   Journalt   __init__(   t   selft   logt   journaledServiceR   (    (    sH   /usr/lib/python2.7/dist-packages/twisted/persisted/journal/rowjournal.pyR   '   s    			c         C   s   |  j  j t | f ƒ d S(   s+   Mark on object for updating when sync()ing.N(   R   t   appendt   UPDATE(   R	   t   obj(    (    sH   /usr/lib/python2.7/dist-packages/twisted/persisted/journal/rowjournal.pyt	   updateRow-   s    c         C   s   |  j  j t | f ƒ d S(   s,   Mark on object for inserting when sync()ing.N(   R   R   t   INSERT(   R	   R   (    (    sH   /usr/lib/python2.7/dist-packages/twisted/persisted/journal/rowjournal.pyt	   insertRow1   s    c         C   s   |  j  j t | f ƒ d S(   s+   Mark on object for deleting when sync()ing.N(   R   R   t   DELETE(   R	   R   (    (    sH   /usr/lib/python2.7/dist-packages/twisted/persisted/journal/rowjournal.pyt	   deleteRow5   s    i    c            s5   ˆ j  ƒ  } | j ‡  ‡ ‡ ‡ ‡ ‡ f d †  ƒ | S(   s8   Flush all objects to the database and then load objects.c      
      s(   ˆ j  j ˆ d ˆ d ˆ d ˆ d ˆ  ƒS(   Nt	   parentRowt   datat   whereClauset   forceChildren(   R   t   loadObjectsFrom(   t   result(   R   R	   t	   tableNameR   R   R   (    sH   /usr/lib/python2.7/dist-packages/twisted/persisted/journal/rowjournal.pyt   <lambda><   s   	(   t   synct   addCallback(   R	   R   R   R   R   R   t   d(    (   R   R	   R   R   R   R   sH   /usr/lib/python2.7/dist-packages/twisted/persisted/journal/rowjournal.pyR   9   s    %c         C   s×   |  j  r t d ‚ n  i |  j j t 6|  j j t 6|  j j t 6} g  } x. |  j	 D]# \ } } | j
 | | | ƒ ƒ qR Wg  |  _	 | rÆ d |  _  |  j j j |  j |  j | ƒ } | j |  j ƒ | St j d ƒ Sd S(   s   Commit changes to database.s   sync already in progressi   N(   R   t
   ValueErrorR   t   insertRowSQLR   t   updateRowSQLR   t   deleteRowSQLR   R   R   t   dbpoolt   runInteractiont   _synct   latestIndexR   t	   _syncDoneR   t   succeed(   R	   t	   comandMapt   sqlCommandst   kindR   R   (    (    sH   /usr/lib/python2.7/dist-packages/twisted/persisted/journal/rowjournal.pyR   A   s    			!c         C   s3   x | D] } | j  | ƒ q W| j d | ƒ d S(   s'   Do the actual database synchronization.s(   UPDATE journalinfo SET commandIndex = %dN(   t   executet   update(   R	   t   txnt   indexR   t   c(    (    sH   /usr/lib/python2.7/dist-packages/twisted/persisted/journal/rowjournal.pyR%   T   s    c         C   s   d |  _  | S(   Ni    (   R   (   R	   R   (    (    sH   /usr/lib/python2.7/dist-packages/twisted/persisted/journal/rowjournal.pyR'   Z   s    	c         C   s=   |  j  j j ƒ  } | j ƒ  } | j d ƒ | j ƒ  d d S(   s&   Return command index of last snapshot.s$   SELECT commandIndex FROM journalinfoi    (   R   R#   t   connectt   cursorR,   t   fetchall(   R	   t   connR2   (    (    sH   /usr/lib/python2.7/dist-packages/twisted/persisted/journal/rowjournal.pyt   getLastSnapshot^   s    N(   t   __name__t
   __module__t   __doc__R   R   R   R   t   NoneR   R   R%   R'   R5   (    (    (    sH   /usr/lib/python2.7/dist-packages/twisted/persisted/journal/rowjournal.pyR      s   							(   R8   t
   __future__R    t   twisted.internetR   R   t   rangeR   R   R   R   R   (    (    (    sH   /usr/lib/python2.7/dist-packages/twisted/persisted/journal/rowjournal.pyt   <module>   s
   