Edgewall Software

Ticket #1271: cache.py

File cache.py, 6.2 KB (added by Manuzhai, 3 years ago)

New patch to do the main work for this in a different place.

Line 
1# -*- coding: iso8859-1 -*-
2#
3# Copyright (C) 2005 Edgewall Software
4# Copyright (C) 2005 Christopher Lenz <cmlenz@gmx.de>
5# All rights reserved.
6#
7# This software is licensed as described in the file COPYING, which
8# you should have received as part of this distribution. The terms
9# are also available at http://trac.edgewall.com/license.html.
10#
11# This software consists of voluntary contributions made by many
12# individuals. For the exact contribution history, see the revision
13# history and logs, available at http://projects.edgewall.com/trac/.
14#
15# Author: Christopher Lenz <cmlenz@gmx.de>
16
17from trac.util import TracError
18from trac.versioncontrol import Changeset, Node, Repository, Authorizer
19
20
21_kindmap = {'D': Node.DIRECTORY, 'F': Node.FILE}
22_actionmap = {'A': Changeset.ADD, 'C': Changeset.COPY,
23              'D': Changeset.DELETE, 'E': Changeset.EDIT,
24              'M': Changeset.MOVE}
25
26
27class CachedRepository(Repository):
28
29    def __init__(self, db, repos, authz, log):
30        Repository.__init__(self, repos.name, authz, log)
31        self.db = db
32        self.repos = repos
33        self.synced = 0
34
35    def close(self):
36        self.repos.close()
37
38    def get_changeset(self, rev):
39        if not self.synced:
40            self.sync()
41            self.synced = 1
42        return CachedChangeset(self.repos.normalize_rev(rev), self.db,
43                               self.authz)
44
45    def sync(self, first = None, last = None):
46        self.log.debug("Checking whether sync with repository is needed")
47        cursor = self.db.cursor()
48
49        # -- repository used for populating the cache
50        cursor.execute("SELECT value FROM system WHERE name='repository_dir'")
51        row = cursor.fetchone()
52        if row:
53            previous_repository_dir = row[0]
54        else: # no 'repository_dir' stored yet, assume everything's OK
55            previous_repository_dir = self.name
56
57        if self.name != previous_repository_dir:
58            raise TracError, ("The 'repository_dir' has changed, "
59                              "a 'trac-admin resync' operation is needed.")
60
61        youngest_stored = self.repos.get_youngest_rev_in_cache(self.db)
62        if youngest_stored != str(self.repos.youngest_rev) or first or last:
63            authz = self.repos.authz
64            self.repos.authz = Authorizer() # remove permission checking
65
66            kindmap = dict(zip(_kindmap.values(), _kindmap.keys()))
67            actionmap = dict(zip(_actionmap.values(), _actionmap.keys()))
68           
69            if first == None and not youngest_stored:
70                first = self.repos.oldest_rev
71            elif first == None or self.rev_older_than(youngest_stored, first):
72                first = self.repos.next_rev(youngest_stored)
73            else:
74                first = self.normalize_rev(first)
75           
76            if last == None:
77                last = self.repos.youngest_rev
78            else:
79                last = self.normalize_rev(last)
80           
81            self.log.info("Syncing with repository (%s to %s)" % (first, last))
82           
83            current_rev = first
84            while current_rev is not None or self.repos.rev_older_than(last, current_rev):
85                changeset = self.repos.get_changeset(current_rev)
86                cursor.execute("INSERT OR REPLACE INTO revision (rev, time, "
87                               "author, message) VALUES (%s,%s,%s,%s)",
88                               (str(current_rev), changeset.date,
89                                changeset.author, changeset.message))
90                for path,kind,action,base_path,base_rev in changeset.get_changes():
91                    self.log.debug("Caching node change in [%s]: %s"
92                                   % (current_rev, (path, kind, action,
93                                      base_path, base_rev)))
94                    kind = kindmap[kind]
95                    action = actionmap[action]
96                    cursor.execute("INSERT INTO node_change (rev,path,kind,"
97                                   "change,base_path,base_rev) "
98                                   "VALUES (%s,%s,%s,%s,%s,%s)",
99                                   (str(current_rev), path, kind, action,
100                                   base_path, base_rev))
101                current_rev = self.repos.next_rev(current_rev)
102            self.db.commit()
103            self.repos.authz = authz # restore permission checking
104
105    def get_node(self, path, rev=None):
106        return self.repos.get_node(path, rev)
107
108    def has_node(self, path, rev):
109        return self.repos.has_node(path, rev)
110
111    def get_oldest_rev(self):
112        return self.repos.oldest_rev
113
114    def get_youngest_rev(self):
115        return self.repos.youngest_rev
116
117    def previous_rev(self, rev):
118        return self.repos.previous_rev(rev)
119
120    def next_rev(self, rev):
121        return self.repos.next_rev(rev)
122
123    def rev_older_than(self, rev1, rev2):
124        return self.repos.rev_older_than(rev1, rev2)
125
126    def get_path_history(self, path, rev=None, limit=None):
127        return self.repos.get_path_history(path, rev, limit)
128
129    def normalize_path(self, path):
130        return self.repos.normalize_path(path)
131
132    def normalize_rev(self, rev):
133        return self.repos.normalize_rev(rev)
134
135
136class CachedChangeset(Changeset):
137
138    def __init__(self, rev, db, authz):
139        self.db = db
140        self.authz = authz
141        cursor = self.db.cursor()
142        cursor.execute("SELECT time,author,message FROM revision "
143                       "WHERE rev=%s", (rev,))
144        row = cursor.fetchone()
145        if row:
146            date, author, message = row
147            Changeset.__init__(self, rev, message, author, int(date))
148        else:
149            raise TracError, "No changeset %s in the repository" % rev
150
151    def get_changes(self):
152        cursor = self.db.cursor()
153        cursor.execute("SELECT path,kind,change,base_path,base_rev "
154                       "FROM node_change WHERE rev=%s "
155                       "ORDER BY path", (self.rev,))
156        for path, kind, change, base_path, base_rev in cursor:
157            if not self.authz.has_permission(path):
158                # FIXME: what about the base_path?
159                continue
160            kind = _kindmap[kind]
161            change = _actionmap[change]
162            yield path, kind, change, base_path, base_rev