bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
| 
1417.1.6
by Robert Collins
 introduce transactions for grouping actions done to and with branches  | 
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_weave(self, id, weave):  | 
|
33  | 
"""Add weave to the map with a given id."""  | 
|
34  | 
if self._weave_key(id) in self._map:  | 
|
35  | 
raise errors.BzrError('weave %s already in the identity map' % id)  | 
|
36  | 
self._map[self._weave_key(id)] = weave  | 
|
37  | 
||
38  | 
def find_weave(self, id):  | 
|
39  | 
"""Return the weave for 'id', or None if it is not present."""  | 
|
40  | 
return self._map.get(self._weave_key(id), None)  | 
|
41  | 
||
42  | 
def __init__(self):  | 
|
43  | 
super(IdentityMap, self).__init__()  | 
|
44  | 
self._map = {}  | 
|
45  | 
||
46  | 
def _weave_key(self, id):  | 
|
47  | 
"""Return the key for a weaves id."""  | 
|
48  | 
return "weave-" + id  | 
|
49  | 
||
50  | 
||
51  | 
class NullIdentityMap(object):  | 
|
52  | 
"""A pretend in memory map from object id to instance.  | 
|
53  | 
    
 | 
|
54  | 
    A NullIdentityMap is an Identity map that does not store anything in it.
 | 
|
55  | 
    """
 | 
|
56  | 
||
57  | 
def add_weave(self, id, weave):  | 
|
58  | 
"""See IdentityMap.add_weave."""  | 
|
59  | 
||
60  | 
def find_weave(self, id):  | 
|
61  | 
"""See IdentityMap.find_weave."""  | 
|
62  | 
return None  | 
|
63  |