o
    <i                   	   @   s   d dl Z d dlZd dlZd dlmZ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dB fddZdede
defddZdS )    N)ABCabstractmethod)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	dB 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   V/home/ubuntu/veenaModal/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_locationI   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   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dB defd	d
Z  ZS )SimpleLocationProviderr   r	   c                    s   t  || d S r(   )superr   )r   r   r	   	__class__r   r   r   h   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   k   s
   z(SimpleLocationProvider.new_data_locationr(   )	r)   r*   r+   r-   r   r   r   r   __classcell__r   r   r2   r   r0   g   s    $r0   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d	B 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   )	r1   r   r   r   r   r	   $WRITE_OBJECT_STORE_PARTITIONED_PATHS,WRITE_OBJECT_STORE_PARTITIONED_PATHS_DEFAULTr:   )r   r   r	   r   r2   r   r   r   z   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   r4   _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   )r6   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   )r6   ENTROPY_DIR_DEPTHENTROPY_DIR_LENGTHrangeappendlenjoin)rG   total_entropy_lengthhash_with_dirsir   r   r   rC      s   
z+ObjectStoreLocationProvider._dirs_from_hashr(   )r)   r*   r+   r@   rI   rH   boolr.   r-   r   r   r   r   staticmethodr=   rC   r5   r   r   r2   r   r6   s   s   
 
r6   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
yQ   tjd|  ttjd Y d S w )	N.   r   r   z; should be full path (module.CustomLocationProvider), got: 'Could not initialize LocationProvider: )exc_info)splitrL   r   r   r!   WRITE_PY_LOCATION_PROVIDER_IMPLrM   	importlibimport_modulegetattrModuleNotFoundErrorloggerwarningisEnabledForloggingDEBUG)	rS   r   r	   
path_partsr   module_name
class_namemoduleclass_r   r   r   _import_location_provider   s(   



ri   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: %srW   )r   r   r   r   rZ   ri   r_   infor!   r   OBJECT_STORE_ENABLEDOBJECT_STORE_ENABLED_DEFAULTr6   r0   )r   r	   r   rS   location_providerr   r   r   load_location_provider   s   


rn   )r[   rb   r"   abcr   r   rA   pyiceberg.partitioningr   pyiceberg.typedefr   pyiceberg.utils.propertiesr   	getLoggerr)   r_   r   r0   r6   r-   ri   rn   r   r   r   r   <module>   s,   
H3
