bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
| 
1417.1.6
by Robert Collins
 introduce transactions for grouping actions done to and with branches  | 
1  | 
# Copyright (C) 2005 by Canonical Ltd
 | 
2  | 
#   Authors: Robert Collins <robert.collins@canonical.com>
 | 
|
3  | 
#
 | 
|
4  | 
# This program is free software; you can redistribute it and/or modify
 | 
|
5  | 
# it under the terms of the GNU General Public License as published by
 | 
|
6  | 
# the Free Software Foundation; either version 2 of the License, or
 | 
|
7  | 
# (at your option) any later version.
 | 
|
8  | 
#
 | 
|
9  | 
# This program is distributed in the hope that it will be useful,
 | 
|
10  | 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|
11  | 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|
12  | 
# GNU General Public License for more details.
 | 
|
13  | 
#
 | 
|
14  | 
# You should have received a copy of the GNU General Public License
 | 
|
15  | 
# along with this program; if not, write to the Free Software
 | 
|
16  | 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 | 
|
17  | 
||
18  | 
"""This module provides a transactional facility.
 | 
|
19  | 
||
20  | 
Transactions provide hooks to allow data objects (i.e. inventory weaves or
 | 
|
21  | 
the revision-history file) to be placed in a registry and retrieved later
 | 
|
22  | 
during the same transaction.  This allows for repeated read isolation. At
 | 
|
23  | 
the end of a transaction, a callback is issued to each registered changed
 | 
|
24  | 
item informing it whether it should commit or not. We provide a two layer
 | 
|
25  | 
facility - domain objects are notified first, then data objects.
 | 
|
26  | 
||
27  | 
Read only transactions raise an assert when objects are listed as dirty
 | 
|
28  | 
against them - preventing unintended writes. Once all the data storage is
 | 
|
29  | 
hooked into this facility, it might be nice to have a readonly transaction
 | 
|
30  | 
that just excepts on commit, for testing or simulating of things.
 | 
|
31  | 
||
32  | 
Write transactions queue all changes in the transaction (which may in the 
 | 
|
33  | 
future involve writing them to uncommitted atomic files in preparation 
 | 
|
34  | 
for commit - i.e. on network connections where latency matters) and then
 | 
|
35  | 
notify each object of commit or rollback.
 | 
|
36  | 
||
37  | 
Both read and write transactions *may* flush unchanged objects out of 
 | 
|
38  | 
memory, unless they are marked as 'preserve' which indicates that 
 | 
|
39  | 
repeated reads cannot be obtained if the object is ejected.
 | 
|
40  | 
"""
 | 
|
41  | 
||
| 
1417.1.10
by Robert Collins
 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.  | 
42  | 
import sys  | 
43  | 
||
| 
1417.1.6
by Robert Collins
 introduce transactions for grouping actions done to and with branches  | 
44  | 
import bzrlib.errors as errors  | 
45  | 
from bzrlib.identitymap import IdentityMap, NullIdentityMap  | 
|
| 
1417.1.10
by Robert Collins
 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.  | 
46  | 
from bzrlib.trace import mutter  | 
| 
1417.1.6
by Robert Collins
 introduce transactions for grouping actions done to and with branches  | 
47  | 
|
48  | 
||
49  | 
class ReadOnlyTransaction(object):  | 
|
50  | 
"""A read only unit of work for data objects."""  | 
|
51  | 
||
52  | 
def commit(self):  | 
|
53  | 
"""ReadOnlyTransactions cannot commit."""  | 
|
54  | 
raise errors.CommitNotPossible('In a read only transaction')  | 
|
55  | 
||
56  | 
def finish(self):  | 
|
57  | 
"""Clean up this transaction  | 
|
58  | 
||
59  | 
        This will rollback on transactions that can if they have nto been
 | 
|
60  | 
        committed.
 | 
|
61  | 
        """
 | 
|
62  | 
||
63  | 
def __init__(self):  | 
|
| 
1417.1.10
by Robert Collins
 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.  | 
64  | 
super(ReadOnlyTransaction, self).__init__()  | 
| 
1417.1.6
by Robert Collins
 introduce transactions for grouping actions done to and with branches  | 
65  | 
self.map = IdentityMap()  | 
| 
1417.1.10
by Robert Collins
 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.  | 
66  | 
self._clean_objects = set()  | 
67  | 
self._clean_queue = []  | 
|
68  | 
self._limit = -1  | 
|
69  | 
self._precious_objects = set()  | 
|
| 
1417.1.6
by Robert Collins
 introduce transactions for grouping actions done to and with branches  | 
70  | 
|
| 
1417.1.10
by Robert Collins
 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.  | 
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()  | 
|
| 
1417.1.6
by Robert Collins
 introduce transactions for grouping actions done to and with branches  | 
82  | 
|
| 
1417.1.8
by Robert Collins
 use transactions in the weave store interface, which enables caching for log  | 
83  | 
def register_dirty(self, an_object):  | 
84  | 
"""Register an_object as being dirty."""  | 
|
85  | 
raise errors.ReadOnlyError(  | 
|
86  | 
"Cannot dirty objects in a read only transaction")  | 
|
87  | 
||
| 
1417.1.6
by Robert Collins
 introduce transactions for grouping actions done to and with branches  | 
88  | 
def rollback(self):  | 
89  | 
"""Let people call this even though nothing has to happen."""  | 
|
90  | 
||
| 
1417.1.10
by Robert Collins
 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.  | 
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  | 
||
| 
1417.1.6
by Robert Collins
 introduce transactions for grouping actions done to and with branches  | 
124  | 
|
125  | 
class PassThroughTransaction(object):  | 
|
126  | 
"""A pass through transaction  | 
|
127  | 
    
 | 
|
128  | 
    - all actions are committed immediately.
 | 
|
129  | 
    - rollback is not supported.
 | 
|
130  | 
    - commit() is a no-op.
 | 
|
131  | 
    """
 | 
|
132  | 
||
133  | 
def commit(self):  | 
|
134  | 
"""PassThroughTransactions have nothing to do."""  | 
|
135  | 
||
136  | 
def finish(self):  | 
|
137  | 
"""Clean up this transaction  | 
|
138  | 
||
139  | 
        This will rollback on transactions that can if they have nto been
 | 
|
140  | 
        committed.
 | 
|
141  | 
        """
 | 
|
142  | 
||
143  | 
def __init__(self):  | 
|
| 
1417.1.10
by Robert Collins
 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.  | 
144  | 
super(PassThroughTransaction, self).__init__()  | 
| 
1417.1.6
by Robert Collins
 introduce transactions for grouping actions done to and with branches  | 
145  | 
self.map = NullIdentityMap()  | 
146  | 
||
| 
1417.1.10
by Robert Collins
 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.  | 
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  | 
        """
 | 
|
| 
1417.1.6
by Robert Collins
 introduce transactions for grouping actions done to and with branches  | 
153  | 
|
| 
1417.1.8
by Robert Collins
 use transactions in the weave store interface, which enables caching for log  | 
154  | 
def register_dirty(self, an_object):  | 
155  | 
"""Register an_object as being dirty."""  | 
|
156  | 
||
| 
1417.1.6
by Robert Collins
 introduce transactions for grouping actions done to and with branches  | 
157  | 
def rollback(self):  | 
158  | 
"""Cannot rollback a pass through transaction."""  | 
|
159  | 
raise errors.AlreadyCommitted  | 
|
| 
1417.1.10
by Robert Collins
 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.  | 
160  | 
|
161  | 
def set_cache_size(self, ignored):  | 
|
162  | 
"""Do nothing, we are passing through."""  |