
    Ng                         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dS )    N)Path)IterableOptional)modelszstorage.dbmzstorage.sqlitelocationreturnc                    t          |           t          z  }t          |           t          z  }|                                rd S |                                sd S 	 t	          j        t          |          d          }t          j        t          |                    }|	                                }|
                    d           |                                D ]}||         }t          |t                    r|                    d          }t          j        |          }t                               |          }|
                    d|t          j        |          f           |                                 |                                 |                                 |                                 d S # t,          $ r0}	t/          j        d|	           t/          j        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
             [/var/www/html/ai-engine/env/lib/python3.11/site-packages/qdrant_client/local/persistence.pytry_migrate_to_sqliter/      s   H~~ 55HH~~ 11H ??  hs8}}c22oc(mm,,jjll 	YZZZ##%% 	 	C$E#s## *jj)),s##C.99#>>JKK2N5))    	

		   8!<<<Y	
 	
 	
 s   EF; ;
G5+G00G5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dS )r   NCHECK_SAME_THREADr*   r   c                 t    t          j        t          j        |                                        d          S )Nr   )base64	b64encoder   dumpsdecode)clsr*   s     r.   r   z CollectionPersistence.encode_key?   s+    S 1 12299'BBB    Fr   force_disable_check_same_threadc                 .   t          |           t          |          t          z  | _        | j        j                            dd           | j        m|du rit          j        d          5 }|	                    d          
                                d         }|dk    | j        _        ddd           n# 1 swxY w Y   |rd| j        _        t          j        t          | j                  | j        	          | _        |                                  dS )
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.V,, 	P &--d (**Q 
 4>3O0	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P + 	5/4DN,$2H
 
 
 	s   ,>B66B:=B:c                 8    | j                                          d S )N)rB   r    )rD   s    r.   r    zCollectionPersistence.closed   s    r8   c                     | j                                         }|                    d           | j                                          d S )Nr   )rB   r   r   r   )rD   r   s     r.   rC   z#CollectionPersistence._ensure_tableg   sB    $$&&\]]]r8   pointc                    |                      |j                  }t          j        |          }| j                                        }|                    d|t          j        |          f           | j        	                                 dS )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   s     ooeh''U##$$&&9u%%	
 	
 	
 	r8   point_idc                     |                      |          }| j                                        }|                    d|f           | j                                         dS )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   sb     ooh''$$&&-F	
 	
 	
 	r8   c              #      K   | j                                         }|                    d           |                                D ]}t	          j        |d                   V  dS )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   sn       $$&&1222??$$ 	' 	'C,s1v&&&&&&	' 	'r8   )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 C C C C [C  t    B      
V/ D    *v5 $    	'hv12 	' 	' 	' 	' 	' 	'r8   r   c                  ,   dd l } |                                 5 }t          |          }t          j        dg dddi          }|                    |           |                                D ]
}||k    sJ  ~t          |          }|                                D ]
}||k    sJ  |                    |j                   |                    |j                   |                                D ]}J d            	 d d d            d S # 1 swxY w Y   d S )Nr      )g      ?g       @g      @a)rL   vectorpayloadFzShould not load anything)	tempfileTemporaryDirectoryr   r   r]   rM   rT   rP   rL   )rd   tmpdirpersistencerJ   loaded_point_s         r.   test_persistencerj      s   OOO		$	$	&	& 5&+F33"a#qRRRE"""',,.. 	 	L5((((+F33',,.. 	 	L5((((58$$$58$$$!!## 	5 	5A44444	5!5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5s   C"D		DDrU   )r3   r   r#   r   r   pathlibr   typingr   r   qdrant_client.httpr   r   r   r   r/   r   rj   r^   r8   r.   <module>rn      s     



          % % % % % % % % % % % % % %% $ *C *D * * * *Z\' \' \' \' \' \' \' \'~5 5 5 5 5 5r8   