
    i                        d Z ddlmZ ddlZddlmZmZmZmZm	Z	m
Z
mZ ddlmZmZ ddlmZ erddlmZmZmZmZmZmZmZ ddlmZ dd	lmZ ddd
Z G d de      Zy)a  

This wrapper intercepts calls through the store interface and implements
thread-safe logging of destructive operations (adds / removes) in reverse.
This is persisted on the store instance and the reverse operations are
executed In order to return the store to the state it was when the transaction
began Since the reverse operations are persisted on the store, the store
itself acts as a transaction.

Calls to commit or rollback, flush the list of reverse operations This
provides thread-safe atomicity and isolation (assuming concurrent operations
occur with different store instances), but no durability (transactions are
persisted in memory and won't be available to reverse operations after the
system fails): A and I out of ACID.

    )annotationsN)TYPE_CHECKINGAny	GeneratorIteratorListOptionalTuple)ConjunctiveGraphGraph)Store)_ContextIdentifierType_ContextType_ObjectType_PredicateType_SubjectType_TriplePatternType_TripleType)Result)URIRef)addremovec                      e Zd ZddZdddZdddZddZddZ	 d	 	 	 	 	 	 	 ddZ	 d	 	 	 	 	 ddZ		 d	 	 	 	 	 dd	Z
ddd
Z	 d	 	 	 ddZdddZd dZd!dZd"dZd#dZd#dZy)$AuditableStorec                    || _         |j                  | _        d| _        d| _        g | _        t        j                         | _        y )NFT)storecontext_awareformula_awaretransaction_aware
reverseOps	threadingRLockrollbackLock)selfr   s     NC:\Projects\mas-dev\.venv\Lib\site-packages\rdflib/plugins/stores/auditable.py__init__zAuditableStore.__init__/   sF    
"00 #!%  	 &OO-    c                :    | j                   j                  ||      S N)r   open)r$   configurationcreates      r%   r*   zAuditableStore.openA   s    zz}f55r'   c                8    | j                   j                          y r)   )r   close)r$   commit_pending_transactions     r%   r.   zAuditableStore.closeD   s    

r'   c                :    | j                   j                  |       y r)   )r   destroy)r$   r+   s     r%   r1   zAuditableStore.destroyG   s    

=)r'   c                :     | j                   j                  |i |S r)   )r   query)r$   argskws      r%   r3   zAuditableStore.queryJ   s    tzz,,,r'   c                2   |\  }}}t         d   }|r|nt        j                         }|5  |&|j                  | j                  |j
                        nd }||j
                  nd }t        | j                  j                  ||            r
	 d d d        y | j                  j                  ||||df       	 | j                  j                  ||||df       | j                  j                  |||f||       d d d        y # t        $ r Y 4w xY w# 1 sw Y   y xY w)Nr   r   )destructiveOpLocksr!   r"   	__class__r   
identifierlisttriplesr    appendr   
ValueErrorr   )	r$   triplecontextquotedspolockctxIds	            r%   r   zAuditableStore.addM   s    	Aq!%(t!2 & !!$**g.@.@A 
 +2*=G&&4EDJJ&&vw78 T OO""Aq!UH#=>&&1a'>? JJNNAq!9gv6 T   Ts6   A!D D5 C> D>	D
D	D

DDNc           
     B   |\  }}}t         d   }|r|nt        j                         }|5  |&|j                  | j                  |j
                        nd }||j
                  nd }d ||||fv r|r?|j                  |||f      D ]'  \  }}	}
	 | j                  j                  ||	|
|df       ) nt        | j                        j                  |||f      D ]2  \  }}	}
}	 | j                  j                  ||	|
|j
                  df       4 nIt        | j                  |||f|            s
	 d d d        y 	 | j                  j                  ||||df       | j                  j                  |||f|       d d d        y # t        $ r$ | j                  j                  ||	|
|df       Y %w xY w# t        $ r. | j                  j                  ||	|
|j
                  df       Y w xY w# t        $ r# | j                  j                  ||||df       Y w xY w# 1 sw Y   y xY w)Nr   r   )r7   r!   r"   r8   r   r9   r;   r    r   r=   r<   r   quadsr:   )r$   spor?   subject	predicateobject_rD   rE   rA   rB   rC   ctxs               r%   r   zAuditableStore.removec   s+    '*#G!(+t!2
 & !!$**g.@.@A 
 +2*=G&&4EGW==#*??GY3P#Q1aL OO22Aq!UH3MN $R )9(D(J(J )W5)1aU OO22Aq!S^^X3VW) DLL'9g)FPQ; T<XOO** )WeXF
 JJw	7;WEI T  * L OO22Aq!UE3JKL  * U OO22Aq!S^^U3STU " XOO**GYPU+VWXE Tsx   A H E<+2H*F,"H4 G&H<)F)%H(F))H,3G#H"G##H&)HHHHHc              #     K   |\  }}}|&|j                  | j                  |j                        nd }| j                  j                  |||f|      D ]  \  \  }}}}	|||f|	f  y wr)   )r8   r   r9   r;   )
r$   r>   r?   suprobrA   rB   rC   cgs
             r%   r;   zAuditableStore.triples   s}      R " djj'*<*<= 	
 "ZZ//RgFMIQ1ra)R- Gs   A&A(c                    |&|j                  | j                  |j                        nd }| j                  j                  |      S r)   )r8   r   r9   __len__)r$   r?   s     r%   rS   zAuditableStore.__len__   sE     " djj'*<*<= 	
 zz!!'**r'   c              #  T   K   | j                   j                  |      D ]  }|  y wr)   )r   contexts)r$   r>   rL   s      r%   rU   zAuditableStore.contexts   s&      ::&&v.CI /s   &(c                @    | j                   j                  |||       y )N)override)r   bind)r$   prefix	namespacerW   s       r%   rX   zAuditableStore.bind   s    

	H=r'   c                8    | j                   j                  |      S r)   )r   rY   )r$   rZ   s     r%   rY   zAuditableStore.prefix   s    zz  ++r'   c                8    | j                   j                  |      S r)   )r   rZ   )r$   rY   s     r%   rZ   zAuditableStore.namespace   s    zz##F++r'   c                6    | j                   j                         S r)   )r   
namespacesr$   s    r%   r^   zAuditableStore.namespaces   s    zz$$&&r'   c                    g | _         y r)   )r    r_   s    r%   commitzAuditableStore.commit   s	    r'   c           	     Z   | j                   5  | j                  D ]t  \  }}}}}|dk(  r4| j                  j                  |||ft	        | j                  |             B| j                  j                  |||ft	        | j                  |             v g | _        d d d        y # 1 sw Y   y xY w)Nr   )r#   r    r   r   r   r   )r$   rI   rJ   objr?   ops         r%   rollbackzAuditableStore.rollback   s     8<4C";JJNN )S15W3M JJ%% )S15W3M 9H !DO s   BB!!B*)r   r   )T)r+   strr,   boolreturnzOptional[int])F)r/   rg   rh   None)r+   rf   rh   ri   )r4   r   r5   r   rh   r   )r>   r   r?   r   r@   rg   rh   ri   r)   )rH   r   r?   Optional[_ContextType]rh   ri   )r>   r   r?   rj   rh   z>Iterator[Tuple[_TripleType, Iterator[Optional[_ContextType]]]])r?   rj   )r>   zOptional[_TripleType]rh   z#Generator[_ContextType, None, None])rY   rf   rZ   r   rW   rg   rh   ri   )rZ   r   rh   zOptional[str])rY   rf   rh   zOptional[URIRef])rh   zIterator[Tuple[str, URIRef]])rh   ri   )__name__
__module____qualname__r&   r*   r.   r1   r3   r   r   r;   rS   rU   rX   rY   rZ   r^   ra   re    r'   r%   r   r   .   s    .$6*- JO7!7,87BF7	7. JN*F%*F0F*F	*FZ MQ
 (
 3I
 	G
 + /3+	,>,,'!r'   r   )__doc__
__future__r   r!   typingr   r   r   r   r   r	   r
   rdflib.graphr   r   rdflib.storer   r   r   r   r   r   r   r   rdflib.queryr   rdflib.termr   r7   r   rn   r'   r%   <module>rv      sY   " #  Q Q Q 0    $"  Y!U Y!r'   