~ubuntu-branches/ubuntu/hardy/bzr/hardy-updates

« back to all changes in this revision

Viewing changes to bzrlib/identitymap.py

  • Committer: Bazaar Package Importer
  • Author(s): Jeff Bailey
  • Date: 2005-11-07 13:17:53 UTC
  • Revision ID: james.westby@ubuntu.com-20051107131753-qsy145z1rfug5i27
Tags: upstream-0.6.2
ImportĀ upstreamĀ versionĀ 0.6.2

Show diffs side-by-side

added added

removed removed

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