~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transactions.py

  • Committer: Robert Collins
  • Date: 2005-10-11 01:05:24 UTC
  • mto: This revision was merged to the branch mainline in revision 1438.
  • Revision ID: robertc@robertcollins.net-20051011010524-e258bc8d051cc9d2
add a cache bound to Transactions, and a precious facility, so that we keep inventory.weave in memory, but can discard weaves for other such 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
 
42
44
import bzrlib.errors as errors
43
45
from bzrlib.identitymap import IdentityMap, NullIdentityMap
 
46
from bzrlib.trace import mutter
44
47
 
45
48
 
46
49
class ReadOnlyTransaction(object):
58
61
        """
59
62
 
60
63
    def __init__(self):
 
64
        super(ReadOnlyTransaction, self).__init__()
61
65
        self.map = IdentityMap()
 
66
        self._clean_objects = set()
 
67
        self._clean_queue = []
 
68
        self._limit = -1
 
69
        self._precious_objects = set()
62
70
 
63
 
    def register_clean(self, an_object):
64
 
        """Register an_object as being clean."""
 
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()
65
82
 
66
83
    def register_dirty(self, an_object):
67
84
        """Register an_object as being dirty."""
71
88
    def rollback(self):
72
89
        """Let people call this even though nothing has to happen."""
73
90
 
 
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
 
74
124
        
75
125
class PassThroughTransaction(object):
76
126
    """A pass through transaction
91
141
        """
92
142
 
93
143
    def __init__(self):
 
144
        super(PassThroughTransaction, self).__init__()
94
145
        self.map = NullIdentityMap()
95
146
 
96
 
    def register_clean(self, an_object):
97
 
        """Register an_object as being clean."""
 
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
        """
98
153
 
99
154
    def register_dirty(self, an_object):
100
155
        """Register an_object as being dirty."""
102
157
    def rollback(self):
103
158
        """Cannot rollback a pass through transaction."""
104
159
        raise errors.AlreadyCommitted
 
160
 
 
161
    def set_cache_size(self, ignored):
 
162
        """Do nothing, we are passing through."""