o
    1Xxi.                  
   @   s`  d dl Z d dlZd dlmZ d dlZd dlZd dlmZmZ d dl	m
Z
 d dlmZ d dlmZ d dlZd dlZd dlmZmZ d dlmZmZ d dlmZmZ d dlm  m  mZ d dlm  m  mZ d d	l m!Z!m"Z"m#Z# d d
l$m%Z%m&Z&m'Z'm(Z( d dl)Z)d dl*Z*d dl+Z+d dl,m-Z. d dl/m0Z0 eg ef Z1eddddddde+2 dgZ3ej4de3ddej5deeddf fddZ6ej7ej8de9ddddddZ:ee:e;e:ddedej<dej=ddfdd Z>deddfd!d"Z?ded#e@d$ej=d%eddf
d&d'ZAd(d) ZBG d*d+ d+e!ZCd,ZDG d-d. d.e#ZEd/ejFdeddfd0d1ZGdeddfd2d3ZHdS )4    N)
Connection)	GeneratorCallable)UUID)given)	ClientAPI	ServerAPI)SettingsSystem)SegmentManagerVectorReader)EmbeddingStateMachineStatestraceEmbeddingStateMachineBase)run_state_machine_as_testruleprecondition
initialize)Client)DefaultEmbeddingFunctionzchromadb.api.segment.SegmentAPIz chromadb.db.impl.sqlite.SqliteDBz7chromadb.segment.impl.manager.local.LocalSegmentManagerT)chroma_api_implchroma_sysdb_implchroma_producer_implchroma_consumer_implchroma_segment_manager_implallow_resetis_persistentpersist_directorymodule)scopeparamsrequestreturnc                 c   sP    | j }|j}tj|stj|dd |V  tj|r&tj|dd d S d S )NT)exist_ok)ignore_errors)paramr   ospathexistsmakedirsshutilrmtree)r!   configuration	save_path r.   b/home/air/biblejyuku/back/venv/lib/python3.10/site-packages/chromadb/test/property/test_persist.pysettings2   s   r0   
   )with_hnsw_paramswith_persistent_hnsw_paramsmax_hnsw_batch_sizemax_hnsw_sync_thresholdcoll)key)collection_strategyembeddings_strategyr8   r9   c                 C   s  t | }|  t|}|  |j|j|j|jd}|j	di | t
|| t
|| t
|| t
|| t
j|||jd |  ~~t | }|  t|}|j|j|jd}t
|| t
|| t
|| t
|| t
j|||jd |  ~~d S )Nnamemetadataembedding_function)r=   r;   r=   r.   )r
   startClientCreatorfrom_systemresetcreate_collectionr;   r<   r=   add
invariantscountmetadatas_matchdocuments_match	ids_matchann_accuracystopget_collection)r0   r8   r9   system_1client_1r6   system_2client_2r.   r.   r/   test_persistK   sR   	

rQ   c                    sn  t | }|  t|}|jddddd}|t}||jt	 dt
f fdd}| }|jdd	gd
gdggd | |ksCJ | }|jdgdggd | |ksWJ | }|jg dd
gdgdggd | |kspJ | }|jdgdggd |jdd	gd
gdggd | |ksJ | }|jdgdggd |jdd	gd
gdggd | |ksJ | }d S )Ntest   )hnsw:batch_sizehnsw:sync_threshold)r;   r<   r"   c                      s2   t d z	tj  W S  ty   Y dS w )Ng?)timesleepr&   r'   getmtime_get_metadata_fileFileNotFoundErrorr.   segmentr.   r/   get_index_last_modified_at   s   
z7test_sync_threshold.<locals>.get_index_last_modified_at12g      ?g       @)ids
embeddings3g      @)r_   r`   rc   4g      @5g      @)r
   r?   r@   rA   rC   instancer   get_segmentidr   floatrD   upsert)r0   systemclient
collectionmanagerr^   last_modified_atr.   r\   r/   test_sync_threshold   s6   



rp   collection_name
record_setconnc              
   C   s   z;t | }|  t|}|j|t d}t|| t	|| t
|| t|| t|| |  W d S  tyN } z|| |d }~ww )Nr>   )r
   r?   r@   rA   rL   
strategies"not_implemented_embedding_functionrE   rF   rG   rH   rI   rJ   rK   	Exceptionsend)r0   rq   rr   rs   rk   rl   r6   er.   r.   r/   load_and_check   s&   

ry   c                  C   s:   zt d} | dg | W S  ty   t d Y S w )N
forkserverchromadbspawn)multiprocessingget_contextset_forkserver_preloadrv   )ctxr.   r.   r/   get_multiprocessing_context   s   
r   c                   @   s   e Zd ZdZdS )#PersistEmbeddingsStateMachineStatespersistN)__name__
__module____qualname__r   r.   r.   r.   r/   r      s    r      c                       sx   e Zd Zdedef fddZeeddej	fddZe
d	d
 e dddZdeddfddZdddZ  ZS )PersistEmbeddingsStateMachinerl   r0   c                    s.   || _ || _t| _| j   t | j  d S N)rl   r0    MIN_STATE_CHANGES_BEFORE_PERSIST(min_state_changes_left_before_persistingrB   super__init__)selfrl   r0   	__class__r.   r/   r      s
   
z&PersistEmbeddingsStateMachine.__init__rm   rm   c                 C   sX   | j   | j j|j|j|jd| _|j| _td | t	j
 tjg g g g d| _d S )Nr:   init)ra   	metadatas	documentsrb   )rl   rB   rC   r;   r<   r=   rm   r   on_state_changer   r   rt   StateMachineRecordSetrecord_set_state)r   rm   r.   r.   r/   r      s   
z(PersistEmbeddingsStateMachine.initializec                 C   s   t | jd dko| jdkS )Nra      r   )lenr   r   r   r.   r.   r/   <lambda>
  s    
z&PersistEmbeddingsStateMachine.<lambda>r"   Nc                 C   sp   |  tj | jj}t \}}t }|jt	| j
|| j|fd}|  |  | r2| }||  d S )N)targetargs)r   r   r   rm   r;   r}   Piper   Processry   r0   r   r?   joinpollrecvclose)r   rq   conn1conn2r   prx   r.   r.   r/   r   	  s   z%PersistEmbeddingsStateMachine.persist	new_statec                 C   s&   |t jkr
t| _d S |  jd8  _d S )Nr   )r   r   r   r   )r   r   r.   r.   r/   r      s   
z-PersistEmbeddingsStateMachine.on_state_changec                 C   s   | j   d S r   )rl   rB   r   r.   r.   r/   teardown(  s   z&PersistEmbeddingsStateMachine.teardown)r"   N)r   r   r   r   r	   r   r   collection_strt   
Collectionr   r   r   strr   r   __classcell__r.   r.   r   r/   r      s    r   caplogc                    s,   |  tj t t fdd d S )Nc                      s   t  dS )Nr0   rl   )r   r.   rl   r0   r.   r/   r   2  s    z/test_persist_embeddings_state.<locals>.<lambda>)	set_levelloggingERRORr{   r   r   )r   r0   r.   r   r/   test_persist_embeddings_state,  s
   
r   c                 C   s   t | }t| |d}|jtjdddddddt tddd i g dd	d

d |jg dddgddgddggg dd dd |j	dgd |jdgddggd gd dd |
  d S )Nr   A00   rS   )zhnsw:construction_efzhnsw:search_efzhnsw:MrT   rU   z$0851f751-2f11-4424-ab23-4ae97074887a   FT)
r;   r<   r=   rh   	dimensiondtypeknown_metadata_keysknown_document_keywordshas_documentshas_embeddingsr   )0r_   r`   r   )NNN)ra   rb   r   r   )rr   r   )ra   r   )r{   r   r   r   rt   r   r   r   add_embeddingsdelete_by_idsfields_match)r0   rl   stater.   r.   r/   test_delete_add_after_persist7  sJ   

r   )Ir   r}   multiprocessing.connectionr   multiprocessing.contextrW   typingr   r   uuidr   
hypothesisr   hypothesis.strategiesrt   stpytestr{   chromadb.apir   r   chromadb.configr	   r
   chromadb.segmentr   r   !chromadb.test.property.strategiesrR   property!chromadb.test.property.invariantsrE   &chromadb.test.property.test_embeddingsr   r   r   hypothesis.statefulr   r   r   r   r&   r*   tempfilechromadb.api.clientr   r@   "chromadb.utils.embedding_functionsr   CreatePersistAPImkdtempconfigurationsfixtureFixtureRequestr0   sharedcollectionsjustr   
recordsetsr   	RecordSetrQ   rp   r   ry   r   r   r   r   LogCaptureFixturer   r   r.   r.   r.   r/   <module>   s     7;
;
