HEX
Server: Apache
System: Linux infong-uk86 4.4.400-icpu-106 #2 SMP Mon Sep 15 08:23:40 UTC 2025 x86_64
User: u44115835 (4976590)
PHP: 8.4.17
Disabled: NONE
Upload Files
File: //kunden/lib/python3/dist-packages/breezy/__pycache__/chunk_writer.cpython-39.pyc
a

�*�^�.�@s<dZddlmZddlZddlmZmZGdd�de�ZdS)z@ChunkWriter: write compressed data out with a fixed upper bound.�)�absolute_importN)�Z_FINISH�Z_SYNC_FLUSHc@sHeZdZdZdZdZddd�Zdd	�Zddd�Zddd�Z	ddd�Z
d
S)�ChunkWriterayChunkWriter allows writing of compressed data with a fixed size.

    If less data is supplied than fills a chunk, the chunk is padded with
    NULL bytes. If more data is supplied, then the writer packs as much
    in as it can, but never splits any item it was given.

    The algorithm for packing is open to improvement! Current it is:
     - write the bytes given
     - if the total seen bytes so far exceeds the chunk size, flush.

    :cvar _max_repack: To fit the maximum number of entries into a node, we
        will sometimes start over and compress the whole list to get tighter
        packing. We get diminishing returns after a while, so this limits the
        number of times we will try.
        The default is to try to avoid recompressing entirely, but setting this
        to something like 20 will give maximum compression.

    :cvar _max_zsync: Another tunable nob. If _max_repack is set to 0, then you
        can limit the number of times we will try to pack more data into a
        node. This allows us to do a single compression pass, rather than
        trying until we overflow, and then recompressing again.
    )r�)�rrFcCsP||_t��|_g|_g|_d|_d|_d|_d|_	d|_
||_|j|d�dS)aACreate a ChunkWriter to write chunk_size chunks.

        :param chunk_size: The total byte count to emit at the end of the
            chunk.
        :param reserved: How many bytes to allow for reserved data. reserved
            data space can only be written to via the write(...,
            reserved=True).
        rN)�for_size)
�
chunk_size�zlib�compressobj�
compressor�bytes_in�
bytes_list�
bytes_out_len�unflushed_in_bytes�
num_repack�	num_zsync�unused_bytes�
reserved_size�set_optimize)�selfr	�reservedZoptimize_for_size�r�5/usr/lib/python3/dist-packages/breezy/chunk_writer.py�__init__bs	
zChunkWriter.__init__cCs~d|_|j�t�}|j�|�|jt|�7_|j|jkrPt	d|j|jf��|j|j}|rp|j�d|�|j|j
|fS)a�Finish the chunk.

        This returns the final compressed chunk, and either None, or the
        bytes that did not fit in the chunk.

        :return: (compressed_bytes, unused_bytes, num_nulls_needed)

            * compressed_bytes: a list of bytes that were output from the
              compressor. If the compressed length was not exactly chunk_size,
              the final string will be a string of all null bytes to pad this
              to chunk_size
            * unused_bytes: None, or the last bytes that were added, which we
              could not fit.
            * num_nulls_needed: How many nulls are padded at the end
        Nz:Somehow we ended up with too much compressed data, %d > %d�)r
r�flushrr�appendr�lenr	�AssertionErrorr)r�outZnulls_neededrrr�finishys
�zChunkWriter.finishTcCs"|rtj}ntj}|\|_|_dS)z�Change how we optimize our writes.

        :param for_size: If True, optimize for minimum space usage, otherwise
            optimize for fastest writing speed.
        :return: None
        N)r�_repack_opts_for_size�_repack_opts_for_speed�_max_repack�
_max_zsync)rrZoptsrrrr�szChunkWriter.set_optimizeNc	Csrt��}g}|j}|j}|jD]}||�}|r||�q|rZ||�}||�t�7}||�ttt	|��}|||fS)a�Recompress the current bytes_in, and optionally more.

        :param extra_bytes: Optional, if supplied we will add it with
            Z_SYNC_FLUSH
        :return: (bytes_out, bytes_out_len, alt_compressed)

            * bytes_out: is the compressed bytes returned from the compressor
            * bytes_out_len: the length of the compressed output
            * compressor: An object with everything packed in so far, and
              Z_SYNC_FLUSH called.
        )
r
rr�compressr
rr�sum�mapr)	rZextra_bytesr�	bytes_outrr&Zaccepted_bytesr rrrr�_recompress_all_bytes_in�s

z$ChunkWriter._recompress_all_bytes_incCs�|j|jkr|s||_dS|r&|j}n|j|j}|j}|jt|�}||jd}||kr�|�	|�}|r�|j
�|�|jt|�7_|j�|�|jt|�7_�nN|j
d7_
|jdkr�|j
|jkr�|jd7_||_dS|�	|�}||�t�7}d|_|�r(|j
�|�|jt|�7_|jdk�r:d}nd}|j||k�r\|j�|�n�|jd7_|�|�\}	}
}|j|jk�r�|jd7_|
d|k�r�|��\}	}
}||_|jd|_|	|_
|
|_||_dS||_|j�|�|	|_
|
|_dS)a4Write some bytes to the chunk.

        If the bytes fit, False is returned. Otherwise True is returned
        and the bytes have not been added to the chunk.

        :param bytes: The bytes to include
        :param reserved: If True, we can use the space reserved in the
            constructor.
        T�
�r�dF)rr$rr	rrrrrr&rrr
rr%rrr*)r�bytesrZcapacity�compZnext_unflushedZremaining_capacityr Z
safety_marginr)Zthis_lenrrrr�write�sj

	
��zChunkWriter.write)rF)T)N)F)�__name__�
__module__�__qualname__�__doc__r#r"rr!rr*r0rrrrrsD


r)r4Z
__future__rr
rr�objectrrrrr�<module>s