~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/changecache.py

  • Committer: Michael Hudson
  • Date: 2008-06-25 04:03:34 UTC
  • mfrom: (173.1.2 loggerhead.no_sqlite)
  • Revision ID: michael.hudson@canonical.com-20080625040334-x2daw2vgg2z2d0ew
merge martin's sqlite_optional branch

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