o ]LbA@sLddlZddlmZddlmZmZdZedZddZGd d d e Z dS) N)_)errorutilisCpartial read of revlog %s; expected %d bytes from offset %d, got %dcCs||d@dko |dkS)Nr)nrrH/usr/lib/python3/dist-packages/mercurial/revlogutils/randomaccessfile.py_is_power_of_twosr c@sheZdZdZ dddZddZddd Zejdd d Z ejd d Z dddZ dddZ ddZ dS)randomaccessfilezDAccessing arbitrary chuncks of data within a file, with some cachingNcCsNt|sJ||_||_||_d|_d|_d|_d|_|r%|\|_|_dSdSNr)r openerfilenamedefault_cached_chunk_sizewriting_handlereading_handle _cached_chunk_cached_chunk_position)selfrrr initial_cacherrr __init__s zrandomaccessfile.__init__cCsd|_d|_dSr )rr)rrrr clear_cache1s zrandomaccessfile.clear_cachercCs|j|j|dS)zReturn a file object)mode)rr)rrrrr _open5szrandomaccessfile._openccsl|dur |VdS|jr|jVdS|jr|jVdS| }|VWddS1s/wYdS)z%File object suitable for reading dataN)rrr)rexisting_file_objfprrr _open_read9s    "zrandomaccessfile._open_readc csx|jdur7|jdur7|jdur7|}||_zdVWd|_nd|_wWddS1s0wYdSdVdS)z4Context manager that keeps the file open for readingN)rrrr)rrrrr readingOs    " zrandomaccessfile.readingcCsh||}|j}|t|j}||kr-||kr-||kr!||kr!|jS||}t|j||S||||S)aRead a chunk of bytes from the file. Accepts an absolute offset, length to read, and an optional existing file handle to read from. If an existing file handle is passed, it will be seeked and the original seek position will NOT be restored. Returns a str or buffer of raw byte data. Raises if the requested number of bytes could not be read. )rlenrrbuffer_read_and_update_cache)roffsetlengthrend cache_start cache_endrelative_startrrr read_chunk`s zrandomaccessfile.read_chunkc Cs||jd@}|||j|jd@|}||}||||}Wdn1s1wY|||||}t||} | |krXt|j||| f} t | ||ks`||krgt |||S|S)Nr) rrseekread_add_cached_chunkr PARTIAL_READ_MSGrr RevlogErrorrr!) rr#r$r real_offset real_lengthfile_objdatarelative_offsetgotmessagerrr r"ys&        z'randomaccessfile._read_and_update_cachecCsL|jt|j|krt|jt|tkr|j|7_dS||_||_dS)z|Add to or replace the cached data chunk. Accepts an absolute offset and the data that is at that location. N)rr r_MAX_CACHED_CHUNK_SIZE)rr#r2rrr r,s  z"randomaccessfile._add_cached_chunk)N)r)__name__ __module__ __qualname____doc__rrr contextlibcontextmanagerrrr)r"r,rrrr r s       r ) r;i18nrrrr6r-r objectr rrrr s