hgext3rd/evolve/depthcache.py
author Pierre-Yves David <pierre-yves.david@octobus.net>
Thu, 13 Sep 2018 17:08:05 +0200
branchstable
changeset 4099 84b203a60788
parent 3975 68d414cde579
child 4100 f21187478dcc
permissions -rw-r--r--
depthcache: ignore permission and OS errors when writing This cache is related to the obshashrange one and we update it lazily by default. This can be an issue when pulling locally from a read only repository that was not configured for a more aggressive cache warming. The raised permission error was uncaught and could crash the whole process. Errors during Cache update should not block Mercurial operations.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3403
73920cb25af3 stablerange: add missing licence and copyright header
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3366
diff changeset
     1
# Code dedicated to the caching of changeset depth
73920cb25af3 stablerange: add missing licence and copyright header
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3366
diff changeset
     2
#
73920cb25af3 stablerange: add missing licence and copyright header
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3366
diff changeset
     3
# These stable ranges are use for obsolescence markers discovery
73920cb25af3 stablerange: add missing licence and copyright header
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3366
diff changeset
     4
#
73920cb25af3 stablerange: add missing licence and copyright header
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3366
diff changeset
     5
# Copyright 2017 Pierre-Yves David <pierre-yves.david@ens-lyon.org>
73920cb25af3 stablerange: add missing licence and copyright header
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3366
diff changeset
     6
#
73920cb25af3 stablerange: add missing licence and copyright header
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3366
diff changeset
     7
# This software may be used and distributed according to the terms of the
73920cb25af3 stablerange: add missing licence and copyright header
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3366
diff changeset
     8
# GNU General Public License version 2 or any later version.
73920cb25af3 stablerange: add missing licence and copyright header
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3366
diff changeset
     9
3236
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    10
from __future__ import absolute_import
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    11
3240
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    12
import array
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    13
3236
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    14
from mercurial import (
3240
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    15
    localrepo,
3236
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    16
    scmutil,
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    17
)
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    18
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    19
from . import (
3408
f4ea9652661d cachevfs: use a compatibility later for all access
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3403
diff changeset
    20
    compat,
3240
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    21
    error,
3236
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    22
    exthelper,
3240
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    23
    genericcaches,
3310
87cb2635352b depthcache: use parents filter in depth cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3299
diff changeset
    24
    utility,
3236
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    25
)
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    26
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    27
from mercurial.i18n import _
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    28
3310
87cb2635352b depthcache: use parents filter in depth cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3299
diff changeset
    29
filterparents = utility.filterparents
87cb2635352b depthcache: use parents filter in depth cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3299
diff changeset
    30
3236
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    31
eh = exthelper.exthelper()
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    32
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    33
def simpledepth(repo, rev):
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    34
    """simple but obviously right implementation of depth"""
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    35
    return len(repo.revs('::%d', rev))
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    36
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    37
@eh.command(
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    38
    'debugdepth',
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    39
    [
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    40
        ('r', 'rev', [], 'revs to print depth for'),
3240
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    41
        ('', 'method', 'cached', "one of 'simple', 'cached', 'compare'"),
3236
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    42
    ],
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    43
    _('REVS'))
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    44
def debugdepth(ui, repo, **opts):
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    45
    """display depth of REVS
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    46
    """
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    47
    revs = scmutil.revrange(repo, opts['rev'])
3240
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    48
    method = opts['method']
3299
c089495d7701 depth: fix compare mode for `debugdepth`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3242
diff changeset
    49
    if method in ('cached', 'compare'):
3240
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    50
        cache = repo.depthcache
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    51
        cache.save(repo)
3236
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    52
    for r in revs:
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    53
        ctx = repo[r]
3240
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    54
        if method == 'simple':
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    55
            depth = simpledepth(repo, r)
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    56
        elif method == 'cached':
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    57
            depth = cache.get(r)
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    58
        elif method == 'compare':
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    59
            simple = simpledepth(repo, r)
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    60
            cached = cache.get(r)
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    61
            if simple != cached:
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    62
                raise error.Abort('depth differ for revision %s: %d != %d'
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    63
                                  % (ctx, simple, cached))
3299
c089495d7701 depth: fix compare mode for `debugdepth`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3242
diff changeset
    64
            depth = simple
3240
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    65
        else:
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    66
            raise error.Abort('unknown method "%s"' % method)
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    67
        ui.write('%s %d\n' % (ctx, depth))
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    68
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    69
@eh.reposetup
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    70
def setupcache(ui, repo):
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    71
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    72
    class depthcacherepo(repo.__class__):
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    73
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    74
        @localrepo.unfilteredpropertycache
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    75
        def depthcache(self):
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    76
            cache = depthcache()
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    77
            cache.update(self)
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    78
            return cache
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    79
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    80
        @localrepo.unfilteredmethod
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    81
        def destroyed(self):
3332
7d4c157b6519 stablerange: add a new 'firstmerge' cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3310
diff changeset
    82
            if 'depthcache' in vars(self):
3240
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    83
                self.depthcache.clear()
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    84
            super(depthcacherepo, self).destroyed()
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    85
3968
37178a2d3557 compat: drop compatibility layer for updatecaches
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3504
diff changeset
    86
        @localrepo.unfilteredmethod
37178a2d3557 compat: drop compatibility layer for updatecaches
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3504
diff changeset
    87
        def updatecaches(self, tr=None, **kwargs):
37178a2d3557 compat: drop compatibility layer for updatecaches
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3504
diff changeset
    88
            if utility.shouldwarmcache(self, tr):
37178a2d3557 compat: drop compatibility layer for updatecaches
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3504
diff changeset
    89
                self.depthcache.update(self)
37178a2d3557 compat: drop compatibility layer for updatecaches
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3504
diff changeset
    90
                self.depthcache.save(self)
37178a2d3557 compat: drop compatibility layer for updatecaches
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3504
diff changeset
    91
            super(depthcacherepo, self).updatecaches(tr, **kwargs)
3240
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    92
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    93
    repo.__class__ = depthcacherepo
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    94
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    95
class depthcache(genericcaches.changelogsourcebase):
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    96
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    97
    _filepath = 'evoext-depthcache-00'
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    98
    _cachename = 'evo-ext-depthcache'
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    99
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   100
    def __init__(self):
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   101
        super(depthcache, self).__init__()
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   102
        self._data = array.array('l')
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   103
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   104
    def get(self, rev):
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   105
        if len(self._data) <= rev:
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   106
            raise error.ProgrammingError('depthcache must be warmed before use')
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   107
        return self._data[rev]
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   108
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   109
    def _updatefrom(self, repo, data):
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   110
        """compute the rev of one revision, assert previous revision has an hot cache
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   111
        """
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   112
        cl = repo.unfiltered().changelog
3241
0d2c095aeb2a depthcache: issue a progress bar when loading the depth cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3240
diff changeset
   113
        total = len(data)
0d2c095aeb2a depthcache: issue a progress bar when loading the depth cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3240
diff changeset
   114
0d2c095aeb2a depthcache: issue a progress bar when loading the depth cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3240
diff changeset
   115
        def progress(pos, rev):
0d2c095aeb2a depthcache: issue a progress bar when loading the depth cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3240
diff changeset
   116
            repo.ui.progress('updating depth cache',
0d2c095aeb2a depthcache: issue a progress bar when loading the depth cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3240
diff changeset
   117
                             pos, 'rev %s' % rev, unit='revision', total=total)
0d2c095aeb2a depthcache: issue a progress bar when loading the depth cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3240
diff changeset
   118
        progress(0, '')
0d2c095aeb2a depthcache: issue a progress bar when loading the depth cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3240
diff changeset
   119
        for idx, rev in enumerate(data, 1):
3240
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   120
            assert rev == len(self._data), (rev, len(self._data))
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   121
            self._data.append(self._depth(cl, rev))
3241
0d2c095aeb2a depthcache: issue a progress bar when loading the depth cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3240
diff changeset
   122
            if not (idx % 10000): # progress as a too high performance impact
0d2c095aeb2a depthcache: issue a progress bar when loading the depth cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3240
diff changeset
   123
                progress(idx, rev)
0d2c095aeb2a depthcache: issue a progress bar when loading the depth cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3240
diff changeset
   124
        progress(None, '')
3240
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   125
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   126
    def _depth(self, changelog, rev):
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   127
        cl = changelog
3310
87cb2635352b depthcache: use parents filter in depth cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3299
diff changeset
   128
        ps = filterparents(cl.parentrevs(rev))
87cb2635352b depthcache: use parents filter in depth cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3299
diff changeset
   129
        if not ps:
3240
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   130
            # root case
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   131
            return 1
3310
87cb2635352b depthcache: use parents filter in depth cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3299
diff changeset
   132
        elif len(ps) == 1:
3240
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   133
            # linear commit case
3310
87cb2635352b depthcache: use parents filter in depth cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3299
diff changeset
   134
            return self.get(ps[0]) + 1
3240
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   135
        # merge case, must find the amount of exclusive content
3310
87cb2635352b depthcache: use parents filter in depth cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3299
diff changeset
   136
        p1, p2 = ps
3240
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   137
        depth_p1 = self.get(p1)
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   138
        depth_p2 = self.get(p2)
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   139
        # computing depth of a merge
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   140
        ancnodes = cl.commonancestorsheads(cl.node(p1), cl.node(p2))
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   141
        if not ancnodes:
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   142
            # unrelated branch, (no common root)
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   143
            revdepth = depth_p1 + depth_p2 + 1
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   144
        elif len(ancnodes) == 1:
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   145
            # one unique branch point:
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   146
            # we can compute depth without any walk
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   147
            ancrev = cl.rev(ancnodes[0])
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   148
            depth_anc = self.get(ancrev)
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   149
            revdepth = depth_p1 + (depth_p2 - depth_anc) + 1
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   150
        else:
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   151
            # we pick the parent that is that is
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   152
            # * the deepest (less changeset outside of it),
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   153
            # * lowest revs because more chance to have descendant of other "above"
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   154
            parents = [(p1, depth_p1), (p2, depth_p2)]
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   155
            parents.sort(key=lambda x: (x[1], -x[0]))
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   156
            revdepth = parents[1][1]
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   157
            revdepth += len(cl.findmissingrevs(common=[parents[1][0]],
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   158
                                               heads=[parents[0][0]]))
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   159
            revdepth += 1 # the merge revision
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   160
        return revdepth
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   161
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   162
    # cache internal logic
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   163
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   164
    def clear(self, reset=False):
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   165
        """invalidate the cache content
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   166
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   167
        if 'reset' is passed, we detected a strip and the cache will have to be
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   168
        recomputed.
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   169
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   170
        Subclasses MUST overide this method to actually affect the cache data.
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   171
        """
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   172
        super(depthcache, self).clear()
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   173
        self._data = array.array('l')
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   174
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   175
    # crude version of a cache, to show the kind of information we have to store
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   176
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   177
    def load(self, repo):
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   178
        """load data from disk"""
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   179
        assert repo.filtername is None
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   180
3408
f4ea9652661d cachevfs: use a compatibility later for all access
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3403
diff changeset
   181
        cachevfs = compat.getcachevfs(repo)
f4ea9652661d cachevfs: use a compatibility later for all access
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3403
diff changeset
   182
        data = cachevfs.tryread(self._filepath)
3240
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   183
        self._data = array.array('l')
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   184
        if not data:
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   185
            self._cachekey = self.emptykey
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   186
        else:
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   187
            headerdata = data[:self._cachekeysize]
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   188
            self._cachekey = self._deserializecachekey(headerdata)
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   189
            self._data.fromstring(data[self._cachekeysize:])
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   190
        self._ondiskkey = self._cachekey
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   191
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   192
    def save(self, repo):
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   193
        """save the data to disk
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   194
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   195
        Format is pretty simple, we serialise the cache key and then drop the
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   196
        bytearray.
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   197
        """
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   198
        if self._cachekey is None or self._cachekey == self._ondiskkey:
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   199
            return
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   200
4099
84b203a60788 depthcache: ignore permission and OS errors when writing
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3975
diff changeset
   201
        try:
84b203a60788 depthcache: ignore permission and OS errors when writing
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3975
diff changeset
   202
            cachevfs = compat.getcachevfs(repo)
84b203a60788 depthcache: ignore permission and OS errors when writing
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3975
diff changeset
   203
            cachefile = cachevfs(self._filepath, 'w', atomictemp=True)
84b203a60788 depthcache: ignore permission and OS errors when writing
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3975
diff changeset
   204
            headerdata = self._serializecachekey()
84b203a60788 depthcache: ignore permission and OS errors when writing
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3975
diff changeset
   205
            cachefile.write(headerdata)
84b203a60788 depthcache: ignore permission and OS errors when writing
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3975
diff changeset
   206
            cachefile.write(self._data.tostring())
84b203a60788 depthcache: ignore permission and OS errors when writing
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3975
diff changeset
   207
            cachefile.close()
84b203a60788 depthcache: ignore permission and OS errors when writing
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3975
diff changeset
   208
        except (IOError, OSError) as exc:
84b203a60788 depthcache: ignore permission and OS errors when writing
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3975
diff changeset
   209
            repo.ui.debug('depthcache: could not write update %s\n' % exc)