~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/changecache.py

  • Committer: Tom Haddon
  • Date: 2008-11-24 20:26:26 UTC
  • mto: This revision was merged to the branch mainline in revision 246.
  • Revision ID: tom.haddon@canonical.com-20081124202626-x0sva3e7zxlqtckw
Allow the script to run as root or another user, but start the loggerhead process as the correct user

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
#
18
18
 
19
19
"""
20
 
a cache for chewed-up 'file change' data structures, which are basically just
21
 
a different way of storing a revision delta.  the cache improves lookup times
22
 
10x over bazaar's xml revision structure, though, so currently still worth
23
 
doing.
 
20
a cache for chewed-up "change" data structures, which are basically just a
 
21
different way of storing a revision.  the cache improves lookup times 10x
 
22
over bazaar's xml revision structure, though, so currently still worth doing.
24
23
 
25
24
once a revision is committed in bazaar, it never changes, so once we have
26
25
cached a change, it's good forever.
28
27
 
29
28
import cPickle
30
29
import os
31
 
import tempfile
 
30
 
 
31
from loggerhead import util
 
32
from loggerhead.lockfile import LockFile
 
33
 
 
34
with_lock = util.with_lock('_lock', 'ChangeCache')
32
35
 
33
36
try:
34
37
    from sqlite3 import dbapi2
35
38
except ImportError:
36
39
    from pysqlite2 import dbapi2
37
40
 
38
 
# We take an optimistic approach to concurrency here: we might do work twice
39
 
# in the case of races, but not crash or corrupt data.
40
41
 
41
42
class FakeShelf(object):
42
43
 
43
44
    def __init__(self, filename):
44
45
        create_table = not os.path.exists(filename)
45
 
        if create_table:
46
 
            # To avoid races around creating the database, we create the db in
47
 
            # a temporary file and rename it into the ultimate location.
48
 
            fd, path = tempfile.mkstemp(dir=os.path.dirname(filename))
49
 
            self._create_table(path)
50
 
            os.rename(path, filename)
51
46
        self.connection = dbapi2.connect(filename)
52
47
        self.cursor = self.connection.cursor()
 
48
        if create_table:
 
49
            self._create_table()
53
50
 
54
 
    def _create_table(self, filename):
55
 
        con = dbapi2.connect(filename)
56
 
        cur = con.cursor()
57
 
        cur.execute(
 
51
    def _create_table(self):
 
52
        self.cursor.execute(
58
53
            "create table RevisionData "
59
54
            "(revid binary primary key, data binary)")
60
 
        con.commit()
61
 
        con.close()
 
55
        self.connection.commit()
62
56
 
63
57
    def _serialize(self, obj):
64
 
        return dbapi2.Binary(cPickle.dumps(obj, protocol=2))
 
58
        r = dbapi2.Binary(cPickle.dumps(obj, protocol=2))
 
59
        return r
65
60
 
66
61
    def _unserialize(self, data):
67
62
        return cPickle.loads(str(data))
75
70
        else:
76
71
            return self._unserialize(filechange[0])
77
72
 
78
 
    def add(self, revid, object):
79
 
        try:
 
73
    def add(self, revid_obj_pairs):
 
74
        for (r, d) in revid_obj_pairs:
80
75
            self.cursor.execute(
81
76
                "insert into revisiondata (revid, data) values (?, ?)",
82
 
                (revid, self._serialize(object)))
83
 
            self.connection.commit()
84
 
        except dbapi2.IntegrityError:
85
 
            # If another thread or process attempted to set the same key, we
86
 
            # assume it set it to the same value and carry on with our day.
87
 
            pass
 
77
                (r, self._serialize(d)))
 
78
        self.connection.commit()
88
79
 
89
80
 
90
81
class FileChangeCache(object):
97
88
 
98
89
        self._changes_filename = os.path.join(cache_path, 'filechanges.sql')
99
90
 
100
 
    def get_file_changes(self, entry):
 
91
        # use a lockfile since the cache folder could be shared across
 
92
        # different processes.
 
93
        self._lock = LockFile(os.path.join(cache_path, 'filechange-lock'))
 
94
 
 
95
    @with_lock
 
96
    def get_file_changes(self, entries):
 
97
        out = []
 
98
        missing_entries = []
 
99
        missing_entry_indices = []
101
100
        cache = FakeShelf(self._changes_filename)
102
 
        changes = cache.get(entry.revid)
103
 
        if changes is None:
104
 
            changes = self.history.get_file_changes_uncached(entry)
105
 
            cache.add(entry.revid, changes)
106
 
        return changes
 
101
        for entry in entries:
 
102
            changes = cache.get(entry.revid)
 
103
            if changes is not None:
 
104
                out.append(changes)
 
105
            else:
 
106
                missing_entries.append(entry)
 
107
                missing_entry_indices.append(len(out))
 
108
                out.append(None)
 
109
        if missing_entries:
 
110
            missing_changes = self.history.get_file_changes_uncached(
 
111
                                  missing_entries)
 
112
            revid_changes_pairs = []
 
113
            for i, entry, changes in zip(
 
114
                missing_entry_indices, missing_entries, missing_changes):
 
115
                revid_changes_pairs.append((entry.revid, changes))
 
116
                out[i] = changes
 
117
            cache.add(revid_changes_pairs)
 
118
        return out