31
31
from loggerhead import util
32
32
from loggerhead.lockfile import LockFile
34
35
with_lock = util.with_lock('_lock', 'ChangeCache')
37
from sqlite3 import dbapi2
37
SQLITE_INTERFACE = os.environ.get('SQLITE_INTERFACE', 'sqlite')
39
if SQLITE_INTERFACE == 'pysqlite2':
39
40
from pysqlite2 import dbapi2
42
elif SQLITE_INTERFACE == 'sqlite':
43
import sqlite as dbapi2
46
raise AssertionError("bad sqlite interface %r!?"%SQLITE_INTERFACE)
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)
42
56
class FakeShelf(object):
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()
49
62
self._create_table()
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()
57
68
def _serialize(self, obj):
58
69
r = dbapi2.Binary(cPickle.dumps(obj, protocol=2))
61
71
def _unserialize(self, data):
62
72
return cPickle.loads(str(data))
64
73
def get(self, revid):
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:
71
79
return self._unserialize(filechange[0])
73
80
def add(self, revid_obj_pairs):
74
for (r, d) in revid_obj_pairs:
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()
81
86
class FileChangeCache(object):
83
87
def __init__(self, history, cache_path):
84
88
self.history = history
107
111
missing_entry_indices.append(len(out))
109
113
if missing_entries:
110
missing_changes = self.history.get_file_changes_uncached(
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):