o
    ED©i1  ã                   @   s¸   d dl mZ zd dlmZ W n ey   edƒ‚w 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__© ú]/var/www/Datamplify/venv/lib/python3.10/site-packages/clickhouse_driver/streams/compressed.pyr      s   
z$CompressedBlockOutputStream.__init__c                 C   ó   t |ƒS r   r   ©r   Údatar   r   r   Úget_compressed_hash   ó   z/CompressedBlockOutputStream.get_compressed_hashc                 C   sx   | j  ¡  |  ¡ }t|ƒ}|  |¡}t|| jƒ | j}d}||k r5| j |||| … ¡ ||7 }||k s!| j ¡  d S )Nr   )	r   ÚflushÚget_compressedÚlenr#   r   r   r   Úwrite)r   Ú
compressedÚcompressed_sizeÚcompressed_hashÚ
block_sizeÚir   r   r   Úfinalize    s   

þz$CompressedBlockOutputStream.finalizec                 C   sH   t ƒ }| jjd urt| jj|ƒ d}nd}| j |¡}| |¡ | ¡ S ©Nr   r   )r   r   Úmethod_byter   Ú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   r8   r   )r   Úfinr   r   r   r   r   B   s   z#CompressedBlockInputStream.__init__c                 C   r    r   r   r!   r   r   r   r#   G   r$   z.CompressedBlockInputStream.get_compressed_hashc                 C   sH   t | jƒ}t| jƒ}t|ƒ}|| jƒ}|jd urd}nd}| |||¡S r/   )r   r9   r   r   r0   Úget_decompressed_data)r   r+   r0   Údecompressor_clsÚdecompressorr3   r   r   r   r:   J   s   



ÿz%CompressedBlockInputStream.read_block)r4   r5   r6   r   r#   r:   r7   r   r   r   r   r8   A   s    r8   N)Úior   Úclickhouse_cityhash.cityhashr   ÚImportErrorÚRuntimeErrorÚnativer   r   Úbufferedreaderr	   Úbufferedwriterr
   Úcompressionr   Údefinesr   Úreaderr   r   Úwriterr   r   r   r8   r   r   r   r   Ú<module>   s"    ÿÿ.