/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: Robert Collins
  • Date: 2010-05-06 23:41:35 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506234135-yivbzczw1sejxnxc
Lock methods on ``Tree``, ``Branch`` and ``Repository`` are now
expected to return an object which can be used to unlock them. This reduces
duplicate code when using cleanups. The previous 'tokens's returned by
``Branch.lock_write`` and ``Repository.lock_write`` are now attributes
on the result of the lock_write. ``repository.RepositoryWriteLockResult``
and ``branch.BranchWriteLockResult`` document this. (Robert Collins)

``log._get_info_for_log_files`` now takes an add_cleanup callable.
(Robert Collins)

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