changeset 27576:6cd3044985c2

lrucachedict: add copy method This diff implements the standard dict copy() method for lrucachedicts, which will be used in the pushrebase extension to make a copy of the manifestcache.
author Eric Sumner <ericsumner@fb.com>
date Wed, 30 Dec 2015 13:10:53 -0800
parents 44f717c87903
children f9e755736b0e
files mercurial/util.py tests/test-lrucachedict.py tests/test-lrucachedict.py.out
diffstat 3 files changed, 63 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/util.py	Thu Dec 31 09:55:56 2015 +0100
+++ b/mercurial/util.py	Wed Dec 30 13:10:53 2015 -0800
@@ -622,6 +622,15 @@
 
         self._cache.clear()
 
+    def copy(self):
+        result = lrucachedict(self._capacity)
+        n = self._head.prev
+        # Iterate in oldest-to-newest order, so the copy has the right ordering
+        for i in range(len(self._cache)):
+            result[n.key] = n.value
+            n = n.prev
+        return result
+
     def _movetohead(self, node):
         """Mark a node as the newest, making it the new head.
 
--- a/tests/test-lrucachedict.py	Thu Dec 31 09:55:56 2015 +0100
+++ b/tests/test-lrucachedict.py	Wed Dec 30 13:10:53 2015 -0800
@@ -1,11 +1,11 @@
 from mercurial import util
 
-def printifpresent(d, xs):
+def printifpresent(d, xs, name='d'):
     for x in xs:
         present = x in d
-        print "'%s' in d: %s" % (x, present)
+        print "'%s' in %s: %s" % (x, name, present)
         if present:
-            print "d['%s']: %s" % (x, d[x])
+            print "%s['%s']: %s" % (name, x, d[x])
 
 def test_lrucachedict():
     d = util.lrucachedict(4)
@@ -42,5 +42,29 @@
     d['b']
     printifpresent(d, ['a', 'b'])
 
+    # test copy method
+    d = util.lrucachedict(4)
+    d['a'] = 'va3'
+    d['b'] = 'vb3'
+    d['c'] = 'vc3'
+    d['d'] = 'vd3'
+
+    dc = d.copy()
+
+    # all of these should be present
+    print "\nAll of these should be present:"
+    printifpresent(dc, ['a', 'b', 'c', 'd'], 'dc')
+
+    # 'a' should be dropped because it was least recently used
+    print "\nAll of these except 'a' should be present:"
+    dc['e'] = 've3'
+    printifpresent(dc, ['a', 'b', 'c', 'd', 'e'], 'dc')
+
+    # contents and order of original dict should remain unchanged
+    print "\nThese should be in reverse alphabetical order and read 'v?3':"
+    dc['b'] = 'vb3_new'
+    for k in list(iter(d)):
+        print "d['%s']: %s" % (k, d[k])
+
 if __name__ == '__main__':
     test_lrucachedict()
--- a/tests/test-lrucachedict.py.out	Thu Dec 31 09:55:56 2015 +0100
+++ b/tests/test-lrucachedict.py.out	Wed Dec 30 13:10:53 2015 -0800
@@ -33,3 +33,30 @@
 d['a']: 1
 'b' in d: True
 d['b']: 2
+
+All of these should be present:
+'a' in dc: True
+dc['a']: va3
+'b' in dc: True
+dc['b']: vb3
+'c' in dc: True
+dc['c']: vc3
+'d' in dc: True
+dc['d']: vd3
+
+All of these except 'a' should be present:
+'a' in dc: False
+'b' in dc: True
+dc['b']: vb3
+'c' in dc: True
+dc['c']: vc3
+'d' in dc: True
+dc['d']: vd3
+'e' in dc: True
+dc['e']: ve3
+
+These should be in reverse alphabetical order and read 'v?3':
+d['d']: vd3
+d['c']: vc3
+d['b']: vb3
+d['a']: va3