~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/identitymap.py

  • Committer: Ian Clatworthy
  • Date: 2007-03-22 01:02:07 UTC
  • mto: (2370.2.1 jam-integration)
  • mto: This revision was merged to the branch mainline in revision 2371.
  • Revision ID: ian.clatworthy@internode.on.net-20070322010207-r8xcwjshipoahmcx
Minor corrections to HACKING

Show diffs side-by-side

added added

removed removed

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