B
    F.\              	   @   s<  d Z ddlmZmZmZ ddlZddlZddlZddlm	Z	m
Z
 y*ddlZedZeddrhed W n ek
r   d	ZY nX dd
lmZmZ de	e	diZed ed	< dZeeZee  ddlZejejded< W dQ R X ee* ddlZdd Zejeded< dZW dQ R X eep ddlZyddlZej jZ!ej jZ"W n" ek
rr   ej#Z!ej$Z"Y nX dd Z%dd Z&e%e&ded< dZW dQ R X ee* ddlZe
ejdddejded< W dQ R X ej'(dZ)e)dkre)ekre)Zn e*de)d+e,e-e.ef dd Z/d"ddZ0d d! ZdS )#zY
Record known compressors

Includes utilities for determining whether or not to compress
    )print_functiondivisionabsolute_importN)identitypartial   bloscZ
releasegilTF)ignoringensure_bytes)compress
decompresszlibc             C   s    t | ttfrt| } t| S )N)
isinstance
memoryview	bytearraybytessnappyr   )data r   ?lib/python3.7/site-packages/distributed/protocol/compression.py_fixed_snappy_decompress-   s    r   r   c             C   s>   yt | S  tk
r8   t| ttfr2t t| S  Y nX d S )N)lz4_compress	TypeErrorr   r   r   r   )r   r   r   r   _fixed_lz4_compressG   s    r   c          	   C   sB   yt | S  ttfk
r<   t| ttfr6t t| S  Y nX d S )N)lz4_decompress
ValueErrorr   r   r   r   r   )r   r   r   r   _fixed_lz4_decompressP   s    r   lz4   )clevelcnamezdistributed.comm.compressionautoz<Default compression '%s' not found.
Choices include auto, %sz, c                s    fddt |D }g }x8t|dd D ]$\}}|t| ||d   q.W ||d    fddt||D }dtt|S )z Sample a bytestring from many locations

    Parameters
    ----------
    b: bytes or memoryview
    size: int
        size of each sample to collect
    n: int
        number of samples to collect
    c                s    g | ]}t d t  qS )r   )randomZrandintlen).0j)bsizer   r   
<listcomp>y   s    zbyte_sample.<locals>.<listcomp>N   c                s   g | ]\}} || qS r   r   )r$   startend)r&   r   r   r(      s        )range	enumerateappendminzipjoinmapr
   )r&   r'   nZstartsZendsir+   partsr   )r&   r'   r   byte_samplen   s     r8        @c       	      C   s  t jd}|dkrt}|s$d| fS t| |k r8d| fS t| dkrLd| fS t|}t|}t| d }t| ||}t||dt| krd| fS t| t	kr| j
t|  }nt| }trtrt| t	krtj| | j
ddd	}d
}n|t| }t|d| krd| fS ||fS dS )aX  
    Maybe compress payload

    1.  We don't compress small messages
    2.  We sample the payload in a few spots, compress that, and if it doesn't
        do any good we return the original
    3.  We then compress the full original, it it doesn't compress well then we
        return the original
    4.  We return the compressed result
    zdistributed.comm.compressionr!   Nl        r   g?r   r   )Ztypesizer    r   r   )daskconfiggetdefault_compressionr#   intcompressionsr8   typer   itemsizer   r   r
   )	ZpayloadZmin_sizeZsample_sizeZnsamplescompressionr   ZsamplenbytesZ
compressedr   r   r   maybe_compress   s4    

rD   c             C   s   dd t | d |D S )z: Decompress frames according to information in the header c             S   s    g | ]\}}t | d  |qS )r   )r?   )r$   cframer   r   r   r(      s   zdecompress.<locals>.<listcomp>rB   )r2   )headerZframesr   r   r   r      s    r   )r9   r9   r   )1__doc__Z
__future__r   r   r   Zloggingr"   r:   Ztoolzr   r   r   Zset_nthreadsr5   hasattrZset_releasegilImportErrorZutilsr	   r
   r?   r=   Z	getLogger__name__Zloggerr   r   r   r   r   r   Z	lz4.blockblockr   r   ZLZ4_compressZLZ4_uncompressr   r   r;   r<   defaultr   r3   sortedr4   strr8   rD   r   r   r   r   <module>   sp   







		



3