Mercurial > hg
comparison mercurial/revlog.py @ 47169:a13f72b9ccfb
revlog: move index reading logic in a dedicated method
They are multiple motivation to do it:
* The logic is complicated enough to deserver its own method.
* We will need to reuse this once we put a docket in use.
* This split the actual reading from the processing of the read data better.
Differential Revision: https://phab.mercurial-scm.org/D10595
author | Pierre-Yves David <pierre-yves.david@octobus.net> |
---|---|
date | Mon, 03 May 2021 12:25:56 +0200 |
parents | cacb08f357ed |
children | 6e0ee1810fbd |
comparison
equal
deleted
inserted
replaced
47168:cacb08f357ed | 47169:a13f72b9ccfb |
---|---|
444 % self._chunkcachesize | 444 % self._chunkcachesize |
445 ) | 445 ) |
446 force_nodemap = opts.get(b'devel-force-nodemap', False) | 446 force_nodemap = opts.get(b'devel-force-nodemap', False) |
447 return newversionflags, mmapindexthreshold, force_nodemap | 447 return newversionflags, mmapindexthreshold, force_nodemap |
448 | 448 |
449 def _get_data(self, filepath, mmap_threshold): | |
450 """return a file content with or without mmap | |
451 | |
452 If the file is missing return the empty string""" | |
453 try: | |
454 with self.opener(filepath) as fp: | |
455 if mmap_threshold is not None: | |
456 file_size = self.opener.fstat(fp).st_size | |
457 if file_size >= mmap_threshold: | |
458 # TODO: should .close() to release resources without | |
459 # relying on Python GC | |
460 return util.buffer(util.mmapread(fp)) | |
461 return fp.read() | |
462 except IOError as inst: | |
463 if inst.errno != errno.ENOENT: | |
464 raise | |
465 return b'' | |
466 | |
449 def _loadindex(self): | 467 def _loadindex(self): |
450 | 468 |
451 newversionflags, mmapindexthreshold, force_nodemap = self._init_opts() | 469 newversionflags, mmapindexthreshold, force_nodemap = self._init_opts() |
452 | 470 |
453 if self.postfix is None: | 471 if self.postfix is None: |
463 self._indexfile = index_file | 481 self._indexfile = index_file |
464 self._datafile = data_file | 482 self._datafile = data_file |
465 | 483 |
466 indexdata = b'' | 484 indexdata = b'' |
467 self._initempty = True | 485 self._initempty = True |
468 try: | 486 indexdata = self._get_data(self._indexfile, mmapindexthreshold) |
469 with self._indexfp() as f: | 487 if len(indexdata) > 0: |
470 if ( | 488 versionflags = INDEX_HEADER.unpack(indexdata[:4])[0] |
471 mmapindexthreshold is not None | 489 self._initempty = False |
472 and self.opener.fstat(f).st_size >= mmapindexthreshold | 490 else: |
473 ): | |
474 # TODO: should .close() to release resources without | |
475 # relying on Python GC | |
476 indexdata = util.buffer(util.mmapread(f)) | |
477 else: | |
478 indexdata = f.read() | |
479 if len(indexdata) > 0: | |
480 versionflags = INDEX_HEADER.unpack(indexdata[:4])[0] | |
481 self._initempty = False | |
482 else: | |
483 versionflags = newversionflags | |
484 except IOError as inst: | |
485 if inst.errno != errno.ENOENT: | |
486 raise | |
487 | |
488 versionflags = newversionflags | 491 versionflags = newversionflags |
489 | 492 |
490 flags = self._format_flags = versionflags & ~0xFFFF | 493 flags = self._format_flags = versionflags & ~0xFFFF |
491 fmt = self._format_version = versionflags & 0xFFFF | 494 fmt = self._format_version = versionflags & 0xFFFF |
492 | 495 |