/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: John Ferlito
  • Date: 2009-09-02 04:31:45 UTC
  • mto: (4665.7.1 serve-init)
  • mto: This revision was merged to the branch mainline in revision 4913.
  • Revision ID: johnf@inodes.org-20090902043145-gxdsfw03ilcwbyn5
Add a debian init script for bzr --serve

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