~bzr-pqm/bzr/bzr.dev

2052.3.2 by John Arbash Meinel
Change Copyright .. by Canonical to Copyright ... Canonical
1
# Copyright (C) 2005 Canonical Ltd
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
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 an IdentityMap."""
19
20
2294.1.10 by John Arbash Meinel
Switch all apis over to utf8 file ids. All tests pass
21
from bzrlib import (
22
    errors,
23
    osutils,
24
    )
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
25
26
27
class IdentityMap(object):
28
    """An in memory map from object id to instance.
29
    
30
    An IdentityMap maps from keys to single instances of objects in memory.
31
    We have explicit calls on the map for the root of each inheritance tree
32
    that is store in the map. Look for find_CLASS and add_CLASS methods.
33
    """
34
1417.1.12 by Robert Collins
cache revision history during read transactions
35
    def add_revision_history(self, revision_history):
36
        """Add a revision_history object to the map.
37
38
        There can only be one!
39
        """
40
        if self._revision_history is not None:
41
            raise errors.BzrError("A revision history (%s) is already "
42
                                  "identity map" % self._revision_history)
43
        self._revision_history = revision_history
44
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
45
    def add_weave(self, id, weave):
46
        """Add weave to the map with a given id."""
47
        if self._weave_key(id) in self._map:
48
            raise errors.BzrError('weave %s already in the identity map' % id)
49
        self._map[self._weave_key(id)] = weave
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.
50
        self._reverse_map[weave] = self._weave_key(id)
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
51
1417.1.12 by Robert Collins
cache revision history during read transactions
52
    def find_revision_history(self):
53
        return self._revision_history
54
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
55
    def find_weave(self, id):
56
        """Return the weave for 'id', or None if it is not present."""
57
        return self._map.get(self._weave_key(id), None)
58
59
    def __init__(self):
60
        super(IdentityMap, self).__init__()
61
        self._map = {}
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.
62
        self._reverse_map = {}
1417.1.12 by Robert Collins
cache revision history during read transactions
63
        self._revision_history = None
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
65
    def remove_object(self, an_object):
1417.1.12 by Robert Collins
cache revision history during read transactions
66
        """Remove object from map."""
67
        if isinstance(an_object, list):
68
            if self._revision_history is an_object:
69
                self._revision_history = None
70
            else:
71
                raise KeyError('%r not in identity map' % an_object)
72
        else:
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.
73
            self._map.pop(self._reverse_map[an_object])
74
            self._reverse_map.pop(an_object)
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
75
76
    def _weave_key(self, id):
77
        """Return the key for a weaves id."""
78
        return "weave-" + id
79
80
        
81
class NullIdentityMap(object):
82
    """A pretend in memory map from object id to instance.
83
    
84
    A NullIdentityMap is an Identity map that does not store anything in it.
85
    """
86
87
    def add_weave(self, id, weave):
88
        """See IdentityMap.add_weave."""
89
1417.1.12 by Robert Collins
cache revision history during read transactions
90
    def add_revision_history(self, revision_history):
91
        """See IdentityMap.add_revision_history."""
92
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
93
    def find_weave(self, id):
94
        """See IdentityMap.find_weave."""
95
        return None
96
1417.1.12 by Robert Collins
cache revision history during read transactions
97
    def find_revision_history(self):
98
        """See IdentityMap.find_revision_history."""