~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/graph.py

  • Committer: Jelmer Vernooij
  • Date: 2009-03-28 14:24:46 UTC
  • mfrom: (4211 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4212.
  • Revision ID: jelmer@samba.org-20090328142446-vqi0ksswdurga631
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
import time
18
18
 
121
121
            self._get_parent_map = self._real_provider.get_parent_map
122
122
        else:
123
123
            self._get_parent_map = get_parent_map
124
 
        self._cache = {}
125
 
        self._cache_misses = True
 
124
        self._cache = None
 
125
        self.enable_cache(True)
126
126
 
127
127
    def __repr__(self):
128
128
        return "%s(%r)" % (self.__class__.__name__, self._real_provider)
133
133
            raise AssertionError('Cache enabled when already enabled.')
134
134
        self._cache = {}
135
135
        self._cache_misses = cache_misses
 
136
        self.missing_keys = set()
136
137
 
137
138
    def disable_cache(self):
138
139
        """Disable and clear the cache."""
139
140
        self._cache = None
 
141
        self._cache_misses = None
 
142
        self.missing_keys = set()
140
143
 
141
144
    def get_cached_map(self):
142
145
        """Return any cached get_parent_map values."""
143
146
        if self._cache is None:
144
147
            return None
145
 
        return dict((k, v) for k, v in self._cache.items()
146
 
                    if v is not None)
 
148
        return dict(self._cache)
147
149
 
148
150
    def get_parent_map(self, keys):
149
151
        """See _StackedParentsProvider.get_parent_map."""
150
 
        # Hack to build up the caching logic.
151
 
        ancestry = self._cache
152
 
        if ancestry is None:
153
 
            # Caching is disabled.
154
 
            missing_revisions = set(keys)
155
 
            ancestry = {}
 
152
        cache = self._cache
 
153
        if cache is None:
 
154
            cache = self._get_parent_map(keys)
156
155
        else:
157
 
            missing_revisions = set(key for key in keys if key not in ancestry)
158
 
        if missing_revisions:
159
 
            parent_map = self._get_parent_map(missing_revisions)
160
 
            ancestry.update(parent_map)
161
 
            if self._cache_misses:
162
 
                # None is never a valid parents list, so it can be used to
163
 
                # record misses.
164
 
                ancestry.update(dict((k, None) for k in missing_revisions
165
 
                                     if k not in parent_map))
166
 
        present_keys = [k for k in keys if ancestry.get(k) is not None]
167
 
        return dict((k, ancestry[k]) for k in present_keys)
 
156
            needed_revisions = set(key for key in keys if key not in cache)
 
157
            # Do not ask for negatively cached keys
 
158
            needed_revisions.difference_update(self.missing_keys)
 
159
            if needed_revisions:
 
160
                parent_map = self._get_parent_map(needed_revisions)
 
161
                cache.update(parent_map)
 
162
                if self._cache_misses:
 
163
                    for key in needed_revisions:
 
164
                        if key not in parent_map:
 
165
                            self.note_missing_key(key)
 
166
        result = {}
 
167
        for key in keys:
 
168
            value = cache.get(key)
 
169
            if value is not None:
 
170
                result[key] = value
 
171
        return result
 
172
 
 
173
    def note_missing_key(self, key):
 
174
        """Note that key is a missing key."""
 
175
        if self._cache_misses:
 
176
            self.missing_keys.add(key)
168
177
 
169
178
 
170
179
class Graph(object):
600
609
                                 all_unique_searcher._iterations)
601
610
            unique_tip_searchers = next_unique_searchers
602
611
 
603
 
    @symbol_versioning.deprecated_method(symbol_versioning.one_one)
604
 
    def get_parents(self, revisions):
605
 
        """Find revision ids of the parents of a list of revisions
606
 
 
607
 
        A list is returned of the same length as the input.  Each entry
608
 
        is a list of parent ids for the corresponding input revision.
609
 
 
610
 
        [NULL_REVISION] is used as the parent of the first user-committed
611
 
        revision.  Its parent list is empty.
612
 
 
613
 
        If the revision is not present (i.e. a ghost), None is used in place
614
 
        of the list of parents.
615
 
 
616
 
        Deprecated in bzr 1.2 - please see get_parent_map.
617
 
        """
618
 
        parents = self.get_parent_map(revisions)
619
 
        return [parents.get(r, None) for r in revisions]
620
 
 
621
612
    def get_parent_map(self, revisions):
622
613
        """Get a map of key:parent_list for revisions.
623
614