1
# Copyright (C) 2005-2007 Jelmer Vernooij <jelmer@samba.org>
1
# Copyright (C) 2005-2009 Jelmer Vernooij <jelmer@samba.org>
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 3 of the License, or
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
8
8
# This program is distributed in the hope that it will be useful,
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
from bzrlib import debug
17
from bzrlib.knit import make_file_factory
18
from bzrlib.trace import mutter
19
from bzrlib.revision import NULL_REVISION
20
from bzrlib.versionedfile import ConstantMapper
16
22
from bzrlib.plugins.svn.cache import CacheTable
18
class SqliteCachingParentsProvider(object):
24
class DiskCachingParentsProvider(object):
25
"""Parents provider that caches parents in a SQLite database."""
19
27
def __init__(self, actual, cachedb=None):
20
self.cache = ParentsCache(cachedb)
28
self._cache = ParentsCache(cachedb)
21
29
self.actual = actual
23
31
def get_parent_map(self, keys):
27
parents = self.cache.lookup_parents(k)
35
parents = self._cache.lookup_parents(k)
28
36
if parents is None:
33
ret.update(self.actual.get_parent_map(todo))
41
newfound = self.actual.get_parent_map(todo)
42
for revid, parents in newfound.iteritems():
43
if revid == NULL_REVISION:
45
self._cache.insert_parents(revid, parents)
37
50
class ParentsCache(CacheTable):
38
52
def _create_table(self):
39
53
self.cachedb.executescript("""
40
create table if not exists parent (revision text, parent text);
41
create index if not exists parent_revision on parent (revision);
54
create table if not exists parent (rev text, parent text, idx int);
55
create unique index if not exists rev_parent_idx on parent (rev, idx);
56
create unique index if not exists rev_parent on parent (rev, parent);
58
self._commit_interval = 200
44
60
def insert_parents(self, revid, parents):
45
self.mutter('insert parents: %r -> %r' % (revid, parents))
46
for parent in parents:
47
self.cachedb.execute("insert into parent (revision, parent) VALUES (?, ?)", (revid, parent))
61
if "cache" in debug.debug_flags:
62
mutter('insert parents: %r -> %r', revid, parents)
64
self.cachedb.execute("replace into parent (rev, parent, idx) values (?, NULL, -1)", (revid,))
66
for i, p in enumerate(parents):
67
self.cachedb.execute("replace into parent (rev, parent, idx) values (?, ?, ?)", (revid, p, i))
49
69
def lookup_parents(self, revid):
50
self.mutter('lookup parents: %r' % (revid,))
52
for row in self.cachedb.execute("select parent from parent where revision = ?", (revid,)).fetchall():
70
if "cache" in debug.debug_flags:
71
mutter('lookup parents: %r', revid)
72
rows = self.cachedb.execute("select parent from parent where rev = ? order by idx", (revid, )).fetchall()
75
return tuple([row[0].encode("utf-8") for row in rows if row[0] is not None])