From 5002f590efba1426c1e3982c1ab2ca2b3a234fef Mon Sep 17 00:00:00 2001 From: Matt Wala Date: Mon, 25 Sep 2017 18:52:35 -0500 Subject: [PATCH 1/2] Implement store_if_not_present for persistent dicts. --- pytools/persistent_dict.py | 15 ++++++++++++--- test/test_persistent_dict.py | 33 +++++++++++++++++++++++++++++---- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/pytools/persistent_dict.py b/pytools/persistent_dict.py index 06e0f00..02b14dc 100644 --- a/pytools/persistent_dict.py +++ b/pytools/persistent_dict.py @@ -417,7 +417,10 @@ class _PersistentDictBase(object): self._make_container_dir() - def store(self, key, value): + def store_if_not_present(self, key, value): + self.store(key, value, _skip_if_present=True) + + def store(self, key, value, _skip_if_present=False): raise NotImplementedError() def fetch(self, key): @@ -497,6 +500,7 @@ class WriteOncePersistentDict(_PersistentDictBase): .. automethod:: __getitem__ .. automethod:: __setitem__ .. automethod:: clear + .. automethod:: store_if_not_present """ _PersistentDictBase.__init__(self, identifier, key_builder, container_dir) self._cache = _LRUCache(in_mem_cache_size) @@ -521,7 +525,7 @@ class WriteOncePersistentDict(_PersistentDictBase): "on the lock file '%s'" "--something is wrong" % lock_file) - def store(self, key, value): + def store(self, key, value, _skip_if_present=False): hexdigest_key = self.key_builder(key) cleanup_m = CleanupManager() @@ -533,6 +537,8 @@ class WriteOncePersistentDict(_PersistentDictBase): delete_on_error=False) if item_dir_m.existed: + if _skip_if_present: + return raise ReadOnlyEntryError(key) item_dir_m.mkdir() @@ -642,10 +648,11 @@ class PersistentDict(_PersistentDictBase): .. automethod:: __setitem__ .. automethod:: __delitem__ .. automethod:: clear + .. automethod:: store_if_not_present """ _PersistentDictBase.__init__(self, identifier, key_builder, container_dir) - def store(self, key, value): + def store(self, key, value, _skip_if_present=False): hexdigest_key = self.key_builder(key) cleanup_m = CleanupManager() @@ -657,6 +664,8 @@ class PersistentDict(_PersistentDictBase): delete_on_error=True) if item_dir_m.existed: + if _skip_if_present: + return item_dir_m.reset() item_dir_m.mkdir() diff --git a/test/test_persistent_dict.py b/test/test_persistent_dict.py index dfb1cdc..6dc8a13 100644 --- a/test/test_persistent_dict.py +++ b/test/test_persistent_dict.py @@ -81,13 +81,24 @@ def test_persistent_dict_storage_and_lookup(): # }}} - # {{{ check not found + # {{{ check store_if_not_present - with pytest.raises(NoSuchEntryError): - pdict[3000] + for k, v in zip(keys, values): + pdict.store_if_not_present(k, d[k] + 2) + + for k, v in d.items(): + assert d[k] + 1 == pdict[k] + + pdict.store_if_not_present(2001, 2001) + assert pdict[2001] == 2001 # }}} + # check not found + + with pytest.raises(NoSuchEntryError): + pdict[3000] + finally: shutil.rmtree(tmpdir) @@ -154,7 +165,11 @@ def test_persistent_dict_cache_collisions(): del pdict[key2] # check presence after deletion - pdict[key1] + assert pdict[key1] == 1 + + # check store_if_not_present + pdict.store_if_not_present(key2, 2) + assert pdict[key1] == 1 finally: shutil.rmtree(tmpdir) @@ -198,6 +213,12 @@ def test_write_once_persistent_dict_storage_and_lookup(in_mem_cache_size): with pytest.raises(NoSuchEntryError): pdict[1] + # check store_if_not_present + pdict.store_if_not_present(0, 2) + assert pdict[0] == 1 + pdict.store_if_not_present(1, 1) + assert pdict[1] == 1 + finally: shutil.rmtree(tmpdir) @@ -270,6 +291,10 @@ def test_write_once_persistent_dict_cache_collisions(): with pytest.raises(ReadOnlyEntryError): pdict[key2] = 1 + # check store_if_not_present + pdict.store_if_not_present(key2, 1) + assert pdict[key1] == 1 + finally: shutil.rmtree(tmpdir) -- GitLab From b37c6d1445fa9af9fc4cd98561b52281fc56b308 Mon Sep 17 00:00:00 2001 From: Matt Wala Date: Mon, 25 Sep 2017 18:52:47 -0500 Subject: [PATCH 2/2] Bump version. --- pytools/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pytools/version.py b/pytools/version.py index a63be76..4bbc230 100644 --- a/pytools/version.py +++ b/pytools/version.py @@ -1,3 +1,3 @@ -VERSION = (2017, 5) +VERSION = (2017, 6) VERSION_STATUS = "" VERSION_TEXT = ".".join(str(x) for x in VERSION) + VERSION_STATUS -- GitLab