~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
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
6379.6.3 by Jelmer Vernooij
Use absolute_import.
20
from __future__ import absolute_import
21
2294.1.10 by John Arbash Meinel
Switch all apis over to utf8 file ids. All tests pass
22
from bzrlib import (
23
    errors,
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.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
29
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
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
35
    def add_weave(self, id, weave):
36
        """Add weave to the map with a given id."""
37
        if self._weave_key(id) in self._map:
38
            raise errors.BzrError('weave %s already in the identity map' % id)
39
        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.
40
        self._reverse_map[weave] = self._weave_key(id)
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
41
42
    def find_weave(self, id):
43
        """Return the weave for 'id', or None if it is not present."""
44
        return self._map.get(self._weave_key(id), None)
45
46
    def __init__(self):
47
        super(IdentityMap, self).__init__()
48
        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.
49
        self._reverse_map = {}
50
51
    def remove_object(self, an_object):
1417.1.12 by Robert Collins
cache revision history during read transactions
52
        """Remove object from map."""
53
        if isinstance(an_object, list):
2375.1.5 by Andrew Bennetts
Deal with review comments from Robert:
54
            raise KeyError('%r not in identity map' % an_object)
1417.1.12 by Robert Collins
cache revision history during read transactions
55
        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.
56
            self._map.pop(self._reverse_map[an_object])
57
            self._reverse_map.pop(an_object)
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
58
59
    def _weave_key(self, id):
60
        """Return the key for a weaves id."""
61
        return "weave-" + id
62
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
63
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
64
class NullIdentityMap(object):
65
    """A pretend in memory map from object id to instance.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
66
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
67
    A NullIdentityMap is an Identity map that does not store anything in it.
68
    """
69
70
    def add_weave(self, id, weave):
71
        """See IdentityMap.add_weave."""
72
73
    def find_weave(self, id):
74
        """See IdentityMap.find_weave."""
75
        return None