
    4YshU                     n   d dl Z d dlmZ d dlmZmZ d dlmZ d dlm	Z	m
Z
mZ d dlmZ d dlmZ d Z	 d d	lmZ d
Zer@ej*                  Zej.                  ej0                  z  ej2                  z  ZdZej2                  Z e j:                  e      ZdgZ  G d de	      Z!d Z"d Z#d Z$d Z%y# e$ r dZY xw xY w)    N)Thread)existsabspath)mkdir)StoreVALID_STORENO_STORE)URIRef)pathname2urlc                 $    | j                  d      S Nutf-8)encode)us    /var/www/sten-cake5-migrate2.hellocrow.space/lexinfo-master/env/lib/python3.12/site-packages/rdflib/plugins/stores/berkeleydb.pybbr   
   s    88G    )dbTFi   
BerkeleyDBc                        e Zd ZdZdZdZdZdZdZd fd	Z	d Z
 ee
      ZddZd Zdd	Zd
 Zd ZddZddZddZd dZddZd dZd Zd Zd Zd Zd dZd Zd Zd Zd dZ d dZ! xZ"S )!r   a;      A store that allows for on-disk persistent using BerkeleyDB, a fast
    key/value DB.

    This store implementation used to be known, previous to rdflib 6.0.0
    as 'Sleepycat' due to that being the then name of the Python wrapper
    for BerkeleyDB.

    This store allows for quads as well as triples. See examples of use
    in both the `examples.berkeleydb_example` and `test.test_store_berkeleydb`
    files.

    **NOTE on installation**:

    To use this store, you must have BerkeleyDB installed on your system
    separately to Python (`brew install berkeley-db` on a Mac) and also have
    the BerkeleyDB Python wrapper installed (`pip install berkeleydb`).
    You may need to install BerkeleyDB Python wrapper like this:
    `YES_I_HAVE_THE_RIGHT_TO_USE_THIS_BERKELEY_DB_VERSION=1 pip install berkeleydb`
    TFNc                     t         st        d      d| _        || _        t        t
        |   |       | j                  j                  | _	        | j                  j                  | _        y )Nz/Unable to import berkeleydb, store is unusable.F)	has_bsddbImportError_BerkeleyDB__open_BerkeleyDB__identifiersuperr   __init__node_picklerloads_loadsdumps_dumps)selfconfiguration
identifier	__class__s      r   r   zBerkeleyDB.__init__C   sV    OPP&j$(7''--''--r   c                     | j                   S N)r   r#   s    r   __get_identifierzBerkeleyDB.__get_identifierL   s       r   c                 8   t        |      s'|du rt        |       | j                  |       nt        S t	        j
                         }|j                  dt               |j                  t        d       |j                  |t        t        j                  z         |S )NTr      )r   r   creater	   r   DBEnvset_cachesize	CACHESIZE	set_flagsENVSETFLAGSopenENVFLAGS	DB_CREATE)r#   homeDirr-   db_envs       r   _init_db_environmentzBerkeleyDB._init_db_environmentQ   so    g~gG$Q	*a(GX45r   c                     | j                   S r(   )r   r)   s    r   is_openzBerkeleyDB.is_open`   s    {{r   c                 	   t         st        S |}| j                  "t        t	        t        |                  | _        | j                  ||      }|t        k(  rt        S || _        d| _        d }t        j                  }t        }| j                  du r|t        j                  z  }|r|t        j                  z  }d}d}	d gdz  | _        d gdz  | _        t#        dd      D ]  }
 t%        |
      dj'                  d      dj'                  d      dj'                  d      fd	j'                  d            j)                         }t        j*                  |      }|j-                  |	       |j/                  |||||       || j                  |
<   |t%        |
      t1        |
      f| j                   |
<    i }t#        dd
      D ]  }
g }t#        dd      D ]L  }d}d}t#        ||dz         D ]  }|
d|dz  z  z  r|dz  }|dz  } n d|z
  }|j3                  ||f||f       N |j5                          |d   \  }}}d }| j                  |    ||||z         t1        |      t7        || j8                        f||
<    || _        t        j*                  |      | _        | j<                  j-                  |	       | j<                  j/                  d||||       t        j*                  |      | _        | j>                  j-                  |	       | j>                  j/                  d||||       t        j*                  |      | _         | j@                  j-                  |	       | j@                  j/                  d||||       t        j*                  |      | _!        | jB                  j-                  |	       | jB                  j/                  d|t        jD                  ||       t        j*                  |      | _#        | jF                  j-                  |	       | jF                  j/                  d|t        jH                  ||       d| _%        tM        | jN                        }|jQ                  d       |jS                          || _*        tV        S )NTi  r      slatin-1poc   r,      c                       fd}|S )Nc              3   d   K   |d n| }|k  r| |dz      |dz  }|k  rd y w)N r<   r,    )triplecontextiendstarts      r   
get_prefixz<BerkeleyDB.open.<locals>.get_prefix_func.<locals>.get_prefix   sI      %Ac'$QUm+Q c' Hs   '00rH   )rM   rL   rN   s   `` r   get_prefix_funcz(BerkeleyDB.open.<locals>.get_prefix_func   s    	 "!r   contexts	namespaceprefixk2ii2kF)target),r   r	   r   r
   r   r   r8   r7   r   r   DB_BTREEDBOPENFLAGStransaction_awareDB_AUTO_COMMITr5   _BerkeleyDB__indicies_BerkeleyDB__indicies_inforangeto_key_funcr   decodeDBr1   r3   from_key_funcappendsortresults_from_key_func_from_string_BerkeleyDB__lookup_dict_BerkeleyDB__contexts_BerkeleyDB__namespace_BerkeleyDB__prefix_BerkeleyDB__k2iDB_HASH_BerkeleyDB__i2kDB_RECNO_BerkeleyDB__needs_syncr   _BerkeleyDB__sync_run	setDaemonrM   _BerkeleyDB__sync_threadr   )r#   pathr-   r6   r7   dbnamedbtypedbopenflagsdbmode
dbsetflagsrK   
index_nameindexlookupresultsrM   scorelenj	tie_breakrO   ts                         r   r3   zBerkeleyDB.openc   s   O$ &|GG4D'E FD**7F;XO "!!T)2,,,K2<<'K
 
  
  q! 		PA'QI&

9(=szz)?TU

9% fh  EE&MEOOJ'JJz66;G!&DOOA',k!nmA>N&OD  #		P q! $	AGq! 
Aueai0 AA!a%L) %
q I		 2E3?@
A LLN 'E5#" &us{3e$%eT->->?	F1I?$	L $%%-!!*-ZfM55="":.k66;Of
+8VV[&IUU6]


Z(

vrzz;GUU6]


Z(

vr{{KH!$//*	D		r   c                    ddl m}m } 	 d\  }}| j                  r| j                  r |       x}}d| _        | j                  r |d       | j                  r |       }d| _         |       |z
  |kD  s |       |z
  |kD  r-d| _        t        j                  d       | j                          n| j                  rvn |d       | j                  ry y # t        $ r}t        j                  |       Y d }~y d }~ww xY w)Nr   )sleeptime)
   i,  Fg?syncr,   )	r   r   r   rm   loggerdebugr   	Exception	exception)r#   r   r   min_secondsmax_secondst0t1es           r   
__sync_runzBerkeleyDB.__sync_run   s    $	 '.$K++$$"f$B(-D%++c
,,!%B05D-6B;4k8Q05D-"LL0 IIK! ++ !H ++   	 Q	 s   B.C 9C 	C9C44C9c                 d   | j                   r| j                  D ]  }|j                           | j                  j                          | j                  j                          | j
                  j                          | j                  j                          | j                  j                          y y r(   )r   rZ   r   rf   rg   rh   rk   ri   )r#   rK   s     r   r   zBerkeleyDB.sync   sx    ;;__ OO  "!!#MM JJOOJJOO r   c                    d| _         | j                  j                          | j                  D ]  }|j	                           | j
                  j	                          | j                  j	                          | j                  j	                          | j                  j	                          | j                  j	                          | j                  j	                          y )NF)r   rp   joinrZ   closerf   rg   rh   rk   ri   r7   )r#   commit_pending_transactionrK   s      r   r   zBerkeleyDB.close   s    ! 	AGGI	 



r   c                    |\  }}}| j                   sJ d       || k7  sJ d       t        j                  | |||f||       | j                  } |||      }	 |||      }
 |||      } |||      }| j                  \  }}}|j                  t        |d|	d|
d|d      |      }|| j                  j                  t        |      d|       |j                  t        dd|	d|
d|d      |      xs dj                  d      }t        |j                  dj                  d                  }|j                  t        |             dj                  d      j                  |      }|J |j                  t        |d|	d|
d|d      d|       |j                  t        |d|
d|d|	d      d|       |j                  t        |d|d|	d|
d      d|       |s{|j                  t        dd|	d|
d|d      ||       |j                  t        dd|
d|d|	d      ||       |j                  t        dd|d|	d|
d      ||       d	| _        yy)
z6        Add a triple to the store of triples.
        The Store must be open.z$Can not add triple directly to storetxn^Nr   rG   r>   T)r   r   add
_to_stringrZ   getr   rf   putr   setsplitr   rm   )r#   rI   rJ   quotedr   subject	predicateobjectr   r=   r?   r@   rA   cspocposcospvaluecontexts_valuerP   s                      r   r   zBerkeleyDB.add  s:    (.$)V{{555{$F FF		$)V4gvF__
wC(yc*v3'wC(??dDaAq9:D=OO1s4!XXRAq12 &  &9%  >//

90EFGHLLA ZZ	277AN!---HHR!Q156HEHHR!Q156HEHHR!Q156HEb!Q:;^QTUb!Q:;^QTUb!Q:;^QTU $D' r   c                 \   |\  }}}| j                   \  }}	}
|j                  dj                  d      j                  dj                  d      |||dj                  d      g      |      xs dj                  d      }t	        |j                  dj                  d                  }|j                  |       dj                  d      j                  |      }| j                  D ]#  \  }}}|j                   ||||f|      |       % |s|rC| j                  D ]3  \  }}}|j                   ||||fdj                  d            ||       5 y | j                  D ]3  \  }}}	 |j                   ||||fdj                  d            |       5 y y # t        j                  $ r Y Mw xY w)Nr   r>   rG   r   )rZ   r   r   r   r   r   discardr[   deleter   r   DBNotFoundError)r#   sporA   r   r   r=   r?   r@   r   r   r   r   rP   rK   _to_key	_from_keys                   r   __removezBerkeleyDB.__remove,  s   1a??dDHH

9%**YYy)1aBIIi4HI 	   $ yy# 	 ~++CJJy,ABCI.33H=%)%9%9 	5!Aw	HHWaAY*H4	5-1-A-A )Aw	EEAq	299Y+?@&   .2-A-A )Aw	!QBIIi4H!IsS  -- s   %,FF+*F+c                 6   |\  }}}| j                   sJ d       t        j                  | |||f|       | j                  }||| k(  rd }|||| |||      } |||      }	 |||      }
 |||      }| j                  d   j                  t        |d|d|	d|
d      |      }|C| j                  t        |      t        |	      t        |
      ft        |      |       d| _        y y | j                  \  }}}| j                  |||f||      \  }}}}|j                  |      }	 |j                  |      }d}|j                          |r0|\  }}|j                  |      }	 |j                  |        t        |d             }|j                          |j!                  |      r ||      \  }}}	}
||j                  ||      xs dj#                  d	      }t%        |j'                  dj#                  d	                  }|j)                  dj#                  d	             |D ]4  }| j*                  D ]#  \  }}}|j-                   |||	|
f|      |       % 6 n| j                  ||	|
f||       nn|r0|5|3|1|/	 | j.                  j-                  t         |||            |       || _        y # t        j                  $ r d }d}Y w xY w# t        j                  $ r d }Y kw xY w# t        j                  $ r Y Ww xY w)
Nr   r   r   r   TFnextrG   r>   )r   r   remover   rZ   r   r   _BerkeleyDB__removerm   _BerkeleyDB__lookupcursor	set_ranger   r   r   getattr
startswithr   r   r   r   r[   r   rf   )r#   r   rJ   r   r   r   r   r   r=   r?   r@   rA   r   r   r   r   rx   rR   from_keyresults_from_keyr   current
needs_synckeyr   rP   rK   r   _s                                r   r   zBerkeleyDB.removeL  sJ   %("F{{555{TGY7A__
$ %"#7,A9#.A6s+A7,AOOA&**21a.K+LRU*VE r!ubeRU3RUD$(! !  $D$8<)V,g3 9F 95E68%5 \\c\*F# **62!
 LLN$
U#.#$$S)5gff57G >>&)!)#JAq!Q).3C)@)XBIIiDX#&~';';CJJy<Q'R#S RYYy%9:!) IA151E1E I7A !!QA)>C HII q!Qi<1 4 "?y'8V^..z's;<# /  !+DS %% #"
# )) #"G#6 -- s6   3K 1"K& .L K#"K#&K?>K?LLc              #   h  K   | j                   sJ d       |\  }}}||| k(  rd}| j                  |||f||      \  }}}	}
|j                  |      }	 |j                  |      }|j                          |r|\  }}|j                  |      }	 |j                  |        t        |d             }|j                          |r3|j                  |      r"|j                  ||      } |
|||||       ny|ryy# t        j
                  $ r d}Y w xY w# t        j
                  $ r d}Y }w xY ww)z)A generator over all the triples matchingr   Nr   r   )
r   r   r   r   r   r   r   r   r   r   )r#   r   rJ   r   r   r   r   rx   rR   r   r   r   r   r   r   r   s                   r   tripleszBerkeleyDB.triples  sR    {{555{%("F$ 59MMi('s 5B 5
1vx!1 #&	&&v.G 	 JC\\c\*F  %1'&&13 LLNs~~f-!&3C!8&sGYWW  !! 	G	 %% sU   AD2C< %)D2"D 1AD2:D2<DD2DD2D/,D2.D//D2c                    | j                   sJ d       ||| k(  rd }|dj                  d      }nt        d| j                  |      z        }| j                  d   }|j                         }|j                  |      }d}|r1|\  }}|j                  |      r|dz  } t        |d             }nn|r1|j                          |S )Nr   r   r>   z%s^r   r,   r   )
r   r   r   r   rZ   r   r   r   r   r   )	r#   rJ   rR   rx   r   r   countr   r   s	            r   __len__zBerkeleyDB.__len__  s    {{555{$?ZZ	*F 889F"""6* JC~~f%
1'&&13  	r   c                     |j                  d      }|j                  d      }| j                  j                  |      }|r| j                  j	                  |       || j                  |<   || j                  |<   y r   )r   rh   r   rg   r   )r#   rR   rQ   bound_prefixs       r   bindzBerkeleyDB.bind  si    w'$$W-	}}((3##L1#)i #, r   c                     |j                  d      }| j                  j                  |d       }|t        |j	                  d            S y r   )r   rg   r   r
   r^   )r#   rR   nss      r   rQ   zBerkeleyDB.namespace  sD    w'!!&$/>"))G,--r   c                     |j                  d      }| j                  j                  |d       }||j                  d      S y r   )r   rh   r   r^   )r#   rQ   rR   s      r   rR   zBerkeleyDB.prefix  s@    $$W-	""9d3==))r   c              #   N  K   | j                   j                         }g }|j                         }|rJ|\  }}|j                  |j	                  d      |j	                  d      f        t        |d             }|rJ|j                          |D ]  \  }}|t        |      f  y w)Nr   r   )rg   r   firstra   r^   r   r   r
   )r#   r   rz   r   rR   rQ   s         r   
namespaceszBerkeleyDB.namespaces  s     !!((*,,. 'FINNFMM'2I4D4DW4MNO-gff-/G	 
 	!( 	,FI&+++	,s   A8B%;*B%c              #     K   | j                   }| j                  }|r|\  }}} ||      } ||      } ||      }| j                  d   j                  t	        dd|d|d|d            }|r3|j                  dj                  d            D ]  }|s ||        y y | j                  }	|	j                         }
|
j                         }|
j                          |rX|\  }} ||      }| |	j                         }
	 |
j                  |        t        |
d             }|
j                          |rWy y # t        j                  $ r d }Y ,w xY ww)Nr   rG   r   r>   r   )rd   r   rZ   r   r   r   r   rf   r   r   r   r   r   r   r   )r#   rI   rd   r   r=   r?   r@   rP   rA   rx   r   r   r   r   rJ   s                  r   rP   zBerkeleyDB.contexts  s?    ((__
GAq!1A1A1Aq)--b2q!Q1O.PQH!

9(=> .A*1o-. 
 OOE\\^FllnGLLN$
U&s+#$$S)5gff57G   )) #"G#s7   BEA-E"D; &E9E;EEEEc                 l    | j                   j                  t        | j                  |            d       y )Nr   )rf   r   r   r   r#   graphs     r   	add_graphzBerkeleyDB.add_graph  s$    Btu56<r   c                 (    | j                  d|       y )N)NNN)r   r   s     r   remove_graphzBerkeleyDB.remove_graph  s    &.r   c                 l    | j                   j                  t        |            }| j                  |      S r(   )rk   r   intr    )r#   rK   ks      r   rd   zBerkeleyDB._from_string"  s&    JJNN3q6"{{1~r   c                 v   | j                  |      }| j                  j                  ||      }|x| j                  r d| j                  j                  ||      z  }nd| j                  j                  |      z  }| j                  j                  ||j                         |       |S |j                         }|S )Nr   z%s)	r"   ri   r   rX   rk   ra   r   r   r^   )r#   termr   r   rK   s        r   r   zBerkeleyDB._to_string&  s    KKJJNN1#N&9 %%4::,,Q444::,,Q//JJNN1ahhjcN2  
Ar   c           	      $   |\  }}}| j                   }|
 |||      }d}||dz  } |||      }||dz  } |||      }||dz  } |||      }| j                  |   \  }	}
}}t        dj                   |
|||f|                  }|	|||fS )Nr   r   r,   rC      r   )r   re   r   r   )r#   r   rJ   r   r   r   r   r   rK   rx   prefix_funcr   r   rR   s                 r   __lookupzBerkeleyDB.__lookup6  s    %("F__
 c2GFA c2G FA"9#6IFAC0F9=9K9KA9N6{H&6 CHH['9f)EwOPQfh(888r   )NN)T)F)FNr(   )#__name__
__module____qualname____doc__context_awareformula_awarerX   graph_awarer7   r   _BerkeleyDB__get_identifierpropertyr%   r8   r:   r3   rn   r   r   r   r   r   r   r   r   rQ   rR   r   rP   r   r   rd   r   r   __classcell__)r&   s   @r   r   r   '   s    * MMKF.! *+Jqf 0
&%P@K+Z#J4-,@=/ 9r   c                       fd}|S )Nc           	          dj                  d      j                  || dz     | dz   dz     | dz   dz     dj                  d      f      S )zTakes a string; returns keyr   r>   r<   r,   rC   rG   )r   r   )rI   rJ   rK   s     r   to_keyzto_key_func.<locals>.to_keyM  s^    zz)$))q1uA{#A{#		)$
 	
r   rH   )rK   r   s   ` r   r]   r]   L  s    

 Mr   c                       fd}|S )Nc                     | j                  dj                  d            }|d   |dz
  dz   dz  dz      |dz
  dz   dz  dz      |dz
  dz   dz  dz      fS )zTakes a key; returns stringr   r>   r   r<   r,   rC   r   r   )r   partsrK   s     r   r   zfrom_key_func.<locals>.from_key]  sv    		#**Y/0!H1q519/A%&1q519/A%&1q519/A%&	
 	
r   rH   )rK   r   s   ` r   r`   r`   \  s    
 Or   c                       fd}|S )Nc                 H   | j                  dj                  d            }| 	|d
z
  dz   dz  dz            }n|}| 	|d
z
  dz   dz  dz            }n|}| 	|d
z
  dz   dz  dz            }n|}|||f	fd|j                  dj                  d            D        fS )zCTakes a key and subject, predicate, object; returns tuple for yieldr   r>   r<   r   r,   rC   c              3   4   K   | ]  }|s |        y wr(   rH   ).0rA   from_strings     r   	<genexpr>z:results_from_key_func.<locals>.from_key.<locals>.<genexpr>~  s     VTU[^Vs   r   )r   r   r   r   r   r   r=   r?   r@   r   rK   s            r   r   z'results_from_key_func.<locals>.from_keyk  s    		#**Y/0? E1q519/A"567AAE1q519/A"567AA>E1q519/A"567AA1IV^%9%9#**Y:O%PV
 	
r   rH   )rK   r   r   s   `` r   rc   rc   j  s    
, Or   c                 L    d\  }}}| dz  rd}| dz  rd}| dz  rd}|d|d|S )	Nz???r,   r=   rC   r?   r   r@   ,rH   )rK   r=   r?   r@   s       r   readable_indexr     s>    GAq!1u1u1uAq!!r   )&logging	threadingr   os.pathr   r   osr   rdflib.storer   r   r	   rdflib.termr
   urllib.requestr   r   
berkeleydbr   r   r   DB_CDB_ALLDBr2   DB_INIT_MPOOLDB_INIT_CDB	DB_THREADr4   r0   rW   	getLoggerr   r   __all__r   r]   r`   rc   r   rH   r   r   <module>r     s      #  5 5  'I
  //K"..02<<?H I ,,K			8	$.b9 b9J 4"e  Is   B* *B43B4