
    =wg^                     r   d Z ddlmZ ddl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mZ ddlmZ dd	lmZmZmZ d
ZdZ G d de      Z G d de      Z G d de      Z G d de      Z G d de      Zd#dZd$dZd#dZ d#dZ!d#dZ"d#dZ# G d de$      Z%d Z& G d d e%      Z' G d! d"e$      Z(y)%zSContains the main functions/classes for creating, maintaining, and using
an index.
    )divisionN)timesleep)__version__)toc_loaders)picklestring_type)ensure_schema)	_INT_SIZE_FLOAT_SIZE
_LONG_SIZEMAINic                       e Zd Zy)	LockErrorN)__name__
__module____qualname__     C/var/www/horilla/myenv/lib/python3.12/site-packages/whoosh/index.pyr   r   1   s    r   r   c                       e Zd ZdZy)
IndexErrorzGeneric index error.Nr   r   r   __doc__r   r   r   r   r   5   s    r   r   c                       e Zd ZdZddZy)IndexVersionErrorzRaised when you try to open an index using a format that the current
    version of Whoosh cannot read. That is, when the index you're trying to
    open is either not backward or forward compatible with this version of
    Whoosh.
    Nc                 L    t         j                  | |       || _        || _        y N)	Exception__init__versionrelease)selfmsgr!   r"   s       r   r    zIndexVersionError.__init__@   s     4%r   r   )r   r   r   r   r    r   r   r   r   r   9   s    r   r   c                       e Zd ZdZy)OutOfDateErrorz^Raised when you try to commit changes to an index which is not the
    latest generation.
    Nr   r   r   r   r&   r&   F   s    r   r&   c                       e Zd ZdZy)EmptyIndexErrorzIRaised when you try to work with an index that has no indexed terms.
    Nr   r   r   r   r(   r(   L   s    r   r(   c                 \    ddl m} |st        } ||       }t        j	                  |||      S )a  Convenience function to create an index in a directory. Takes care of
    creating a FileStorage object for you.

    :param dirname: the path string of the directory in which to create the
        index.
    :param schema: a :class:`whoosh.fields.Schema` object describing the
        index's fields.
    :param indexname: the name of the index to create; you only need to specify
        this if you are creating multiple indexes within the same storage
        object.
    :returns: :class:`Index`
    r   FileStorage)whoosh.filedb.filestorer+   _DEF_INDEX_NAME	FileIndexcreate)dirnameschema	indexnamer+   storages        r   	create_inr4   S   s.     4#	'"GGVY77r   c                 N    ddl m} |t        } || |      }t        |||      S )aD  Convenience function for opening an index in a directory. Takes care of
    creating a FileStorage object for you. dirname is the filename of the
    directory in containing the index. indexname is the name of the index to
    create; you only need to specify this if you have multiple indexes within
    the same storage object.

    :param dirname: the path string of the directory in which to create the
        index.
    :param indexname: the name of the index to create; you only need to specify
        this if you have multiple indexes within the same storage object.
    r   r*   )readonly)r1   r2   )r,   r+   r-   r.   )r0   r2   r6   r1   r+   r3   s         r   open_dirr7   i   s-     4#	'H5GWVyAAr   c                     t         j                  j                  |       r!	 t        | |      }|j	                         dkD  S y# t
        $ r Y yw xY w)zReturns True if dirname contains a Whoosh index.

    :param dirname: the file path of a directory.
    :param indexname: the name of the index. If None, the default index name is
        used.
    r2   F)ospathexistsr7   latest_generationr(   )r0   r2   ixs      r   	exists_inr@   ~   sU     
ww~~g	'Y7B'')B..   		s   A 	AAc                 $    | j                  |      S )zDeprecated; use ``storage.index_exists()``.

    :param storage: a store.Storage object.
    :param indexname: the name of the index. If None, the default index name is
        used.
    )index_exists)r3   r2   s     r   r=   r=      s     	**r   c                 8    ddl m}  ||       }t        ||      S )a  Returns a tuple of (release_version, format_version), where
    release_version is the release version number of the Whoosh code that
    created the index -- e.g. (0, 1, 24) -- and format_version is the version
    number of the on-disk format used for the index -- e.g. -102.

    You should avoid attaching significance to the second number (the index
    version). This is simply a version number for the TOC file and probably
    should not have been exposed in a public interface. The best way to check
    if the current version of Whoosh can open an index is to actually try to
    open it and see if it raises a ``whoosh.index.IndexVersionError`` exception.

    Note that the release and format version are available as attributes on the
    Index object in Index.release and Index.version.

    :param dirname: the file path of a directory containing an index.
    :param indexname: the name of the index. If None, the default index name is
        used.
    :returns: ((major_ver, minor_ver, build_ver), format_ver)
    r   r*   r9   )r,   r+   r!   )r0   r2   r+   r3   s       r   
version_inrD      s    * 4'"G7i00r   c                     	 |t         }| j                  |      }|j                  |j                  fS # t        $ r( t        j                         d   }d|j                  fcY S w xY w)a  Returns a tuple of (release_version, format_version), where
    release_version is the release version number of the Whoosh code that
    created the index -- e.g. (0, 1, 24) -- and format_version is the version
    number of the on-disk format used for the index -- e.g. -102.

    You should avoid attaching significance to the second number (the index
    version). This is simply a version number for the TOC file and probably
    should not have been exposed in a public interface. The best way to check
    if the current version of Whoosh can open an index is to actually try to
    open it and see if it raises a ``whoosh.index.IndexVersionError`` exception.

    Note that the release and format version are available as attributes on the
    Index object in Index.release and Index.version.

    :param storage: a store.Storage object.
    :param indexname: the name of the index. If None, the default index name is
        used.
    :returns: ((major_ver, minor_ver, build_ver), format_ver)
    N   )r-   
open_indexr"   r!   r   sysexc_info)r3   r2   r?   es       r   r!   r!      sc    *!'I	*

BJJ'' !LLN1aii  !s   03 .A$#A$c                       e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd ZddZd ZddZddZy)Indexz3Represents an indexed collection of documents.
    c                      y)zCloses any open resources held by the Index object itself. This may
        not close all resources being used everywhere, for example by a
        Searcher object.
        Nr   r#   s    r   closezIndex.close   s    
 	r   c                 h    | j                         }|j                  ||       |j                          y)zAdds a field to the index's schema.

        :param fieldname: the name of the field to add.
        :param fieldspec: an instantiated :class:`whoosh.fields.FieldType`
            object.
        N)writer	add_fieldcommit)r#   	fieldname	fieldspecws       r   rR   zIndex.add_field   s&     KKM	Iy)	
r   c                 f    | j                         }|j                  |       |j                          y)a  Removes the named field from the index's schema. Depending on the
        backend implementation, this may or may not actually remove existing
        data for the field from the index. Optimizing the index should always
        clear out existing data for a removed field.
        N)rQ   remove_fieldrS   )r#   rT   rV   s      r   rX   zIndex.remove_field   s$     KKM	y!	
r   c                      y)zReturns the generation number of the latest generation of this
        index, or -1 if the backend doesn't support versioning.
        r:   r   rN   s    r   r>   zIndex.latest_generation        r   c                     | S )zReturns a new Index object representing the latest generation
        of this index (if this object is the latest generation, or the backend
        doesn't support versioning, returns self).

        :returns: :class:`Index`
        r   rN   s    r   refreshzIndex.refresh   s	     r   c                      y)zReturns True if this object represents the latest generation of
        this index. Returns False if this object is not the latest generation
        (that is, someone else has updated the index since you opened this
        object).
        Tr   rN   s    r   
up_to_datezIndex.up_to_date  s     r   c                      y)zwReturns the last modified time of the index, or -1 if the backend
        doesn't support last-modified times.
        r:   r   rN   s    r   last_modifiedzIndex.last_modified  rZ   r   c                     t         )zyReturns True if this index is empty (that is, it has never had any
        documents successfully written to it.
        NotImplementedErrorrN   s    r   is_emptyzIndex.is_empty  s
     "!r   c                      y)z,Optimizes this index, if necessary.
        Nr   rN   s    r   optimizezIndex.optimize  s     	r   c                     | j                         }	 |j                         |j                          S # |j                          w xY w)z\Returns the total number of documents, DELETED OR UNDELETED,
        in this index.
        )readerdoc_count_allrO   r#   rs     r   ri   zIndex.doc_count_all!  s1    
 KKM	??$GGIAGGI	   2 Ac                     | j                         }	 |j                         |j                          S # |j                          w xY w)zGReturns the total number of UNDELETED documents in this index.
        )rh   	doc_countrO   rj   s     r   rn   zIndex.doc_count,  s0     KKM	;;=GGIAGGIrl   c                 @    ddl m}  || j                         fd| i|S )zReturns a Searcher object for this index. Keyword arguments are
        passed to the Searcher object's constructor.

        :rtype: :class:`whoosh.searching.Searcher`
        r   )Searcher	fromindex)whoosh.searchingrp   rh   )r#   kwargsrp   s      r   searcherzIndex.searcher6  s"     	.@@@@r   c                     | j                         }	 |j                  |      |j                          S # |j                          w xY w)zDReturns the total length of the field across all documents.
        )rh   field_lengthrO   r#   rT   rk   s      r   rv   zIndex.field_length@  s3     KKM	>>),GGIAGGI	   3 Ac                     | j                         }	 |j                  |      |j                          S # |j                          w xY w)zFReturns the maximum length of the field across all documents.
        )rh   max_field_lengthrO   rw   s      r   rz   zIndex.max_field_lengthJ  s5     KKM	%%i0GGIAGGIrx   Nc                     t         )a  Returns an IndexReader object for this index.

        :param reuse: an existing reader. Some implementations may recycle
            resources from this existing reader to create the new reader. Note
            that any resources in the "recycled" reader that are not used by
            the new reader will be CLOSED, so you CANNOT use it afterward.
        :rtype: :class:`whoosh.reading.IndexReader`
        rb   )r#   reuses     r   rh   zIndex.readerT  s
     "!r   c                     t         )zkReturns an IndexWriter object for this index.

        :rtype: :class:`whoosh.writing.IndexWriter`
        rb   )r#   rs   s     r   rQ   zIndex.writer`  s
    
 "!r   c                 l    | j                         }|j                  |||       |j                          y N)rt   )rQ   delete_by_termrS   )r#   rT   textrt   rV   s        r   r   zIndex.delete_by_termg  s+    KKM	D8<	
r   c                 j    | j                         }|j                  ||       |j                          y r   )rQ   delete_by_queryrS   )r#   qrt   rV   s       r   r   zIndex.delete_by_queryl  s)    KKM	!h/	
r   r   )r   r   r   r   rO   rR   rX   r>   r\   r^   r`   rd   rf   ri   rn   rt   rv   rz   rh   rQ   r   r   r   r   r   rL   rL      sb    
	"
	A
""
r   rL   c                    t        d |D              }t        j                  |      }t        j                  |      }t               }| D ]  }|j	                  d      r|j                  |      }	|j                  |      }
|	r0t        |	j                  d            |k7  sW|j                  |       i|
sl|
j                  d      }||vs|j                  |        |D ]  }	 | j                  |        y # t        $ r Y #w xY w)Nc              3   <   K   | ]  }|j                           y wr   
segment_id).0ss     r   	<genexpr>zclean_files.<locals>.<genexpr>z  s     A1As   .rF   )setTOC_pattern_segment_pattern
startswithmatchintgroupadddelete_fileOSError)r3   r2   gensegmentscurrent_segment_names
tocpattern
segpatterntodeletefilenametocmsegmnames               r   clean_filesr   t  s      AAAi(J%%i0JuH 's#))4::a=!S(X&::a=D00X&'  	)  		s   $C88	DDc                       e Zd ZdefdZeefd       Zd Zd Zd Z	d Z
d Zd	 Zdd
Zd Zd Zd Zd Zed        Zed        Zed        Zedd       ZddZy)r.   Nc                 0   ddl m} t        ||      st        d|z        t        |t              st        d|z        |rt        |      }|| _        || _        || _        t        j                  | j                  | j                  | j                         y )Nr   )Storagez%r is not a Storage objectzindexname %r is not a stringr1   )r,   r   
isinstance
ValueErrorr	   r
   r3   _schemar2   r   read)r#   r3   r1   r2   r   s        r   r    zFileIndex.__init__  s}    3'7+9GCDD)[1;iGHH"6*F" 	t~~dllCr   c                 D    t         j                  |||        | |||      S r   )r   r/   )clsr3   r1   r2   s       r   r/   zFileIndex.create  s!    

7FI.7FI..r   c                 h    | j                   j                  d| j                  d| j                  dS )N(z, ))	__class__r   r3   r2   rN   s    r   __repr__zFileIndex.__repr__  s&    #~~66#||T^^= 	=r   c                      y r   r   rN   s    r   rO   zFileIndex.close  s    r   c                 V    t         j                  | j                  | j                        S r   )r   _latest_generationr3   r2   rN   s    r   r>   zFileIndex.latest_generation  s    %%dllDNNCCr   c                     | j                         }t        j                  | j                  |      }| j                  j                  |      S r   )r>   r   	_filenamer2   r3   file_modified)r#   r   r   s      r   r`   zFileIndex.last_modified  s9    $$&==5||))(33r   c                 N    t        | j                         j                        dk(  S )Nr   )len	_read_tocr   rN   s    r   rd   zFileIndex.is_empty  s    4>>#,,-22r   c                 L     | j                   di |}|j                  d       y )NT)rf   r   )rQ   rS   )r#   rs   rV   s      r   rf   zFileIndex.optimize  s#    DKK!&!	$r   c                 L    |dkD  rddl m}  || fd|i|S ddlm}  || fi |S )NrF   r   )MpWriterprocs)SegmentWriter)whoosh.multiprocr   whoosh.writingr   )r#   r   rs   r   r   s        r   rQ   zFileIndex.writer  s4    191D88884 000r   c                 X    | j                   j                  | j                  dz   |z         S )z_Returns a lock object that you can try to call acquire() on to
        lock the index.
        _)r3   lockr2   )r#   r   s     r   r   zFileIndex.lock  s'    
 ||  #!5!<==r   c                 n    t         j                  | j                  | j                  | j                        S )Nr   )r   r   r3   r2   r   rN   s    r   r   zFileIndex._read_toc  s#    xxdnnT\\xJJr   c                 6    | j                         j                  S r   )r   r   rN   s    r   	_segmentszFileIndex._segments  s    ~~(((r   c                 6    | j                         j                  S r   )r   r1   rN   s    r   _current_schemazFileIndex._current_schema  s    ~~&&&r   c                 "    | j                         S r   )r   rN   s    r   r1   zFileIndex.schema  s    ##%%r   c                 6    | j                         j                  S r   )r   r"   rN   s    r   r"   zFileIndex.release      ~~'''r   c                 6    | j                         j                  S r   )r   r!   rN   s    r   r!   zFileIndex.version  r   r   c                    ddl mm}m} i 	 t	        |      dk(  r- |      j                         D ]  }|j                           S |r4|j                         D 	cg c]  \  }}	|	 }
}}	t        d |
D              fd}t	        |      dk(  r0 ||d         j                         D ]  }|j                           S |D cg c]
  } ||       }
} ||
      j                         D ]  }|j                           S c c}	}w c c}w # j                         D ]  }|j                           w xY w)Nr   )SegmentReaderMultiReaderEmptyReaderc              3   @   K   | ]  }|j                         |f  y wr   
generation)r   rk   s     r   r   z$FileIndex._reader.<locals>.<genexpr>  s     E 3Es   c                 X    | j                         }|v r
|   }|= |S  |       S )Nr   r   )segmentsegidrk   r   r   reusabler1   r3   s      r   	segreaderz$FileIndex._reader.<locals>.segreader  sF    **,H$ A H(&'4>@ @r   rF   r   )	whoosh.readingr   r   r   r   valuesrO   leaf_readersdict)r   r3   r1   r   r   r|   r   r   rk   r   readersr   r   r   r   s    `` `        @@r   _readerzFileIndex._reader  s@    	KJ&	8}! #6*B __& 	?  */););)=>A1>>EWEE
@ @ 8}! !!- __& 	 >FF'9W-FF"7zB__& 	9 ?2 G __& 	s5   D D #D/6D D DD D 'Ec                 :   d}|dkD  rP	 | j                         }| j                  | j                  |j                  |j                  |j
                  |      S y y # t        $ r1 t        j                         d   }|dz  }|dk  r|t        d       Y nw xY w|dkD  rF)N
   r   )r|   rF   g?)
r   r   r3   r1   r   r   IOErrorrH   rI   r   )r#   r|   retriesinforJ   s        r   rh   zFileIndex.reader  s    k~~'||DLL$++t}}$(OO5 $ B B	   	
 LLN1%1a<Gd	 ks   AA 7BB)rF   r   )r   r   r   r-   r    classmethodr/   r   rO   r>   r`   rd   rf   rQ   r   r   r   r   propertyr1   r"   r!   r   rh   r   r   r   r.   r.     s    '+ D$ /> / /=D4
3 1>K)' & & ( ( ( ( , ,\r   r.   c                       e Zd ZdZeefdZed        Zed        Z	ed        Z
ed        Zeefd       Zedd	       Zd
 Zy)r   zObject representing the state of the index after a commit. Essentially
    a container for the index's schema and the list of segment objects.
    c                 J    || _         || _        || _        || _        || _        y r   )r1   r   r   r!   r"   )r#   r1   r   r   r!   r"   s         r   r    zTOC.__init__8  s%     $r   c                     d|d|dS )Nr   z.tocr   )r   r2   r   s      r   r   zTOC._filename@  s    (#..r   c                 2    t        j                  d|z        S )Nz^_%s_([0-9]+).toc$recompiler   r2   s     r   r   zTOC._patternD  s    zz.:;;r   c                 2    t        j                  d|z        S )Nz(%s_[0-9a-z]+)[.][A-Za-z0-9_.]+r   r   s     r   r   zTOC._segment_patternH  s    zz;iGHHr   c                     | j                  |      }d}|D ]:  }|j                  |      }|st        t        |j	                  d            |      }< |S )Nr:   rF   )r   r   maxr   r   )r   r3   r2   patternmxr   ms          r   r   zTOC._latest_generationL  sT    ,,y) 	.Hh'AQWWQZ"-	. 	r   c                     t        |      }d|z  }|D ]%  }|j                  |      s|j                  |       '  | |g d      }|j                  ||       y )Nz_%s_r   )r
   r   r   write)r   r3   r1   r2   prefixr   tocs          r   r/   z
TOC.createW  sa    v& )# 	.H""6*##H-	.
 &"a 		'9%r   Nc                 J   |(| j                  ||      }|dk  rt        d|d|      | j                  ||      }|j                  |      fd} |dt                |dt
                |dt               j                         dk(  st        d	      j                         }j                         j                         j                         f}|t        k7  r/|t        v rt        |   }	 |	|||      \  }}
nt        d
|z  |      |rj                          n#t        j                  j!                               }t#        |      }j                         }||k(  sJ j                         }j%                         }
j'                           | ||
|||      S )Nr   zIndex z does not exist in c                 X    j                         }||k7  rt        d| d|d|      y )Nz3Index was created on different architecture: saved z = z, this computer = )read_varintr   )r   targetszstreams      r   
check_sizezTOC.read.<locals>.check_sizeq  s6    ##%BV| $("f"6 7 7 r   r   longfloatz"Number misread: byte order problemzCan't read format %s)r!   r"   )r   r(   r   	open_filer   r   r   read_intr   r   _CURRENT_TOC_VERSIONr   r   skip_stringr   loadsread_stringr
   read_picklerO   )r   r3   r2   r   r1   tocfilenamer   r!   r"   loaderr   	index_genr   r   s                @r   r   zTOC.reade  s   ;(()<CQw%)2G'= > > mmIs3"";/	7 	5)$6:&7K( F*ABB//#%%'););)=%%') **+%$W-#)&#vw#G '(>(H(/1 1
 ""$f&8&8&:;"6*F )I)###!A))+H68S'7KKr   c           
      v   t        | j                        }|j                          | j                  || j                        }|dt               }|j                  |      }|j                  t               |j                  t               |j                  t               |j                  d       |j                  t               t        d d D ]  }|j                  |        	 |j                  t        j                   |d             |j                  | j                         |j                  d       |j-                  | j.                         |j1                          |j3                  ||d	
       y # t        j"                  $ r |j%                         D ]  \  }}		 t        j                   |	       # t        j"                  $ r5 t'        j(                         d   }
t        j"                  |
d|d|	      t*        $ r+ t'        j(                         d   }
t+        |
d|d|	      w xY w  w xY w)Nr   r        rF    =r   T)safe)r
   r1   cleanr   r   r   create_filewrite_varintr   r   r   	write_intr  r   write_stringr   dumpsPicklingErroritemsrH   rI   	TypeErrorwrite_pickler   rO   rename_file)r#   r3   r2   r1   r
  tempfilenamer   numrT   fieldrJ   s              r   r   z	TOC.write  s   t{{+ nnY@"-tv6$$\2I&J'K( -.r? 	%C$	%	VQ 78 	)DMM* 	L+DA) ## 	$*LLN H 	5HLL'++ Sq)A ..Q	5/QRR  Hq)A#!Y$FGGHH 	s%   '%E8 8)H8"F87H88A:H22H8)NN)r   r   r   r   r  r   r    r   r   r   r   r   r-   r/   r   r   r   r   r   r   r   3  s    
 .{ / / < < I I   /> & & 5L 5Ln(Br   r   r   )NFN))r   
__future__r   os.pathr;   r   rH   r   r   whooshr   whoosh.legacyr   whoosh.compatr   r	   whoosh.fieldsr
   whoosh.systemr   r   r   r-   r  r   r   r   r   r&   r(   r4   r7   r@   r=   rD   r!   objectrL   r   r.   r   r   r   r   <module>r)     s   8       % - ' < <  
		 	 

 
Z j 8,B*$+14!DXF Xz@Z Z~RB& RBr   