
    -i                         d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	m
Z
mZ  G d d          Zd Z G d	 d
e          ZdS )    )defaultdictwraps)
MethodType)cdb_environment)PreconditionsExceptionRevConflictDatabasec                   :    e Zd ZddZd Zd Zedd            ZdS )th_safe_genNc                 0    || _         || _        || _        d S N)lock_th_safe_gen__genname)selfr   genls       _/var/www/html/speakWrite/venv/lib/python3.11/site-packages/codernitydb3/database_safe_shared.py__init__zth_safe_gen.__init__   s    	
			    c                 6    t          | j        | j                  S r   )r   r   r   r   s    r   __iter__zth_safe_gen.__iter__!   s    49dj111r   c                 l    | j         5  t          | j                  cd d d            S # 1 swxY w Y   d S r   )r   nextr   r   s    r   __next__zth_safe_gen.__next__$   s{    Y 	$ 	$
##	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$s   )--c                 H     t                      fd            }|S )Nc                  B     | i |}t          dz   z   |          S N_)r   )argskwargsres
index_namer   	meth_namemethods      r   _innerz#th_safe_gen.wrapper.<locals>._inner*   s3    &$)&))CzC/);S!DDDr   r   )r'   r%   r&   r   r(   s   ```` r   wrapperzth_safe_gen.wrapper(   sM    	v	E 	E 	E 	E 	E 	E 	E 
	E r   r   )__name__
__module____qualname__r   r   r   staticmethodr)    r   r   r   r      sf           
2 2 2$ $ $    \  r   r   c                 @     t                      fd            }|S )Nc                  L    5   | i |cd d d            S # 1 swxY w Y   d S r   r.   )r"   r#   r   r'   s     r   r(   zsafe_wrapper.<locals>._inner3   s     	+ 	+64*6**	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+s   r   )r'   r   r(   s   `` r   safe_wrapperr1   2   s9    
6]]+ + + + + ]+ Mr   c                        e Zd Z fdZd Zd Zd Z fdZ fdZ fdZ	 fdZ
 fd	Z fd
Z fdZ fdZ fdZ fdZ fdZ fdZ fdZ fdZ fdZd Zd Z xZS )SafeDatabasec                      t          t          |           j        |g|R i | t          t          d                   | _        t	          d                     | _        t	          d                     | _        i | _        d S N	rlock_obj)	superr3   r   r   r   indexes_locksclose_open_lock	main_lockid_revs)r   pathr"   r#   	__class__s       r   r   zSafeDatabase.__init__<   su    *lD!!*4A$AAA&AAA()EFF.{;==(577r   c                    | j         |         }dD ]v}t          ||          }t          ||dz   d           r d S t                              |||| j        |                   }t          |||           t          ||dz   |           wd S )N)allget_many_orig)indexes_namesgetattrr   r)   r8   setattr)r   r   indcmm_fixeds         r   __patch_index_genszSafeDatabase.__patch_index_gensC   s     &$ 	) 	)AQAsAK.. !))!T1d6H6NOOGCG$$$CWa((((	) 	)r   c           	         | j         |         }| j        |         }t          |          D ][}t          ||          }|                    d          s4t          |t                    rt          ||t          ||                     \|j	        }t          |          D ][}t          ||          }|                    d          s4t          |t                    rt          ||t          ||                     \d S r    )
rB   r8   dirrC   
startswith
isinstancer   rD   r1   storage)r   r   rE   r   currmethstors          r   __patch_index_methodsz"SafeDatabase.__patch_index_methodsM   s     &!$'HH 	= 	=D3%%D??3'' =JtZ,H,H =T<d#;#;<<<{II 	> 	>D4&&D??3'' >JtZ,H,H >dLt$<$<===	> 	>r   c                 Z    |                      |           |                     |           d S r   )"_SafeDatabase__patch_index_methods_SafeDatabase__patch_index_gens)r   r   s     r   __patch_indexzSafeDatabase.__patch_indexZ   s0    ""4(((%%%%%r   c                     | j         5   t          t          |           j        |i |}| j                                        D ]!\  }}t          d                     | j        |<   "|cd d d            S # 1 swxY w Y   d S r5   )r9   r7   r3   
initializerB   itemsr   r8   r   r"   r#   r$   r   r!   r=   s         r   rX   zSafeDatabase.initialize^   s    ! 	 	6%d++6GGGC-3355 J Ja+:;+G+I+I"4((		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   AA33A7:A7c                 *   | j         5   t          t          |           j        |i |}| j                                        D ]6\  }}t          d                     | j        |<   |                     |           7|cd d d            S # 1 swxY w Y   d S r5   )	r9   r7   r3   openrB   rY   r   r8   _SafeDatabase__patch_indexrZ   s         r   r\   zSafeDatabase.opene   s    ! 	 	0%d++0$A&AAC-3355 ) )a+:;+G+I+I"4(""4((((	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	   A2BBBc                 *   | j         5   t          t          |           j        |i |}| j                                        D ]6\  }}t          d                     | j        |<   |                     |           7|cd d d            S # 1 swxY w Y   d S r5   )	r9   r7   r3   createrB   rY   r   r8   r]   rZ   s         r   r`   zSafeDatabase.createm   s    ! 	 	2%d++2DCFCCC-3355 ) )a+:;+G+I+I"4(""4((((	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	r^   c                     | j         5  t          t          |                                           cd d d            S # 1 swxY w Y   d S r   )r9   r7   r3   closer   r=   s    r   rb   zSafeDatabase.closeu   s    ! 	5 	5t,,2244	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5   '=AAc                     | j         5  t          t          |                                           cd d d            S # 1 swxY w Y   d S r   )r9   r7   r3   destroyrc   s    r   rf   zSafeDatabase.destroyy   s    ! 	7 	7t,,4466	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7rd   c                     | j         5   t          t          |           j        |i |}| j        r1t          d                     | j        |<   |                     |           |cd d d            S # 1 swxY w Y   d S r5   )r:   r7   r3   	add_indexopenedr   r8   r]   r   r"   r#   r$   r=   s       r   rh   zSafeDatabase.add_index}   s    ^ 	 	5%d++5tFvFFC{ (*9+*F*H*H"3'""3'''	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	   AA00A47A4c                     | j         |j                 5  t          t          |                               ||||           d d d            d S # 1 swxY w Y   d S r   )r8   r   r7   r3   _single_update_index)r   indexdatadb_datadoc_idr=   s        r   rm   z!SafeDatabase._single_update_index   s    
+ 	K 	K, ,,UD'6JJJ	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K   ,AAAc                     | j         |j                 5  t          t          |                               ||||           d d d            d S # 1 swxY w Y   d S r   )r8   r   r7   r3   _single_delete_index)r   rn   ro   rq   old_datar=   s        r   rt   z!SafeDatabase._single_delete_index   s    
+ 	L 	L, ,,UD&(KKK	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	Lrr   c                     | j         5   t          t          |           j        |i |}| j        r1t          d                     | j        |<   |                     |           |cd d d            S # 1 swxY w Y   d S r5   )r:   r7   r3   
edit_indexri   r   r8   r]   rj   s       r   rw   zSafeDatabase.edit_index   s    ^ 	 	6%d++6GGGC{ (*9+*F*H*H"3'""3'''	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	rk   c                     	 | j                                           t          t          |           j        |i | | j                                          d S # | j                                          w xY wr   )r:   acquirer7   r3   set_indexesrelease)r   r"   r#   r=   s      r   rz   zSafeDatabase.set_indexes   sr    	%N""$$$1E,%%14B6BBBN""$$$$$DN""$$$$s   9A A2c                 v   t          |t                    r(|| j        vrt          d|z            | j        |         }|j        dz   }| j                                         || j        v r| j        |j        dz            }n9t          d                     | j        |j        dz   <   | j        |j        dz            }| j        	                                 	 |                                  t          t          |           j        |g|R i | |	                                 d S # |	                                 w xY w)NzNo index named %sreindr6   )rM   strrB   r   r   r:   ry   r8   r   r{   r7   r3   reindex_index)r   rn   r"   r#   keyr   r=   s         r   r   zSafeDatabase.reindex_index   s;   eS!! 	.D...,-@5-HIII&u-Ej7"   $$$$%ej7&:;DD +:+*F*H*H uz& ' (%ej7&:;D   	LLNNN3E,%%3EKDKKKFKKKLLNNNNNDLLNNNNs   8D" "D8c                     	 | j                                          t          t          |                                            | j                                          d S # | j                                          w xY wr   )r:   ry   r7   r3   flushr{   rc   s    r   r   zSafeDatabase.flush   l    	%N""$$$,%%++---N""$$$$$DN""$$$$   A A A9c                     	 | j                                          t          t          |                                            | j                                          d S # | j                                          w xY wr   )r:   ry   r7   r3   fsyncr{   rc   s    r   r   zSafeDatabase.fsync   r   r   c                     | j         d         5  t          t          |                               ||          cd d d            S # 1 swxY w Y   d S Nid)r8   r7   r3   _update_id_index)r   _revro   r=   s      r   r   zSafeDatabase._update_id_index   s    % 	J 	Jt,,==dDII	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	Js   )AA	A	c                     | j         d         5  t          t          |                               |||          cd d d            S # 1 swxY w Y   d S r   )r8   r7   r3   _delete_id_index)r   _idr   ro   r=   s       r   r   zSafeDatabase._delete_id_index   s    % 	O 	Ot,,==c4NN	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	Os   *AA
A
c                    |                      ||          \  }}}| j        5  || j        |<   d d d            n# 1 swxY w Y   | j        dd          D ]h}| j        5  | j                            |          }||k    r	 d d d             n1	 d d d            n# 1 swxY w Y   |                     ||||           i| j        5  | j        |         |k    r| j        |= d d d            n# 1 swxY w Y   ||fS )N   )r   r:   r;   indexesgetrm   )r   r   ro   r   new_revrp   rn   curr_revs           r   _update_indexeszSafeDatabase._update_indexes   s    $ 5 5dD A AWg^ 	( 	( 'DL	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	(\!""% 	A 	AE  <++C00w&&       &               %%eT7C@@@@^ 	& 	&|C G++L%	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& G|s1   9= ="BB	B	C))C-0C-c                    |                      d|          }|d         |k    rt                      | j        5  || j        |<   d d d            n# 1 swxY w Y   | j        dd          D ]}|                     ||||           |                     |||           | j        5  | j        |         |k    r| j        |= d d d            d S # 1 swxY w Y   d S )Nr   r   r   )r   r	   r:   r;   r   rt   r   )r   r   r   ro   ru   rn   s         r   _delete_indexeszSafeDatabase._delete_indexes   sq   88D#&&Ft##--^ 	% 	% $DL	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	%\!""% 	B 	BE%%eT3AAAAc4...^ 	& 	&|C D((L%	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	&s#   AAA"C		CC)r*   r+   r,   r   rU   rT   r]   rX   r\   r`   rb   rf   rh   rm   rt   rw   rz   r   r   r   r   r   r   r   __classcell__)r=   s   @r   r3   r3   ;   s           ) ) )> > >& & &            5 5 5 5 57 7 7 7 7    K K K K K
L L L L L
    % % % % %    (% % % % %% % % % %J J J J JO O O O O  & & & & & & &r   r3   N)collectionsr   	functoolsr   typesr   codernitydb3.envr   codernitydb3.databaser   r	   r
   r   r1   r3   r.   r   r   <module>r      s   & $ # # # # #             , , , , , , O O O O O O O O O O       .  g& g& g& g& g&8 g& g& g& g& g&r   