Merge 0.4.
[jelmer/subvertpy.git] / parents.py
1 # Copyright (C) 2005-2007 Jelmer Vernooij <jelmer@samba.org>
2  
3 # This program is free software; you can redistribute it and/or modify
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
6 # (at your option) any later version.
7
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12
13 # You should have received a copy of the GNU General Public License
14 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 from bzrlib.plugins.svn.cache import CacheTable
17
18 class SqliteCachingParentsProvider(object):
19     def __init__(self, actual, cachedb=None):
20         self.cache = ParentsCache(cachedb)
21         self.actual = actual
22
23     def get_parent_map(self, keys):
24         ret = {}
25         todo = set()
26         for k in keys:
27             parents = self.cache.lookup_parents(k)
28             if parents is None:
29                 todo.add(k)
30             else:
31                 ret[k] = parents
32         if len(todo):
33             ret.update(self.actual.get_parent_map(todo))
34         return ret
35
36
37 class ParentsCache(CacheTable):
38     def _create_table(self):
39         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);
42         """)
43
44     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))
48
49     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 == []:
55             return None
56         return tuple(ret)
57