U
    dª/e‚	  ã                   @   s†   d dl mZ ddlmZ ddlmZmZ ddlmZ zd dl	m
Z
 W n ek
r`   edƒ‚Y nX G dd	„ d	eƒZG d
d„ deƒZdS )é    )ÚBytesIOé   )Úread_binary_uint32)Úwrite_binary_uint8Úwrite_binary_uint32)Úerrors)ÚCityHash128z:Package clickhouse-cityhash is required to use compressionc                       sH   e Zd ZdZdZdZ‡ fdd„Zdd„ Zdd„ Zd	d
„ Z	dd„ Z
‡  ZS )ÚBaseCompressorz5
    Partial file-like object with write method.
    Nc                    s   t ƒ | _tt| ƒ ¡  d S ©N)r   ÚdataÚsuperr	   Ú__init__)Úself©Ú	__class__© úF/tmp/pip-unpacked-wheel-mds_o03w/clickhouse_driver/compression/base.pyr      s    zBaseCompressor.__init__c                 C   s$   | j  ¡ }| j  d¡ | j  ¡  |S )Nr   )r   ÚgetvalueÚseekÚtruncate)r   Úvaluer   r   r   Ú	get_value   s    

zBaseCompressor.get_valuec                 C   s   | j  |¡ d S r
   )r   Úwrite)r   Zp_strr   r   r   r   !   s    zBaseCompressor.writec                 C   s   t ‚d S r
   ©ÚNotImplementedError)r   r   r   r   r   Úcompress_data$   s    zBaseCompressor.compress_datac                 C   sV   t ƒ }|  ¡ }|  |¡}|d d }t|t|ƒ |ƒ tt|ƒ|ƒ | |¡ | ¡ S ©Né   )r   r   r   r   Úlenr   r   )r   Úextra_header_sizeÚrvr   Ú
compressedÚheader_sizer   r   r   Úget_compressed_data'   s    

z"BaseCompressor.get_compressed_data)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚmethodÚmethod_byter   r   r   r   r#   Ú__classcell__r   r   r   r   r	      s   r	   c                       s<   e Zd ZdZdZ‡ fdd„Zdd„ Zdd„ Zdd	„ Z‡  Z	S )
ÚBaseDecompressorNc                    s   || _ tt| ƒ ¡  d S r
   )Ústreamr   r+   r   )r   Úreal_streamr   r   r   r   :   s    zBaseDecompressor.__init__c                 C   s   t ‚d S r
   r   )r   r   Úuncompressed_sizer   r   r   Údecompress_data>   s    z BaseDecompressor.decompress_datac                 C   s   t |ƒ|krt ¡ ‚d S r
   )r   r   ZChecksumDoesntMatchError)r   Zcompressed_dataÚcompressed_hashr   r   r   Ú
check_hashA   s    zBaseDecompressor.check_hashc           	      C   s€   t | jƒ}|| d }t| j |¡ƒ}tƒ }t||ƒ t||ƒ | | ¡ ¡ |  | ¡ |¡ t |ƒ}| |d ¡}|  	||¡S r   )
r   r,   r   Úreadr   r   r   r   r1   r/   )	r   r)   r0   r   Zsize_with_headerZcompressed_sizer!   Zblock_checkr.   r   r   r   Úget_decompressed_dataE   s    


z&BaseDecompressor.get_decompressed_data)
r$   r%   r&   r(   r)   r   r/   r1   r3   r*   r   r   r   r   r+   6   s   r+   N)Úior   Úreaderr   Úwriterr   r   Ú r   Zclickhouse_cityhash.cityhashr   ÚImportErrorÚRuntimeErrorÚobjectr	   r+   r   r   r   r   Ú<module>   s   ÿ
'