]> arthur.barton.de Git - bup.git/commitdiff
vfs: use a type prefix for all cache keys
authorRob Browning <rlb@defaultvalue.org>
Sat, 7 Jul 2018 20:32:34 +0000 (15:32 -0500)
committerRob Browning <rlb@defaultvalue.org>
Sat, 12 Jan 2019 17:08:32 +0000 (11:08 -0600)
This ensures the keys are more obviously unique, and can be identified
by just examining the fixed-length prefix.

Signed-off-by: Rob Browning <rlb@defaultvalue.org>
Tested-by: Rob Browning <rlb@defaultvalue.org>
lib/bup/t/tvfs.py
lib/bup/vfs.py

index 3000992fcb034802318a77cc1d3f1010297b09c0..34a6fa24f58bb0103572aea4c7cafb9ee7b043e0 100644 (file)
@@ -37,10 +37,10 @@ def test_cache_behavior():
         wvpasseq({}, vfs._cache)
         wvpasseq([], vfs._cache_keys)
         wvfail(vfs._cache_keys)
-        wvexcept(AssertionError, vfs.cache_notice, 'x', 1)
-        key_0 = b'\0' * 20
-        key_1 = b'\1' * 20
-        key_2 = b'\2' * 20
+        wvexcept(Exception, vfs.cache_notice, 'x', 1)
+        key_0 = 'itm:' + b'\0' * 20
+        key_1 = 'itm:' + b'\1' * 20
+        key_2 = 'itm:' + b'\2' * 20
         vfs.cache_notice(key_0, 'something')
         wvpasseq({key_0 : 'something'}, vfs._cache)
         wvpasseq([key_0], vfs._cache_keys)
index 99ca85d39426a0de4e35617625872d9b804f9491..5bca295b89cff9d9396d651482c34ee9d16f1a92 100644 (file)
@@ -268,28 +268,28 @@ def is_valid_cache_key(x):
     """Return logically true if x looks like it could be a valid cache key
     (with respect to structure).  Current valid cache entries:
       res:... -> resolution
-      commit_oid -> commit
-      commit_oid + ':r' -> rev-list
-         i.e. rev-list -> {'.', commit, '2012...', next_commit, ...}
+      itm:OID -> Commit
+      rvl:OID -> {'.', commit, '2012...', next_commit, ...}
     """
     # Suspect we may eventually add "(container_oid, name) -> ...", and others.
     x_t = type(x)
     if x_t is bytes:
-        if len(x) == 20:
+        tag = x[:4]
+        if tag in ('itm:', 'rvl:') and len(x) == 24:
             return True
-        if len(x) == 22 and x.endswith(b':r'):
-            return True
-        if x.startswith('res:'):
+        if tag == 'res:':
             return True
 
 def cache_get(key):
     global _cache
-    assert is_valid_cache_key(key)
+    if not is_valid_cache_key(key):
+        raise Exception('invalid cache key: ' + repr(key))
     return _cache.get(key)
 
 def cache_notice(key, value):
     global _cache, _cache_keys, _cache_max_items
-    assert is_valid_cache_key(key)
+    if not is_valid_cache_key(key):
+        raise Exception('invalid cache key: ' + repr(key))
     if key in _cache:
         return
     if len(_cache) < _cache_max_items:
@@ -307,13 +307,14 @@ def cache_get_commit_item(oid, need_meta=True):
     When need_meta is true don't return a cached item that only has a
     mode."""
     # tree might be stored independently, or as '.' with its entries.
-    item = cache_get(oid)
+    commit_key = b'itm:' + oid
+    item = cache_get(commit_key)
     if item:
         if not need_meta:
             return item
         if isinstance(item.meta, Metadata):
             return item
-    entries = cache_get(oid + b':r')
+    entries = cache_get(b'rvl:' + oid)
     if entries:
         return entries['.']
 
@@ -443,7 +444,8 @@ def _commit_item_from_oid(repo, oid, require_meta):
         meta = _find_treeish_oid_metadata(repo, commit.oid)
         if meta:
             commit = commit._replace(meta=meta)
-    cache_notice(oid, commit)
+    commit_key = b'itm:' + oid
+    cache_notice(commit_key, commit)
     return commit
 
 def _revlist_item_from_oid(repo, oid, require_meta):
@@ -616,7 +618,8 @@ def _item_for_rev(rev):
     if item:
         return item
     item = Commit(meta=default_dir_mode, oid=tree_oid, coid=coid)
-    cache_notice(item.coid, item)
+    commit_key = b'itm:' + coid
+    cache_notice(commit_key, item)
     return item
 
 def cache_commit(repo, oid):
@@ -639,7 +642,8 @@ def cache_commit(repo, oid):
         name = next(rev_names)
         entries[name] = item
     entries['latest'] = latest
-    cache_notice(latest.coid + b':r', entries)
+    revlist_key = b'rvl:' + latest.coid
+    cache_notice(revlist_key, entries)
     return entries
 
 def revlist_items(repo, oid, names):
@@ -653,7 +657,8 @@ def revlist_items(repo, oid, names):
 
     # For now, don't worry about the possibility of the contents being
     # "too big" for the cache.
-    entries = cache_get(oid + b':r')
+    revlist_key = b'rvl:' + oid
+    entries = cache_get(revlist_key)
     if not entries:
         entries = cache_commit(repo, oid)