~bzr-pqm/bzr/bzr.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# Copyright (C) 2005 by Canonical Ltd
#   Authors: Robert Collins <robert.collins@canonical.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

"""This module provides a transactional facility.

Transactions provide hooks to allow data objects (i.e. inventory weaves or
the revision-history file) to be placed in a registry and retrieved later
during the same transaction.  This allows for repeated read isolation. At
the end of a transaction, a callback is issued to each registered changed
item informing it whether it should commit or not. We provide a two layer
facility - domain objects are notified first, then data objects.

Read only transactions raise an assert when objects are listed as dirty
against them - preventing unintended writes. Once all the data storage is
hooked into this facility, it might be nice to have a readonly transaction
that just excepts on commit, for testing or simulating of things.

Write transactions queue all changes in the transaction (which may in the 
future involve writing them to uncommitted atomic files in preparation 
for commit - i.e. on network connections where latency matters) and then
notify each object of commit or rollback.

Both read and write transactions *may* flush unchanged objects out of 
memory, unless they are marked as 'preserve' which indicates that 
repeated reads cannot be obtained if the object is ejected.
"""

import bzrlib.errors as errors
from bzrlib.identitymap import IdentityMap, NullIdentityMap


class ReadOnlyTransaction(object):
    """A read only unit of work for data objects."""

    def commit(self):
        """ReadOnlyTransactions cannot commit."""
        raise errors.CommitNotPossible('In a read only transaction')

    def finish(self):
        """Clean up this transaction

        This will rollback on transactions that can if they have nto been
        committed.
        """

    def __init__(self):
        self.map = IdentityMap()

    def register_clean(self, an_object):
        """Register an_object as being clean."""

    def register_dirty(self, an_object):
        """Register an_object as being dirty."""
        raise errors.ReadOnlyError(
            "Cannot dirty objects in a read only transaction")

    def rollback(self):
        """Let people call this even though nothing has to happen."""

        
class PassThroughTransaction(object):
    """A pass through transaction
    
    - all actions are committed immediately.
    - rollback is not supported.
    - commit() is a no-op.
    """

    def commit(self):
        """PassThroughTransactions have nothing to do."""

    def finish(self):
        """Clean up this transaction

        This will rollback on transactions that can if they have nto been
        committed.
        """

    def __init__(self):
        self.map = NullIdentityMap()

    def register_clean(self, an_object):
        """Register an_object as being clean."""

    def register_dirty(self, an_object):
        """Register an_object as being dirty."""

    def rollback(self):
        """Cannot rollback a pass through transaction."""
        raise errors.AlreadyCommitted