o
    uyii                   	   @   s   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Zd dl	m
Z
 d dlmZ d dlmZ eeZG dd deZG d	d
 d
eZG dd deZdedededee fddZdededefddZdS )    N)ABCabstractmethod)Optional)PartitionKey)
Properties)property_as_boolc                   @   s   e Zd ZU dZeed< eed< eed< eed< dedefddZedd	ed
e	e
 defddZddedefddZdedefddZdS )LocationProviderzA base class for location providers, that provide file locations for a table's write tasks.

    Args:
        table_location (str): The table's base storage location.
        table_properties (Properties): The table's properties.
    table_locationtable_properties	data_pathmetadata_pathc                 C   s   || _ || _ddlm} ||j }r|d| _n
| j d d| _||j }r5|d| _	d S | j d d| _	d S )Nr   TableProperties/z/dataz	/metadata)
r	   r
   pyiceberg.tabler   getWRITE_DATA_PATHrstripr   WRITE_METADATA_PATHr   )selfr	   r
   r   path r   \/home/ubuntu/maya3_transcribe/venv/lib/python3.10/site-packages/pyiceberg/table/locations.py__init__.   s   zLocationProvider.__init__Ndata_file_namepartition_keyreturnc                 C   s   dS )af  Return a fully-qualified data file location for the given filename.

        Args:
            data_file_name (str): The name of the data file.
            partition_key (Optional[PartitionKey]): The data file's partition key. If None, the data is not partitioned.

        Returns:
            str: A fully-qualified location URI for the data file.
        Nr   r   r   r   r   r   r   new_data_location>   s    z"LocationProvider.new_data_locationr   new_versionc                 C   s8   |dk rt d| d|ddt  d}| |S )aA  Return a fully-qualified metadata file location for a new table version.

        Args:
            new_version (int): Version number of the metadata file.

        Returns:
            str: fully-qualified URI for the new table metadata file.

        Raises:
            ValueError: If the version is negative.
        r   zTable metadata version: `z ` must be a non-negative integer05d-z.metadata.json)
ValueErroruuiduuid4new_metadata_location)r   r   	file_namer   r   r    new_table_metadata_file_locationJ   s   
z1LocationProvider.new_table_metadata_file_locationmetadata_file_namec                 C   s   | j  d| S )zReturn a fully-qualified metadata file location for the given filename.

        Args:
            metadata_file_name (str): Name of the metadata file.

        Returns:
            str: A fully-qualified location URI for the metadata file.
        r   )r   )r   r(   r   r   r   r%   \   s   	z&LocationProvider.new_metadata_locationN)r   )__name__
__module____qualname____doc__str__annotations__r   r   r   r   r   r   intr'   r%   r   r   r   r   r       s   
 r   c                       sB   e Zd Zdedef fddZddedee defd	d
Z  Z	S )SimpleLocationProviderr	   r
   c                    s   t  || d S r)   )superr   )r   r	   r
   	__class__r   r   r   i   s   zSimpleLocationProvider.__init__Nr   r   r   c                 C   s.   |r| j  d|  d| S | j  d| S )Nr   )r   to_pathr   r   r   r   r   l   s
   z(SimpleLocationProvider.new_data_locationr)   )
r*   r+   r,   r.   r   r   r   r   r   __classcell__r   r   r3   r   r1   h   s    $r1   c                       s   e Zd ZU dZdZdZeed< dede	f fddZ
dd
edee defddZed
edefddZededefddZ  ZS )ObjectStoreLocationProvider         _include_partition_pathsr	   r
   c                    s2   t  || ddlm} t| j|j|j| _d S )Nr   r   )	r2   r   r   r   r   r
   $WRITE_OBJECT_STORE_PARTITIONED_PATHS,WRITE_OBJECT_STORE_PARTITIONED_PATHS_DEFAULTr;   )r   r	   r
   r   r3   r   r   r   {   s   
z$ObjectStoreLocationProvider.__init__Nr   r   r   c                 C   s^   | j r|r| |  d| S | |}| j r$| j d| d| S | j d| d| S )Nr   r!   )r;   r   r5   _compute_hashr   )r   r   r   hashed_pathr   r   r   r      s   

z-ObjectStoreLocationProvider.new_data_locationc                 C   s:   dt j> }t| |d @ |B }t t|t j d  S )N   )r7   HASH_BINARY_STRING_BITSmmh3hash_dirs_from_hashbin)r   top_mask	hash_coder   r   r   r>      s   
z)ObjectStoreLocationProvider._compute_hash	file_hashc                 C   sd   t jt j }g }td|t jD ]}|| ||t j   qt| |kr-|| |d  d|S )ztDivides hash into directories for optimized orphan removal operation using ENTROPY_DIR_DEPTH and ENTROPY_DIR_LENGTH.r   Nr   )r7   ENTROPY_DIR_DEPTHENTROPY_DIR_LENGTHrangeappendlenjoin)rH   total_entropy_lengthhash_with_dirsir   r   r   rD      s   
z+ObjectStoreLocationProvider._dirs_from_hashr)   )r*   r+   r,   rA   rJ   rI   boolr/   r.   r   r   r   r   r   staticmethodr>   rD   r6   r   r   r3   r   r7   t   s   
 
r7   location_provider_implr	   r
   r   c           
   
   C   s   z9|  d}t|dk rddlm} t|j d|  d|d d |d }}t|}t	||}|||W S  t
yV }	 ztjd|  |	d W Y d }	~	d S d }	~	ww )	N.   r   r   z; should be full path (module.CustomLocationProvider), got: 'Could not initialize LocationProvider: )exc_info)splitrM   r   r   r"   WRITE_PY_LOCATION_PROVIDER_IMPLrN   	importlibimport_modulegetattrModuleNotFoundErrorloggerwarning)
rT   r	   r
   
path_partsr   module_name
class_namemoduleclass_excr   r   r   _import_location_provider   s    


rh   c                 C   sx   ddl m} | d} ||j }r*t|| | }r#td| |S td| t	||j
|jr7t| |S t| |S )Nr   r   r   zLoaded LocationProvider: %srX   )r   r   r   r   r[   rh   r`   infor"   r   OBJECT_STORE_ENABLEDOBJECT_STORE_ENABLED_DEFAULTr7   r1   )r	   r
   r   rT   location_providerr   r   r   load_location_provider   s   


rm   )r\   loggingr#   abcr   r   typingr   rB   pyiceberg.partitioningr   pyiceberg.typedefr   pyiceberg.utils.propertiesr   	getLoggerr*   r`   r   r1   r7   r.   rh   rm   r   r   r   r   <module>   s.   
H3
