~bzr-pqm/bzr/bzr.dev

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
42
import bzrlib.errors as errors
43
from bzrlib.identitymap import IdentityMap, NullIdentityMap
44
45
46
class ReadOnlyTransaction(object):
47
    """A read only unit of work for data objects."""
48
49
    def commit(self):
50
        """ReadOnlyTransactions cannot commit."""
51
        raise errors.CommitNotPossible('In a read only transaction')
52
53
    def finish(self):
54
        """Clean up this transaction
55
56
        This will rollback on transactions that can if they have nto been
57
        committed.
58
        """
59
60
    def __init__(self):
61
        self.map = IdentityMap()
62
63
    def register_clean(self, an_object):
64
        """Register an_object as being clean."""
65
1417.1.8 by Robert Collins
use transactions in the weave store interface, which enables caching for log
66
    def register_dirty(self, an_object):
67
        """Register an_object as being dirty."""
68
        raise errors.ReadOnlyError(
69
            "Cannot dirty objects in a read only transaction")
70
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
71
    def rollback(self):
72
        """Let people call this even though nothing has to happen."""
73
74
        
75
class PassThroughTransaction(object):
76
    """A pass through transaction
77
    
78
    - all actions are committed immediately.
79
    - rollback is not supported.
80
    - commit() is a no-op.
81
    """
82
83
    def commit(self):
84
        """PassThroughTransactions have nothing to do."""
85
86
    def finish(self):
87
        """Clean up this transaction
88
89
        This will rollback on transactions that can if they have nto been
90
        committed.
91
        """
92
93
    def __init__(self):
94
        self.map = NullIdentityMap()
95
96
    def register_clean(self, an_object):
97
        """Register an_object as being clean."""
98
1417.1.8 by Robert Collins
use transactions in the weave store interface, which enables caching for log
99
    def register_dirty(self, an_object):
100
        """Register an_object as being dirty."""
101
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
102
    def rollback(self):
103
        """Cannot rollback a pass through transaction."""
104
        raise errors.AlreadyCommitted