##// END OF EJS Templates
randomaccessfile: drop explicit passing of file description...
marmoute -
r51920:3314c41c default
parent child Browse files
Show More
@@ -55,17 +55,13 b' class randomaccessfile:'
55 return self.opener(self.filename, mode=mode)
55 return self.opener(self.filename, mode=mode)
56
56
57 @contextlib.contextmanager
57 @contextlib.contextmanager
58 def _open_read(self, existing_file_obj=None):
58 def _read_handle(self):
59 """File object suitable for reading data"""
59 """File object suitable for reading data"""
60 # Use explicit file handle, if given.
61 if existing_file_obj is not None:
62 yield existing_file_obj
63
64 # Use a file handle being actively used for writes, if available.
60 # Use a file handle being actively used for writes, if available.
65 # There is some danger to doing this because reads will seek the
61 # There is some danger to doing this because reads will seek the
66 # file. However, revlog._writeentry performs a SEEK_END before all
62 # file. However, revlog._writeentry performs a SEEK_END before all
67 # writes, so we should be safe.
63 # writes, so we should be safe.
68 elif self.writing_handle:
64 if self.writing_handle:
69 yield self.writing_handle
65 yield self.writing_handle
70
66
71 elif self.reading_handle:
67 elif self.reading_handle:
@@ -93,7 +89,7 b' class randomaccessfile:'
93 else:
89 else:
94 yield
90 yield
95
91
96 def read_chunk(self, offset, length, existing_file_obj=None):
92 def read_chunk(self, offset, length):
97 """Read a chunk of bytes from the file.
93 """Read a chunk of bytes from the file.
98
94
99 Accepts an absolute offset, length to read, and an optional existing
95 Accepts an absolute offset, length to read, and an optional existing
@@ -116,9 +112,9 b' class randomaccessfile:'
116 relative_start = offset - cache_start
112 relative_start = offset - cache_start
117 return util.buffer(self._cached_chunk, relative_start, length)
113 return util.buffer(self._cached_chunk, relative_start, length)
118
114
119 return self._read_and_update_cache(offset, length, existing_file_obj)
115 return self._read_and_update_cache(offset, length)
120
116
121 def _read_and_update_cache(self, offset, length, existing_file_obj=None):
117 def _read_and_update_cache(self, offset, length):
122 # Cache data both forward and backward around the requested
118 # Cache data both forward and backward around the requested
123 # data, in a fixed size window. This helps speed up operations
119 # data, in a fixed size window. This helps speed up operations
124 # involving reading the revlog backwards.
120 # involving reading the revlog backwards.
@@ -127,7 +123,7 b' class randomaccessfile:'
127 (offset + length + self.default_cached_chunk_size)
123 (offset + length + self.default_cached_chunk_size)
128 & ~(self.default_cached_chunk_size - 1)
124 & ~(self.default_cached_chunk_size - 1)
129 ) - real_offset
125 ) - real_offset
130 with self._open_read(existing_file_obj) as file_obj:
126 with self._read_handle() as file_obj:
131 file_obj.seek(real_offset)
127 file_obj.seek(real_offset)
132 data = file_obj.read(real_length)
128 data = file_obj.read(real_length)
133
129
General Comments 0
You need to be logged in to leave comments. Login now