
    =wg                         d Z ddlZddlmZ ddlmZ ddlmZ ddlm	Z	m
Z
mZ ddlmZmZ ddlmZ  G d	 d
ej"                        Z G d de      Z G d de      Zy)a$  
This module contains EXPERIMENTAL support for storing a Whoosh index's files in
the Google App Engine blobstore. This will use a lot of RAM since all files are
loaded into RAM, but it potentially useful as a workaround for the lack of file
storage in Google App Engine.

Use at your own risk, but please report any problems to me so I can fix them.

To create a new index::

    from whoosh.filedb.gae import DatastoreStorage

    ix = DatastoreStorage().create_index(schema)

To open an existing index::

    ix = DatastoreStorage().open_index()
    N)memcache)db)BytesIO)TOC	FileIndex_DEF_INDEX_NAME)ReadOnlyErrorStorage)
StructFilec                        e Zd ZdZ ej
                         Z ej                  d      Z fdZ	e
d        Zd Zd Zd Zd	 Zd
 Zd Zd Z xZS )DatastoreFilezA file-like object that is backed by a BytesIO() object whose contents
    is loaded from a BlobProperty in the app engine datastore.
    r   )defaultc                 J    t        t        | 
  |i | t               | _        y N)superr   __init__r   data)selfargskwargs	__class__s      H/var/www/horilla/myenv/lib/python3.12/site-packages/whoosh/filedb/gae.pyr   zDatastoreFile.__init__'   s     mT+T<V<I	    c                     t        j                  |d      }|4| j                  |      }t        j                  ||j                  d       n	 | |      }t        |j                        |_        |S )Nr   	namespace)value)r   getget_by_key_namesetr   r   r   )clsnamer   files       r   loadfilezDatastoreFile.loadfile+   sZ    T_==&&t,DLLtzz_EU#DDJJ'	r   c                 F   | j                   }| j                         | _         || j                   k7  rqt        t        j                               | _        | j                          t        j                  | j                         j                         | j                   d       y y Nr   r   )
r   getvalueinttimemtimeputr   r    key
id_or_name)r   oldvalues     r   closezDatastoreFile.close6   sj    ::]]_
tzz!TYY[)DJHHJLL..0$**#24 "r   c                 6    | j                   j                         S r   )r   tellr   s    r   r1   zDatastoreFile.tell?   s    yy~~r   c                 8    | j                   j                  |      S r   )r   write)r   r   s     r   r4   zDatastoreFile.writeB   s    yyt$$r   c                 8    | j                   j                  |      S r   )r   read)r   lengths     r   r6   zDatastoreFile.readE   s    yy~~f%%r   c                 4     | j                   j                  | S r   )r   seek)r   r   s     r   r9   zDatastoreFile.seekH   s    tyy~~t$$r   c                 6    | j                   j                         S r   )r   readliner2   s    r   r;   zDatastoreFile.readlineK       yy!!##r   c                 6    | j                   j                         S r   )r   r'   r2   s    r   r'   zDatastoreFile.getvalueN   r<   r   )__name__
__module____qualname____doc__r   BlobPropertyr   IntegerPropertyr*   r   classmethodr$   r/   r1   r4   r6   r9   r;   r'   __classcell__)r   s   @r   r   r      sf     BOOEBq)E  4 %&%$$r   r   c                        e Zd Zd ZddZd Zy)MemcacheLockc                     || _         y r   )r"   r   r"   s     r   r   zMemcacheLock.__init__S   s	    	r   c                     t        j                  | j                  ddd      }|r@|s>dd l}|s8 |j                  d       t        j                  | j                  ddd      }|s8|S )NLih  whooshlocksr   r   g? )r   addr"   r)   sleep)r   blockingvalr)   s       r   acquirezMemcacheLock.acquireV   sV    ll499c3-HC

3ll499b#O  
r   c                 F    t        j                  | j                  d       y )NrL   r   )r   deleter"   r2   s    r   releasezMemcacheLock.releaseb   s    		];r   NF)r>   r?   r@   r   rR   rU    r   r   rG   rG   R   s    
<r   rG   c                   r    e Zd ZdZefdZedfdZd Zd Zd Z	d Z
d	 Zd
 Zd ZddZd Zd Zd ZddZy)DatastoreStoragez}An implementation of :class:`whoosh.store.Storage` that stores files in
    the app engine datastore as blob properties.
    c                 n    | j                   rt        t        j                  | ||       t	        | ||      S r   )readonlyr	   r   creater   )r   schema	indexnames      r   create_indexzDatastoreStorage.create_indexk   s.    ==

4+vy11r   Nc                     t        | ||      S )N)r]   r^   )r   )r   r^   r]   s      r   
open_indexzDatastoreStorage.open_indexr   s    f	BBr   c                     t         j                         }g }|D ]/  }|j                  |j                         j	                                1 |S r   )r   allappendr,   r-   )r   querykeysr#   s       r   listzDatastoreStorage.listu   sD    !!# 	1DKK
--/0	1r   c                      y r   rW   r2   s    r   cleanzDatastoreStorage.clean|   s    r   c                 H     t         fd j                         D              S )Nc              3   @   K   | ]  }j                  |        y wr   )file_length).0fr   s     r   	<genexpr>z.DatastoreStorage.total_size.<locals>.<genexpr>   s     <14##A&<s   )sumrg   r2   s   `r   
total_sizezDatastoreStorage.total_size   s    <		<<<r   c                 0    t         j                  |      d uS r   )r   r   rI   s     r   file_existszDatastoreStorage.file_exists   s    ,,T2$>>r   c                 @    t         j                  |      j                  S r   )r   r   r*   rI   s     r   file_modifiedzDatastoreStorage.file_modified   s    ,,T2888r   c                 R    t        t        j                  |      j                        S r   )lenr   r   r   rI   s     r   rl   zDatastoreStorage.file_length   s    =006<<==r   c                 v    t        j                  |d       t        j                  |      j                         S r&   )r   rT   r   r   rI   s     r   delete_filezDatastoreStorage.delete_file   s*    8,,T299;;r   c                     t         j                  |      }t        |      }|j                  |_        |j                  |_        |j	                          |j                          y )Nkey_name)r   r   r   r*   r+   rT   )r   r"   newnamesafer#   newfiles         r   rename_filezDatastoreStorage.rename_file   sD    ,,T21



r   c                 8    t        t        |      |d       }|S )Nr{   c                 6    | j                   j                         S r   )r#   r/   )sfiles    r   <lambda>z.DatastoreStorage.create_file.<locals>.<lambda>   s    UZZ-=-=-? r   )r"   onclose)r   r   )r   r"   r   rn   s       r   create_filezDatastoreStorage.create_file   s    }d3$?Ar   c                 >    t        t        j                  |            S r   )r   r   r$   )r   r"   r   r   s       r   	open_filezDatastoreStorage.open_file   s    -00677r   c                     t        |      S r   )rG   rI   s     r   lockzDatastoreStorage.lock   s    D!!r   c                 6    t               }|j                         S r   )rY   r\   )r   r"   	tempstores      r   temp_storagezDatastoreStorage.temp_storage   s    $&	!!r   rV   r   )r>   r?   r@   rA   r   r_   ra   rg   ri   rq   rs   ru   rl   ry   r   r   r   r   r   rW   r   r   rY   rY   f   sZ     .= 2 $34 C=?9><
8""r   rY   )rA   r)   google.appengine.apir   google.appengine.extr   whoosh.compatr   whoosh.indexr   r   r   whoosh.filedb.filestorer	   r
   whoosh.filedb.structfiler   Modelr   objectrG   rY   rW   r   r   <module>r      sM   &  ) # ! 8 8 : /0$BHH 0$f<6 <(>"w >"r   