/loggerhead/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/loggerhead/trunk

« back to all changes in this revision

Viewing changes to loggerhead/changecache.py

  • Committer: Toshio Kuratomi
  • Date: 2010-04-18 17:55:33 UTC
  • mto: (464.1.1 mod-wsgi)
  • mto: This revision was merged to the branch mainline in revision 465.
  • Revision ID: toshio@fedoraproject.org-20100418175533-bmrymlv9ir7v7jwg
Merge README.mod_wsgi into the installation instructions in docs/index.rst and add mod_wsgi note to NEWS

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
#
14
14
# You should have received a copy of the GNU General Public License
15
15
# along with this program; if not, write to the Free Software
16
 
# Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335  USA
 
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
17
#
18
18
 
19
19
"""
26
26
cached a change, it's good forever.
27
27
"""
28
28
 
29
 
import pickle
 
29
import cPickle
30
30
import marshal
31
31
import os
32
32
import tempfile
33
33
import zlib
34
34
 
35
 
from sqlite3 import dbapi2
 
35
try:
 
36
    from sqlite3 import dbapi2
 
37
except ImportError:
 
38
    from pysqlite2 import dbapi2
36
39
 
37
40
# We take an optimistic approach to concurrency here: we might do work twice
38
41
# in the case of races, but not crash or corrupt data.
70
73
        con.close()
71
74
 
72
75
    def _serialize(self, obj):
73
 
        return dbapi2.Binary(pickle.dumps(obj, protocol=2))
 
76
        return dbapi2.Binary(cPickle.dumps(obj, protocol=2))
74
77
 
75
78
    def _unserialize(self, data):
76
 
        return pickle.loads(str(data))
 
79
        return cPickle.loads(str(data))
77
80
 
78
81
    def get(self, revid):
79
82
        self.cursor.execute(
96
99
            pass
97
100
 
98
101
 
 
102
class FileChangeCache(object):
 
103
 
 
104
    def __init__(self, cache_path):
 
105
 
 
106
        if not os.path.exists(cache_path):
 
107
            os.mkdir(cache_path)
 
108
 
 
109
        self._changes_filename = os.path.join(cache_path, 'filechanges.sql')
 
110
 
 
111
    def get_file_changes(self, entry):
 
112
        cache = FakeShelf(self._changes_filename)
 
113
        changes = cache.get(entry.revid)
 
114
        if changes is None:
 
115
            changes = self.history.get_file_changes_uncached(entry)
 
116
            cache.add(entry.revid, changes)
 
117
        return changes
 
118
 
 
119
 
99
120
class RevInfoDiskCache(object):
100
121
    """Like `RevInfoMemoryCache` but backed in a sqlite DB."""
101
122
 
112
133
        self.cursor = self.connection.cursor()
113
134
 
114
135
    def get(self, key, revid):
115
 
        if not isinstance(key, bytes):
116
 
            raise TypeError(key)
117
 
        if not isinstance(revid, bytes):
118
 
            raise TypeError(revid)
119
136
        self.cursor.execute(
120
137
            "select revid, data from data where key = ?", (dbapi2.Binary(key),))
121
138
        row = self.cursor.fetchone()
124
141
        elif str(row[0]) != revid:
125
142
            return None
126
143
        else:
127
 
            try:
128
 
                return marshal.loads(zlib.decompress(row[1]))
129
 
            except (EOFError, ValueError, TypeError):
130
 
                return None
 
144
            return marshal.loads(zlib.decompress(row[1]))
131
145
 
132
146
    def set(self, key, revid, data):
133
 
        if not isinstance(key, bytes):
134
 
            raise TypeError(key)
135
 
        if not isinstance(revid, bytes):
136
 
            raise TypeError(revid)
137
147
        try:
138
148
            self.cursor.execute(
139
149
                'delete from data where key = ?', (dbapi2.Binary(key), ))
140
 
            blob = zlib.compress(marshal.dumps(data, 2))
 
150
            blob = zlib.compress(marshal.dumps(data))
141
151
            self.cursor.execute(
142
152
                "insert into data (key, revid, data) values (?, ?, ?)",
143
 
                list(map(dbapi2.Binary, [key, revid, blob])))
 
153
                map(dbapi2.Binary, [key, revid, blob]))
144
154
            self.connection.commit()
145
155
        except dbapi2.IntegrityError:
146
156
            # If another thread or process attempted to set the same key, we