~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/identitymap.py

doc

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 Canonical Ltd
 
1
# Copyright (C) 2005 by Canonical Ltd
2
2
#   Authors: Robert Collins <robert.collins@canonical.com>
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
13
13
#
14
14
# You should have received a copy of the GNU General Public License
15
15
# along with this program; if not, write to the Free Software
16
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
17
 
18
18
"""This module provides an IdentityMap."""
19
19
 
20
20
 
21
 
from bzrlib import (
22
 
    errors,
23
 
    osutils,
24
 
    )
 
21
import bzrlib.errors as errors
25
22
 
26
23
 
27
24
class IdentityMap(object):
28
25
    """An in memory map from object id to instance.
29
 
 
 
26
    
30
27
    An IdentityMap maps from keys to single instances of objects in memory.
31
28
    We have explicit calls on the map for the root of each inheritance tree
32
29
    that is store in the map. Look for find_CLASS and add_CLASS methods.
37
34
        if self._weave_key(id) in self._map:
38
35
            raise errors.BzrError('weave %s already in the identity map' % id)
39
36
        self._map[self._weave_key(id)] = weave
40
 
        self._reverse_map[weave] = self._weave_key(id)
41
37
 
42
38
    def find_weave(self, id):
43
39
        """Return the weave for 'id', or None if it is not present."""
46
42
    def __init__(self):
47
43
        super(IdentityMap, self).__init__()
48
44
        self._map = {}
49
 
        self._reverse_map = {}
50
 
 
51
 
    def remove_object(self, an_object):
52
 
        """Remove object from map."""
53
 
        if isinstance(an_object, list):
54
 
            raise KeyError('%r not in identity map' % an_object)
55
 
        else:
56
 
            self._map.pop(self._reverse_map[an_object])
57
 
            self._reverse_map.pop(an_object)
58
45
 
59
46
    def _weave_key(self, id):
60
47
        """Return the key for a weaves id."""
61
48
        return "weave-" + id
62
49
 
63
 
 
 
50
        
64
51
class NullIdentityMap(object):
65
52
    """A pretend in memory map from object id to instance.
66
 
 
 
53
    
67
54
    A NullIdentityMap is an Identity map that does not store anything in it.
68
55
    """
69
56
 
73
60
    def find_weave(self, id):
74
61
        """See IdentityMap.find_weave."""
75
62
        return None
 
63