Adds code version to cache. (Fixes #129)

Current code version is added to the cache on writting. If the cache
version does not match the code version on read, the cache is ignored
which means it resets.
This commit is contained in:
Olivier Mangin 2018-03-30 19:16:23 -04:00
parent 0d7b44a3f5
commit 801e0c035f
No known key found for this signature in database
GPG Key ID: D72FEC1C3120A884
5 changed files with 28 additions and 8 deletions

View File

@ -1,6 +1,7 @@
from . import filebroker from . import filebroker
from . import endecoder from . import endecoder
from .p3 import pickle from .p3 import pickle
from . import __version__
class DataBroker(object): class DataBroker(object):
@ -22,12 +23,16 @@ class DataBroker(object):
pass pass
def pull_cache(self, name): def pull_cache(self, name):
"""Load cache data from distk. Exceptions are handled by the caller.""" """Load cache data from disk. Exceptions are handled by the caller."""
data_raw = self.filebroker.pull_cachefile(name) data_raw = self.filebroker.pull_cachefile(name)
return pickle.loads(data_raw) cache = pickle.loads(data_raw)
if cache['version'] != __version__:
raise ValueError('Cache not matching code version.')
return cache['data']
def push_cache(self, name, data): def push_cache(self, name, data):
data_raw = pickle.dumps(data) cache_content = {'version': __version__, 'data': data}
data_raw = pickle.dumps(cache_content)
self.filebroker.push_cachefile(name, data_raw) self.filebroker.push_cachefile(name, data_raw)
# filebroker+endecoder # filebroker+endecoder

View File

@ -91,8 +91,6 @@ class DataCache(object):
2. Keeps an up-to-date, pickled version of the repository, to speed up things 2. Keeps an up-to-date, pickled version of the repository, to speed up things
when they are a lot of files. Update are also done only when required. when they are a lot of files. Update are also done only when required.
Changes are detected using data modification timestamps. Changes are detected using data modification timestamps.
For the moment, only (1) is implemented.
""" """
def __init__(self, pubsdir, docsdir, create=False): def __init__(self, pubsdir, docsdir, create=False):
self.pubsdir = pubsdir self.pubsdir = pubsdir

View File

@ -1 +1 @@
__version__ = '0.7.0' __version__ = '0.8.dev1'

View File

@ -16,7 +16,6 @@ class TestDataBroker(fake_env.TestFakeFs):
def test_databroker(self): def test_databroker(self):
ende = endecoder.EnDecoder() ende = endecoder.EnDecoder()
page99_metadata = ende.decode_metadata(str_fixtures.metadata_raw0) page99_metadata = ende.decode_metadata(str_fixtures.metadata_raw0)
page99_bibentry = ende.decode_bibdata(str_fixtures.bibtex_raw0) page99_bibentry = ende.decode_bibdata(str_fixtures.bibtex_raw0)
@ -71,6 +70,25 @@ class TestDataBroker(fake_env.TestFakeFs):
db.remove_doc('docsdir://Page99.pdf') db.remove_doc('docsdir://Page99.pdf')
def test_push_pull_cache(self):
db = databroker.DataBroker('tmp', 'tmp/doc', create=True)
data_in = {'a': 1}
db.push_cache('meta', data_in)
data_out = db.pull_cache('meta')
self.assertEqual(data_in, data_out)
def test_pull_cache_fails_on_version_mismatch(self):
db = databroker.DataBroker('tmp', 'tmp/doc', create=True)
data_in = {'a': 1}
db.push_cache('meta', data_in)
ver = databroker.__version__
databroker.__version__ = '0.0.0'
try:
with self.assertRaises(ValueError):
db.pull_cache('meta')
finally:
databroker.__version__ = ver
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

View File

@ -126,7 +126,6 @@ class TestCacheEntrySet(unittest.TestCase):
value = self.bibcache.pull('a') value = self.bibcache.pull('a')
self.assertEqual(value, 'b') self.assertEqual(value, 'b')
def test_is_outdated_when_unknown_citekey(self): def test_is_outdated_when_unknown_citekey(self):
self.assertTrue(self.metacache._is_outdated('a')) self.assertTrue(self.metacache._is_outdated('a'))