~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transactions.py

  • Committer: Robert Collins
  • Date: 2005-10-08 00:39:04 UTC
  • mfrom: (1185.1.52)
  • Revision ID: robertc@robertcollins.net-20051008003904-aaffaea2778efe3e
merge in martins reweave, integrated to fetch, and a bugfix for commit and upgrade with executable files

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
repeated reads cannot be obtained if the object is ejected.
40
40
"""
41
41
 
42
 
import sys
43
 
 
44
42
import bzrlib.errors as errors
45
43
from bzrlib.identitymap import IdentityMap, NullIdentityMap
46
 
from bzrlib.trace import mutter
47
44
 
48
45
 
49
46
class ReadOnlyTransaction(object):
61
58
        """
62
59
 
63
60
    def __init__(self):
64
 
        super(ReadOnlyTransaction, self).__init__()
65
61
        self.map = IdentityMap()
66
 
        self._clean_objects = set()
67
 
        self._clean_queue = []
68
 
        self._limit = -1
69
 
        self._precious_objects = set()
70
62
 
71
 
    def register_clean(self, an_object, precious=False):
72
 
        """Register an_object as being clean.
73
 
        
74
 
        If the precious hint is True, the object will not
75
 
        be ejected from the object identity map ever.
76
 
        """
77
 
        self._clean_objects.add(an_object)
78
 
        self._clean_queue.append(an_object)
79
 
        if precious:
80
 
            self._precious_objects.add(an_object)
81
 
        self._trim()
 
63
    def register_clean(self, an_object):
 
64
        """Register an_object as being clean."""
82
65
 
83
66
    def register_dirty(self, an_object):
84
67
        """Register an_object as being dirty."""
88
71
    def rollback(self):
89
72
        """Let people call this even though nothing has to happen."""
90
73
 
91
 
    def set_cache_size(self, size):
92
 
        """Set a new cache size."""
93
 
        assert -1 <= size
94
 
        self._limit = size
95
 
        self._trim()
96
 
 
97
 
    def _trim(self):
98
 
        """Trim the cache back if needed."""
99
 
        if self._limit < 0 or self._limit - len(self._clean_objects) > 0:
100
 
            return
101
 
        needed = len(self._clean_objects) - self._limit
102
 
        offset = 0
103
 
        while needed and offset < len(self._clean_objects):
104
 
            # references we know of:
105
 
            # temp passed to getrefcount in our frame
106
 
            # temp in getrefcount's frame
107
 
            # the map forward
108
 
            # the map backwards
109
 
            # _clean_objects
110
 
            # _clean_queue
111
 
            # 1 missing ?
112
 
            if (sys.getrefcount(self._clean_queue[offset]) <= 7 and
113
 
                not self._clean_queue[offset] in self._precious_objects):
114
 
                removed = self._clean_queue[offset]
115
 
                self._clean_objects.remove(removed)
116
 
                del self._clean_queue[offset]
117
 
                self.map.remove_object(removed)
118
 
                mutter('removed object %r', removed)
119
 
                needed -= 1
120
 
            else:
121
 
                offset += 1
122
 
 
123
 
 
124
74
        
125
75
class PassThroughTransaction(object):
126
76
    """A pass through transaction
141
91
        """
142
92
 
143
93
    def __init__(self):
144
 
        super(PassThroughTransaction, self).__init__()
145
94
        self.map = NullIdentityMap()
146
95
 
147
 
    def register_clean(self, an_object, precious=False):
148
 
        """Register an_object as being clean.
149
 
        
150
 
        Note that precious is only a hint, and PassThroughTransaction
151
 
        ignores it.
152
 
        """
 
96
    def register_clean(self, an_object):
 
97
        """Register an_object as being clean."""
153
98
 
154
99
    def register_dirty(self, an_object):
155
100
        """Register an_object as being dirty."""
157
102
    def rollback(self):
158
103
        """Cannot rollback a pass through transaction."""
159
104
        raise errors.AlreadyCommitted
160
 
 
161
 
    def set_cache_size(self, ignored):
162
 
        """Do nothing, we are passing through."""