/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: Breezy landing bot
  • Author(s): Colin Watson
  • Date: 2020-11-16 21:47:08 UTC
  • mfrom: (7521.1.1 remove-lp-workaround)
  • Revision ID: breezy.the.bot@gmail.com-20201116214708-jos209mgxi41oy15
Remove breezy.git workaround for bazaar.launchpad.net.

Merged from https://code.launchpad.net/~cjwatson/brz/remove-lp-workaround/+merge/393710

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