/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 Arbash Meinel
  • Date: 2008-12-09 22:26:40 UTC
  • mto: This revision was merged to the branch mainline in revision 3888.
  • Revision ID: john@arbash-meinel.com-20081209222640-u3iece2ixcd0q7lj
Add a FIFOSizeCache which is constrained based on the size of the values.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2008 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
"""A simple first-in-first-out (FIFO) cache."""
 
18
 
 
19
from collections import deque
 
20
 
 
21
 
 
22
class FIFOCache(dict):
 
23
    """A class which manages a cache of entries, removing old ones."""
 
24
 
 
25
    def __init__(self, max_cache=100, after_cleanup_count=None):
 
26
        dict.__init__(self)
 
27
        self._max_cache = max_cache
 
28
        if after_cleanup_count is None:
 
29
            self._after_cleanup_count = self._max_cache * 8 / 10
 
30
        else:
 
31
            self._after_cleanup_count = min(after_cleanup_count,
 
32
                                            self._max_cache)
 
33
        self._cleanup = {} # map to cleanup functions when items are removed
 
34
        self._queue = deque() # Track when things are accessed
 
35
 
 
36
    def __setitem__(self, key, value):
 
37
        """Add a value to the cache, there will be no cleanup function."""
 
38
        self.add(key, value, cleanup=None)
 
39
 
 
40
    def __delitem__(self, key):
 
41
        # Remove the key from an arbitrary location in the queue
 
42
        self._queue.remove(key)
 
43
        self._remove(key)
 
44
 
 
45
    def add(self, key, value, cleanup=None):
 
46
        """Add a new value to the cache.
 
47
 
 
48
        Also, if the entry is ever removed from the queue, call cleanup.
 
49
        Passing it the key and value being removed.
 
50
 
 
51
        :param key: The key to store it under
 
52
        :param value: The object to store
 
53
        :param cleanup: None or a function taking (key, value) to indicate
 
54
                        'value' should be cleaned up
 
55
        """
 
56
        if key in self:
 
57
            # Remove the earlier reference to this key, adding it again bumps
 
58
            # it to the end of the queue
 
59
            del self[key]
 
60
        self._queue.append(key)
 
61
        dict.__setitem__(self, key, value)
 
62
        if cleanup is not None:
 
63
            self._cleanup[key] = cleanup
 
64
        if len(self) > self._max_cache:
 
65
            self.cleanup()
 
66
 
 
67
    def cleanup(self):
 
68
        """Clear the cache until it shrinks to the requested size.
 
69
 
 
70
        This does not completely wipe the cache, just makes sure it is under
 
71
        the after_cleanup_count.
 
72
        """
 
73
        # Make sure the cache is shrunk to the correct size
 
74
        while len(self) > self._after_cleanup_count:
 
75
            self._remove_oldest()
 
76
        if len(self._queue) != len(self):
 
77
            raise AssertionError('The length of the queue should always equal'
 
78
                ' the length of the dict. %s != %s'
 
79
                % (len(self._queue), len(self)))
 
80
 
 
81
    def clear(self):
 
82
        """Clear out all of the cache."""
 
83
        # Clean up in FIFO order
 
84
        while self:
 
85
            self._remove_oldest()
 
86
 
 
87
    def _remove(self, key):
 
88
        """Remove an entry, making sure to call any cleanup function."""
 
89
        cleanup = self._cleanup.pop(key, None)
 
90
        # We override self.pop() because it doesn't play well with cleanup
 
91
        # functions.
 
92
        val = dict.pop(self, key)
 
93
        if cleanup is not None:
 
94
            cleanup(key, val)
 
95
        return val
 
96
 
 
97
    def _remove_oldest(self):
 
98
        """Remove the oldest entry."""
 
99
        key = self._queue.popleft()
 
100
        self._remove(key)
 
101
 
 
102
    # raise NotImplementedError on dict functions that would mutate the cache
 
103
    # which have not been properly implemented yet.
 
104
    def copy(self):
 
105
        raise NotImplementedError(self.copy)
 
106
 
 
107
    def pop(self, key, default=None):
 
108
        # If there is a cleanup() function, than it is unclear what pop()
 
109
        # should do. Specifically, we would have to call the cleanup on the
 
110
        # value before we return it, which should cause whatever resources were
 
111
        # allocated to be removed, which makes the return value fairly useless.
 
112
        # So instead, we just don't implement it.
 
113
        raise NotImplementedError(self.pop)
 
114
 
 
115
    def popitem(self):
 
116
        # See pop()
 
117
        raise NotImplementedError(self.popitem)
 
118
 
 
119
    def setdefault(self, key, defaultval=None):
 
120
        """similar to dict.setdefault"""
 
121
        if key in self:
 
122
            return self[key]
 
123
        self[key] = defaultval
 
124
        return defaultval
 
125
 
 
126
    def update(self, *args, **kwargs):
 
127
        """Similar to dict.update()"""
 
128
        if len(args) == 1:
 
129
            arg = args[0]
 
130
            if isinstance(arg, dict):
 
131
                for key, val in arg.iteritems():
 
132
                    self.add(key, val)
 
133
            else:
 
134
                for key, val in args[0]:
 
135
                    self.add(key, val)
 
136
        elif len(args) > 1:
 
137
            raise TypeError('update expected at most 1 argument, got %d'
 
138
                            % len(args))
 
139
        if kwargs:
 
140
            for key, val in kwargs.iteritems():
 
141
                self.add(key, val)
 
142
 
 
143
 
 
144
class FIFOSizeCache(FIFOCache):
 
145
    """An FIFOCache that removes things based on the size of the values.
 
146
 
 
147
    This differs in that it doesn't care how many actual items there are,
 
148
    it restricts the cache to be cleaned based on the size of the data.
 
149
    """
 
150
 
 
151
    def __init__(self, max_size=1024*1024, after_cleanup_size=None,
 
152
                 compute_size=None):
 
153
        """Create a new FIFOSizeCache.
 
154
 
 
155
        :param max_size: The max number of bytes to store before we start
 
156
            clearing out entries.
 
157
        :param after_cleanup_size: After cleaning up, shrink everything to this
 
158
            size (defaults to 80% of max_size).
 
159
        :param compute_size: A function to compute the size of a value. If
 
160
            not supplied we default to 'len'.
 
161
        """
 
162
        # Arbitrary, we won't really be using the value anyway.
 
163
        FIFOCache.__init__(self, max_cache=max_size)
 
164
        self._max_size = max_size
 
165
        if after_cleanup_size is None:
 
166
            self._after_cleanup_size = self._max_size * 8 / 10
 
167
        else:
 
168
            self._after_cleanup_size = min(after_cleanup_size, self._max_size)
 
169
 
 
170
        self._value_size = 0
 
171
        self._compute_size = compute_size
 
172
        if compute_size is None:
 
173
            self._compute_size = len
 
174
 
 
175
    def add(self, key, value, cleanup=None):
 
176
        """Add a new value to the cache.
 
177
 
 
178
        Also, if the entry is ever removed from the queue, call cleanup.
 
179
        Passing it the key and value being removed.
 
180
 
 
181
        :param key: The key to store it under
 
182
        :param value: The object to store, this value by itself is >=
 
183
            after_cleanup_size, then we will not store it at all.
 
184
        :param cleanup: None or a function taking (key, value) to indicate
 
185
                        'value' sohuld be cleaned up.
 
186
        """
 
187
        # Even if the new value won't be stored, we need to remove the old
 
188
        # value
 
189
        if key in self:
 
190
            # Remove the earlier reference to this key, adding it again bumps
 
191
            # it to the end of the queue
 
192
            del self[key]
 
193
        value_len = self._compute_size(value)
 
194
        if value_len >= self._after_cleanup_size:
 
195
            return
 
196
        self._queue.append(key)
 
197
        dict.__setitem__(self, key, value)
 
198
        if cleanup is not None:
 
199
            self._cleanup[key] = cleanup
 
200
        self._value_size += value_len
 
201
        if self._value_size > self._max_size:
 
202
            # Time to cleanup
 
203
            self.cleanup()
 
204
 
 
205
    def cleanup(self):
 
206
        """Clear the cache until it shrinks to the requested size.
 
207
 
 
208
        This does not completely wipe the cache, just makes sure it is under
 
209
        the after_cleanup_size.
 
210
        """
 
211
        # Make sure the cache is shrunk to the correct size
 
212
        while self._value_size > self._after_cleanup_size:
 
213
            self._remove_oldest()
 
214
 
 
215
    def _remove(self, key):
 
216
        """Remove an entry, making sure to maintain the invariants."""
 
217
        val = FIFOCache._remove(self, key)
 
218
        self._value_size -= self._compute_size(val)
 
219
        return val