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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
18
"""This module provides an IdentityMap."""
21
import bzrlib.errors as errors
24
26
class IdentityMap(object):
25
27
"""An in memory map from object id to instance.
27
29
An IdentityMap maps from keys to single instances of objects in memory.
28
30
We have explicit calls on the map for the root of each inheritance tree
29
31
that is store in the map. Look for find_CLASS and add_CLASS methods.
32
def add_revision_history(self, revision_history):
33
"""Add a revision_history object to the map.
35
There can only be one!
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
42
34
def add_weave(self, id, weave):
43
35
"""Add weave to the map with a given id."""
44
36
if self._weave_key(id) in self._map:
46
38
self._map[self._weave_key(id)] = weave
47
39
self._reverse_map[weave] = self._weave_key(id)
49
def find_revision_history(self):
50
return self._revision_history
52
41
def find_weave(self, id):
53
42
"""Return the weave for 'id', or None if it is not present."""
54
43
return self._map.get(self._weave_key(id), None)
57
46
super(IdentityMap, self).__init__()
59
48
self._reverse_map = {}
60
self._revision_history = None
62
50
def remove_object(self, an_object):
63
51
"""Remove object from map."""
64
52
if isinstance(an_object, list):
65
if self._revision_history is an_object:
66
self._revision_history = None
68
raise KeyError('%r not in identity map' % an_object)
53
raise KeyError('%r not in identity map' % an_object)
70
55
self._map.pop(self._reverse_map[an_object])
71
56
self._reverse_map.pop(an_object)
74
58
def _weave_key(self, id):
75
59
"""Return the key for a weaves id."""
76
60
return "weave-" + id
79
63
class NullIdentityMap(object):
80
64
"""A pretend in memory map from object id to instance.
82
66
A NullIdentityMap is an Identity map that does not store anything in it.
85
69
def add_weave(self, id, weave):
86
70
"""See IdentityMap.add_weave."""
88
def add_revision_history(self, revision_history):
89
"""See IdentityMap.add_revision_history."""
91
72
def find_weave(self, id):
92
73
"""See IdentityMap.find_weave."""
95
def find_revision_history(self):
96
"""See IdentityMap.find_revision_history."""