/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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
6379.6.7 by Jelmer Vernooij
Move importing from future until after doc string, otherwise the doc string will disappear.
18
"""This module provides an IdentityMap."""
19
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
20
from . import (
2294.1.10 by John Arbash Meinel
Switch all apis over to utf8 file ids. All tests pass
21
    errors,
22
    )
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
23
24
25
class IdentityMap(object):
26
    """An in memory map from object id to instance.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
27
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
28
    An IdentityMap maps from keys to single instances of objects in memory.
29
    We have explicit calls on the map for the root of each inheritance tree
30
    that is store in the map. Look for find_CLASS and add_CLASS methods.
31
    """
32
33
    def add_weave(self, id, weave):
34
        """Add weave to the map with a given id."""
35
        if self._weave_key(id) in self._map:
36
            raise errors.BzrError('weave %s already in the identity map' % id)
37
        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.
38
        self._reverse_map[weave] = self._weave_key(id)
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
39
40
    def find_weave(self, id):
41
        """Return the weave for 'id', or None if it is not present."""
42
        return self._map.get(self._weave_key(id), None)
43
44
    def __init__(self):
45
        super(IdentityMap, self).__init__()
46
        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.
47
        self._reverse_map = {}
48
49
    def remove_object(self, an_object):
1417.1.12 by Robert Collins
cache revision history during read transactions
50
        """Remove object from map."""
51
        if isinstance(an_object, list):
2375.1.5 by Andrew Bennetts
Deal with review comments from Robert:
52
            raise KeyError('%r not in identity map' % an_object)
1417.1.12 by Robert Collins
cache revision history during read transactions
53
        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.
54
            self._map.pop(self._reverse_map[an_object])
55
            self._reverse_map.pop(an_object)
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
56
57
    def _weave_key(self, id):
58
        """Return the key for a weaves id."""
59
        return "weave-" + id
60
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
61
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
62
class NullIdentityMap(object):
63
    """A pretend in memory map from object id to instance.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
64
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
65
    A NullIdentityMap is an Identity map that does not store anything in it.
66
    """
67
68
    def add_weave(self, id, weave):
69
        """See IdentityMap.add_weave."""
70
71
    def find_weave(self, id):
72
        """See IdentityMap.find_weave."""
73
        return None