~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:
33
33
 
34
34
with_lock = util.with_lock('_lock', 'ChangeCache')
35
35
 
36
 
SQLITE_INTERFACE = os.environ.get('SQLITE_INTERFACE', 'sqlite3')
37
 
 
38
 
if SQLITE_INTERFACE == 'sqlite3':
 
36
try:
39
37
    from sqlite3 import dbapi2
40
 
    _param_marker = '?'
41
 
 
42
 
_select_stmt = ("select data from revisiondata where revid = ?"
43
 
                ).replace('?', _param_marker)
44
 
_insert_stmt = ("insert into revisiondata (revid, data) "
45
 
                "values (?, ?)").replace('?', _param_marker)
46
 
 
47
 
 
 
38
except ImportError:
 
39
    from pysqlite2 import dbapi2
48
40
 
49
41
 
50
42
class FakeShelf(object):
 
43
 
51
44
    def __init__(self, filename):
52
45
        create_table = not os.path.exists(filename)
53
46
        self.connection = dbapi2.connect(filename)
54
47
        self.cursor = self.connection.cursor()
55
48
        if create_table:
56
49
            self._create_table()
 
50
 
57
51
    def _create_table(self):
58
52
        self.cursor.execute(
59
53
            "create table RevisionData "
60
54
            "(revid binary primary key, data binary)")
61
55
        self.connection.commit()
 
56
 
62
57
    def _serialize(self, obj):
63
58
        r = dbapi2.Binary(cPickle.dumps(obj, protocol=2))
64
59
        return r
 
60
 
65
61
    def _unserialize(self, data):
66
62
        return cPickle.loads(str(data))
 
63
 
67
64
    def get(self, revid):
68
 
        self.cursor.execute(_select_stmt, (revid,))
 
65
        self.cursor.execute(
 
66
            "select data from revisiondata where revid = ?", (revid, ))
69
67
        filechange = self.cursor.fetchone()
70
68
        if filechange is None:
71
69
            return None
72
70
        else:
73
71
            return self._unserialize(filechange[0])
 
72
 
74
73
    def add(self, revid_obj_pairs):
75
 
        for  (r, d) in revid_obj_pairs:
76
 
            self.cursor.execute(_insert_stmt, (r, self._serialize(d)))
 
74
        for (r, d) in revid_obj_pairs:
 
75
            self.cursor.execute(
 
76
                "insert into revisiondata (revid, data) values (?, ?)",
 
77
                (r, self._serialize(d)))
77
78
        self.connection.commit()
78
79
 
79
80
 
80
81
class FileChangeCache(object):
 
82
 
81
83
    def __init__(self, history, cache_path):
82
84
        self.history = history
83
85
 
105
107
                missing_entry_indices.append(len(out))
106
108
                out.append(None)
107
109
        if missing_entries:
108
 
            missing_changes = self.history.get_file_changes_uncached(missing_entries)
 
110
            missing_changes = self.history.get_file_changes_uncached(
 
111
                                  missing_entries)
109
112
            revid_changes_pairs = []
110
113
            for i, entry, changes in zip(
111
114
                missing_entry_indices, missing_entries, missing_changes):