U
    dª/e1  ã                   @   s¾   d dl mZ zd dlmZ W n ek
r8   edƒ‚Y nX ddlmZmZ ddl	m
Z
 ddlmZ dd	lmZ dd
lmZ ddlmZmZ ddlmZmZ G dd„ deƒZG dd„ deƒZdS )é    )ÚBytesIO©ÚCityHash128z:Package clickhouse-cityhash is required to use compressioné   )ÚBlockOutputStreamÚBlockInputStreamé   )ÚCompressedBufferedReader)ÚCompressedBufferedWriter)Úget_decompressor_cls)ÚBUFFER_SIZE)Úread_binary_uint8Úread_binary_uint128)Úwrite_binary_uint8Úwrite_binary_uint128c                       s4   e Zd Z‡ fdd„Zdd„ Zdd„ Zdd„ Z‡  ZS )	ÚCompressedBlockOutputStreamc                    sB   || _ || _|| _|   ¡ | _t| jtƒ| _tt| ƒ 	| j|¡ d S ©N)
Úcompressor_clsÚcompress_block_sizeÚraw_foutÚ
compressorr
   r   ÚfoutÚsuperr   Ú__init__)Úselfr   r   r   Úcontext©Ú	__class__© úH/tmp/pip-unpacked-wheel-mds_o03w/clickhouse_driver/streams/compressed.pyr      s    
z$CompressedBlockOutputStream.__init__c                 C   s   t |ƒS r   r   ©r   Údatar   r   r   Úget_compressed_hash   s    z/CompressedBlockOutputStream.get_compressed_hashc                 C   sr   | j  ¡  |  ¡ }t|ƒ}|  |¡}t|| jƒ | j}d}||k rd| j |||| … ¡ ||7 }q:| j ¡  d S )Nr   )	r   ÚflushÚget_compressedÚlenr"   r   r   r   Úwrite)r   Ú
compressedZcompressed_sizeÚcompressed_hashÚ
block_sizeÚir   r   r   Úfinalize    s    


z$CompressedBlockOutputStream.finalizec                 C   sH   t ƒ }| jjd k	r&t| jj|ƒ d}nd}| j |¡}| |¡ | ¡ S ©Nr   r   )r   r   Úmethod_byter   Zget_compressed_datar&   Úgetvalue)r   r'   Úextra_header_sizer!   r   r   r   r$   2   s    
z*CompressedBlockOutputStream.get_compressed)Ú__name__Ú
__module__Ú__qualname__r   r"   r+   r$   Ú__classcell__r   r   r   r   r      s   	r   c                       s,   e Zd Z‡ fdd„Zdd„ Zdd„ Z‡  ZS )ÚCompressedBlockInputStreamc                    s(   || _ t| jtƒ}tt| ƒ ||¡ d S r   )Úraw_finr	   Ú
read_blockr   r   r4   r   )r   Zfinr   r   r   r   r   B   s    z#CompressedBlockInputStream.__init__c                 C   s   t |ƒS r   r   r    r   r   r   r"   G   s    z.CompressedBlockInputStream.get_compressed_hashc                 C   sH   t | jƒ}t| jƒ}t|ƒ}|| jƒ}|jd k	r6d}nd}| |||¡S r,   )r   r5   r   r   r-   Zget_decompressed_data)r   r(   r-   Zdecompressor_clsZdecompressorr/   r   r   r   r6   J   s    



  ÿz%CompressedBlockInputStream.read_block)r0   r1   r2   r   r"   r6   r3   r   r   r   r   r4   A   s   r4   N)Úior   Zclickhouse_cityhash.cityhashr   ÚImportErrorÚRuntimeErrorZnativer   r   Zbufferedreaderr	   Zbufferedwriterr
   Úcompressionr   Zdefinesr   Úreaderr   r   Úwriterr   r   r   r4   r   r   r   r   Ú<module>   s   ÿ
.