~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/changecache.py

  • Committer: Michael Hudson
  • Date: 2008-06-18 04:30:50 UTC
  • Revision ID: michael.hudson@canonical.com-20080618043050-u8e5qcj64tf16can
don't insist on python 2.4

Show diffs side-by-side

added added

removed removed

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