~bzr-pqm/bzr/bzr.dev

1563.2.12 by Robert Collins
Checkpointing: created InterObject to factor out common inter object worker code, added InterVersionedFile and tests to allow making join work between any versionedfile.
1
# Copyright (C) 2006 Canonical Ltd
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
18
"""Inter-object utility class."""
19
20
21
class InterObject(object):
22
    """This class represents operations taking place between two objects.
23
24
    Its instances have methods like join or copy_content or fetch, and contain
25
    references to the source and target objects these operations can be 
26
    carried out between.
27
28
    Often we will provide convenience methods on the objects which carry out
29
    operations with another of similar type - they will always forward to
30
    a subclass of InterObject - i.e. 
31
    InterVersionedFile.get(other).method_name(parameters).
32
    """
33
34
    # _optimisers = set()
35
    # Each concrete InterObject type should have its own optimisers set.
36
37
    def __init__(self, source, target):
38
        """Construct a default InterObject instance. Please use 'get'.
39
        
40
        Only subclasses of InterObject should call 
41
        InterObject.__init__ - clients should call InterFOO.get where FOO
42
        is the base type of the objects they are interacting between. I.e.
43
        InterVersionedFile or InterRepository.
44
        get() is a convenience class method which will create an optimised
45
        InterFOO if possible.
46
        """
47
        self.source = source
48
        self.target = target
49
50
    @classmethod
51
    def get(klass, source, target):
52
        """Retrieve a Inter worker object for these objects.
53
54
        :param source: the object to be the 'source' member of
55
                       the InterObject instance.
56
        :param target: the object to be the 'target' member of
57
                       the InterObject instance.
58
        If an optimised worker exists it will be used otherwise
59
        a default Inter worker instance will be created.
60
        """
61
        for provider in klass._optimisers:
62
            if provider.is_compatible(source, target):
63
                return provider(source, target)
64
        return klass(source, target)
65
66
    @classmethod
67
    def register_optimiser(klass, optimiser):
68
        """Register an InterObject optimiser."""
69
        klass._optimisers.add(optimiser)
70
71
    @classmethod
72
    def unregister_optimiser(klass, optimiser):
73
        """Unregister an InterObject optimiser."""
74
        klass._optimisers.remove(optimiser)