/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/fifo_cache.py

  • Committer: Jelmer Vernooij
  • Date: 2020-03-22 20:02:36 UTC
  • mto: (7490.7.7 work)
  • mto: This revision was merged to the branch mainline in revision 7501.
  • Revision ID: jelmer@jelmer.uk-20200322200236-fsbl91ktcn6fcbdd
Fix tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""A simple first-in-first-out (FIFO) cache."""
18
18
 
 
19
from __future__ import absolute_import, division
 
20
 
19
21
from collections import deque
20
22
 
21
23
 
26
28
        dict.__init__(self)
27
29
        self._max_cache = max_cache
28
30
        if after_cleanup_count is None:
29
 
            self._after_cleanup_count = self._max_cache * 8 / 10
 
31
            self._after_cleanup_count = self._max_cache * 8 // 10
30
32
        else:
31
33
            self._after_cleanup_count = min(after_cleanup_count,
32
34
                                            self._max_cache)
33
 
        self._cleanup = {} # map to cleanup functions when items are removed
34
 
        self._queue = deque() # Track when things are accessed
 
35
        self._cleanup = {}  # map to cleanup functions when items are removed
 
36
        self._queue = deque()  # Track when things are accessed
35
37
 
36
38
    def __setitem__(self, key, value):
37
39
        """Add a value to the cache, there will be no cleanup function."""
39
41
 
40
42
    def __delitem__(self, key):
41
43
        # Remove the key from an arbitrary location in the queue
42
 
        remove = getattr(self._queue, 'remove', None)
43
 
        # Python2.5's has deque.remove, but Python2.4 does not
44
 
        if remove is not None:
45
 
            remove(key)
46
 
        else:
47
 
            # TODO: It would probably be faster to pop()/popleft() until we get to the
48
 
            #       key, and then insert those back into the queue. We know
49
 
            #       the key should only be present in one position, and we
50
 
            #       wouldn't need to rebuild the whole queue.
51
 
            self._queue = deque([k for k in self._queue if k != key])
 
44
        self._queue.remove(key)
52
45
        self._remove(key)
53
46
 
54
47
    def add(self, key, value, cleanup=None):
88
81
            self._remove_oldest()
89
82
        if len(self._queue) != len(self):
90
83
            raise AssertionError('The length of the queue should always equal'
91
 
                ' the length of the dict. %s != %s'
92
 
                % (len(self._queue), len(self)))
 
84
                                 ' the length of the dict. %s != %s'
 
85
                                 % (len(self._queue), len(self)))
93
86
 
94
87
    def clear(self):
95
88
        """Clear out all of the cache."""
121
114
        """
122
115
        self._max_cache = max_cache
123
116
        if after_cleanup_count is None:
124
 
            self._after_cleanup_count = max_cache * 8 / 10
 
117
            self._after_cleanup_count = max_cache * 8 // 10
125
118
        else:
126
119
            self._after_cleanup_count = min(max_cache, after_cleanup_count)
127
120
        if len(self) > self._max_cache:
156
149
        if len(args) == 1:
157
150
            arg = args[0]
158
151
            if isinstance(arg, dict):
159
 
                for key, val in arg.iteritems():
160
 
                    self.add(key, val)
 
152
                for key in arg:
 
153
                    self.add(key, arg[key])
161
154
            else:
162
155
                for key, val in args[0]:
163
156
                    self.add(key, val)
165
158
            raise TypeError('update expected at most 1 argument, got %d'
166
159
                            % len(args))
167
160
        if kwargs:
168
 
            for key, val in kwargs.iteritems():
169
 
                self.add(key, val)
 
161
            for key in kwargs:
 
162
                self.add(key, kwargs[key])
170
163
 
171
164
 
172
165
class FIFOSizeCache(FIFOCache):
176
169
    it restricts the cache to be cleaned based on the size of the data.
177
170
    """
178
171
 
179
 
    def __init__(self, max_size=1024*1024, after_cleanup_size=None,
 
172
    def __init__(self, max_size=1024 * 1024, after_cleanup_size=None,
180
173
                 compute_size=None):
181
174
        """Create a new FIFOSizeCache.
182
175
 
191
184
        FIFOCache.__init__(self, max_cache=max_size)
192
185
        self._max_size = max_size
193
186
        if after_cleanup_size is None:
194
 
            self._after_cleanup_size = self._max_size * 8 / 10
 
187
            self._after_cleanup_size = self._max_size * 8 // 10
195
188
        else:
196
189
            self._after_cleanup_size = min(after_cleanup_size, self._max_size)
197
190
 
260
253
        FIFOCache.resize(self, max_size)
261
254
        self._max_size = max_size
262
255
        if after_cleanup_size is None:
263
 
            self._after_cleanup_size = max_size * 8 / 10
 
256
            self._after_cleanup_size = max_size * 8 // 10
264
257
        else:
265
258
            self._after_cleanup_size = min(max_size, after_cleanup_size)
266
259
        if self._value_size > self._max_size:
267
260
            self.cleanup()
268