hgext3rd/evolve/depthcache.py
author Anton Shestakov <av6@dwimlabs.net>
Fri, 08 May 2020 22:50:09 +0800
branchmercurial-4.6
changeset 5368 844b1ad5b34b
parent 5176 b9af7b8f3eee
permissions -rw-r--r--
test-compat: merge mercurial-4.7 into mercurial-4.6
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
5176
b9af7b8f3eee compat: drop import compatibility <= 4.5 for `forcebytestr`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 5137
diff changeset
    19
from mercurial.utils.stringutil import forcebytestr
5137
4fef6b157175 py3-exceptions: wrap more Exceptions in forcebytestr before formatting
willstott101@gmail.com
parents: 4814
diff changeset
    20
3236
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    21
from . import (
3408
f4ea9652661d cachevfs: use a compatibility later for all access
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3403
diff changeset
    22
    compat,
3240
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    23
    error,
3236
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    24
    exthelper,
3240
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    25
    genericcaches,
3310
87cb2635352b depthcache: use parents filter in depth cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3299
diff changeset
    26
    utility,
3236
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    27
)
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    28
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    29
from mercurial.i18n import _
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    30
3310
87cb2635352b depthcache: use parents filter in depth cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3299
diff changeset
    31
filterparents = utility.filterparents
87cb2635352b depthcache: use parents filter in depth cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3299
diff changeset
    32
3236
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    33
eh = exthelper.exthelper()
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    34
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    35
def simpledepth(repo, rev):
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    36
    """simple but obviously right implementation of depth"""
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4806
diff changeset
    37
    return len(repo.revs(b'::%d', rev))
3236
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    38
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    39
@eh.command(
4715
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4488
diff changeset
    40
    b'debugdepth',
3236
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    41
    [
4715
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4488
diff changeset
    42
        (b'r', b'rev', [], b'revs to print depth for'),
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4488
diff changeset
    43
        (b'', b'method', b'cached', b"one of 'simple', 'cached', 'compare'"),
3236
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    44
    ],
4715
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4488
diff changeset
    45
    _(b'REVS'))
3236
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    46
def debugdepth(ui, repo, **opts):
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    47
    """display depth of REVS
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    48
    """
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    49
    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
    50
    method = opts['method']
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4806
diff changeset
    51
    if method in (b'cached', b'compare'):
3240
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    52
        cache = repo.depthcache
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    53
        cache.save(repo)
3236
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    54
    for r in revs:
7c78b0c482a1 depth: basic debugdepth implementation and test
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    55
        ctx = repo[r]
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4806
diff changeset
    56
        if method == b'simple':
3240
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    57
            depth = simpledepth(repo, r)
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4806
diff changeset
    58
        elif method == b'cached':
3240
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    59
            depth = cache.get(r)
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4806
diff changeset
    60
        elif method == b'compare':
3240
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    61
            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
    62
            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
    63
            if simple != cached:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4806
diff changeset
    64
                raise error.Abort(b'depth differ for revision %s: %d != %d'
3240
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    65
                                  % (ctx, simple, cached))
3299
c089495d7701 depth: fix compare mode for `debugdepth`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3242
diff changeset
    66
            depth = simple
3240
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    67
        else:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4806
diff changeset
    68
            raise error.Abort(b'unknown method "%s"' % method)
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4806
diff changeset
    69
        ui.write(b'%s %d\n' % (ctx, depth))
3240
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    70
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    71
@eh.reposetup
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    72
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
    73
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    74
    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
    75
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    76
        @localrepo.unfilteredpropertycache
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    77
        def depthcache(self):
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    78
            cache = depthcache()
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    79
            cache.update(self)
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    80
            return cache
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    81
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    82
        @localrepo.unfilteredmethod
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    83
        def destroyed(self):
4804
079dbf36e884 python3: add raw prefix in cases harder to analyze at the token level
Raphaël Gomès <rgomes@octobus.net>
parents: 4755
diff changeset
    84
            if r'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
    85
                self.depthcache.clear()
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    86
            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
    87
3968
37178a2d3557 compat: drop compatibility layer for updatecaches
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3504
diff changeset
    88
        @localrepo.unfilteredmethod
37178a2d3557 compat: drop compatibility layer for updatecaches
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3504
diff changeset
    89
        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
    90
            if utility.shouldwarmcache(self, tr):
37178a2d3557 compat: drop compatibility layer for updatecaches
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3504
diff changeset
    91
                self.depthcache.update(self)
37178a2d3557 compat: drop compatibility layer for updatecaches
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3504
diff changeset
    92
                self.depthcache.save(self)
37178a2d3557 compat: drop compatibility layer for updatecaches
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3504
diff changeset
    93
            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
    94
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
    95
    repo.__class__ = depthcacherepo
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
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
    98
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4806
diff changeset
    99
    _filepath = b'evoext-depthcache-00'
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4806
diff changeset
   100
    _cachename = b'evo-ext-depthcache'
3240
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   101
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   102
    def __init__(self):
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   103
        super(depthcache, self).__init__()
4806
44629ae21b84 python3: add raw prefix to all array.array() calls
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   104
        self._data = array.array(r'l')
3240
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   105
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   106
    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
   107
        if len(self._data) <= rev:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4806
diff changeset
   108
            raise error.ProgrammingError(b'depthcache must be warmed before use')
3240
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   109
        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
   110
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   111
    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
   112
        """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
   113
        """
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   114
        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
   115
        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
   116
4755
8664231b47ac py3: fix progress() functions to not use "%s" with int
Martin von Zweigbergk <martinvonz@google.com>
parents: 4745
diff changeset
   117
        def progress(pos, rev=None):
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4806
diff changeset
   118
            revstr = b'' if rev is None else (b'rev %d' % rev)
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4806
diff changeset
   119
            compat.progress(repo.ui, b'updating depth cache',
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4806
diff changeset
   120
                            pos, revstr, unit=b'revision', total=total)
4755
8664231b47ac py3: fix progress() functions to not use "%s" with int
Martin von Zweigbergk <martinvonz@google.com>
parents: 4745
diff changeset
   121
        progress(0)
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
        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
   123
            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
   124
            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
   125
            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
   126
                progress(idx, rev)
4755
8664231b47ac py3: fix progress() functions to not use "%s" with int
Martin von Zweigbergk <martinvonz@google.com>
parents: 4745
diff changeset
   127
        progress(None)
3240
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   128
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   129
    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
   130
        cl = changelog
3310
87cb2635352b depthcache: use parents filter in depth cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3299
diff changeset
   131
        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
   132
        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
   133
            # root case
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   134
            return 1
3310
87cb2635352b depthcache: use parents filter in depth cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3299
diff changeset
   135
        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
   136
            # linear commit case
3310
87cb2635352b depthcache: use parents filter in depth cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3299
diff changeset
   137
            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
   138
        # 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
   139
        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
   140
        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
   141
        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
   142
        # 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
   143
        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
   144
        if not ancnodes:
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   145
            # 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
   146
            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
   147
        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
   148
            # 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
   149
            # 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
   150
            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
   151
            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
   152
            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
   153
        else:
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   154
            # 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
   155
            # * 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
   156
            # * 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
   157
            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
   158
            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
   159
            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
   160
            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
   161
                                               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
   162
            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
   163
        return revdepth
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   164
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   165
    # cache internal logic
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
    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
   168
        """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
   169
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   170
        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
   171
        recomputed.
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   172
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   173
        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
   174
        """
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   175
        super(depthcache, self).clear()
4806
44629ae21b84 python3: add raw prefix to all array.array() calls
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   176
        self._data = array.array(r'l')
3240
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   177
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   178
    # 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
   179
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   180
    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
   181
        """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
   182
        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
   183
4488
6c0992ce05f7 compat: drop getcachevfs, repo.cachevfs is supported in hg 4.4
Joerg Sonnenberger <joerg@bec.de>
parents: 4341
diff changeset
   184
        data = repo.cachevfs.tryread(self._filepath)
4806
44629ae21b84 python3: add raw prefix to all array.array() calls
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   185
        self._data = array.array(r'l')
3240
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   186
        if not data:
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   187
            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
   188
        else:
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   189
            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
   190
            self._cachekey = self._deserializecachekey(headerdata)
4745
854637e3d2d0 py3: use array.array.{to,from}bytes() on py3
Martin von Zweigbergk <martinvonz@google.com>
parents: 4715
diff changeset
   191
            compat.arrayfrombytes(self._data, data[self._cachekeysize:])
3240
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   192
        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
   193
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   194
    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
   195
        """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
   196
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   197
        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
   198
        bytearray.
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   199
        """
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   200
        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
   201
            return
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   202
4099
84b203a60788 depthcache: ignore permission and OS errors when writing
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3975
diff changeset
   203
        try:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4806
diff changeset
   204
            cachefile = repo.cachevfs(self._filepath, b'w', atomictemp=True)
4099
84b203a60788 depthcache: ignore permission and OS errors when writing
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3975
diff changeset
   205
            headerdata = self._serializecachekey()
84b203a60788 depthcache: ignore permission and OS errors when writing
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3975
diff changeset
   206
            cachefile.write(headerdata)
4745
854637e3d2d0 py3: use array.array.{to,from}bytes() on py3
Martin von Zweigbergk <martinvonz@google.com>
parents: 4715
diff changeset
   207
            cachefile.write(compat.arraytobytes(self._data))
4099
84b203a60788 depthcache: ignore permission and OS errors when writing
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3975
diff changeset
   208
            cachefile.close()
4100
f21187478dcc depthcache: update the variable tracking on disk state after write
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4099
diff changeset
   209
            self._ondiskkey = self._cachekey
4099
84b203a60788 depthcache: ignore permission and OS errors when writing
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3975
diff changeset
   210
        except (IOError, OSError) as exc:
5137
4fef6b157175 py3-exceptions: wrap more Exceptions in forcebytestr before formatting
willstott101@gmail.com
parents: 4814
diff changeset
   211
            repo.ui.log(b'depthcache', b'could not write update %s\n' % forcebytestr(exc))
4fef6b157175 py3-exceptions: wrap more Exceptions in forcebytestr before formatting
willstott101@gmail.com
parents: 4814
diff changeset
   212
            repo.ui.debug(b'depthcache: could not write update %s\n' % forcebytestr(exc))