randomaccessfile: drop explicit passing of file description
authorPierre-Yves David <pierre-yves.david@octobus.net>
Sat, 30 Sep 2023 02:02:36 +0200
changeset 51027 3314c41c3759
parent 51026 8520db304f01
child 51028 f70ce1aedbcb
randomaccessfile: drop explicit passing of file description The goal of this object is to manage IO, we still have to open the file if necessary, but this is all internal now.
mercurial/revlogutils/randomaccessfile.py
--- a/mercurial/revlogutils/randomaccessfile.py	Tue Sep 26 02:54:50 2023 +0200
+++ b/mercurial/revlogutils/randomaccessfile.py	Sat Sep 30 02:02:36 2023 +0200
@@ -55,17 +55,13 @@
         return self.opener(self.filename, mode=mode)
 
     @contextlib.contextmanager
-    def _open_read(self, existing_file_obj=None):
+    def _read_handle(self):
         """File object suitable for reading data"""
-        # Use explicit file handle, if given.
-        if existing_file_obj is not None:
-            yield existing_file_obj
-
         # Use a file handle being actively used for writes, if available.
         # There is some danger to doing this because reads will seek the
         # file. However, revlog._writeentry performs a SEEK_END before all
         # writes, so we should be safe.
-        elif self.writing_handle:
+        if self.writing_handle:
             yield self.writing_handle
 
         elif self.reading_handle:
@@ -93,7 +89,7 @@
         else:
             yield
 
-    def read_chunk(self, offset, length, existing_file_obj=None):
+    def read_chunk(self, offset, length):
         """Read a chunk of bytes from the file.
 
         Accepts an absolute offset, length to read, and an optional existing
@@ -116,9 +112,9 @@
             relative_start = offset - cache_start
             return util.buffer(self._cached_chunk, relative_start, length)
 
-        return self._read_and_update_cache(offset, length, existing_file_obj)
+        return self._read_and_update_cache(offset, length)
 
-    def _read_and_update_cache(self, offset, length, existing_file_obj=None):
+    def _read_and_update_cache(self, offset, length):
         # Cache data both forward and backward around the requested
         # data, in a fixed size window. This helps speed up operations
         # involving reading the revlog backwards.
@@ -127,7 +123,7 @@
             (offset + length + self.default_cached_chunk_size)
             & ~(self.default_cached_chunk_size - 1)
         ) - real_offset
-        with self._open_read(existing_file_obj) as file_obj:
+        with self._read_handle() as file_obj:
             file_obj.seek(real_offset)
             data = file_obj.read(real_length)