/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 bzrlib/fifo_cache.py

  • Committer: Richard Wilbur
  • Date: 2016-02-04 19:07:28 UTC
  • mto: This revision was merged to the branch mainline in revision 6618.
  • Revision ID: richard.wilbur@gmail.com-20160204190728-p0zvfii6zase0fw7
Update COPYING.txt from the original http://www.gnu.org/licenses/gpl-2.0.txt  (Only differences were in whitespace.)  Thanks to Petr Stodulka for pointing out the discrepancy.

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
 
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
 
        self._queue.remove(key)
 
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])
43
54
        self._remove(key)
44
55
 
45
56
    def add(self, key, value, cleanup=None):
79
90
            self._remove_oldest()
80
91
        if len(self._queue) != len(self):
81
92
            raise AssertionError('The length of the queue should always equal'
82
 
                                 ' the length of the dict. %s != %s'
83
 
                                 % (len(self._queue), len(self)))
 
93
                ' the length of the dict. %s != %s'
 
94
                % (len(self._queue), len(self)))
84
95
 
85
96
    def clear(self):
86
97
        """Clear out all of the cache."""
112
123
        """
113
124
        self._max_cache = max_cache
114
125
        if after_cleanup_count is None:
115
 
            self._after_cleanup_count = max_cache * 8 // 10
 
126
            self._after_cleanup_count = max_cache * 8 / 10
116
127
        else:
117
128
            self._after_cleanup_count = min(max_cache, after_cleanup_count)
118
129
        if len(self) > self._max_cache:
147
158
        if len(args) == 1:
148
159
            arg = args[0]
149
160
            if isinstance(arg, dict):
150
 
                for key in arg:
151
 
                    self.add(key, arg[key])
 
161
                for key, val in arg.iteritems():
 
162
                    self.add(key, val)
152
163
            else:
153
164
                for key, val in args[0]:
154
165
                    self.add(key, val)
156
167
            raise TypeError('update expected at most 1 argument, got %d'
157
168
                            % len(args))
158
169
        if kwargs:
159
 
            for key in kwargs:
160
 
                self.add(key, kwargs[key])
 
170
            for key, val in kwargs.iteritems():
 
171
                self.add(key, val)
161
172
 
162
173
 
163
174
class FIFOSizeCache(FIFOCache):
167
178
    it restricts the cache to be cleaned based on the size of the data.
168
179
    """
169
180
 
170
 
    def __init__(self, max_size=1024 * 1024, after_cleanup_size=None,
 
181
    def __init__(self, max_size=1024*1024, after_cleanup_size=None,
171
182
                 compute_size=None):
172
183
        """Create a new FIFOSizeCache.
173
184
 
182
193
        FIFOCache.__init__(self, max_cache=max_size)
183
194
        self._max_size = max_size
184
195
        if after_cleanup_size is None:
185
 
            self._after_cleanup_size = self._max_size * 8 // 10
 
196
            self._after_cleanup_size = self._max_size * 8 / 10
186
197
        else:
187
198
            self._after_cleanup_size = min(after_cleanup_size, self._max_size)
188
199
 
251
262
        FIFOCache.resize(self, max_size)
252
263
        self._max_size = max_size
253
264
        if after_cleanup_size is None:
254
 
            self._after_cleanup_size = max_size * 8 // 10
 
265
            self._after_cleanup_size = max_size * 8 / 10
255
266
        else:
256
267
            self._after_cleanup_size = min(max_size, after_cleanup_size)
257
268
        if self._value_size > self._max_size:
258
269
            self.cleanup()
 
270