~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
4183.7.1 by Sabin Iacob
update FSF mailing address
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
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
    )
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
24
25
26
class IdentityMap(object):
27
    """An in memory map from object id to instance.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
28
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
29
    An IdentityMap maps from keys to single instances of objects in memory.
30
    We have explicit calls on the map for the root of each inheritance tree
31
    that is store in the map. Look for find_CLASS and add_CLASS methods.
32
    """
33
34
    def add_weave(self, id, weave):
35
        """Add weave to the map with a given id."""
36
        if self._weave_key(id) in self._map:
37
            raise errors.BzrError('weave %s already in the identity map' % id)
38
        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.
39
        self._reverse_map[weave] = self._weave_key(id)
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
40
41
    def find_weave(self, id):
42
        """Return the weave for 'id', or None if it is not present."""
43
        return self._map.get(self._weave_key(id), None)
44
45
    def __init__(self):
46
        super(IdentityMap, self).__init__()
47
        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.
48
        self._reverse_map = {}
49
50
    def remove_object(self, an_object):
1417.1.12 by Robert Collins
cache revision history during read transactions
51
        """Remove object from map."""
52
        if isinstance(an_object, list):
2375.1.5 by Andrew Bennetts
Deal with review comments from Robert:
53
            raise KeyError('%r not in identity map' % an_object)
1417.1.12 by Robert Collins
cache revision history during read transactions
54
        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.
55
            self._map.pop(self._reverse_map[an_object])
56
            self._reverse_map.pop(an_object)
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
57
58
    def _weave_key(self, id):
59
        """Return the key for a weaves id."""
60
        return "weave-" + id
61
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
62
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
63
class NullIdentityMap(object):
64
    """A pretend in memory map from object id to instance.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
65
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
66
    A NullIdentityMap is an Identity map that does not store anything in it.
67
    """
68
69
    def add_weave(self, id, weave):
70
        """See IdentityMap.add_weave."""
71
72
    def find_weave(self, id):
73
        """See IdentityMap.find_weave."""
74
        return None