~ubuntu-branches/debian/sid/bzr-svn/sid

« back to all changes in this revision

Viewing changes to parents.py

  • Committer: Bazaar Package Importer
  • Author(s): Jelmer Vernooij
  • Date: 2009-03-10 14:38:42 UTC
  • mfrom: (1.2.1 upstream) (3.1.4 jaunty)
  • Revision ID: james.westby@ubuntu.com-20090310143842-ucp9fxog1yi3la8f
Tags: 0.5.3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2007 Jelmer Vernooij <jelmer@samba.org>
 
1
# Copyright (C) 2005-2009 Jelmer Vernooij <jelmer@samba.org>
2
2
 
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.
7
7
 
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/>.
15
15
 
 
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
 
21
 
16
22
from bzrlib.plugins.svn.cache import CacheTable
17
23
 
18
 
class SqliteCachingParentsProvider(object):
 
24
class DiskCachingParentsProvider(object):
 
25
    """Parents provider that caches parents in a SQLite database."""
 
26
 
19
27
    def __init__(self, actual, cachedb=None):
20
 
        self.cache = ParentsCache(cachedb)
 
28
        self._cache = ParentsCache(cachedb)
21
29
        self.actual = actual
22
30
 
23
31
    def get_parent_map(self, keys):
24
32
        ret = {}
25
33
        todo = set()
26
34
        for k in keys:
27
 
            parents = self.cache.lookup_parents(k)
 
35
            parents = self._cache.lookup_parents(k)
28
36
            if parents is None:
29
37
                todo.add(k)
30
38
            else:
31
39
                ret[k] = parents
32
 
        if len(todo):
33
 
            ret.update(self.actual.get_parent_map(todo))
 
40
        if len(todo) > 0:
 
41
            newfound = self.actual.get_parent_map(todo)
 
42
            for revid, parents in newfound.iteritems():
 
43
                if revid == NULL_REVISION:
 
44
                    continue
 
45
                self._cache.insert_parents(revid, parents)
 
46
            ret.update(newfound)
34
47
        return ret
35
48
 
36
49
 
37
50
class ParentsCache(CacheTable):
 
51
 
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);
42
57
        """)
 
58
        self._commit_interval = 200
43
59
 
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)
 
63
        if len(parents) == 0:
 
64
            self.cachedb.execute("replace into parent (rev, parent, idx) values (?, NULL, -1)", (revid,))
 
65
        else:
 
66
            for i, p in enumerate(parents):
 
67
                self.cachedb.execute("replace into parent (rev, parent, idx) values (?, ?, ?)", (revid, p, i))
48
68
 
49
69
    def lookup_parents(self, revid):
50
 
        self.mutter('lookup parents: %r' % (revid,))
51
 
        ret = []
52
 
        for row in self.cachedb.execute("select parent from parent where revision = ?", (revid,)).fetchall():
53
 
            ret.append(row[0])
54
 
        if ret == []:
 
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()
 
73
        if len(rows) == 0:
55
74
            return None
56
 
        return tuple(ret)
57
 
 
 
75
        return tuple([row[0].encode("utf-8") for row in rows if row[0] is not None])