
    'Xh                         d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlmZm	Z	 d dl
mZ dZdZdeddfd	Z G d
 d      ZddZy)    N)Path)IterableOptional)modelszstorage.dbmzstorage.sqlitelocationreturnc                 b   t        |       t        z  }t        |       t        z  }|j                         ry |j                         sy 	 t	        j
                  t        |      d      }t        j                  t        |            }|j                         }|j                  d       |j                         D ]y  }||   }t        |t              r|j                  d      }t        j                  |      }t         j#                  |      }|j                  d|t        j$                  |      f       { |j'                          |j)                          |j)                          |j+                          y # t,        $ r2}	t/        j0                  d|	       t/        j0                  d       |	d }	~	ww xY w)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
             I/RAG/venv/lib/python3.12/site-packages/qdrant_client/local/persistence.pytry_migrate_to_sqliter/      sT   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   D*E3 3	F.<-F))F.c                       e Zd ZU dZee   ed<   edej                  de
fd       Zdde
defdZdd	Zdd
Zdej                  ddfdZdej                  ddfdZdeej                     fdZy)r   NCHECK_SAME_THREADr*   r   c                 p    t        j                  t        j                  |            j	                  d      S )Nr   )base64	b64encoder   dumpsdecode)clsr*   s     r.   r   z CollectionPersistence.encode_key?   s'    S 1299'BB    r   force_disable_check_same_threadc                 .   t        |       t        |      t        z  | _        | j                  j                  j                  dd       | j                  X|du rTt        j                  d      5 }|j                  d      j                         d   }|dk7  | j                  _        ddd       |rd| j                  _        t        j                  t        | j                        | j                  	      | _        | j                          y# 1 sw Y   fxY w)
z
        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   r9   tmp_conn
threadsafes        r.   __init__zCollectionPersistence.__init__C   s     	h'X)::""D$"?!!).MQV.V, &--d(*Q 
 4>3O0 - +/4DNN,$2H2H
 	% -,s   /7DDc                 8    | j                   j                          y )N)rB   r    )rD   s    r.   r    zCollectionPersistence.closed   s    r8   c                     | j                   j                         }|j                  d       | j                   j                          y )Nr   )rB   r   r   r   )rD   r   s     r.   rC   z#CollectionPersistence._ensure_tableg   s1    $$&\]r8   pointc                    | j                  |j                        }t        j                  |      }| j                  j                         }|j                  d|t        j                  |      f       | j                  j                          y)zi
        Persist a point in the local storage.
        Args:
            point: point to persist
        z+INSERT OR REPLACE INTO points VALUES (?, ?)N)
r   idr   r5   rB   r   r   r   r   r   )rD   rJ   r*   r+   r   s        r.   persistzCollectionPersistence.persistl   sk     ooehh'U#$$&9u%	
 	r8   point_idc                     | j                  |      }| j                  j                         }|j                  d|f       | j                  j	                          y)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   rB   r   r   r   )rD   rN   r*   r   s       r.   deletezCollectionPersistence.delete   sJ     ooh'$$&-F	
 	r8   c              #      K   | j                   j                         }|j                  d       |j                         D ]  }t	        j
                  |d           yw)zg
        Load a point from the local storage.
        Returns:
            point: loaded point
        zSELECT point FROM pointsr   N)rB   r   r   fetchallr   r   )rD   r   rows      r.   loadzCollectionPersistence.load   sK      $$&12??$C,,s1v&& %s   AA)Fr   N)__name__
__module____qualname__r1   r   bool__annotations__classmethodr   ExtendedPointIdr   r   rG   r    rC   PointStructrM   rP   r   rT    r8   r.   r   r   <   s    (,x~,CV33 C C C t B
V// D *v55 $ 	'hv112 	'r8   r   c                     dd l } | j                         5 }t        |      }t        j                  dg dddi      }|j                  |       |j                         D ]
  }||k(  sJ  n ~t        |      }|j                         D ]
  }||k(  sJ  n |j                  |j                         |j                  |j                         |j                         D ]  }J d        	 d d d        y # 1 sw Y   y xY w)Nr      )g      ?g       @g      @a)rL   vectorpayloadzShould not load anything)	tempfileTemporaryDirectoryr   r   r]   rM   rT   rP   rL   )rd   tmpdirpersistencerJ   loaded_point_s         r.   test_persistencerj      s    		$	$	&&+F3""a#qRE"',,.L5((( / +F3',,.L5((( / 	588$588$!!#A4445 $! 
'	&	&s   CC//C8rU   )r3   r   r#   r   r   pathlibr   typingr   r   qdrant_client.httpr   r   r   r   r/   r   rj   r^   r8   r.   <module>rn      sO     
     % %% $ *C *D *Z\' \'~5r8   