~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/changecache.py

  • Committer: Matt Nordhoff
  • Date: 2009-04-09 17:19:11 UTC
  • mfrom: (324.1.1 trunk)
  • Revision ID: mnordhoff@mattnordhoff.com-20090409171911-2xb66d8ts6gf0dnp
Mention bug # for branches served through http in NEWS. (Jelmer Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
"""
28
28
 
29
29
import cPickle
30
 
import marshal
31
30
import os
32
31
import tempfile
33
 
import zlib
34
32
 
35
33
try:
36
34
    from sqlite3 import dbapi2
40
38
# We take an optimistic approach to concurrency here: we might do work twice
41
39
# in the case of races, but not crash or corrupt data.
42
40
 
43
 
def safe_init_db(filename, init_sql):
44
 
    # To avoid races around creating the database, we create the db in
45
 
    # a temporary file and rename it into the ultimate location.
46
 
    fd, temp_path = tempfile.mkstemp(dir=os.path.dirname(filename))
47
 
    os.close(fd)
48
 
    con = dbapi2.connect(temp_path)
49
 
    cur = con.cursor()
50
 
    cur.execute(init_sql)
51
 
    con.commit()
52
 
    con.close()
53
 
    os.rename(temp_path, filename)
54
 
 
55
41
class FakeShelf(object):
56
42
 
57
43
    def __init__(self, filename):
58
44
        create_table = not os.path.exists(filename)
59
45
        if create_table:
60
 
            safe_init_db(
61
 
                filename, "create table RevisionData "
62
 
                "(revid binary primary key, data binary)")
 
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)
63
51
        self.connection = dbapi2.connect(filename)
64
52
        self.cursor = self.connection.cursor()
65
53
 
101
89
 
102
90
class FileChangeCache(object):
103
91
 
104
 
    def __init__(self, cache_path):
 
92
    def __init__(self, history, cache_path):
 
93
        self.history = history
105
94
 
106
95
        if not os.path.exists(cache_path):
107
96
            os.mkdir(cache_path)
115
104
            changes = self.history.get_file_changes_uncached(entry)
116
105
            cache.add(entry.revid, changes)
117
106
        return changes
118
 
 
119
 
 
120
 
class RevInfoDiskCache(object):
121
 
    """Like `RevInfoMemoryCache` but backed in a sqlite DB."""
122
 
 
123
 
    def __init__(self, cache_path):
124
 
        if not os.path.exists(cache_path):
125
 
            os.mkdir(cache_path)
126
 
        filename = os.path.join(cache_path, 'revinfo.sql')
127
 
        create_table = not os.path.exists(filename)
128
 
        if create_table:
129
 
            safe_init_db(
130
 
                filename, "create table Data "
131
 
                "(key binary primary key, revid binary, data binary)")
132
 
        self.connection = dbapi2.connect(filename)
133
 
        self.cursor = self.connection.cursor()
134
 
 
135
 
    def get(self, key, revid):
136
 
        self.cursor.execute(
137
 
            "select revid, data from data where key = ?", (dbapi2.Binary(key),))
138
 
        row = self.cursor.fetchone()
139
 
        if row is None:
140
 
            return None
141
 
        elif str(row[0]) != revid:
142
 
            return None
143
 
        else:
144
 
            return marshal.loads(zlib.decompress(row[1]))
145
 
 
146
 
    def set(self, key, revid, data):
147
 
        try:
148
 
            self.cursor.execute(
149
 
                'delete from data where key = ?', (dbapi2.Binary(key), ))
150
 
            blob = zlib.compress(marshal.dumps(data))
151
 
            self.cursor.execute(
152
 
                "insert into data (key, revid, data) values (?, ?, ?)",
153
 
                map(dbapi2.Binary, [key, revid, blob]))
154
 
            self.connection.commit()
155
 
        except dbapi2.IntegrityError:
156
 
            # If another thread or process attempted to set the same key, we
157
 
            # don't care too much -- it's only a cache after all!
158
 
            pass