/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): Jelmer Vernooij
  • Date: 2020-08-23 01:15:41 UTC
  • mfrom: (7520.1.4 merge-3.1)
  • Revision ID: breezy.the.bot@gmail.com-20200823011541-nv0oh7nzaganx2qy
Merge lp:brz/3.1.

Merged from https://code.launchpad.net/~jelmer/brz/merge-3.1/+merge/389690

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
        dict.__init__(self)
27
27
        self._max_cache = max_cache
28
28
        if after_cleanup_count is None:
29
 
            self._after_cleanup_count = self._max_cache * 8 / 10
 
29
            self._after_cleanup_count = self._max_cache * 8 // 10
30
30
        else:
31
31
            self._after_cleanup_count = min(after_cleanup_count,
32
32
                                            self._max_cache)
33
 
        self._cleanup = {} # map to cleanup functions when items are removed
34
 
        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
35
35
 
36
36
    def __setitem__(self, key, value):
37
37
        """Add a value to the cache, there will be no cleanup function."""
39
39
 
40
40
    def __delitem__(self, key):
41
41
        # 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])
 
42
        self._queue.remove(key)
52
43
        self._remove(key)
53
44
 
54
45
    def add(self, key, value, cleanup=None):
88
79
            self._remove_oldest()
89
80
        if len(self._queue) != len(self):
90
81
            raise AssertionError('The length of the queue should always equal'
91
 
                ' the length of the dict. %s != %s'
92
 
                % (len(self._queue), len(self)))
 
82
                                 ' the length of the dict. %s != %s'
 
83
                                 % (len(self._queue), len(self)))
93
84
 
94
85
    def clear(self):
95
86
        """Clear out all of the cache."""
121
112
        """
122
113
        self._max_cache = max_cache
123
114
        if after_cleanup_count is None:
124
 
            self._after_cleanup_count = max_cache * 8 / 10
 
115
            self._after_cleanup_count = max_cache * 8 // 10
125
116
        else:
126
117
            self._after_cleanup_count = min(max_cache, after_cleanup_count)
127
118
        if len(self) > self._max_cache:
156
147
        if len(args) == 1:
157
148
            arg = args[0]
158
149
            if isinstance(arg, dict):
159
 
                for key, val in arg.iteritems():
160
 
                    self.add(key, val)
 
150
                for key in arg:
 
151
                    self.add(key, arg[key])
161
152
            else:
162
153
                for key, val in args[0]:
163
154
                    self.add(key, val)
165
156
            raise TypeError('update expected at most 1 argument, got %d'
166
157
                            % len(args))
167
158
        if kwargs:
168
 
            for key, val in kwargs.iteritems():
169
 
                self.add(key, val)
 
159
            for key in kwargs:
 
160
                self.add(key, kwargs[key])
170
161
 
171
162
 
172
163
class FIFOSizeCache(FIFOCache):
176
167
    it restricts the cache to be cleaned based on the size of the data.
177
168
    """
178
169
 
179
 
    def __init__(self, max_size=1024*1024, after_cleanup_size=None,
 
170
    def __init__(self, max_size=1024 * 1024, after_cleanup_size=None,
180
171
                 compute_size=None):
181
172
        """Create a new FIFOSizeCache.
182
173
 
191
182
        FIFOCache.__init__(self, max_cache=max_size)
192
183
        self._max_size = max_size
193
184
        if after_cleanup_size is None:
194
 
            self._after_cleanup_size = self._max_size * 8 / 10
 
185
            self._after_cleanup_size = self._max_size * 8 // 10
195
186
        else:
196
187
            self._after_cleanup_size = min(after_cleanup_size, self._max_size)
197
188
 
260
251
        FIFOCache.resize(self, max_size)
261
252
        self._max_size = max_size
262
253
        if after_cleanup_size is None:
263
 
            self._after_cleanup_size = max_size * 8 / 10
 
254
            self._after_cleanup_size = max_size * 8 // 10
264
255
        else:
265
256
            self._after_cleanup_size = min(max_size, after_cleanup_size)
266
257
        if self._value_size > self._max_size:
267
258
            self.cleanup()
268