a
    –¥»aÐ  ã                   @   sX   d dl mZmZmZmZ d dlmZ d dlm	Z	 d dl
mZ d dlZG dd„ deƒZdS )é    )ÚcoreÚscopeÚ	workspaceÚ_import_c_extension)ÚReader)ÚDataset)Úfrom_column_listNc                       s\   e Zd ZdZd‡ fdd„	Zdd„ Zdd	d
„Zdd„ Zdd„ Zdd„ Z	dd„ Z
dd„ Z‡  ZS )ÚDBFileReaderZdb_file_readerNéd   Fc                    s    |d usJ dƒ‚|t  ¡ v s2J dj|t  ¡ d�ƒ‚tj |¡| _|| _|p\dj|  ¡ | j	d�| _
|| _|| _tt| ƒ |  |¡¡ t| j| j
d ƒ| _d | _d S )Nzdb_path can't be None.zMdb_type [{db_type}] is not available. 
Choose one of these: {registered_dbs}.)Údb_typeÚregistered_dbsz{db_name}_{default_name_suffix})Zdb_nameÚdefault_name_suffixZ_dataset)ÚCr   ÚformatÚosÚpathÚ
expanduserÚdb_pathr   Ú_extract_db_name_from_db_pathr   ÚnameÚ
batch_sizeÚ	loop_overÚsuperr	   Ú__init__Ú_init_reader_schemar   Z_schemaÚdsÚ	ds_reader)Úselfr   r   r   r   r   Úfield_names©Ú	__class__© úV/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/caffe2/python/db_file_reader.pyr   )   s$    	ýÿþzDBFileReader.__init__c                 C   s   |p|   ¡ d S )NZ_db_file_reader)r   )r   r   r!   r!   r"   Ú
_init_nameI   s    
ÿzDBFileReader._init_namec                    s´   |rt |ƒS | jdkr6tj | j¡s6J dj| jd�ƒ‚t | j	¡� t
 ¡ ‰ W d  ƒ n1 s`0    Y  t tjdg g d| j| jdˆ d�¡ ‡ fdd	„tt ¡ ƒD ƒ}t |ƒ}|S )
a_  Restore a reader schema from the DB file.

        If `field_names` given, restore scheme according to it.

        Overwise, loade blobs from the DB file into the workspace,
        and restore schema from these blob names.
        It is also assumed that:
        1). Each field of the schema have corresponding blobs
            stored in the DB file.
        2). Each blob loaded from the DB file corresponds to
            a field of the schema.
        3). field_names in the original schema are in alphabetic order,
            since blob names loaded to the workspace from the DB file
            will be in alphabetic order.

        Load a set of blobs from a DB file. From names of these blobs,
        restore the DB file schema using `from_column_list(...)`.

        Returns:
            schema: schema.Struct. Used in Reader.__init__(...).
        Úlog_file_dbú"db_path [{db_path}] does not exist©r   NÚLoadT)Úabsolute_pathÚdbr   Zload_allZ
add_prefixc                    s&   g | ]}|  ˆ ¡r|tˆ ƒd … ‘qS )N)Ú
startswithÚlen)Ú.0Z	blob_name©Zblob_prefixr!   r"   Ú
<listcomp>x   s   
ÿz4DBFileReader._init_reader_schema.<locals>.<listcomp>)r   r   r   r   Úexistsr   r   r   Ú	NameScoper   r   ZCurrentNameScoper   ZRunOperatorOnceZCreateOperatorÚsortedZBlobs)r   r   Z	col_namesZschemar!   r-   r"   r   M   s2    
ÿ&øÿ

ÿz DBFileReader._init_reader_schemac                 C   s^   | j r| j  ||¡ nD|  |¡ |  |¡ | jj|| j| jd�| _ | j  |¡ | j  	|¡ dS )ab  From the Dataset, create a _DatasetReader and setup a init_net.

        Make sure the _init_field_blobs_as_empty(...) is only called once.

        Because the underlying NewRecord(...) creats blobs by calling
        NextScopedBlob(...), so that references to previously-initiated
        empty blobs will be lost, causing accessibility issue.
        )r   r   N)
r   Úsetup_exÚ_init_field_blobs_as_emptyÚ_feed_field_blobs_from_db_filer   Zrandom_readerr   r   Zsort_and_shuffleZcomputeoffset)r   Úinit_netZ
finish_netr!   r!   r"   r2      s    	

ýzDBFileReader.setup_exc                 C   s   | j sJ dƒ‚| j  |¡S )Nzsetup_ex must be called first)r   Úread)r   Zread_netr!   r!   r"   r6   •   s    zDBFileReader.readc                 C   s<   t  | j¡� | j |¡ W d  ƒ n1 s.0    Y  dS )z:Initialize dataset field blobs by creating an empty recordN)r   r0   r   r   Z
init_empty)r   r5   r!   r!   r"   r3   ™   s    z'DBFileReader._init_field_blobs_as_emptyc                 C   sT   | j dkr*tj | j¡s*J dj| jd�ƒ‚|jg | j ¡ | j| j d| j 	¡ d� dS )z=Load from the DB file at db_path and feed dataset field blobsr$   r%   r&   T)r)   r   r(   Zsource_blob_namesN)
r   r   r   r/   r   r   r'   r   Z	get_blobsr   )r   Únetr!   r!   r"   r4   ž   s    
ÿúz+DBFileReader._feed_field_blobs_from_db_filec                 C   s   t j | j¡ dd¡d S )zøExtract DB name from DB path

            E.g. given self.db_path=`/tmp/sample.db`, or
            self.db_path = `dper_test_data/cached_reader/sample.db`
            it returns `sample`.

            Returns:
                db_name: str.
        Ú.é   r   )r   r   Úbasenamer   Úrsplit)r   r!   r!   r"   r   ¬   s    
z*DBFileReader._extract_db_name_from_db_path)Nr
   FN)N)Ú__name__Ú
__module__Ú__qualname__r   r   r#   r   r2   r6   r3   r4   r   Ú__classcell__r!   r!   r   r"   r	      s       ù 
2r	   )Zcaffe2.pythonr   r   r   r   r   Zcaffe2.python.dataior   Zcaffe2.python.datasetr   Zcaffe2.python.schemar   r   r	   r!   r!   r!   r"   Ú<module>   s
   