##// END OF EJS Templates
util: ability to change capacity when copying lrucachedict...
Gregory Szorc -
r39601:2dcc68c7 default
parent child Browse files
Show More
@@ -1311,8 +1311,19 b' class lrucachedict(object):'
1311
1311
1312 self._cache.clear()
1312 self._cache.clear()
1313
1313
1314 def copy(self):
1314 def copy(self, capacity=None):
1315 result = lrucachedict(self.capacity)
1315 """Create a new cache as a copy of the current one.
1316
1317 By default, the new cache has the same capacity as the existing one.
1318 But, the cache capacity can be changed as part of performing the
1319 copy.
1320
1321 Items in the copy have an insertion/access order matching this
1322 instance.
1323 """
1324
1325 capacity = capacity or self.capacity
1326 result = lrucachedict(capacity)
1316
1327
1317 # We copy entries by iterating in oldest-to-newest order so the copy
1328 # We copy entries by iterating in oldest-to-newest order so the copy
1318 # has the correct ordering.
1329 # has the correct ordering.
@@ -1322,6 +1333,8 b' class lrucachedict(object):'
1322 while n.key is _notset and n is not self._head:
1333 while n.key is _notset and n is not self._head:
1323 n = n.prev
1334 n = n.prev
1324
1335
1336 # We could potentially skip the first N items when decreasing capacity.
1337 # But let's keep it simple unless it is a performance problem.
1325 for i in range(len(self._cache)):
1338 for i in range(len(self._cache)):
1326 result[n.key] = n.value
1339 result[n.key] = n.value
1327 n = n.prev
1340 n = n.prev
@@ -118,5 +118,59 b' class testlrucachedict(unittest.TestCase'
118 for key in ('a', 'b', 'c', 'd'):
118 for key in ('a', 'b', 'c', 'd'):
119 self.assertEqual(d[key], 'v%s' % key)
119 self.assertEqual(d[key], 'v%s' % key)
120
120
121 def testcopydecreasecapacity(self):
122 d = util.lrucachedict(5)
123 d['a'] = 'va'
124 d['b'] = 'vb'
125 d['c'] = 'vc'
126 d['d'] = 'vd'
127
128 dc = d.copy(2)
129 for key in ('a', 'b'):
130 self.assertNotIn(key, dc)
131 for key in ('c', 'd'):
132 self.assertIn(key, dc)
133 self.assertEqual(dc[key], 'v%s' % key)
134
135 dc['e'] = 've'
136 self.assertNotIn('c', dc)
137 for key in ('d', 'e'):
138 self.assertIn(key, dc)
139 self.assertEqual(dc[key], 'v%s' % key)
140
141 # Original should remain unchanged.
142 for key in ('a', 'b', 'c', 'd'):
143 self.assertIn(key, d)
144 self.assertEqual(d[key], 'v%s' % key)
145
146 def testcopyincreasecapacity(self):
147 d = util.lrucachedict(5)
148 d['a'] = 'va'
149 d['b'] = 'vb'
150 d['c'] = 'vc'
151 d['d'] = 'vd'
152
153 dc = d.copy(6)
154 for key in ('a', 'b', 'c', 'd'):
155 self.assertIn(key, dc)
156 self.assertEqual(dc[key], 'v%s' % key)
157
158 dc['e'] = 've'
159 dc['f'] = 'vf'
160 for key in ('a', 'b', 'c', 'd', 'e', 'f'):
161 self.assertIn(key, dc)
162 self.assertEqual(dc[key], 'v%s' % key)
163
164 dc['g'] = 'vg'
165 self.assertNotIn('a', dc)
166 for key in ('b', 'c', 'd', 'e', 'f', 'g'):
167 self.assertIn(key, dc)
168 self.assertEqual(dc[key], 'v%s' % key)
169
170 # Original should remain unchanged.
171 for key in ('a', 'b', 'c', 'd'):
172 self.assertIn(key, d)
173 self.assertEqual(d[key], 'v%s' % key)
174
121 if __name__ == '__main__':
175 if __name__ == '__main__':
122 silenttestrunner.main(__name__)
176 silenttestrunner.main(__name__)
General Comments 0
You need to be logged in to leave comments. Login now