util: add method to peek item in lrucachedict
authorYuya Nishihara <yuya@tcha.org>
Wed, 31 Oct 2018 22:29:05 +0900
changeset 53645 0c638ff69f5c10bddf1b795c08dc5a6dd4f3ad66
parent 53644 2525faf4ecdbe7418b47ea87fce24cb52e657308
child 53646 7cda0cacbbf653c58a60b77828526122836b5ad7
push id1079
push usergszorc@mozilla.com
push dateMon, 10 Dec 2018 19:44:59 +0000
util: add method to peek item in lrucachedict I want a function that doesn't unnecessarily update the internal state of the cache dict after fork().
mercurial/util.py
tests/test-lrucachedict.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -1332,16 +1332,30 @@ class lrucachedict(object):
     # Additional dict methods.
 
     def get(self, k, default=None):
         try:
             return self.__getitem__(k)
         except KeyError:
             return default
 
+    def peek(self, k, default=_notset):
+        """Get the specified item without moving it to the head
+
+        Unlike get(), this doesn't mutate the internal state. But be aware
+        that it doesn't mean peek() is thread safe.
+        """
+        try:
+            node = self._cache[k]
+            return node.value
+        except KeyError:
+            if default is _notset:
+                raise
+            return default
+
     def clear(self):
         n = self._head
         while n.key is not _notset:
             self.totalcost -= n.cost
             n.markempty()
             n = n.next
 
         self._cache.clear()
--- a/tests/test-lrucachedict.py
+++ b/tests/test-lrucachedict.py
@@ -74,16 +74,31 @@ class testlrucachedict(unittest.TestCase
         d['c'] = 'vc'
 
         self.assertIsNone(d.get('missing'))
         self.assertEqual(list(d), ['c', 'b', 'a'])
 
         self.assertEqual(d.get('a'), 'va')
         self.assertEqual(list(d), ['a', 'c', 'b'])
 
+    def testpeek(self):
+        d = util.lrucachedict(4)
+        d['a'] = 'va'
+        d['b'] = 'vb'
+        d['c'] = 'vc'
+
+        with self.assertRaises(KeyError):
+            d.peek('missing')
+        self.assertEqual(list(d), ['c', 'b', 'a'])
+        self.assertIsNone(d.peek('missing', None))
+        self.assertEqual(list(d), ['c', 'b', 'a'])
+
+        self.assertEqual(d.peek('a'), 'va')
+        self.assertEqual(list(d), ['c', 'b', 'a'])
+
     def testcopypartial(self):
         d = util.lrucachedict(4)
         d.insert('a', 'va', cost=4)
         d.insert('b', 'vb', cost=2)
 
         dc = d.copy()
 
         self.assertEqual(len(dc), 2)