
    <i                         S SK r S SKrS SKrS SKrS SKrS SKJr  S SKJr  S SK	J
r
  SrSrS\SS4S	 jr " S
 S5      rSS jrg)    N)Path)Iterable)modelszstorage.dbmzstorage.sqlitelocationreturnc                    [        U 5      [        -  n[        U 5      [        -  nUR                  5       (       a  g UR                  5       (       d  g  [        R
                  " [        U5      S5      n[        R                  " [        U5      5      nUR                  5       nUR                  S5        UR                  5        H  nX6   n[        U[        5      (       a  UR                  S5      n[        R                  " U5      n[         R#                  U5      nUR                  SU[        R$                  " U5      45        M     UR'                  5         UR)                  5         UR)                  5         UR+                  5         g ! [,         a4  n	[.        R0                  " SU	5        [.        R0                  " S5        U	eS n	A	ff = f)NcCCREATE TABLE IF NOT EXISTS points (id TEXT PRIMARY KEY, point BLOB)utf-8z INSERT INTO points VALUES (?, ?)z Failed to migrate dbm to sqlite:zKPlease try to use previous version of qdrant-client or re-create collection)r   STORAGE_FILE_NAME_OLDSTORAGE_FILE_NAMEexistsdbmopenstrsqlite3connectcursorexecutekeys
isinstanceencodepickleloadsCollectionPersistence
encode_keyBinarycommitcloseunlink	Exceptionloggingerror)
r   dbm_pathsql_pathdbm_storageconcurkeyvalue
sqlite_keyes
             Y/home/james-whalen/.local/lib/python3.13/site-packages/qdrant_client/local/persistence.pytry_migrate_to_sqliter.      s[   H~ 55HH~ 11H?? hhs8}c2ooc(m,jjl 	YZ##%C$E#s##jj),,s#C.99#>JKK2NN5) & 	

		 8!<Y	
 s   D4F 
G/G  Gc                       \ rS rSr% Sr\S-  \S'   \S\R                  S\
4S j5       rSS\
S\4S	 jjrSS
 jrSS jrS\R                  SS4S jrS\R                  SS4S jrS\\R                     4S jrSrg)r   <   NCHECK_SAME_THREADr)   r   c                 t    [         R                  " [        R                  " U5      5      R	                  S5      $ )Nr   )base64	b64encoder   dumpsdecode)clsr)   s     r-   r    CollectionPersistence.encode_key?   s'    S 1299'BB    r   force_disable_check_same_threadc                 >   [        U5        [        U5      [        -  U l        U R                  R                  R                  SSS9  U R                  cZ  USL aU  [        R                  " S5       nUR                  S5      R                  5       S   nUS:g  U R                  l        SSS5        U(       a  SU R                  l        [        R                  " [        U R                  5      U R                  S	9U l        U R                  5         g! , (       d  f       Nn= f)
zk
Create or load a collection from the local storage.
Args:
    location: path to the collection directory.
T)exist_okparentsNFz:memory:zNselect * from pragma_compile_options where compile_options like 'THREADSAFE=%'r   zTHREADSAFE=1)check_same_thread)r.   r   r   r   parentmkdirr1   r   r   r   fetchone	__class__r   storage_ensure_table)selfr   r:   tmp_conn
threadsafes        r-   __init__CollectionPersistence.__init__C   s     	h'X)::""D$"?!!).MQV.V, &--d(*Q 
 4>3O0 - +/4DNN,"44

 	' -,s   /7D
Dc                 8    U R                   R                  5         g )N)rC   r   )rE   s    r-   r   CollectionPersistence.closee   s    r9   c                     U R                   R                  5       nUR                  S5        U R                   R                  5         g )Nr
   )rC   r   r   r   )rE   r   s     r-   rD   #CollectionPersistence._ensure_tableh   s1    $$&\]r9   pointc                    U R                  UR                  5      n[        R                  " U5      nU R                  R                  5       nUR                  SU[        R                  " U5      45        U R                  R                  5         g)zI
Persist a point in the local storage.
Args:
    point: point to persist
z+INSERT OR REPLACE INTO points VALUES (?, ?)N)
r   idr   r5   rC   r   r   r   r   r   )rE   rN   r)   r*   r   s        r-   persistCollectionPersistence.persistm   sk     ooehh'U#$$&9u%	
 	r9   point_idc                     U R                  U5      nU R                  R                  5       nUR                  SU45        U R                  R	                  5         g)zV
Delete a point from the local storage.
Args:
    point_id: id of the point to delete
zDELETE FROM points WHERE id = ?N)r   rC   r   r   r   )rE   rS   r)   r   s       r-   deleteCollectionPersistence.delete   sJ     ooh'$$&-F	
 	r9   c              #      #    U R                   R                  5       nUR                  S5        UR                  5        H  n[        R
                  " US   5      v   M      g7f)zG
Load a point from the local storage.
Returns:
    point: loaded point
zSELECT point FROM pointsr   N)rC   r   r   fetchallr   r   )rE   r   rows      r-   loadCollectionPersistence.load   sJ      $$&12??$C,,s1v&& %s   AA!)r   rC   )Fr   N)__name__
__module____qualname____firstlineno__r1   bool__annotations__classmethodr   ExtendedPointIdr   r   rH   r   rD   PointStructrQ   rU   r   rZ   __static_attributes__ r9   r-   r   r   <   s    %)td{)CV33 C C C   t  D
V// D *v55 $ 	'hv112 	'r9   r   c                      SS K n U R                  5        n[        U5      n[        R                  " S/ SQSS0S9nUR                  U5        UR                  5        H
  nXC:X  d   e  O   A[        U5      nUR                  5        H
  nXC:X  d   e  O   UR                  UR                  5        UR                  UR                  5        UR                  5        H  n S5       e   S S S 5        g ! , (       d  f       g = f)Nr      )g      ?g       @g      @a)rP   vectorpayloadzShould not load anything)	tempfileTemporaryDirectoryr   r   re   rQ   rZ   rU   rP   )rm   tmpdirpersistencerN   loaded_point_s         r-   test_persistencers      s    		$	$	&&+F3""a#qRE"',,.L((( / +F3',,.L((( / 	588$588$!!#A4445 $! 
'	&	&s   CC//
C=r\   )r3   r   r"   r   r   pathlibr   typingr   qdrant_client.httpr   r   r   r   r.   r   rs   rg   r9   r-   <module>rw      sO     
      %% $ *C *D *Z]' ]'@5r9   