U
    d                     @   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_readerNd   Fc                    s   |d k	st d|t ks2t 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)AssertionError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__ @/tmp/pip-unpacked-wheel-ua33x9lu/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s6tdj| jdt	| j
 t  W 5 Q R X 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   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   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#   r3      s    	

zDBFileReader.setup_exc                 C   s   | j std| j |S )Nzsetup_ex must be called first)r   r   read)r   Zread_netr"   r"   r#   r7      s    zDBFileReader.readc              	   C   s(   t | j | j| W 5 Q R X dS )z:Initialize dataset field blobs by creating an empty recordN)r   r1   r   r   Z
init_empty)r   r6   r"   r"   r#   r4      s    z'DBFileReader._init_field_blobs_as_emptyc                 C   sT   | j dkr*tj| js*t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   r0   r   r   r   r(   r   Z	get_blobsr   )r   netr"   r"   r#   r5      s    
z+DBFileReader._feed_field_blobs_from_db_filec                 C   s   t j| jddd S )zExtract 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   r3   r7   r4   r5   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
   