hgext3rd/evolve/stablerangecache.py
author Martin von Zweigbergk <martinvonz@google.com>
Fri, 19 Apr 2019 10:47:12 -0700
changeset 4591 9242b05eb593
parent 4487 d972041022cb
child 4735 168c270e8347
permissions -rw-r--r--
evolve: use standard InterventionRequired instead of MergeFailure
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 "stable ranges"
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
3308
65af5f9f232b stablerange: abstract the bit able to store cache into sql
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3307
diff changeset
    10
import abc
3342
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
    11
import heapq
3953
2174de498a69 stablerange: build closure a bit less inefficiently
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3952
diff changeset
    12
import random
2237
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
    13
import sqlite3
3342
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
    14
import time
2130
d784622dd5dc stablerange: move the range class in the new module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2129
diff changeset
    15
2125
e0a25339ff17 stablerange: move 'depth' inside a new 'stablerange' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
    16
from mercurial import (
2237
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
    17
    error,
2125
e0a25339ff17 stablerange: move 'depth' inside a new 'stablerange' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
    18
    localrepo,
4020
47b00d3b8ea2 stablerangecache: fix output in the drifted case
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4019
diff changeset
    19
    node as nodemod,
2237
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
    20
    util,
2125
e0a25339ff17 stablerange: move 'depth' inside a new 'stablerange' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
    21
)
e0a25339ff17 stablerange: move 'depth' inside a new 'stablerange' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
    22
3307
a1ab2588a628 stablerange: split pure algorithm part from the on disk cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3306
diff changeset
    23
from . import (
4341
d1aab9d82f5b evolve: adapt for deprecated ui.progress()
Martin von Zweigbergk <martinvonz@google.com>
parents: 4137
diff changeset
    24
    compat,
3307
a1ab2588a628 stablerange: split pure algorithm part from the on disk cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3306
diff changeset
    25
    exthelper,
3342
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
    26
    genericcaches,
3307
a1ab2588a628 stablerange: split pure algorithm part from the on disk cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3306
diff changeset
    27
    stablerange,
3346
f4e28b781143 stablerange: use mergepoint based algorithm for the official stable range
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3344
diff changeset
    28
    utility,
3307
a1ab2588a628 stablerange: split pure algorithm part from the on disk cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3306
diff changeset
    29
)
a1ab2588a628 stablerange: split pure algorithm part from the on disk cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3306
diff changeset
    30
2129
d07bb7cbae2f stablesort: move into the stablerange module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2128
diff changeset
    31
from mercurial.i18n import _
d07bb7cbae2f stablesort: move into the stablerange module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2128
diff changeset
    32
2125
e0a25339ff17 stablerange: move 'depth' inside a new 'stablerange' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
    33
eh = exthelper.exthelper()
3342
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
    34
4055
555028f992eb obshashrange: issue a warning if the cache takes a long time to load
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4029
diff changeset
    35
LONG_WARNING_TIME = 60
555028f992eb obshashrange: issue a warning if the cache takes a long time to load
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4029
diff changeset
    36
555028f992eb obshashrange: issue a warning if the cache takes a long time to load
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4029
diff changeset
    37
LONG_MESSAGE = """Stable range cache is taking a while to load
555028f992eb obshashrange: issue a warning if the cache takes a long time to load
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4029
diff changeset
    38
555028f992eb obshashrange: issue a warning if the cache takes a long time to load
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4029
diff changeset
    39
Your repository is probably big.
555028f992eb obshashrange: issue a warning if the cache takes a long time to load
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4029
diff changeset
    40
555028f992eb obshashrange: issue a warning if the cache takes a long time to load
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4029
diff changeset
    41
Stable range are used for discovery missing osbsolescence markers during
555028f992eb obshashrange: issue a warning if the cache takes a long time to load
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4029
diff changeset
    42
exchange. While the algorithm we use can scale well for large repositories, the
555028f992eb obshashrange: issue a warning if the cache takes a long time to load
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4029
diff changeset
    43
naive python implementation that you are using is not very efficient, the
555028f992eb obshashrange: issue a warning if the cache takes a long time to load
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4029
diff changeset
    44
storage backend for that cache neither.
555028f992eb obshashrange: issue a warning if the cache takes a long time to load
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4029
diff changeset
    45
555028f992eb obshashrange: issue a warning if the cache takes a long time to load
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4029
diff changeset
    46
This computation will finish in a finite amount of time, even for repositories
555028f992eb obshashrange: issue a warning if the cache takes a long time to load
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4029
diff changeset
    47
with millions of revision and many merges. However It might take multiple tens
555028f992eb obshashrange: issue a warning if the cache takes a long time to load
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4029
diff changeset
    48
of minutes to complete in such case.
555028f992eb obshashrange: issue a warning if the cache takes a long time to load
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4029
diff changeset
    49
555028f992eb obshashrange: issue a warning if the cache takes a long time to load
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4029
diff changeset
    50
In the future, better implementation of the algorithm in a more appropriate
555028f992eb obshashrange: issue a warning if the cache takes a long time to load
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4029
diff changeset
    51
language than Python will make it much faster. This data should also get
555028f992eb obshashrange: issue a warning if the cache takes a long time to load
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4029
diff changeset
    52
exchanged between server and clients removing recomputation needs.
555028f992eb obshashrange: issue a warning if the cache takes a long time to load
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4029
diff changeset
    53
555028f992eb obshashrange: issue a warning if the cache takes a long time to load
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4029
diff changeset
    54
In the mean time, got take a break while this cache is warming.
555028f992eb obshashrange: issue a warning if the cache takes a long time to load
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4029
diff changeset
    55
555028f992eb obshashrange: issue a warning if the cache takes a long time to load
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4029
diff changeset
    56
See `hg help -e evolve` for details about how to control obsmarkers discovery and
555028f992eb obshashrange: issue a warning if the cache takes a long time to load
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4029
diff changeset
    57
the update of related cache.
555028f992eb obshashrange: issue a warning if the cache takes a long time to load
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4029
diff changeset
    58
555028f992eb obshashrange: issue a warning if the cache takes a long time to load
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4029
diff changeset
    59
--- Sorry for the delay ---
555028f992eb obshashrange: issue a warning if the cache takes a long time to load
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4029
diff changeset
    60
"""
555028f992eb obshashrange: issue a warning if the cache takes a long time to load
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4029
diff changeset
    61
3342
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
    62
class stablerangeondiskbase(stablerange.stablerangecached,
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
    63
                            genericcaches.changelogsourcebase):
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
    64
    """combine the generic stablerange cache usage with generic changelog one
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
    65
    """
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
    66
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
    67
    def _updatefrom(self, repo, data):
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
    68
        """compute the rev of one revision, assert previous revision has an hot cache
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
    69
        """
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
    70
        repo = repo.unfiltered()
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
    71
        ui = repo.ui
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
    72
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
    73
        rangeheap = []
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
    74
        for r in data:
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
    75
            rangeheap.append((r, 0))
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
    76
        total = len(data)
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
    77
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
    78
        heappop = heapq.heappop
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
    79
        heappush = heapq.heappush
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
    80
        heapify = heapq.heapify
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
    81
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
    82
        original = set(rangeheap)
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
    83
        seen = 0
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
    84
        # progress report is showing up in the profile for small and fast
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
    85
        # repository so we only update it every so often
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
    86
        progress_each = 100
4055
555028f992eb obshashrange: issue a warning if the cache takes a long time to load
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4029
diff changeset
    87
        initial_time = progress_last = time.time()
555028f992eb obshashrange: issue a warning if the cache takes a long time to load
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4029
diff changeset
    88
        warned_long = False
3342
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
    89
        heapify(rangeheap)
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
    90
        while rangeheap:
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
    91
            rangeid = heappop(rangeheap)
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
    92
            if rangeid in original:
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
    93
                if not seen % progress_each:
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
    94
                    # if a lot of time passed, report more often
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
    95
                    progress_new = time.time()
4055
555028f992eb obshashrange: issue a warning if the cache takes a long time to load
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4029
diff changeset
    96
                    if not warned_long and LONG_WARNING_TIME < (progress_new - initial_time):
555028f992eb obshashrange: issue a warning if the cache takes a long time to load
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4029
diff changeset
    97
                        repo.ui.warn(LONG_MESSAGE)
4089
ecd90548a10d stablerange: only issue the message once
Gerald Squelart <gsquelart@mozilla.com>
parents: 4055
diff changeset
    98
                        warned_long = True
3342
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
    99
                    if (1 < progress_each) and (0.1 < progress_new - progress_last):
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
   100
                        progress_each /= 10
4341
d1aab9d82f5b evolve: adapt for deprecated ui.progress()
Martin von Zweigbergk <martinvonz@google.com>
parents: 4137
diff changeset
   101
                    compat.progress(ui, _("filling stablerange cache"), seen,
d1aab9d82f5b evolve: adapt for deprecated ui.progress()
Martin von Zweigbergk <martinvonz@google.com>
parents: 4137
diff changeset
   102
                                    total=total, unit=_("changesets"))
3342
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
   103
                    progress_last = progress_new
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
   104
                seen += 1
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
   105
                original.remove(rangeid) # might have been added from other source
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
   106
            rangeid = rangeid
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
   107
            if self._getsub(rangeid) is None:
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
   108
                for sub in self.subranges(repo, rangeid):
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
   109
                    if self._getsub(sub) is None:
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
   110
                        heappush(rangeheap, sub)
4341
d1aab9d82f5b evolve: adapt for deprecated ui.progress()
Martin von Zweigbergk <martinvonz@google.com>
parents: 4137
diff changeset
   111
        compat.progress(ui, _("filling stablerange cache"), None, total=total)
3342
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
   112
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
   113
    def clear(self, reset=False):
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
   114
        super(stablerangeondiskbase, self).clear()
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
   115
        self._subrangescache.clear()
3194aec207ae stablerange: add a base class for on disk stored stablerange class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3308
diff changeset
   116
2237
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   117
#############################
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   118
### simple sqlite caching ###
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   119
#############################
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   120
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   121
_sqliteschema = [
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   122
    """CREATE TABLE range(rev INTEGER  NOT NULL,
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   123
                          idx INTEGER NOT NULL,
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   124
                          PRIMARY KEY(rev, idx));""",
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   125
    """CREATE TABLE subranges(listidx INTEGER NOT NULL,
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   126
                              suprev  INTEGER NOT NULL,
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   127
                              supidx  INTEGER NOT NULL,
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   128
                              subrev  INTEGER NOT NULL,
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   129
                              subidx  INTEGER NOT NULL,
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   130
                              PRIMARY KEY(listidx, suprev, supidx),
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   131
                              FOREIGN KEY (suprev, supidx) REFERENCES range(rev, idx),
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   132
                              FOREIGN KEY (subrev, subidx) REFERENCES range(rev, idx)
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   133
    );""",
2240
ecb993892c61 stablerange: warm cache before using it server side
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2238
diff changeset
   134
    "CREATE INDEX subranges_index ON subranges (suprev, supidx);",
3356
24b72cbc486f stablerange: add an sql index on subranges id too
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3355
diff changeset
   135
    "CREATE INDEX superranges_index ON subranges (subrev, subidx);",
2240
ecb993892c61 stablerange: warm cache before using it server side
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2238
diff changeset
   136
    "CREATE INDEX range_index ON range (rev, idx);",
4022
082b59126099 sqlcache: initialize meta table last
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4020
diff changeset
   137
    """CREATE TABLE meta(schemaversion INTEGER NOT NULL,
082b59126099 sqlcache: initialize meta table last
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4020
diff changeset
   138
                         tiprev        INTEGER NOT NULL,
082b59126099 sqlcache: initialize meta table last
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4020
diff changeset
   139
                         tipnode       BLOB    NOT NULL
082b59126099 sqlcache: initialize meta table last
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4020
diff changeset
   140
                        );""",
2237
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   141
]
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   142
_newmeta = "INSERT INTO meta (schemaversion, tiprev, tipnode) VALUES (?,?,?);"
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   143
_updatemeta = "UPDATE meta SET tiprev = ?, tipnode = ?;"
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   144
_updaterange = "INSERT INTO range(rev, idx) VALUES (?,?);"
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   145
_updatesubranges = """INSERT
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   146
                       INTO subranges(listidx, suprev, supidx, subrev, subidx)
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   147
                       VALUES (?,?,?,?,?);"""
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   148
_queryexist = "SELECT name FROM sqlite_master WHERE type='table' AND name='meta';"
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   149
_querymeta = "SELECT schemaversion, tiprev, tipnode FROM meta;"
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   150
_queryrange = "SELECT * FROM range WHERE (rev = ? AND idx = ?);"
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   151
_querysubranges = """SELECT subrev, subidx
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   152
                     FROM subranges
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   153
                     WHERE (suprev = ? AND supidx = ?)
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   154
                     ORDER BY listidx;"""
3953
2174de498a69 stablerange: build closure a bit less inefficiently
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3952
diff changeset
   155
2174de498a69 stablerange: build closure a bit less inefficiently
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3952
diff changeset
   156
_querysuperrangesmain = """SELECT DISTINCT suprev, supidx
2174de498a69 stablerange: build closure a bit less inefficiently
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3952
diff changeset
   157
                           FROM subranges
2174de498a69 stablerange: build closure a bit less inefficiently
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3952
diff changeset
   158
                           WHERE %s;"""
2174de498a69 stablerange: build closure a bit less inefficiently
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3952
diff changeset
   159
2174de498a69 stablerange: build closure a bit less inefficiently
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3952
diff changeset
   160
_querysuperrangesbody = '(subrev = %d and subidx = %d)'
2174de498a69 stablerange: build closure a bit less inefficiently
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3952
diff changeset
   161
2174de498a69 stablerange: build closure a bit less inefficiently
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3952
diff changeset
   162
def _make_querysuperranges(ranges):
2174de498a69 stablerange: build closure a bit less inefficiently
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3952
diff changeset
   163
    # building a tree of OR would allow for more ranges
2174de498a69 stablerange: build closure a bit less inefficiently
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3952
diff changeset
   164
    body = ' OR '.join(_querysuperrangesbody % r for r in ranges)
2174de498a69 stablerange: build closure a bit less inefficiently
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3952
diff changeset
   165
    return _querysuperrangesmain % body
2237
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   166
3308
65af5f9f232b stablerange: abstract the bit able to store cache into sql
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3307
diff changeset
   167
class stablerangesqlbase(stablerange.stablerangecached):
65af5f9f232b stablerange: abstract the bit able to store cache into sql
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3307
diff changeset
   168
    """class that can handle all the bits needed to store range into sql
65af5f9f232b stablerange: abstract the bit able to store cache into sql
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3307
diff changeset
   169
    """
2237
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   170
3308
65af5f9f232b stablerange: abstract the bit able to store cache into sql
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3307
diff changeset
   171
    __metaclass__ = abc.ABCMeta
2237
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   172
3308
65af5f9f232b stablerange: abstract the bit able to store cache into sql
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3307
diff changeset
   173
    _schemaversion = None
65af5f9f232b stablerange: abstract the bit able to store cache into sql
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3307
diff changeset
   174
    _cachefile = None
65af5f9f232b stablerange: abstract the bit able to store cache into sql
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3307
diff changeset
   175
65af5f9f232b stablerange: abstract the bit able to store cache into sql
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3307
diff changeset
   176
    def __init__(self, repo, **kwargs):
65af5f9f232b stablerange: abstract the bit able to store cache into sql
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3307
diff changeset
   177
        super(stablerangesqlbase, self).__init__(**kwargs)
2400
9a53ed7e5540 stablerangecache: avoid crash when 'cache/' directory is missing
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2381
diff changeset
   178
        self._vfs = repo.vfs
4487
d972041022cb evolve: when using evolve, cache files belong into the master repo
Joerg Sonnenberger <joerg@bec.de>
parents: 4341
diff changeset
   179
        self._path = repo.cachevfs.join(self._cachefile)
2237
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   180
        self._cl = repo.unfiltered().changelog # (okay to keep an old one)
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   181
        self._ondisktiprev = None
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   182
        self._ondisktipnode = None
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   183
        self._unsavedsubranges = {}
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   184
3355
c261eece1eab obshashrange: less brutal reset when receiving markers on existing node
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3353
diff changeset
   185
    def contains(self, repo, revs):
3954
b53bf9942e0a obshashrange: do not search for affected stable range cache is unavailable
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3953
diff changeset
   186
        con = self._con
b53bf9942e0a obshashrange: do not search for affected stable range cache is unavailable
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3953
diff changeset
   187
        assert con is not None
3355
c261eece1eab obshashrange: less brutal reset when receiving markers on existing node
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3353
diff changeset
   188
        new = set()
c261eece1eab obshashrange: less brutal reset when receiving markers on existing node
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3353
diff changeset
   189
        known = set()
c261eece1eab obshashrange: less brutal reset when receiving markers on existing node
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3353
diff changeset
   190
        depth = repo.depthcache.get
c261eece1eab obshashrange: less brutal reset when receiving markers on existing node
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3353
diff changeset
   191
        for r in revs:
c261eece1eab obshashrange: less brutal reset when receiving markers on existing node
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3353
diff changeset
   192
            new.add((r, depth(r) - 1))
c261eece1eab obshashrange: less brutal reset when receiving markers on existing node
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3353
diff changeset
   193
            new.add((r, 0))
3954
b53bf9942e0a obshashrange: do not search for affected stable range cache is unavailable
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3953
diff changeset
   194
        while new:
3953
2174de498a69 stablerange: build closure a bit less inefficiently
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3952
diff changeset
   195
            if len(new) < 300:
2174de498a69 stablerange: build closure a bit less inefficiently
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3952
diff changeset
   196
                sample = new
2174de498a69 stablerange: build closure a bit less inefficiently
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3952
diff changeset
   197
            else:
2174de498a69 stablerange: build closure a bit less inefficiently
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3952
diff changeset
   198
                sample = random.sample(new, 300)
2174de498a69 stablerange: build closure a bit less inefficiently
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3952
diff changeset
   199
            known.update(sample)
2174de498a69 stablerange: build closure a bit less inefficiently
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3952
diff changeset
   200
            query = _make_querysuperranges(sample)
2174de498a69 stablerange: build closure a bit less inefficiently
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3952
diff changeset
   201
            ranges = set(con.execute(query).fetchall())
3355
c261eece1eab obshashrange: less brutal reset when receiving markers on existing node
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3353
diff changeset
   202
            new.update(ranges)
c261eece1eab obshashrange: less brutal reset when receiving markers on existing node
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3353
diff changeset
   203
            new -= known
c261eece1eab obshashrange: less brutal reset when receiving markers on existing node
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3353
diff changeset
   204
        return sorted(known)
c261eece1eab obshashrange: less brutal reset when receiving markers on existing node
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3353
diff changeset
   205
2237
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   206
    def _getsub(self, rangeid):
3308
65af5f9f232b stablerange: abstract the bit able to store cache into sql
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3307
diff changeset
   207
        # 1) check the in memory cache
65af5f9f232b stablerange: abstract the bit able to store cache into sql
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3307
diff changeset
   208
        # 2) check the sqlcaches (and warm in memory cache we want we find)
2237
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   209
        cache = self._subrangescache
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   210
        if rangeid not in cache and rangeid[0] <= self._ondisktiprev and self._con is not None:
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   211
            value = None
4024
210f8abdfcd7 sqlcache: protect read query too
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4023
diff changeset
   212
            try:
210f8abdfcd7 sqlcache: protect read query too
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4023
diff changeset
   213
                result = self._con.execute(_queryrange, rangeid).fetchone()
210f8abdfcd7 sqlcache: protect read query too
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4023
diff changeset
   214
                if result is not None: # database know about this node (skip in the future?)
210f8abdfcd7 sqlcache: protect read query too
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4023
diff changeset
   215
                    value = self._con.execute(_querysubranges, rangeid).fetchall()
210f8abdfcd7 sqlcache: protect read query too
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4023
diff changeset
   216
                # in memory caching of the value
210f8abdfcd7 sqlcache: protect read query too
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4023
diff changeset
   217
                cache[rangeid] = value
4027
130a60a51fff sqlcache: also catch malformed database error
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4026
diff changeset
   218
            except (sqlite3.DatabaseError, sqlite3.OperationalError):
4024
210f8abdfcd7 sqlcache: protect read query too
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4023
diff changeset
   219
                # something is wrong with the sqlite db
210f8abdfcd7 sqlcache: protect read query too
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4023
diff changeset
   220
                # Since this is a cache, we ignore it.
210f8abdfcd7 sqlcache: protect read query too
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4023
diff changeset
   221
                if '_con' in vars(self):
210f8abdfcd7 sqlcache: protect read query too
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4023
diff changeset
   222
                    del self._con
210f8abdfcd7 sqlcache: protect read query too
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4023
diff changeset
   223
                self._unsavedsubranges.clear()
210f8abdfcd7 sqlcache: protect read query too
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4023
diff changeset
   224
2237
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   225
        return cache.get(rangeid)
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   226
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   227
    def _setsub(self, rangeid, value):
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   228
        assert rangeid not in self._unsavedsubranges
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   229
        self._unsavedsubranges[rangeid] = value
3308
65af5f9f232b stablerange: abstract the bit able to store cache into sql
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3307
diff changeset
   230
        super(stablerangesqlbase, self)._setsub(rangeid, value)
2237
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   231
2400
9a53ed7e5540 stablerangecache: avoid crash when 'cache/' directory is missing
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2381
diff changeset
   232
    def _db(self):
9a53ed7e5540 stablerangecache: avoid crash when 'cache/' directory is missing
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2381
diff changeset
   233
        try:
9a53ed7e5540 stablerangecache: avoid crash when 'cache/' directory is missing
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2381
diff changeset
   234
            util.makedirs(self._vfs.dirname(self._path))
9a53ed7e5540 stablerangecache: avoid crash when 'cache/' directory is missing
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2381
diff changeset
   235
        except OSError:
9a53ed7e5540 stablerangecache: avoid crash when 'cache/' directory is missing
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2381
diff changeset
   236
            return None
4017
c307ce1d9009 sqlcache: passe better connection option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3966
diff changeset
   237
        con = sqlite3.connect(self._path, timeout=30, isolation_level="IMMEDIATE")
2400
9a53ed7e5540 stablerangecache: avoid crash when 'cache/' directory is missing
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2381
diff changeset
   238
        con.text_factory = str
9a53ed7e5540 stablerangecache: avoid crash when 'cache/' directory is missing
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2381
diff changeset
   239
        return con
9a53ed7e5540 stablerangecache: avoid crash when 'cache/' directory is missing
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2381
diff changeset
   240
2237
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   241
    @util.propertycache
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   242
    def _con(self):
2400
9a53ed7e5540 stablerangecache: avoid crash when 'cache/' directory is missing
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2381
diff changeset
   243
        con = self._db()
9a53ed7e5540 stablerangecache: avoid crash when 'cache/' directory is missing
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2381
diff changeset
   244
        if con is None:
9a53ed7e5540 stablerangecache: avoid crash when 'cache/' directory is missing
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2381
diff changeset
   245
            return None
2237
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   246
        cur = con.execute(_queryexist)
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   247
        if cur.fetchone() is None:
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   248
            return None
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   249
        meta = con.execute(_querymeta).fetchone()
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   250
        if meta is None:
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   251
            return None
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   252
        if meta[0] != self._schemaversion:
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   253
            return None
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   254
        if len(self._cl) <= meta[1]:
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   255
            return None
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   256
        if self._cl.node(meta[1]) != meta[2]:
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   257
            return None
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   258
        self._ondisktiprev = meta[1]
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   259
        self._ondisktipnode = meta[2]
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   260
        if self._tiprev < self._ondisktiprev:
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   261
            self._tiprev = self._ondisktiprev
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   262
            self._tipnode = self._ondisktipnode
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   263
        return con
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   264
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   265
    def _save(self, repo):
4023
f59b262400fb sqlite: fast path when nothing to save
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4022
diff changeset
   266
        if not self._unsavedsubranges:
f59b262400fb sqlite: fast path when nothing to save
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4022
diff changeset
   267
            return
4018
b3517f834f83 sqlcache: cache OperationError when saving
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4017
diff changeset
   268
        try:
b3517f834f83 sqlcache: cache OperationError when saving
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4017
diff changeset
   269
            return self._trysave(repo)
4105
dda7746647f2 stablerangecache: be more paranoid in the errors we catch
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4089
diff changeset
   270
        except (IOError, OSError, sqlite3.DatabaseError, sqlite3.OperationalError, sqlite3.IntegrityError) as exc:
4019
8a6a2c37c0fa sqlcache: also ignore integrity error
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4018
diff changeset
   271
            # Catch error that may arise under stress
8a6a2c37c0fa sqlcache: also ignore integrity error
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4018
diff changeset
   272
            #
8a6a2c37c0fa sqlcache: also ignore integrity error
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4018
diff changeset
   273
            # operational error catch read-only and locked database
8a6a2c37c0fa sqlcache: also ignore integrity error
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4018
diff changeset
   274
            # IntegrityError catch Unique constraint error that may arise
8a6a2c37c0fa sqlcache: also ignore integrity error
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4018
diff changeset
   275
            if '_con' in vars(self):
8a6a2c37c0fa sqlcache: also ignore integrity error
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4018
diff changeset
   276
                del self._con
8a6a2c37c0fa sqlcache: also ignore integrity error
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4018
diff changeset
   277
            self._unsavedsubranges.clear()
4018
b3517f834f83 sqlcache: cache OperationError when saving
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4017
diff changeset
   278
            repo.ui.log('evoext-cache', 'error while saving new data: %s' % exc)
4109
d562316c548f caches: issue both debug and blackbox log message
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4105
diff changeset
   279
            repo.ui.debug('evoext-cache: error while saving new data: %s' % exc)
4018
b3517f834f83 sqlcache: cache OperationError when saving
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4017
diff changeset
   280
b3517f834f83 sqlcache: cache OperationError when saving
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4017
diff changeset
   281
    def _trysave(self, repo):
2237
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   282
        repo = repo.unfiltered()
3240
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   283
        repo.depthcache.save(repo)
3966
ea7687a9a4b8 obshashrange: force saving of stablesort and firstmerge cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3964
diff changeset
   284
        repo.stablesort.save(repo)
ea7687a9a4b8 obshashrange: force saving of stablesort and firstmerge cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3964
diff changeset
   285
        repo.firstmergecache.save(repo)
3240
9361149224a7 depthcache: move to a dedicated object and storage
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3236
diff changeset
   286
        if not self._unsavedsubranges:
2237
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   287
            return # no new data
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   288
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   289
        if self._con is None:
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   290
            util.unlinkpath(self._path, ignoremissing=True)
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   291
            if '_con' in vars(self):
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   292
                del self._con
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   293
2400
9a53ed7e5540 stablerangecache: avoid crash when 'cache/' directory is missing
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2381
diff changeset
   294
            con = self._db()
9a53ed7e5540 stablerangecache: avoid crash when 'cache/' directory is missing
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2381
diff changeset
   295
            if con is None:
9a53ed7e5540 stablerangecache: avoid crash when 'cache/' directory is missing
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2381
diff changeset
   296
                return
2237
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   297
            with con:
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   298
                for req in _sqliteschema:
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   299
                    con.execute(req)
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   300
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   301
                meta = [self._schemaversion,
4026
b375398074b0 stablerange: rework saving logic to be clearer and safer
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4024
diff changeset
   302
                        nodemod.nullrev,
b375398074b0 stablerange: rework saving logic to be clearer and safer
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4024
diff changeset
   303
                        nodemod.nullid,
2237
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   304
                ]
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   305
                con.execute(_newmeta, meta)
4026
b375398074b0 stablerange: rework saving logic to be clearer and safer
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4024
diff changeset
   306
                self._ondisktiprev = nodemod.nullrev
b375398074b0 stablerange: rework saving logic to be clearer and safer
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4024
diff changeset
   307
                self._ondisktipnode = nodemod.nullid
2237
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   308
        else:
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   309
            con = self._con
4026
b375398074b0 stablerange: rework saving logic to be clearer and safer
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4024
diff changeset
   310
        with con:
2237
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   311
            meta = con.execute(_querymeta).fetchone()
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   312
            if meta[2] != self._ondisktipnode or meta[1] != self._ondisktiprev:
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   313
                # drifting is currently an issue because this means another
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   314
                # process might have already added the cache line we are about
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   315
                # to add. This will confuse sqlite
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   316
                msg = _('stable-range cache: skipping write, '
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   317
                        'database drifted under my feet\n')
4020
47b00d3b8ea2 stablerangecache: fix output in the drifted case
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4019
diff changeset
   318
                hint = _('(disk: %s-%s vs mem: %s-%s)\n')
47b00d3b8ea2 stablerangecache: fix output in the drifted case
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4019
diff changeset
   319
                data = (nodemod.hex(meta[2]), meta[1],
47b00d3b8ea2 stablerangecache: fix output in the drifted case
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4019
diff changeset
   320
                        nodemod.hex(self._ondisktipnode), self._ondisktiprev)
2237
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   321
                repo.ui.warn(msg)
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   322
                repo.ui.warn(hint % data)
4026
b375398074b0 stablerange: rework saving logic to be clearer and safer
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4024
diff changeset
   323
                self._unsavedsubranges.clear()
2237
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   324
                return
4026
b375398074b0 stablerange: rework saving logic to be clearer and safer
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4024
diff changeset
   325
            else:
b375398074b0 stablerange: rework saving logic to be clearer and safer
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4024
diff changeset
   326
                self._saverange(con, repo)
b375398074b0 stablerange: rework saving logic to be clearer and safer
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4024
diff changeset
   327
                meta = [self._tiprev,
b375398074b0 stablerange: rework saving logic to be clearer and safer
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4024
diff changeset
   328
                        self._tipnode,
b375398074b0 stablerange: rework saving logic to be clearer and safer
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4024
diff changeset
   329
                ]
b375398074b0 stablerange: rework saving logic to be clearer and safer
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4024
diff changeset
   330
                con.execute(_updatemeta, meta)
2237
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   331
        self._ondisktiprev = self._tiprev
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   332
        self._ondisktipnode = self._tipnode
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   333
        self._unsavedsubranges.clear()
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   334
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   335
    def _saverange(self, con, repo):
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   336
        repo = repo.unfiltered()
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   337
        data = []
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   338
        allranges = set()
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   339
        for key, value in self._unsavedsubranges.items():
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   340
            allranges.add(key)
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   341
            for idx, sub in enumerate(value):
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   342
                data.append((idx, key[0], key[1], sub[0], sub[1]))
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   343
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   344
        con.executemany(_updaterange, allranges)
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   345
        con.executemany(_updatesubranges, data)
98e0369b548b stablerange: introduce ondisk caching through sqlite
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2236
diff changeset
   346
3343
86a03229d32d stablerange: introduce a sql backed version of the generic on disk cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3342
diff changeset
   347
class stablerangesql(stablerangesqlbase, stablerangeondiskbase):
86a03229d32d stablerange: introduce a sql backed version of the generic on disk cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3342
diff changeset
   348
    """base clase able to preserve data to disk as sql"""
86a03229d32d stablerange: introduce a sql backed version of the generic on disk cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3342
diff changeset
   349
86a03229d32d stablerange: introduce a sql backed version of the generic on disk cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3342
diff changeset
   350
    __metaclass__ = abc.ABCMeta
86a03229d32d stablerange: introduce a sql backed version of the generic on disk cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3342
diff changeset
   351
86a03229d32d stablerange: introduce a sql backed version of the generic on disk cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3342
diff changeset
   352
    # self._cachekey = (tiprev, tipnode)
86a03229d32d stablerange: introduce a sql backed version of the generic on disk cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3342
diff changeset
   353
86a03229d32d stablerange: introduce a sql backed version of the generic on disk cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3342
diff changeset
   354
    @property
86a03229d32d stablerange: introduce a sql backed version of the generic on disk cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3342
diff changeset
   355
    def _tiprev(self):
86a03229d32d stablerange: introduce a sql backed version of the generic on disk cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3342
diff changeset
   356
        return self._cachekey[0]
86a03229d32d stablerange: introduce a sql backed version of the generic on disk cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3342
diff changeset
   357
86a03229d32d stablerange: introduce a sql backed version of the generic on disk cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3342
diff changeset
   358
    @_tiprev.setter
86a03229d32d stablerange: introduce a sql backed version of the generic on disk cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3342
diff changeset
   359
    def _tiprev(self, value):
86a03229d32d stablerange: introduce a sql backed version of the generic on disk cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3342
diff changeset
   360
        self._cachekey = (value, self._cachekey[1])
86a03229d32d stablerange: introduce a sql backed version of the generic on disk cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3342
diff changeset
   361
86a03229d32d stablerange: introduce a sql backed version of the generic on disk cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3342
diff changeset
   362
    @property
86a03229d32d stablerange: introduce a sql backed version of the generic on disk cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3342
diff changeset
   363
    def _tipnode(self):
86a03229d32d stablerange: introduce a sql backed version of the generic on disk cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3342
diff changeset
   364
        return self._cachekey[1]
86a03229d32d stablerange: introduce a sql backed version of the generic on disk cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3342
diff changeset
   365
86a03229d32d stablerange: introduce a sql backed version of the generic on disk cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3342
diff changeset
   366
    @_tipnode.setter
86a03229d32d stablerange: introduce a sql backed version of the generic on disk cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3342
diff changeset
   367
    def _tipnode(self, value):
86a03229d32d stablerange: introduce a sql backed version of the generic on disk cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3342
diff changeset
   368
        self._cachekey = (self._cachekey[0], value)
86a03229d32d stablerange: introduce a sql backed version of the generic on disk cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3342
diff changeset
   369
86a03229d32d stablerange: introduce a sql backed version of the generic on disk cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3342
diff changeset
   370
    def clear(self, reset=False):
86a03229d32d stablerange: introduce a sql backed version of the generic on disk cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3342
diff changeset
   371
        super(stablerangesql, self).clear(reset=reset)
3348
895e16385a4f stablerange: be more cautious when deleting the sql connection
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3347
diff changeset
   372
        if '_con' in vars(self):
895e16385a4f stablerange: be more cautious when deleting the sql connection
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3347
diff changeset
   373
            del self._con
3343
86a03229d32d stablerange: introduce a sql backed version of the generic on disk cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3342
diff changeset
   374
        self._subrangescache.clear()
86a03229d32d stablerange: introduce a sql backed version of the generic on disk cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3342
diff changeset
   375
86a03229d32d stablerange: introduce a sql backed version of the generic on disk cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3342
diff changeset
   376
    def load(self, repo):
3346
f4e28b781143 stablerange: use mergepoint based algorithm for the official stable range
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3344
diff changeset
   377
        """load data from disk"""
f4e28b781143 stablerange: use mergepoint based algorithm for the official stable range
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3344
diff changeset
   378
        assert repo.filtername is None
f4e28b781143 stablerange: use mergepoint based algorithm for the official stable range
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3344
diff changeset
   379
        self._cachekey = self.emptykey
f4e28b781143 stablerange: use mergepoint based algorithm for the official stable range
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3344
diff changeset
   380
f4e28b781143 stablerange: use mergepoint based algorithm for the official stable range
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3344
diff changeset
   381
        if self._con is not None:
f4e28b781143 stablerange: use mergepoint based algorithm for the official stable range
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3344
diff changeset
   382
            self._cachekey = (self._ondisktiprev, self._ondisktipnode)
f4e28b781143 stablerange: use mergepoint based algorithm for the official stable range
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3344
diff changeset
   383
        self._ondiskkey = self._cachekey
3343
86a03229d32d stablerange: introduce a sql backed version of the generic on disk cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3342
diff changeset
   384
86a03229d32d stablerange: introduce a sql backed version of the generic on disk cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3342
diff changeset
   385
    def save(self, repo):
3346
f4e28b781143 stablerange: use mergepoint based algorithm for the official stable range
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3344
diff changeset
   386
        if self._cachekey is None or self._cachekey == self._ondiskkey:
f4e28b781143 stablerange: use mergepoint based algorithm for the official stable range
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3344
diff changeset
   387
            return
3343
86a03229d32d stablerange: introduce a sql backed version of the generic on disk cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3342
diff changeset
   388
        self._save(repo)
86a03229d32d stablerange: introduce a sql backed version of the generic on disk cache
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3342
diff changeset
   389
3344
4d054737a79c stablerange: have a mergepoint based sql backed class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3343
diff changeset
   390
class mergepointsql(stablerangesql, stablerange.stablerange_mergepoint):
4d054737a79c stablerange: have a mergepoint based sql backed class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3343
diff changeset
   391
4137
21a3c051ca6c stablerange: fix slicing of arbitrary ranges
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4109
diff changeset
   392
    _schemaversion = 3
4487
d972041022cb evolve: when using evolve, cache files belong into the master repo
Joerg Sonnenberger <joerg@bec.de>
parents: 4341
diff changeset
   393
    _cachefile = 'evoext_stablerange_v2.sqlite'
3346
f4e28b781143 stablerange: use mergepoint based algorithm for the official stable range
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3344
diff changeset
   394
    _cachename = 'evo-ext-stablerange-mergepoint'
3344
4d054737a79c stablerange: have a mergepoint based sql backed class
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3343
diff changeset
   395
3308
65af5f9f232b stablerange: abstract the bit able to store cache into sql
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3307
diff changeset
   396
class sqlstablerange(stablerangesqlbase, stablerange.stablerange):
65af5f9f232b stablerange: abstract the bit able to store cache into sql
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3307
diff changeset
   397
65af5f9f232b stablerange: abstract the bit able to store cache into sql
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3307
diff changeset
   398
    _schemaversion = 1
4487
d972041022cb evolve: when using evolve, cache files belong into the master repo
Joerg Sonnenberger <joerg@bec.de>
parents: 4341
diff changeset
   399
    _cachefile = 'evoext_stablerange_v1.sqlite'
3308
65af5f9f232b stablerange: abstract the bit able to store cache into sql
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3307
diff changeset
   400
65af5f9f232b stablerange: abstract the bit able to store cache into sql
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3307
diff changeset
   401
    def warmup(self, repo, upto=None):
65af5f9f232b stablerange: abstract the bit able to store cache into sql
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3307
diff changeset
   402
        self._con # make sure the data base is loaded
65af5f9f232b stablerange: abstract the bit able to store cache into sql
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3307
diff changeset
   403
        try:
65af5f9f232b stablerange: abstract the bit able to store cache into sql
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3307
diff changeset
   404
            # samelessly lock the repo to ensure nobody will update the repo
65af5f9f232b stablerange: abstract the bit able to store cache into sql
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3307
diff changeset
   405
            # concurently. This should not be too much of an issue if we warm
65af5f9f232b stablerange: abstract the bit able to store cache into sql
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3307
diff changeset
   406
            # at the end of the transaction.
65af5f9f232b stablerange: abstract the bit able to store cache into sql
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3307
diff changeset
   407
            #
65af5f9f232b stablerange: abstract the bit able to store cache into sql
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3307
diff changeset
   408
            # XXX However, we lock even if we are up to date so we should check
65af5f9f232b stablerange: abstract the bit able to store cache into sql
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3307
diff changeset
   409
            # before locking
65af5f9f232b stablerange: abstract the bit able to store cache into sql
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3307
diff changeset
   410
            with repo.lock():
65af5f9f232b stablerange: abstract the bit able to store cache into sql
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3307
diff changeset
   411
                super(sqlstablerange, self).warmup(repo, upto)
65af5f9f232b stablerange: abstract the bit able to store cache into sql
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3307
diff changeset
   412
                self._save(repo)
65af5f9f232b stablerange: abstract the bit able to store cache into sql
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3307
diff changeset
   413
        except error.LockError:
65af5f9f232b stablerange: abstract the bit able to store cache into sql
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3307
diff changeset
   414
            # Exceptionnally we are noisy about it since performance impact is
65af5f9f232b stablerange: abstract the bit able to store cache into sql
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3307
diff changeset
   415
            # large We should address that before using this more widely.
65af5f9f232b stablerange: abstract the bit able to store cache into sql
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3307
diff changeset
   416
            repo.ui.warn('stable-range cache: unable to lock repo while warming\n')
65af5f9f232b stablerange: abstract the bit able to store cache into sql
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3307
diff changeset
   417
            repo.ui.warn('(cache will not be saved)\n')
65af5f9f232b stablerange: abstract the bit able to store cache into sql
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3307
diff changeset
   418
            super(sqlstablerange, self).warmup(repo, upto)
65af5f9f232b stablerange: abstract the bit able to store cache into sql
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3307
diff changeset
   419
2125
e0a25339ff17 stablerange: move 'depth' inside a new 'stablerange' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
   420
@eh.reposetup
e0a25339ff17 stablerange: move 'depth' inside a new 'stablerange' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
   421
def setupcache(ui, repo):
e0a25339ff17 stablerange: move 'depth' inside a new 'stablerange' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
   422
e0a25339ff17 stablerange: move 'depth' inside a new 'stablerange' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
   423
    class stablerangerepo(repo.__class__):
e0a25339ff17 stablerange: move 'depth' inside a new 'stablerange' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
   424
e0a25339ff17 stablerange: move 'depth' inside a new 'stablerange' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
   425
        @localrepo.unfilteredpropertycache
e0a25339ff17 stablerange: move 'depth' inside a new 'stablerange' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
   426
        def stablerange(self):
3646
0dd393a32567 stablerangecache: use self instead of repo in stablerangerepo.stablerange()
Anton Shestakov <av6@dwimlabs.net>
parents: 3504
diff changeset
   427
            cache = mergepointsql(self)
0dd393a32567 stablerangecache: use self instead of repo in stablerangerepo.stablerange()
Anton Shestakov <av6@dwimlabs.net>
parents: 3504
diff changeset
   428
            cache.update(self)
3346
f4e28b781143 stablerange: use mergepoint based algorithm for the official stable range
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3344
diff changeset
   429
            return cache
2125
e0a25339ff17 stablerange: move 'depth' inside a new 'stablerange' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
   430
2236
c0e2ba85e76a stablerange: drop the cache on 'destroyed'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2235
diff changeset
   431
        @localrepo.unfilteredmethod
c0e2ba85e76a stablerange: drop the cache on 'destroyed'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2235
diff changeset
   432
        def destroyed(self):
c0e2ba85e76a stablerange: drop the cache on 'destroyed'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2235
diff changeset
   433
            if 'stablerange' in vars(self):
3346
f4e28b781143 stablerange: use mergepoint based algorithm for the official stable range
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3344
diff changeset
   434
                self.stablerange.clear()
2236
c0e2ba85e76a stablerange: drop the cache on 'destroyed'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2235
diff changeset
   435
                del self.stablerange
2287
18b8dc058f75 repo: properly progate "destroyed" call to super class
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2240
diff changeset
   436
            super(stablerangerepo, self).destroyed()
2236
c0e2ba85e76a stablerange: drop the cache on 'destroyed'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2235
diff changeset
   437
3968
37178a2d3557 compat: drop compatibility layer for updatecaches
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3966
diff changeset
   438
        @localrepo.unfilteredmethod
37178a2d3557 compat: drop compatibility layer for updatecaches
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3966
diff changeset
   439
        def updatecaches(self, tr=None, **kwargs):
37178a2d3557 compat: drop compatibility layer for updatecaches
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3966
diff changeset
   440
            if utility.shouldwarmcache(self, tr):
37178a2d3557 compat: drop compatibility layer for updatecaches
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3966
diff changeset
   441
                self.stablerange.update(self)
37178a2d3557 compat: drop compatibility layer for updatecaches
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3966
diff changeset
   442
                self.stablerange.save(self)
37178a2d3557 compat: drop compatibility layer for updatecaches
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3966
diff changeset
   443
            super(stablerangerepo, self).updatecaches(tr, **kwargs)
2238
aac765e84de3 stablerange: warm cache on transaction (if obshashrange is enabled)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 2237
diff changeset
   444
2125
e0a25339ff17 stablerange: move 'depth' inside a new 'stablerange' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
   445
    repo.__class__ = stablerangerepo
3952
a7794f5abacd discovery: make sure repository wrapping happens in the right order
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3659
diff changeset
   446
a7794f5abacd discovery: make sure repository wrapping happens in the right order
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3659
diff changeset
   447
eh.merge(stablerange.eh)