~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transactions.py

  • Committer: Robert Collins
  • Date: 2006-03-07 12:39:38 UTC
  • mfrom: (1594 +trunk)
  • mto: (1594.2.4 integration)
  • mto: This revision was merged to the branch mainline in revision 1596.
  • Revision ID: robertc@robertcollins.net-20060307123938-f75ff66ebcc0c4d0
Merge in bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 Canonical Ltd
 
1
# Copyright (C) 2005 by Canonical Ltd
2
2
#   Authors: Robert Collins <robert.collins@canonical.com>
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
13
13
#
14
14
# You should have received a copy of the GNU General Public License
15
15
# along with this program; if not, write to the Free Software
16
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
17
 
18
18
"""This module provides a transactional facility.
19
19
 
30
30
write ordering approach we use for consistency 'dirty' is a misleading term.
31
31
A dirty object is one we have modified.
32
32
 
33
 
Both read and write transactions *may* flush unchanged objects out of
34
 
memory, unless they are marked as 'precious' which indicates that
 
33
Both read and write transactions *may* flush unchanged objects out of 
 
34
memory, unless they are marked as 'precious' which indicates that 
35
35
repeated reads cannot be obtained if the object is ejected, or that
36
36
the object is an expensive one for obtaining.
37
37
"""
59
59
 
60
60
    def is_clean(self, an_object):
61
61
        """Return True if an_object is clean."""
62
 
        return (an_object in self._clean_objects)
 
62
        return an_object in self._clean_objects
63
63
 
64
64
    def register_clean(self, an_object, precious=False):
65
65
        """Register an_object as being clean.
66
 
 
 
66
        
67
67
        If the precious hint is True, the object will not
68
68
        be ejected from the object identity map ever.
69
69
        """
79
79
 
80
80
    def set_cache_size(self, size):
81
81
        """Set a new cache size."""
82
 
        if size < -1:
83
 
            raise ValueError(size)
 
82
        assert -1 <= size
84
83
        self._limit = size
85
84
        self._trim()
86
85
 
110
109
            else:
111
110
                offset += 1
112
111
 
113
 
    def writeable(self):
114
 
        """Read only transactions do not allow writes."""
115
 
 
116
112
 
117
113
class WriteTransaction(ReadOnlyTransaction):
118
114
    """A write transaction
122
118
    - dirty objects are retained.
123
119
    """
124
120
 
125
 
    def finish(self):
126
 
        """Clean up this transaction."""
127
 
        for thing in self._dirty_objects:
128
 
            callback = getattr(thing, 'transaction_finished', None)
129
 
            if callback is not None:
130
 
                callback()
131
 
 
132
121
    def __init__(self):
133
122
        super(WriteTransaction, self).__init__()
134
123
        self._dirty_objects = set()
135
124
 
136
125
    def is_dirty(self, an_object):
137
126
        """Return True if an_object is dirty."""
138
 
        return (an_object in self._dirty_objects)
 
127
        return an_object in self._dirty_objects
139
128
 
140
129
    def register_dirty(self, an_object):
141
130
        """Register an_object as being dirty.
142
 
 
 
131
        
143
132
        Dirty objects are not ejected from the identity map
144
 
        until the transaction finishes and get informed
145
 
        when the transaction finishes.
 
133
        until the transaction finishes.
146
134
        """
147
135
        self._dirty_objects.add(an_object)
148
136
        if self.is_clean(an_object):
150
138
            del self._clean_queue[self._clean_queue.index(an_object)]
151
139
        self._trim()
152
140
 
153
 
    def writeable(self):
154
 
        """Write transactions allow writes."""
155
 
        return True
156
 
 
157
 
 
 
141
        
158
142
class PassThroughTransaction(object):
159
143
    """A pass through transaction
160
 
 
 
144
    
161
145
    - nothing is cached.
162
146
    - nothing ever gets into the identity map.
163
147
    """
164
148
 
165
149
    def finish(self):
166
150
        """Clean up this transaction."""
167
 
        for thing in self._dirty_objects:
168
 
            callback = getattr(thing, 'transaction_finished', None)
169
 
            if callback is not None:
170
 
                callback()
171
151
 
172
152
    def __init__(self):
173
153
        super(PassThroughTransaction, self).__init__()
174
154
        self.map = NullIdentityMap()
175
 
        self._dirty_objects = set()
176
155
 
177
156
    def register_clean(self, an_object, precious=False):
178
157
        """Register an_object as being clean.
179
 
 
 
158
        
180
159
        Note that precious is only a hint, and PassThroughTransaction
181
160
        ignores it.
182
161
        """
183
162
 
184
163
    def register_dirty(self, an_object):
185
 
        """Register an_object as being dirty.
186
 
 
187
 
        Dirty objects get informed
188
 
        when the transaction finishes.
189
 
        """
190
 
        self._dirty_objects.add(an_object)
 
164
        """Register an_object as being dirty."""
191
165
 
192
166
    def set_cache_size(self, ignored):
193
167
        """Do nothing, we are passing through."""
194
 
 
195
 
    def writeable(self):
196
 
        """Pass through transactions allow writes."""
197
 
        return True