|
| 1 | +# Copyright (c) Facebook, Inc. and its affiliates. |
| 2 | +# |
| 3 | +# This source code is licensed under the MIT license found in the |
| 4 | +# LICENSE file in the root directory of this source tree. |
| 5 | + |
| 6 | +import os |
| 7 | +import typing as tp |
| 8 | + |
| 9 | + |
| 10 | +def _safe_readline(fd) -> str: |
| 11 | + pos = fd.tell() |
| 12 | + while True: |
| 13 | + try: |
| 14 | + return fd.readline() |
| 15 | + except UnicodeDecodeError: |
| 16 | + pos -= 1 |
| 17 | + fd.seek(pos) # search where this character begins |
| 18 | + |
| 19 | + |
| 20 | +def find_offsets(filename: str, num_chunks: int) -> tp.List[int]: |
| 21 | + """ |
| 22 | + given a file and a number of chuncks, find the offsets in the file |
| 23 | + to be able to chunk around full lines. |
| 24 | + """ |
| 25 | + with open(filename, "r", encoding="utf-8") as f: |
| 26 | + size = os.fstat(f.fileno()).st_size |
| 27 | + chunk_size = size // num_chunks |
| 28 | + offsets = [0 for _ in range(num_chunks + 1)] |
| 29 | + for i in range(1, num_chunks): |
| 30 | + f.seek(chunk_size * i) |
| 31 | + _safe_readline(f) |
| 32 | + offsets[i] = f.tell() |
| 33 | + offsets[-1] = size |
| 34 | + return offsets |
| 35 | + |
| 36 | + |
| 37 | +class ChunkLineIterator: |
| 38 | + """ |
| 39 | + Iterator to properly iterate over lines of a file chunck. |
| 40 | + """ |
| 41 | + |
| 42 | + def __init__(self, fd, start_offset: int, end_offset: int): |
| 43 | + self._fd = fd |
| 44 | + self._start_offset = start_offset |
| 45 | + self._end_offset = end_offset |
| 46 | + |
| 47 | + def __iter__(self) -> tp.Iterable[str]: |
| 48 | + self._fd.seek(self._start_offset) |
| 49 | + # next(f) breaks f.tell(), hence readline() must be used |
| 50 | + line = _safe_readline(self._fd) |
| 51 | + while line: |
| 52 | + pos = self._fd.tell() |
| 53 | + # f.tell() does not always give the byte position in the file |
| 54 | + # sometimes it skips to a very large number |
| 55 | + # it is unlikely that through a normal read we go from |
| 56 | + # end bytes to end + 2**32 bytes (4 GB) and this makes it unlikely |
| 57 | + # that the procedure breaks by the undeterministic behavior of |
| 58 | + # f.tell() |
| 59 | + if ( |
| 60 | + self._end_offset > 0 |
| 61 | + and pos > self._end_offset |
| 62 | + and pos < self._end_offset + 2**32 |
| 63 | + ): |
| 64 | + break |
| 65 | + yield line |
| 66 | + line = self._fd.readline() |
| 67 | + |
| 68 | + |
| 69 | +class Chunker: |
| 70 | + """ |
| 71 | + contextmanager to read a chunck of a file line by line. |
| 72 | + """ |
| 73 | + |
| 74 | + def __init__(self, path: str, start_offset: int, end_offset: int): |
| 75 | + self.path = path |
| 76 | + self.start_offset = start_offset |
| 77 | + self.end_offset = end_offset |
| 78 | + |
| 79 | + def __enter__(self) -> ChunkLineIterator: |
| 80 | + self.fd = open(self.path, "r", encoding="utf-8") |
| 81 | + return ChunkLineIterator(self.fd, self.start_offset, self.end_offset) |
| 82 | + |
| 83 | + def __exit__(self, exc_type, exc_val, exc_tb) -> None: |
| 84 | + self.fd.close() |
0 commit comments