~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/chk_map.py

  • Committer: John Arbash Meinel
  • Date: 2011-05-11 11:35:28 UTC
  • mto: This revision was merged to the branch mainline in revision 5851.
  • Revision ID: john@arbash-meinel.com-20110511113528-qepibuwxicjrbb2h
Break compatibility with python <2.6.

This includes auditing the code for places where we were doing
explicit 'sys.version' checks and removing them as appropriate.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2008-2011 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""Persistent maps from tuple_of_strings->string using CHK stores.
 
18
 
 
19
Overview and current status:
 
20
 
 
21
The CHKMap class implements a dict from tuple_of_strings->string by using a trie
 
22
with internal nodes of 8-bit fan out; The key tuples are mapped to strings by
 
23
joining them by \x00, and \x00 padding shorter keys out to the length of the
 
24
longest key. Leaf nodes are packed as densely as possible, and internal nodes
 
25
are all an additional 8-bits wide leading to a sparse upper tree.
 
26
 
 
27
Updates to a CHKMap are done preferentially via the apply_delta method, to
 
28
allow optimisation of the update operation; but individual map/unmap calls are
 
29
possible and supported. Individual changes via map/unmap are buffered in memory
 
30
until the _save method is called to force serialisation of the tree.
 
31
apply_delta records its changes immediately by performing an implicit _save.
 
32
 
 
33
TODO:
 
34
-----
 
35
 
 
36
Densely packed upper nodes.
 
37
 
 
38
"""
 
39
 
 
40
import heapq
 
41
import threading
 
42
 
 
43
from bzrlib import lazy_import
 
44
lazy_import.lazy_import(globals(), """
 
45
from bzrlib import (
 
46
    errors,
 
47
    )
 
48
""")
 
49
from bzrlib import (
 
50
    lru_cache,
 
51
    osutils,
 
52
    registry,
 
53
    static_tuple,
 
54
    trace,
 
55
    )
 
56
from bzrlib.static_tuple import StaticTuple
 
57
 
 
58
# approx 4MB
 
59
# If each line is 50 bytes, and you have 255 internal pages, with 255-way fan
 
60
# out, it takes 3.1MB to cache the layer.
 
61
_PAGE_CACHE_SIZE = 4*1024*1024
 
62
# Per thread caches for 2 reasons:
 
63
# - in the server we may be serving very different content, so we get less
 
64
#   cache thrashing.
 
65
# - we avoid locking on every cache lookup.
 
66
_thread_caches = threading.local()
 
67
# The page cache.
 
68
_thread_caches.page_cache = None
 
69
 
 
70
def _get_cache():
 
71
    """Get the per-thread page cache.
 
72
 
 
73
    We need a function to do this because in a new thread the _thread_caches
 
74
    threading.local object does not have the cache initialized yet.
 
75
    """
 
76
    page_cache = getattr(_thread_caches, 'page_cache', None)
 
77
    if page_cache is None:
 
78
        # We are caching bytes so len(value) is perfectly accurate
 
79
        page_cache = lru_cache.LRUSizeCache(_PAGE_CACHE_SIZE)
 
80
        _thread_caches.page_cache = page_cache
 
81
    return page_cache
 
82
 
 
83
 
 
84
def clear_cache():
 
85
    _get_cache().clear()
 
86
 
 
87
 
 
88
# If a ChildNode falls below this many bytes, we check for a remap
 
89
_INTERESTING_NEW_SIZE = 50
 
90
# If a ChildNode shrinks by more than this amount, we check for a remap
 
91
_INTERESTING_SHRINKAGE_LIMIT = 20
 
92
 
 
93
 
 
94
def _search_key_plain(key):
 
95
    """Map the key tuple into a search string that just uses the key bytes."""
 
96
    return '\x00'.join(key)
 
97
 
 
98
 
 
99
search_key_registry = registry.Registry()
 
100
search_key_registry.register('plain', _search_key_plain)
 
101
 
 
102
 
 
103
class CHKMap(object):
 
104
    """A persistent map from string to string backed by a CHK store."""
 
105
 
 
106
    __slots__ = ('_store', '_root_node', '_search_key_func')
 
107
 
 
108
    def __init__(self, store, root_key, search_key_func=None):
 
109
        """Create a CHKMap object.
 
110
 
 
111
        :param store: The store the CHKMap is stored in.
 
112
        :param root_key: The root key of the map. None to create an empty
 
113
            CHKMap.
 
114
        :param search_key_func: A function mapping a key => bytes. These bytes
 
115
            are then used by the internal nodes to split up leaf nodes into
 
116
            multiple pages.
 
117
        """
 
118
        self._store = store
 
119
        if search_key_func is None:
 
120
            search_key_func = _search_key_plain
 
121
        self._search_key_func = search_key_func
 
122
        if root_key is None:
 
123
            self._root_node = LeafNode(search_key_func=search_key_func)
 
124
        else:
 
125
            self._root_node = self._node_key(root_key)
 
126
 
 
127
    def apply_delta(self, delta):
 
128
        """Apply a delta to the map.
 
129
 
 
130
        :param delta: An iterable of old_key, new_key, new_value tuples.
 
131
            If new_key is not None, then new_key->new_value is inserted
 
132
            into the map; if old_key is not None, then the old mapping
 
133
            of old_key is removed.
 
134
        """
 
135
        has_deletes = False
 
136
        # Check preconditions first.
 
137
        as_st = StaticTuple.from_sequence
 
138
        new_items = set([as_st(key) for (old, key, value) in delta
 
139
                         if key is not None and old is None])
 
140
        existing_new = list(self.iteritems(key_filter=new_items))
 
141
        if existing_new:
 
142
            raise errors.InconsistentDeltaDelta(delta,
 
143
                "New items are already in the map %r." % existing_new)
 
144
        # Now apply changes.
 
145
        for old, new, value in delta:
 
146
            if old is not None and old != new:
 
147
                self.unmap(old, check_remap=False)
 
148
                has_deletes = True
 
149
        for old, new, value in delta:
 
150
            if new is not None:
 
151
                self.map(new, value)
 
152
        if has_deletes:
 
153
            self._check_remap()
 
154
        return self._save()
 
155
 
 
156
    def _ensure_root(self):
 
157
        """Ensure that the root node is an object not a key."""
 
158
        if type(self._root_node) is StaticTuple:
 
159
            # Demand-load the root
 
160
            self._root_node = self._get_node(self._root_node)
 
161
 
 
162
    def _get_node(self, node):
 
163
        """Get a node.
 
164
 
 
165
        Note that this does not update the _items dict in objects containing a
 
166
        reference to this node. As such it does not prevent subsequent IO being
 
167
        performed.
 
168
 
 
169
        :param node: A tuple key or node object.
 
170
        :return: A node object.
 
171
        """
 
172
        if type(node) is StaticTuple:
 
173
            bytes = self._read_bytes(node)
 
174
            return _deserialise(bytes, node,
 
175
                search_key_func=self._search_key_func)
 
176
        else:
 
177
            return node
 
178
 
 
179
    def _read_bytes(self, key):
 
180
        try:
 
181
            return _get_cache()[key]
 
182
        except KeyError:
 
183
            stream = self._store.get_record_stream([key], 'unordered', True)
 
184
            bytes = stream.next().get_bytes_as('fulltext')
 
185
            _get_cache()[key] = bytes
 
186
            return bytes
 
187
 
 
188
    def _dump_tree(self, include_keys=False):
 
189
        """Return the tree in a string representation."""
 
190
        self._ensure_root()
 
191
        res = self._dump_tree_node(self._root_node, prefix='', indent='',
 
192
                                   include_keys=include_keys)
 
193
        res.append('') # Give a trailing '\n'
 
194
        return '\n'.join(res)
 
195
 
 
196
    def _dump_tree_node(self, node, prefix, indent, include_keys=True):
 
197
        """For this node and all children, generate a string representation."""
 
198
        result = []
 
199
        if not include_keys:
 
200
            key_str = ''
 
201
        else:
 
202
            node_key = node.key()
 
203
            if node_key is not None:
 
204
                key_str = ' %s' % (node_key[0],)
 
205
            else:
 
206
                key_str = ' None'
 
207
        result.append('%s%r %s%s' % (indent, prefix, node.__class__.__name__,
 
208
                                     key_str))
 
209
        if type(node) is InternalNode:
 
210
            # Trigger all child nodes to get loaded
 
211
            list(node._iter_nodes(self._store))
 
212
            for prefix, sub in sorted(node._items.iteritems()):
 
213
                result.extend(self._dump_tree_node(sub, prefix, indent + '  ',
 
214
                                                   include_keys=include_keys))
 
215
        else:
 
216
            for key, value in sorted(node._items.iteritems()):
 
217
                # Don't use prefix nor indent here to line up when used in
 
218
                # tests in conjunction with assertEqualDiff
 
219
                result.append('      %r %r' % (tuple(key), value))
 
220
        return result
 
221
 
 
222
    @classmethod
 
223
    def from_dict(klass, store, initial_value, maximum_size=0, key_width=1,
 
224
        search_key_func=None):
 
225
        """Create a CHKMap in store with initial_value as the content.
 
226
 
 
227
        :param store: The store to record initial_value in, a VersionedFiles
 
228
            object with 1-tuple keys supporting CHK key generation.
 
229
        :param initial_value: A dict to store in store. Its keys and values
 
230
            must be bytestrings.
 
231
        :param maximum_size: The maximum_size rule to apply to nodes. This
 
232
            determines the size at which no new data is added to a single node.
 
233
        :param key_width: The number of elements in each key_tuple being stored
 
234
            in this map.
 
235
        :param search_key_func: A function mapping a key => bytes. These bytes
 
236
            are then used by the internal nodes to split up leaf nodes into
 
237
            multiple pages.
 
238
        :return: The root chk of the resulting CHKMap.
 
239
        """
 
240
        root_key = klass._create_directly(store, initial_value,
 
241
            maximum_size=maximum_size, key_width=key_width,
 
242
            search_key_func=search_key_func)
 
243
        if type(root_key) is not StaticTuple:
 
244
            raise AssertionError('we got a %s instead of a StaticTuple'
 
245
                                 % (type(root_key),))
 
246
        return root_key
 
247
 
 
248
    @classmethod
 
249
    def _create_via_map(klass, store, initial_value, maximum_size=0,
 
250
                        key_width=1, search_key_func=None):
 
251
        result = klass(store, None, search_key_func=search_key_func)
 
252
        result._root_node.set_maximum_size(maximum_size)
 
253
        result._root_node._key_width = key_width
 
254
        delta = []
 
255
        for key, value in initial_value.items():
 
256
            delta.append((None, key, value))
 
257
        root_key = result.apply_delta(delta)
 
258
        return root_key
 
259
 
 
260
    @classmethod
 
261
    def _create_directly(klass, store, initial_value, maximum_size=0,
 
262
                         key_width=1, search_key_func=None):
 
263
        node = LeafNode(search_key_func=search_key_func)
 
264
        node.set_maximum_size(maximum_size)
 
265
        node._key_width = key_width
 
266
        as_st = StaticTuple.from_sequence
 
267
        node._items = dict([(as_st(key), val) for key, val
 
268
                                               in initial_value.iteritems()])
 
269
        node._raw_size = sum([node._key_value_len(key, value)
 
270
                              for key,value in node._items.iteritems()])
 
271
        node._len = len(node._items)
 
272
        node._compute_search_prefix()
 
273
        node._compute_serialised_prefix()
 
274
        if (node._len > 1
 
275
            and maximum_size
 
276
            and node._current_size() > maximum_size):
 
277
            prefix, node_details = node._split(store)
 
278
            if len(node_details) == 1:
 
279
                raise AssertionError('Failed to split using node._split')
 
280
            node = InternalNode(prefix, search_key_func=search_key_func)
 
281
            node.set_maximum_size(maximum_size)
 
282
            node._key_width = key_width
 
283
            for split, subnode in node_details:
 
284
                node.add_node(split, subnode)
 
285
        keys = list(node.serialise(store))
 
286
        return keys[-1]
 
287
 
 
288
    def iter_changes(self, basis):
 
289
        """Iterate over the changes between basis and self.
 
290
 
 
291
        :return: An iterator of tuples: (key, old_value, new_value). Old_value
 
292
            is None for keys only in self; new_value is None for keys only in
 
293
            basis.
 
294
        """
 
295
        # Overview:
 
296
        # Read both trees in lexographic, highest-first order.
 
297
        # Any identical nodes we skip
 
298
        # Any unique prefixes we output immediately.
 
299
        # values in a leaf node are treated as single-value nodes in the tree
 
300
        # which allows them to be not-special-cased. We know to output them
 
301
        # because their value is a string, not a key(tuple) or node.
 
302
        #
 
303
        # corner cases to beware of when considering this function:
 
304
        # *) common references are at different heights.
 
305
        #    consider two trees:
 
306
        #    {'a': LeafNode={'aaa':'foo', 'aab':'bar'}, 'b': LeafNode={'b'}}
 
307
        #    {'a': InternalNode={'aa':LeafNode={'aaa':'foo', 'aab':'bar'},
 
308
        #                        'ab':LeafNode={'ab':'bar'}}
 
309
        #     'b': LeafNode={'b'}}
 
310
        #    the node with aaa/aab will only be encountered in the second tree
 
311
        #    after reading the 'a' subtree, but it is encountered in the first
 
312
        #    tree immediately. Variations on this may have read internal nodes
 
313
        #    like this.  we want to cut the entire pending subtree when we
 
314
        #    realise we have a common node.  For this we use a list of keys -
 
315
        #    the path to a node - and check the entire path is clean as we
 
316
        #    process each item.
 
317
        if self._node_key(self._root_node) == self._node_key(basis._root_node):
 
318
            return
 
319
        self._ensure_root()
 
320
        basis._ensure_root()
 
321
        excluded_keys = set()
 
322
        self_node = self._root_node
 
323
        basis_node = basis._root_node
 
324
        # A heap, each element is prefix, node(tuple/NodeObject/string),
 
325
        # key_path (a list of tuples, tail-sharing down the tree.)
 
326
        self_pending = []
 
327
        basis_pending = []
 
328
        def process_node(node, path, a_map, pending):
 
329
            # take a node and expand it
 
330
            node = a_map._get_node(node)
 
331
            if type(node) == LeafNode:
 
332
                path = (node._key, path)
 
333
                for key, value in node._items.items():
 
334
                    # For a LeafNode, the key is a serialized_key, rather than
 
335
                    # a search_key, but the heap is using search_keys
 
336
                    search_key = node._search_key_func(key)
 
337
                    heapq.heappush(pending, (search_key, key, value, path))
 
338
            else:
 
339
                # type(node) == InternalNode
 
340
                path = (node._key, path)
 
341
                for prefix, child in node._items.items():
 
342
                    heapq.heappush(pending, (prefix, None, child, path))
 
343
        def process_common_internal_nodes(self_node, basis_node):
 
344
            self_items = set(self_node._items.items())
 
345
            basis_items = set(basis_node._items.items())
 
346
            path = (self_node._key, None)
 
347
            for prefix, child in self_items - basis_items:
 
348
                heapq.heappush(self_pending, (prefix, None, child, path))
 
349
            path = (basis_node._key, None)
 
350
            for prefix, child in basis_items - self_items:
 
351
                heapq.heappush(basis_pending, (prefix, None, child, path))
 
352
        def process_common_leaf_nodes(self_node, basis_node):
 
353
            self_items = set(self_node._items.items())
 
354
            basis_items = set(basis_node._items.items())
 
355
            path = (self_node._key, None)
 
356
            for key, value in self_items - basis_items:
 
357
                prefix = self._search_key_func(key)
 
358
                heapq.heappush(self_pending, (prefix, key, value, path))
 
359
            path = (basis_node._key, None)
 
360
            for key, value in basis_items - self_items:
 
361
                prefix = basis._search_key_func(key)
 
362
                heapq.heappush(basis_pending, (prefix, key, value, path))
 
363
        def process_common_prefix_nodes(self_node, self_path,
 
364
                                        basis_node, basis_path):
 
365
            # Would it be more efficient if we could request both at the same
 
366
            # time?
 
367
            self_node = self._get_node(self_node)
 
368
            basis_node = basis._get_node(basis_node)
 
369
            if (type(self_node) == InternalNode
 
370
                and type(basis_node) == InternalNode):
 
371
                # Matching internal nodes
 
372
                process_common_internal_nodes(self_node, basis_node)
 
373
            elif (type(self_node) == LeafNode
 
374
                  and type(basis_node) == LeafNode):
 
375
                process_common_leaf_nodes(self_node, basis_node)
 
376
            else:
 
377
                process_node(self_node, self_path, self, self_pending)
 
378
                process_node(basis_node, basis_path, basis, basis_pending)
 
379
        process_common_prefix_nodes(self_node, None, basis_node, None)
 
380
        self_seen = set()
 
381
        basis_seen = set()
 
382
        excluded_keys = set()
 
383
        def check_excluded(key_path):
 
384
            # Note that this is N^2, it depends on us trimming trees
 
385
            # aggressively to not become slow.
 
386
            # A better implementation would probably have a reverse map
 
387
            # back to the children of a node, and jump straight to it when
 
388
            # a common node is detected, the proceed to remove the already
 
389
            # pending children. bzrlib.graph has a searcher module with a
 
390
            # similar problem.
 
391
            while key_path is not None:
 
392
                key, key_path = key_path
 
393
                if key in excluded_keys:
 
394
                    return True
 
395
            return False
 
396
 
 
397
        loop_counter = 0
 
398
        while self_pending or basis_pending:
 
399
            loop_counter += 1
 
400
            if not self_pending:
 
401
                # self is exhausted: output remainder of basis
 
402
                for prefix, key, node, path in basis_pending:
 
403
                    if check_excluded(path):
 
404
                        continue
 
405
                    node = basis._get_node(node)
 
406
                    if key is not None:
 
407
                        # a value
 
408
                        yield (key, node, None)
 
409
                    else:
 
410
                        # subtree - fastpath the entire thing.
 
411
                        for key, value in node.iteritems(basis._store):
 
412
                            yield (key, value, None)
 
413
                return
 
414
            elif not basis_pending:
 
415
                # basis is exhausted: output remainder of self.
 
416
                for prefix, key, node, path in self_pending:
 
417
                    if check_excluded(path):
 
418
                        continue
 
419
                    node = self._get_node(node)
 
420
                    if key is not None:
 
421
                        # a value
 
422
                        yield (key, None, node)
 
423
                    else:
 
424
                        # subtree - fastpath the entire thing.
 
425
                        for key, value in node.iteritems(self._store):
 
426
                            yield (key, None, value)
 
427
                return
 
428
            else:
 
429
                # XXX: future optimisation - yield the smaller items
 
430
                # immediately rather than pushing everything on/off the
 
431
                # heaps. Applies to both internal nodes and leafnodes.
 
432
                if self_pending[0][0] < basis_pending[0][0]:
 
433
                    # expand self
 
434
                    prefix, key, node, path = heapq.heappop(self_pending)
 
435
                    if check_excluded(path):
 
436
                        continue
 
437
                    if key is not None:
 
438
                        # a value
 
439
                        yield (key, None, node)
 
440
                    else:
 
441
                        process_node(node, path, self, self_pending)
 
442
                        continue
 
443
                elif self_pending[0][0] > basis_pending[0][0]:
 
444
                    # expand basis
 
445
                    prefix, key, node, path = heapq.heappop(basis_pending)
 
446
                    if check_excluded(path):
 
447
                        continue
 
448
                    if key is not None:
 
449
                        # a value
 
450
                        yield (key, node, None)
 
451
                    else:
 
452
                        process_node(node, path, basis, basis_pending)
 
453
                        continue
 
454
                else:
 
455
                    # common prefix: possibly expand both
 
456
                    if self_pending[0][1] is None:
 
457
                        # process next self
 
458
                        read_self = True
 
459
                    else:
 
460
                        read_self = False
 
461
                    if basis_pending[0][1] is None:
 
462
                        # process next basis
 
463
                        read_basis = True
 
464
                    else:
 
465
                        read_basis = False
 
466
                    if not read_self and not read_basis:
 
467
                        # compare a common value
 
468
                        self_details = heapq.heappop(self_pending)
 
469
                        basis_details = heapq.heappop(basis_pending)
 
470
                        if self_details[2] != basis_details[2]:
 
471
                            yield (self_details[1],
 
472
                                basis_details[2], self_details[2])
 
473
                        continue
 
474
                    # At least one side wasn't a simple value
 
475
                    if (self._node_key(self_pending[0][2]) ==
 
476
                        self._node_key(basis_pending[0][2])):
 
477
                        # Identical pointers, skip (and don't bother adding to
 
478
                        # excluded, it won't turn up again.
 
479
                        heapq.heappop(self_pending)
 
480
                        heapq.heappop(basis_pending)
 
481
                        continue
 
482
                    # Now we need to expand this node before we can continue
 
483
                    if read_self and read_basis:
 
484
                        # Both sides start with the same prefix, so process
 
485
                        # them in parallel
 
486
                        self_prefix, _, self_node, self_path = heapq.heappop(
 
487
                            self_pending)
 
488
                        basis_prefix, _, basis_node, basis_path = heapq.heappop(
 
489
                            basis_pending)
 
490
                        if self_prefix != basis_prefix:
 
491
                            raise AssertionError(
 
492
                                '%r != %r' % (self_prefix, basis_prefix))
 
493
                        process_common_prefix_nodes(
 
494
                            self_node, self_path,
 
495
                            basis_node, basis_path)
 
496
                        continue
 
497
                    if read_self:
 
498
                        prefix, key, node, path = heapq.heappop(self_pending)
 
499
                        if check_excluded(path):
 
500
                            continue
 
501
                        process_node(node, path, self, self_pending)
 
502
                    if read_basis:
 
503
                        prefix, key, node, path = heapq.heappop(basis_pending)
 
504
                        if check_excluded(path):
 
505
                            continue
 
506
                        process_node(node, path, basis, basis_pending)
 
507
        # print loop_counter
 
508
 
 
509
    def iteritems(self, key_filter=None):
 
510
        """Iterate over the entire CHKMap's contents."""
 
511
        self._ensure_root()
 
512
        if key_filter is not None:
 
513
            as_st = StaticTuple.from_sequence
 
514
            key_filter = [as_st(key) for key in key_filter]
 
515
        return self._root_node.iteritems(self._store, key_filter=key_filter)
 
516
 
 
517
    def key(self):
 
518
        """Return the key for this map."""
 
519
        if type(self._root_node) is StaticTuple:
 
520
            return self._root_node
 
521
        else:
 
522
            return self._root_node._key
 
523
 
 
524
    def __len__(self):
 
525
        self._ensure_root()
 
526
        return len(self._root_node)
 
527
 
 
528
    def map(self, key, value):
 
529
        """Map a key tuple to value.
 
530
        
 
531
        :param key: A key to map.
 
532
        :param value: The value to assign to key.
 
533
        """
 
534
        key = StaticTuple.from_sequence(key)
 
535
        # Need a root object.
 
536
        self._ensure_root()
 
537
        prefix, node_details = self._root_node.map(self._store, key, value)
 
538
        if len(node_details) == 1:
 
539
            self._root_node = node_details[0][1]
 
540
        else:
 
541
            self._root_node = InternalNode(prefix,
 
542
                                search_key_func=self._search_key_func)
 
543
            self._root_node.set_maximum_size(node_details[0][1].maximum_size)
 
544
            self._root_node._key_width = node_details[0][1]._key_width
 
545
            for split, node in node_details:
 
546
                self._root_node.add_node(split, node)
 
547
 
 
548
    def _node_key(self, node):
 
549
        """Get the key for a node whether it's a tuple or node."""
 
550
        if type(node) is tuple:
 
551
            node = StaticTuple.from_sequence(node)
 
552
        if type(node) is StaticTuple:
 
553
            return node
 
554
        else:
 
555
            return node._key
 
556
 
 
557
    def unmap(self, key, check_remap=True):
 
558
        """remove key from the map."""
 
559
        key = StaticTuple.from_sequence(key)
 
560
        self._ensure_root()
 
561
        if type(self._root_node) is InternalNode:
 
562
            unmapped = self._root_node.unmap(self._store, key,
 
563
                check_remap=check_remap)
 
564
        else:
 
565
            unmapped = self._root_node.unmap(self._store, key)
 
566
        self._root_node = unmapped
 
567
 
 
568
    def _check_remap(self):
 
569
        """Check if nodes can be collapsed."""
 
570
        self._ensure_root()
 
571
        if type(self._root_node) is InternalNode:
 
572
            self._root_node = self._root_node._check_remap(self._store)
 
573
 
 
574
    def _save(self):
 
575
        """Save the map completely.
 
576
 
 
577
        :return: The key of the root node.
 
578
        """
 
579
        if type(self._root_node) is StaticTuple:
 
580
            # Already saved.
 
581
            return self._root_node
 
582
        keys = list(self._root_node.serialise(self._store))
 
583
        return keys[-1]
 
584
 
 
585
 
 
586
class Node(object):
 
587
    """Base class defining the protocol for CHK Map nodes.
 
588
 
 
589
    :ivar _raw_size: The total size of the serialized key:value data, before
 
590
        adding the header bytes, and without prefix compression.
 
591
    """
 
592
 
 
593
    __slots__ = ('_key', '_len', '_maximum_size', '_key_width',
 
594
                 '_raw_size', '_items', '_search_prefix', '_search_key_func'
 
595
                )
 
596
 
 
597
    def __init__(self, key_width=1):
 
598
        """Create a node.
 
599
 
 
600
        :param key_width: The width of keys for this node.
 
601
        """
 
602
        self._key = None
 
603
        # Current number of elements
 
604
        self._len = 0
 
605
        self._maximum_size = 0
 
606
        self._key_width = key_width
 
607
        # current size in bytes
 
608
        self._raw_size = 0
 
609
        # The pointers/values this node has - meaning defined by child classes.
 
610
        self._items = {}
 
611
        # The common search prefix
 
612
        self._search_prefix = None
 
613
 
 
614
    def __repr__(self):
 
615
        items_str = str(sorted(self._items))
 
616
        if len(items_str) > 20:
 
617
            items_str = items_str[:16] + '...]'
 
618
        return '%s(key:%s len:%s size:%s max:%s prefix:%s items:%s)' % (
 
619
            self.__class__.__name__, self._key, self._len, self._raw_size,
 
620
            self._maximum_size, self._search_prefix, items_str)
 
621
 
 
622
    def key(self):
 
623
        return self._key
 
624
 
 
625
    def __len__(self):
 
626
        return self._len
 
627
 
 
628
    @property
 
629
    def maximum_size(self):
 
630
        """What is the upper limit for adding references to a node."""
 
631
        return self._maximum_size
 
632
 
 
633
    def set_maximum_size(self, new_size):
 
634
        """Set the size threshold for nodes.
 
635
 
 
636
        :param new_size: The size at which no data is added to a node. 0 for
 
637
            unlimited.
 
638
        """
 
639
        self._maximum_size = new_size
 
640
 
 
641
    @classmethod
 
642
    def common_prefix(cls, prefix, key):
 
643
        """Given 2 strings, return the longest prefix common to both.
 
644
 
 
645
        :param prefix: This has been the common prefix for other keys, so it is
 
646
            more likely to be the common prefix in this case as well.
 
647
        :param key: Another string to compare to
 
648
        """
 
649
        if key.startswith(prefix):
 
650
            return prefix
 
651
        pos = -1
 
652
        # Is there a better way to do this?
 
653
        for pos, (left, right) in enumerate(zip(prefix, key)):
 
654
            if left != right:
 
655
                pos -= 1
 
656
                break
 
657
        common = prefix[:pos+1]
 
658
        return common
 
659
 
 
660
    @classmethod
 
661
    def common_prefix_for_keys(cls, keys):
 
662
        """Given a list of keys, find their common prefix.
 
663
 
 
664
        :param keys: An iterable of strings.
 
665
        :return: The longest common prefix of all keys.
 
666
        """
 
667
        common_prefix = None
 
668
        for key in keys:
 
669
            if common_prefix is None:
 
670
                common_prefix = key
 
671
                continue
 
672
            common_prefix = cls.common_prefix(common_prefix, key)
 
673
            if not common_prefix:
 
674
                # if common_prefix is the empty string, then we know it won't
 
675
                # change further
 
676
                return ''
 
677
        return common_prefix
 
678
 
 
679
 
 
680
# Singleton indicating we have not computed _search_prefix yet
 
681
_unknown = object()
 
682
 
 
683
class LeafNode(Node):
 
684
    """A node containing actual key:value pairs.
 
685
 
 
686
    :ivar _items: A dict of key->value items. The key is in tuple form.
 
687
    :ivar _size: The number of bytes that would be used by serializing all of
 
688
        the key/value pairs.
 
689
    """
 
690
 
 
691
    __slots__ = ('_common_serialised_prefix',)
 
692
 
 
693
    def __init__(self, search_key_func=None):
 
694
        Node.__init__(self)
 
695
        # All of the keys in this leaf node share this common prefix
 
696
        self._common_serialised_prefix = None
 
697
        if search_key_func is None:
 
698
            self._search_key_func = _search_key_plain
 
699
        else:
 
700
            self._search_key_func = search_key_func
 
701
 
 
702
    def __repr__(self):
 
703
        items_str = str(sorted(self._items))
 
704
        if len(items_str) > 20:
 
705
            items_str = items_str[:16] + '...]'
 
706
        return \
 
707
            '%s(key:%s len:%s size:%s max:%s prefix:%s keywidth:%s items:%s)' \
 
708
            % (self.__class__.__name__, self._key, self._len, self._raw_size,
 
709
            self._maximum_size, self._search_prefix, self._key_width, items_str)
 
710
 
 
711
    def _current_size(self):
 
712
        """Answer the current serialised size of this node.
 
713
 
 
714
        This differs from self._raw_size in that it includes the bytes used for
 
715
        the header.
 
716
        """
 
717
        if self._common_serialised_prefix is None:
 
718
            bytes_for_items = 0
 
719
            prefix_len = 0
 
720
        else:
 
721
            # We will store a single string with the common prefix
 
722
            # And then that common prefix will not be stored in any of the
 
723
            # entry lines
 
724
            prefix_len = len(self._common_serialised_prefix)
 
725
            bytes_for_items = (self._raw_size - (prefix_len * self._len))
 
726
        return (9 # 'chkleaf:\n'
 
727
            + len(str(self._maximum_size)) + 1
 
728
            + len(str(self._key_width)) + 1
 
729
            + len(str(self._len)) + 1
 
730
            + prefix_len + 1
 
731
            + bytes_for_items)
 
732
 
 
733
    @classmethod
 
734
    def deserialise(klass, bytes, key, search_key_func=None):
 
735
        """Deserialise bytes, with key key, into a LeafNode.
 
736
 
 
737
        :param bytes: The bytes of the node.
 
738
        :param key: The key that the serialised node has.
 
739
        """
 
740
        key = static_tuple.expect_static_tuple(key)
 
741
        return _deserialise_leaf_node(bytes, key,
 
742
                                      search_key_func=search_key_func)
 
743
 
 
744
    def iteritems(self, store, key_filter=None):
 
745
        """Iterate over items in the node.
 
746
 
 
747
        :param key_filter: A filter to apply to the node. It should be a
 
748
            list/set/dict or similar repeatedly iterable container.
 
749
        """
 
750
        if key_filter is not None:
 
751
            # Adjust the filter - short elements go to a prefix filter. All
 
752
            # other items are looked up directly.
 
753
            # XXX: perhaps defaultdict? Profiling<rinse and repeat>
 
754
            filters = {}
 
755
            for key in key_filter:
 
756
                if len(key) == self._key_width:
 
757
                    # This filter is meant to match exactly one key, yield it
 
758
                    # if we have it.
 
759
                    try:
 
760
                        yield key, self._items[key]
 
761
                    except KeyError:
 
762
                        # This key is not present in this map, continue
 
763
                        pass
 
764
                else:
 
765
                    # Short items, we need to match based on a prefix
 
766
                    length_filter = filters.setdefault(len(key), set())
 
767
                    length_filter.add(key)
 
768
            if filters:
 
769
                filters = filters.items()
 
770
                for item in self._items.iteritems():
 
771
                    for length, length_filter in filters:
 
772
                        if item[0][:length] in length_filter:
 
773
                            yield item
 
774
                            break
 
775
        else:
 
776
            for item in self._items.iteritems():
 
777
                yield item
 
778
 
 
779
    def _key_value_len(self, key, value):
 
780
        # TODO: Should probably be done without actually joining the key, but
 
781
        #       then that can be done via the C extension
 
782
        return (len(self._serialise_key(key)) + 1
 
783
                + len(str(value.count('\n'))) + 1
 
784
                + len(value) + 1)
 
785
 
 
786
    def _search_key(self, key):
 
787
        return self._search_key_func(key)
 
788
 
 
789
    def _map_no_split(self, key, value):
 
790
        """Map a key to a value.
 
791
 
 
792
        This assumes either the key does not already exist, or you have already
 
793
        removed its size and length from self.
 
794
 
 
795
        :return: True if adding this node should cause us to split.
 
796
        """
 
797
        self._items[key] = value
 
798
        self._raw_size += self._key_value_len(key, value)
 
799
        self._len += 1
 
800
        serialised_key = self._serialise_key(key)
 
801
        if self._common_serialised_prefix is None:
 
802
            self._common_serialised_prefix = serialised_key
 
803
        else:
 
804
            self._common_serialised_prefix = self.common_prefix(
 
805
                self._common_serialised_prefix, serialised_key)
 
806
        search_key = self._search_key(key)
 
807
        if self._search_prefix is _unknown:
 
808
            self._compute_search_prefix()
 
809
        if self._search_prefix is None:
 
810
            self._search_prefix = search_key
 
811
        else:
 
812
            self._search_prefix = self.common_prefix(
 
813
                self._search_prefix, search_key)
 
814
        if (self._len > 1
 
815
            and self._maximum_size
 
816
            and self._current_size() > self._maximum_size):
 
817
            # Check to see if all of the search_keys for this node are
 
818
            # identical. We allow the node to grow under that circumstance
 
819
            # (we could track this as common state, but it is infrequent)
 
820
            if (search_key != self._search_prefix
 
821
                or not self._are_search_keys_identical()):
 
822
                return True
 
823
        return False
 
824
 
 
825
    def _split(self, store):
 
826
        """We have overflowed.
 
827
 
 
828
        Split this node into multiple LeafNodes, return it up the stack so that
 
829
        the next layer creates a new InternalNode and references the new nodes.
 
830
 
 
831
        :return: (common_serialised_prefix, [(node_serialised_prefix, node)])
 
832
        """
 
833
        if self._search_prefix is _unknown:
 
834
            raise AssertionError('Search prefix must be known')
 
835
        common_prefix = self._search_prefix
 
836
        split_at = len(common_prefix) + 1
 
837
        result = {}
 
838
        for key, value in self._items.iteritems():
 
839
            search_key = self._search_key(key)
 
840
            prefix = search_key[:split_at]
 
841
            # TODO: Generally only 1 key can be exactly the right length,
 
842
            #       which means we can only have 1 key in the node pointed
 
843
            #       at by the 'prefix\0' key. We might want to consider
 
844
            #       folding it into the containing InternalNode rather than
 
845
            #       having a fixed length-1 node.
 
846
            #       Note this is probably not true for hash keys, as they
 
847
            #       may get a '\00' node anywhere, but won't have keys of
 
848
            #       different lengths.
 
849
            if len(prefix) < split_at:
 
850
                prefix += '\x00'*(split_at - len(prefix))
 
851
            if prefix not in result:
 
852
                node = LeafNode(search_key_func=self._search_key_func)
 
853
                node.set_maximum_size(self._maximum_size)
 
854
                node._key_width = self._key_width
 
855
                result[prefix] = node
 
856
            else:
 
857
                node = result[prefix]
 
858
            sub_prefix, node_details = node.map(store, key, value)
 
859
            if len(node_details) > 1:
 
860
                if prefix != sub_prefix:
 
861
                    # This node has been split and is now found via a different
 
862
                    # path
 
863
                    result.pop(prefix)
 
864
                new_node = InternalNode(sub_prefix,
 
865
                    search_key_func=self._search_key_func)
 
866
                new_node.set_maximum_size(self._maximum_size)
 
867
                new_node._key_width = self._key_width
 
868
                for split, node in node_details:
 
869
                    new_node.add_node(split, node)
 
870
                result[prefix] = new_node
 
871
        return common_prefix, result.items()
 
872
 
 
873
    def map(self, store, key, value):
 
874
        """Map key to value."""
 
875
        if key in self._items:
 
876
            self._raw_size -= self._key_value_len(key, self._items[key])
 
877
            self._len -= 1
 
878
        self._key = None
 
879
        if self._map_no_split(key, value):
 
880
            return self._split(store)
 
881
        else:
 
882
            if self._search_prefix is _unknown:
 
883
                raise AssertionError('%r must be known' % self._search_prefix)
 
884
            return self._search_prefix, [("", self)]
 
885
 
 
886
    _serialise_key = '\x00'.join
 
887
 
 
888
    def serialise(self, store):
 
889
        """Serialise the LeafNode to store.
 
890
 
 
891
        :param store: A VersionedFiles honouring the CHK extensions.
 
892
        :return: An iterable of the keys inserted by this operation.
 
893
        """
 
894
        lines = ["chkleaf:\n"]
 
895
        lines.append("%d\n" % self._maximum_size)
 
896
        lines.append("%d\n" % self._key_width)
 
897
        lines.append("%d\n" % self._len)
 
898
        if self._common_serialised_prefix is None:
 
899
            lines.append('\n')
 
900
            if len(self._items) != 0:
 
901
                raise AssertionError('If _common_serialised_prefix is None'
 
902
                    ' we should have no items')
 
903
        else:
 
904
            lines.append('%s\n' % (self._common_serialised_prefix,))
 
905
            prefix_len = len(self._common_serialised_prefix)
 
906
        for key, value in sorted(self._items.items()):
 
907
            # Always add a final newline
 
908
            value_lines = osutils.chunks_to_lines([value + '\n'])
 
909
            serialized = "%s\x00%s\n" % (self._serialise_key(key),
 
910
                                         len(value_lines))
 
911
            if not serialized.startswith(self._common_serialised_prefix):
 
912
                raise AssertionError('We thought the common prefix was %r'
 
913
                    ' but entry %r does not have it in common'
 
914
                    % (self._common_serialised_prefix, serialized))
 
915
            lines.append(serialized[prefix_len:])
 
916
            lines.extend(value_lines)
 
917
        sha1, _, _ = store.add_lines((None,), (), lines)
 
918
        self._key = StaticTuple("sha1:" + sha1,).intern()
 
919
        bytes = ''.join(lines)
 
920
        if len(bytes) != self._current_size():
 
921
            raise AssertionError('Invalid _current_size')
 
922
        _get_cache().add(self._key, bytes)
 
923
        return [self._key]
 
924
 
 
925
    def refs(self):
 
926
        """Return the references to other CHK's held by this node."""
 
927
        return []
 
928
 
 
929
    def _compute_search_prefix(self):
 
930
        """Determine the common search prefix for all keys in this node.
 
931
 
 
932
        :return: A bytestring of the longest search key prefix that is
 
933
            unique within this node.
 
934
        """
 
935
        search_keys = [self._search_key_func(key) for key in self._items]
 
936
        self._search_prefix = self.common_prefix_for_keys(search_keys)
 
937
        return self._search_prefix
 
938
 
 
939
    def _are_search_keys_identical(self):
 
940
        """Check to see if the search keys for all entries are the same.
 
941
 
 
942
        When using a hash as the search_key it is possible for non-identical
 
943
        keys to collide. If that happens enough, we may try overflow a
 
944
        LeafNode, but as all are collisions, we must not split.
 
945
        """
 
946
        common_search_key = None
 
947
        for key in self._items:
 
948
            search_key = self._search_key(key)
 
949
            if common_search_key is None:
 
950
                common_search_key = search_key
 
951
            elif search_key != common_search_key:
 
952
                return False
 
953
        return True
 
954
 
 
955
    def _compute_serialised_prefix(self):
 
956
        """Determine the common prefix for serialised keys in this node.
 
957
 
 
958
        :return: A bytestring of the longest serialised key prefix that is
 
959
            unique within this node.
 
960
        """
 
961
        serialised_keys = [self._serialise_key(key) for key in self._items]
 
962
        self._common_serialised_prefix = self.common_prefix_for_keys(
 
963
            serialised_keys)
 
964
        return self._common_serialised_prefix
 
965
 
 
966
    def unmap(self, store, key):
 
967
        """Unmap key from the node."""
 
968
        try:
 
969
            self._raw_size -= self._key_value_len(key, self._items[key])
 
970
        except KeyError:
 
971
            trace.mutter("key %s not found in %r", key, self._items)
 
972
            raise
 
973
        self._len -= 1
 
974
        del self._items[key]
 
975
        self._key = None
 
976
        # Recompute from scratch
 
977
        self._compute_search_prefix()
 
978
        self._compute_serialised_prefix()
 
979
        return self
 
980
 
 
981
 
 
982
class InternalNode(Node):
 
983
    """A node that contains references to other nodes.
 
984
 
 
985
    An InternalNode is responsible for mapping search key prefixes to child
 
986
    nodes.
 
987
 
 
988
    :ivar _items: serialised_key => node dictionary. node may be a tuple,
 
989
        LeafNode or InternalNode.
 
990
    """
 
991
 
 
992
    __slots__ = ('_node_width',)
 
993
 
 
994
    def __init__(self, prefix='', search_key_func=None):
 
995
        Node.__init__(self)
 
996
        # The size of an internalnode with default values and no children.
 
997
        # How many octets key prefixes within this node are.
 
998
        self._node_width = 0
 
999
        self._search_prefix = prefix
 
1000
        if search_key_func is None:
 
1001
            self._search_key_func = _search_key_plain
 
1002
        else:
 
1003
            self._search_key_func = search_key_func
 
1004
 
 
1005
    def add_node(self, prefix, node):
 
1006
        """Add a child node with prefix prefix, and node node.
 
1007
 
 
1008
        :param prefix: The search key prefix for node.
 
1009
        :param node: The node being added.
 
1010
        """
 
1011
        if self._search_prefix is None:
 
1012
            raise AssertionError("_search_prefix should not be None")
 
1013
        if not prefix.startswith(self._search_prefix):
 
1014
            raise AssertionError("prefixes mismatch: %s must start with %s"
 
1015
                % (prefix,self._search_prefix))
 
1016
        if len(prefix) != len(self._search_prefix) + 1:
 
1017
            raise AssertionError("prefix wrong length: len(%s) is not %d" %
 
1018
                (prefix, len(self._search_prefix) + 1))
 
1019
        self._len += len(node)
 
1020
        if not len(self._items):
 
1021
            self._node_width = len(prefix)
 
1022
        if self._node_width != len(self._search_prefix) + 1:
 
1023
            raise AssertionError("node width mismatch: %d is not %d" %
 
1024
                (self._node_width, len(self._search_prefix) + 1))
 
1025
        self._items[prefix] = node
 
1026
        self._key = None
 
1027
 
 
1028
    def _current_size(self):
 
1029
        """Answer the current serialised size of this node."""
 
1030
        return (self._raw_size + len(str(self._len)) + len(str(self._key_width)) +
 
1031
            len(str(self._maximum_size)))
 
1032
 
 
1033
    @classmethod
 
1034
    def deserialise(klass, bytes, key, search_key_func=None):
 
1035
        """Deserialise bytes to an InternalNode, with key key.
 
1036
 
 
1037
        :param bytes: The bytes of the node.
 
1038
        :param key: The key that the serialised node has.
 
1039
        :return: An InternalNode instance.
 
1040
        """
 
1041
        key = static_tuple.expect_static_tuple(key)
 
1042
        return _deserialise_internal_node(bytes, key,
 
1043
                                          search_key_func=search_key_func)
 
1044
 
 
1045
    def iteritems(self, store, key_filter=None):
 
1046
        for node, node_filter in self._iter_nodes(store, key_filter=key_filter):
 
1047
            for item in node.iteritems(store, key_filter=node_filter):
 
1048
                yield item
 
1049
 
 
1050
    def _iter_nodes(self, store, key_filter=None, batch_size=None):
 
1051
        """Iterate over node objects which match key_filter.
 
1052
 
 
1053
        :param store: A store to use for accessing content.
 
1054
        :param key_filter: A key filter to filter nodes. Only nodes that might
 
1055
            contain a key in key_filter will be returned.
 
1056
        :param batch_size: If not None, then we will return the nodes that had
 
1057
            to be read using get_record_stream in batches, rather than reading
 
1058
            them all at once.
 
1059
        :return: An iterable of nodes. This function does not have to be fully
 
1060
            consumed.  (There will be no pending I/O when items are being returned.)
 
1061
        """
 
1062
        # Map from chk key ('sha1:...',) to (prefix, key_filter)
 
1063
        # prefix is the key in self._items to use, key_filter is the key_filter
 
1064
        # entries that would match this node
 
1065
        keys = {}
 
1066
        shortcut = False
 
1067
        if key_filter is None:
 
1068
            # yielding all nodes, yield whatever we have, and queue up a read
 
1069
            # for whatever we are missing
 
1070
            shortcut = True
 
1071
            for prefix, node in self._items.iteritems():
 
1072
                if node.__class__ is StaticTuple:
 
1073
                    keys[node] = (prefix, None)
 
1074
                else:
 
1075
                    yield node, None
 
1076
        elif len(key_filter) == 1:
 
1077
            # Technically, this path could also be handled by the first check
 
1078
            # in 'self._node_width' in length_filters. However, we can handle
 
1079
            # this case without spending any time building up the
 
1080
            # prefix_to_keys, etc state.
 
1081
 
 
1082
            # This is a bit ugly, but TIMEIT showed it to be by far the fastest
 
1083
            # 0.626us   list(key_filter)[0]
 
1084
            #       is a func() for list(), 2 mallocs, and a getitem
 
1085
            # 0.489us   [k for k in key_filter][0]
 
1086
            #       still has the mallocs, avoids the func() call
 
1087
            # 0.350us   iter(key_filter).next()
 
1088
            #       has a func() call, and mallocs an iterator
 
1089
            # 0.125us   for key in key_filter: pass
 
1090
            #       no func() overhead, might malloc an iterator
 
1091
            # 0.105us   for key in key_filter: break
 
1092
            #       no func() overhead, might malloc an iterator, probably
 
1093
            #       avoids checking an 'else' clause as part of the for
 
1094
            for key in key_filter:
 
1095
                break
 
1096
            search_prefix = self._search_prefix_filter(key)
 
1097
            if len(search_prefix) == self._node_width:
 
1098
                # This item will match exactly, so just do a dict lookup, and
 
1099
                # see what we can return
 
1100
                shortcut = True
 
1101
                try:
 
1102
                    node = self._items[search_prefix]
 
1103
                except KeyError:
 
1104
                    # A given key can only match 1 child node, if it isn't
 
1105
                    # there, then we can just return nothing
 
1106
                    return
 
1107
                if node.__class__ is StaticTuple:
 
1108
                    keys[node] = (search_prefix, [key])
 
1109
                else:
 
1110
                    # This is loaded, and the only thing that can match,
 
1111
                    # return
 
1112
                    yield node, [key]
 
1113
                    return
 
1114
        if not shortcut:
 
1115
            # First, convert all keys into a list of search prefixes
 
1116
            # Aggregate common prefixes, and track the keys they come from
 
1117
            prefix_to_keys = {}
 
1118
            length_filters = {}
 
1119
            for key in key_filter:
 
1120
                search_prefix = self._search_prefix_filter(key)
 
1121
                length_filter = length_filters.setdefault(
 
1122
                                    len(search_prefix), set())
 
1123
                length_filter.add(search_prefix)
 
1124
                prefix_to_keys.setdefault(search_prefix, []).append(key)
 
1125
 
 
1126
            if (self._node_width in length_filters
 
1127
                and len(length_filters) == 1):
 
1128
                # all of the search prefixes match exactly _node_width. This
 
1129
                # means that everything is an exact match, and we can do a
 
1130
                # lookup into self._items, rather than iterating over the items
 
1131
                # dict.
 
1132
                search_prefixes = length_filters[self._node_width]
 
1133
                for search_prefix in search_prefixes:
 
1134
                    try:
 
1135
                        node = self._items[search_prefix]
 
1136
                    except KeyError:
 
1137
                        # We can ignore this one
 
1138
                        continue
 
1139
                    node_key_filter = prefix_to_keys[search_prefix]
 
1140
                    if node.__class__ is StaticTuple:
 
1141
                        keys[node] = (search_prefix, node_key_filter)
 
1142
                    else:
 
1143
                        yield node, node_key_filter
 
1144
            else:
 
1145
                # The slow way. We walk every item in self._items, and check to
 
1146
                # see if there are any matches
 
1147
                length_filters = length_filters.items()
 
1148
                for prefix, node in self._items.iteritems():
 
1149
                    node_key_filter = []
 
1150
                    for length, length_filter in length_filters:
 
1151
                        sub_prefix = prefix[:length]
 
1152
                        if sub_prefix in length_filter:
 
1153
                            node_key_filter.extend(prefix_to_keys[sub_prefix])
 
1154
                    if node_key_filter: # this key matched something, yield it
 
1155
                        if node.__class__ is StaticTuple:
 
1156
                            keys[node] = (prefix, node_key_filter)
 
1157
                        else:
 
1158
                            yield node, node_key_filter
 
1159
        if keys:
 
1160
            # Look in the page cache for some more bytes
 
1161
            found_keys = set()
 
1162
            for key in keys:
 
1163
                try:
 
1164
                    bytes = _get_cache()[key]
 
1165
                except KeyError:
 
1166
                    continue
 
1167
                else:
 
1168
                    node = _deserialise(bytes, key,
 
1169
                        search_key_func=self._search_key_func)
 
1170
                    prefix, node_key_filter = keys[key]
 
1171
                    self._items[prefix] = node
 
1172
                    found_keys.add(key)
 
1173
                    yield node, node_key_filter
 
1174
            for key in found_keys:
 
1175
                del keys[key]
 
1176
        if keys:
 
1177
            # demand load some pages.
 
1178
            if batch_size is None:
 
1179
                # Read all the keys in
 
1180
                batch_size = len(keys)
 
1181
            key_order = list(keys)
 
1182
            for batch_start in range(0, len(key_order), batch_size):
 
1183
                batch = key_order[batch_start:batch_start + batch_size]
 
1184
                # We have to fully consume the stream so there is no pending
 
1185
                # I/O, so we buffer the nodes for now.
 
1186
                stream = store.get_record_stream(batch, 'unordered', True)
 
1187
                node_and_filters = []
 
1188
                for record in stream:
 
1189
                    bytes = record.get_bytes_as('fulltext')
 
1190
                    node = _deserialise(bytes, record.key,
 
1191
                        search_key_func=self._search_key_func)
 
1192
                    prefix, node_key_filter = keys[record.key]
 
1193
                    node_and_filters.append((node, node_key_filter))
 
1194
                    self._items[prefix] = node
 
1195
                    _get_cache().add(record.key, bytes)
 
1196
                for info in node_and_filters:
 
1197
                    yield info
 
1198
 
 
1199
    def map(self, store, key, value):
 
1200
        """Map key to value."""
 
1201
        if not len(self._items):
 
1202
            raise AssertionError("can't map in an empty InternalNode.")
 
1203
        search_key = self._search_key(key)
 
1204
        if self._node_width != len(self._search_prefix) + 1:
 
1205
            raise AssertionError("node width mismatch: %d is not %d" %
 
1206
                (self._node_width, len(self._search_prefix) + 1))
 
1207
        if not search_key.startswith(self._search_prefix):
 
1208
            # This key doesn't fit in this index, so we need to split at the
 
1209
            # point where it would fit, insert self into that internal node,
 
1210
            # and then map this key into that node.
 
1211
            new_prefix = self.common_prefix(self._search_prefix,
 
1212
                                            search_key)
 
1213
            new_parent = InternalNode(new_prefix,
 
1214
                search_key_func=self._search_key_func)
 
1215
            new_parent.set_maximum_size(self._maximum_size)
 
1216
            new_parent._key_width = self._key_width
 
1217
            new_parent.add_node(self._search_prefix[:len(new_prefix)+1],
 
1218
                                self)
 
1219
            return new_parent.map(store, key, value)
 
1220
        children = [node for node, _
 
1221
                          in self._iter_nodes(store, key_filter=[key])]
 
1222
        if children:
 
1223
            child = children[0]
 
1224
        else:
 
1225
            # new child needed:
 
1226
            child = self._new_child(search_key, LeafNode)
 
1227
        old_len = len(child)
 
1228
        if type(child) is LeafNode:
 
1229
            old_size = child._current_size()
 
1230
        else:
 
1231
            old_size = None
 
1232
        prefix, node_details = child.map(store, key, value)
 
1233
        if len(node_details) == 1:
 
1234
            # child may have shrunk, or might be a new node
 
1235
            child = node_details[0][1]
 
1236
            self._len = self._len - old_len + len(child)
 
1237
            self._items[search_key] = child
 
1238
            self._key = None
 
1239
            new_node = self
 
1240
            if type(child) is LeafNode:
 
1241
                if old_size is None:
 
1242
                    # The old node was an InternalNode which means it has now
 
1243
                    # collapsed, so we need to check if it will chain to a
 
1244
                    # collapse at this level.
 
1245
                    trace.mutter("checking remap as InternalNode -> LeafNode")
 
1246
                    new_node = self._check_remap(store)
 
1247
                else:
 
1248
                    # If the LeafNode has shrunk in size, we may want to run
 
1249
                    # a remap check. Checking for a remap is expensive though
 
1250
                    # and the frequency of a successful remap is very low.
 
1251
                    # Shrinkage by small amounts is common, so we only do the
 
1252
                    # remap check if the new_size is low or the shrinkage
 
1253
                    # amount is over a configurable limit.
 
1254
                    new_size = child._current_size()
 
1255
                    shrinkage = old_size - new_size
 
1256
                    if (shrinkage > 0 and new_size < _INTERESTING_NEW_SIZE
 
1257
                        or shrinkage > _INTERESTING_SHRINKAGE_LIMIT):
 
1258
                        trace.mutter(
 
1259
                            "checking remap as size shrunk by %d to be %d",
 
1260
                            shrinkage, new_size)
 
1261
                        new_node = self._check_remap(store)
 
1262
            if new_node._search_prefix is None:
 
1263
                raise AssertionError("_search_prefix should not be None")
 
1264
            return new_node._search_prefix, [('', new_node)]
 
1265
        # child has overflown - create a new intermediate node.
 
1266
        # XXX: This is where we might want to try and expand our depth
 
1267
        # to refer to more bytes of every child (which would give us
 
1268
        # multiple pointers to child nodes, but less intermediate nodes)
 
1269
        child = self._new_child(search_key, InternalNode)
 
1270
        child._search_prefix = prefix
 
1271
        for split, node in node_details:
 
1272
            child.add_node(split, node)
 
1273
        self._len = self._len - old_len + len(child)
 
1274
        self._key = None
 
1275
        return self._search_prefix, [("", self)]
 
1276
 
 
1277
    def _new_child(self, search_key, klass):
 
1278
        """Create a new child node of type klass."""
 
1279
        child = klass()
 
1280
        child.set_maximum_size(self._maximum_size)
 
1281
        child._key_width = self._key_width
 
1282
        child._search_key_func = self._search_key_func
 
1283
        self._items[search_key] = child
 
1284
        return child
 
1285
 
 
1286
    def serialise(self, store):
 
1287
        """Serialise the node to store.
 
1288
 
 
1289
        :param store: A VersionedFiles honouring the CHK extensions.
 
1290
        :return: An iterable of the keys inserted by this operation.
 
1291
        """
 
1292
        for node in self._items.itervalues():
 
1293
            if type(node) is StaticTuple:
 
1294
                # Never deserialised.
 
1295
                continue
 
1296
            if node._key is not None:
 
1297
                # Never altered
 
1298
                continue
 
1299
            for key in node.serialise(store):
 
1300
                yield key
 
1301
        lines = ["chknode:\n"]
 
1302
        lines.append("%d\n" % self._maximum_size)
 
1303
        lines.append("%d\n" % self._key_width)
 
1304
        lines.append("%d\n" % self._len)
 
1305
        if self._search_prefix is None:
 
1306
            raise AssertionError("_search_prefix should not be None")
 
1307
        lines.append('%s\n' % (self._search_prefix,))
 
1308
        prefix_len = len(self._search_prefix)
 
1309
        for prefix, node in sorted(self._items.items()):
 
1310
            if type(node) is StaticTuple:
 
1311
                key = node[0]
 
1312
            else:
 
1313
                key = node._key[0]
 
1314
            serialised = "%s\x00%s\n" % (prefix, key)
 
1315
            if not serialised.startswith(self._search_prefix):
 
1316
                raise AssertionError("prefixes mismatch: %s must start with %s"
 
1317
                    % (serialised, self._search_prefix))
 
1318
            lines.append(serialised[prefix_len:])
 
1319
        sha1, _, _ = store.add_lines((None,), (), lines)
 
1320
        self._key = StaticTuple("sha1:" + sha1,).intern()
 
1321
        _get_cache().add(self._key, ''.join(lines))
 
1322
        yield self._key
 
1323
 
 
1324
    def _search_key(self, key):
 
1325
        """Return the serialised key for key in this node."""
 
1326
        # search keys are fixed width. All will be self._node_width wide, so we
 
1327
        # pad as necessary.
 
1328
        return (self._search_key_func(key) + '\x00'*self._node_width)[:self._node_width]
 
1329
 
 
1330
    def _search_prefix_filter(self, key):
 
1331
        """Serialise key for use as a prefix filter in iteritems."""
 
1332
        return self._search_key_func(key)[:self._node_width]
 
1333
 
 
1334
    def _split(self, offset):
 
1335
        """Split this node into smaller nodes starting at offset.
 
1336
 
 
1337
        :param offset: The offset to start the new child nodes at.
 
1338
        :return: An iterable of (prefix, node) tuples. prefix is a byte
 
1339
            prefix for reaching node.
 
1340
        """
 
1341
        if offset >= self._node_width:
 
1342
            for node in self._items.values():
 
1343
                for result in node._split(offset):
 
1344
                    yield result
 
1345
            return
 
1346
        for key, node in self._items.items():
 
1347
            pass
 
1348
 
 
1349
    def refs(self):
 
1350
        """Return the references to other CHK's held by this node."""
 
1351
        if self._key is None:
 
1352
            raise AssertionError("unserialised nodes have no refs.")
 
1353
        refs = []
 
1354
        for value in self._items.itervalues():
 
1355
            if type(value) is StaticTuple:
 
1356
                refs.append(value)
 
1357
            else:
 
1358
                refs.append(value.key())
 
1359
        return refs
 
1360
 
 
1361
    def _compute_search_prefix(self, extra_key=None):
 
1362
        """Return the unique key prefix for this node.
 
1363
 
 
1364
        :return: A bytestring of the longest search key prefix that is
 
1365
            unique within this node.
 
1366
        """
 
1367
        self._search_prefix = self.common_prefix_for_keys(self._items)
 
1368
        return self._search_prefix
 
1369
 
 
1370
    def unmap(self, store, key, check_remap=True):
 
1371
        """Remove key from this node and its children."""
 
1372
        if not len(self._items):
 
1373
            raise AssertionError("can't unmap in an empty InternalNode.")
 
1374
        children = [node for node, _
 
1375
                          in self._iter_nodes(store, key_filter=[key])]
 
1376
        if children:
 
1377
            child = children[0]
 
1378
        else:
 
1379
            raise KeyError(key)
 
1380
        self._len -= 1
 
1381
        unmapped = child.unmap(store, key)
 
1382
        self._key = None
 
1383
        search_key = self._search_key(key)
 
1384
        if len(unmapped) == 0:
 
1385
            # All child nodes are gone, remove the child:
 
1386
            del self._items[search_key]
 
1387
            unmapped = None
 
1388
        else:
 
1389
            # Stash the returned node
 
1390
            self._items[search_key] = unmapped
 
1391
        if len(self._items) == 1:
 
1392
            # this node is no longer needed:
 
1393
            return self._items.values()[0]
 
1394
        if type(unmapped) is InternalNode:
 
1395
            return self
 
1396
        if check_remap:
 
1397
            return self._check_remap(store)
 
1398
        else:
 
1399
            return self
 
1400
 
 
1401
    def _check_remap(self, store):
 
1402
        """Check if all keys contained by children fit in a single LeafNode.
 
1403
 
 
1404
        :param store: A store to use for reading more nodes
 
1405
        :return: Either self, or a new LeafNode which should replace self.
 
1406
        """
 
1407
        # Logic for how we determine when we need to rebuild
 
1408
        # 1) Implicitly unmap() is removing a key which means that the child
 
1409
        #    nodes are going to be shrinking by some extent.
 
1410
        # 2) If all children are LeafNodes, it is possible that they could be
 
1411
        #    combined into a single LeafNode, which can then completely replace
 
1412
        #    this internal node with a single LeafNode
 
1413
        # 3) If *one* child is an InternalNode, we assume it has already done
 
1414
        #    all the work to determine that its children cannot collapse, and
 
1415
        #    we can then assume that those nodes *plus* the current nodes don't
 
1416
        #    have a chance of collapsing either.
 
1417
        #    So a very cheap check is to just say if 'unmapped' is an
 
1418
        #    InternalNode, we don't have to check further.
 
1419
 
 
1420
        # TODO: Another alternative is to check the total size of all known
 
1421
        #       LeafNodes. If there is some formula we can use to determine the
 
1422
        #       final size without actually having to read in any more
 
1423
        #       children, it would be nice to have. However, we have to be
 
1424
        #       careful with stuff like nodes that pull out the common prefix
 
1425
        #       of each key, as adding a new key can change the common prefix
 
1426
        #       and cause size changes greater than the length of one key.
 
1427
        #       So for now, we just add everything to a new Leaf until it
 
1428
        #       splits, as we know that will give the right answer
 
1429
        new_leaf = LeafNode(search_key_func=self._search_key_func)
 
1430
        new_leaf.set_maximum_size(self._maximum_size)
 
1431
        new_leaf._key_width = self._key_width
 
1432
        # A batch_size of 16 was chosen because:
 
1433
        #   a) In testing, a 4k page held 14 times. So if we have more than 16
 
1434
        #      leaf nodes we are unlikely to hold them in a single new leaf
 
1435
        #      node. This still allows for 1 round trip
 
1436
        #   b) With 16-way fan out, we can still do a single round trip
 
1437
        #   c) With 255-way fan out, we don't want to read all 255 and destroy
 
1438
        #      the page cache, just to determine that we really don't need it.
 
1439
        for node, _ in self._iter_nodes(store, batch_size=16):
 
1440
            if type(node) is InternalNode:
 
1441
                # Without looking at any leaf nodes, we are sure
 
1442
                return self
 
1443
            for key, value in node._items.iteritems():
 
1444
                if new_leaf._map_no_split(key, value):
 
1445
                    return self
 
1446
        trace.mutter("remap generated a new LeafNode")
 
1447
        return new_leaf
 
1448
 
 
1449
 
 
1450
def _deserialise(bytes, key, search_key_func):
 
1451
    """Helper for repositorydetails - convert bytes to a node."""
 
1452
    if bytes.startswith("chkleaf:\n"):
 
1453
        node = LeafNode.deserialise(bytes, key, search_key_func=search_key_func)
 
1454
    elif bytes.startswith("chknode:\n"):
 
1455
        node = InternalNode.deserialise(bytes, key,
 
1456
            search_key_func=search_key_func)
 
1457
    else:
 
1458
        raise AssertionError("Unknown node type.")
 
1459
    return node
 
1460
 
 
1461
 
 
1462
class CHKMapDifference(object):
 
1463
    """Iterate the stored pages and key,value pairs for (new - old).
 
1464
 
 
1465
    This class provides a generator over the stored CHK pages and the
 
1466
    (key, value) pairs that are in any of the new maps and not in any of the
 
1467
    old maps.
 
1468
 
 
1469
    Note that it may yield chk pages that are common (especially root nodes),
 
1470
    but it won't yield (key,value) pairs that are common.
 
1471
    """
 
1472
 
 
1473
    def __init__(self, store, new_root_keys, old_root_keys,
 
1474
                 search_key_func, pb=None):
 
1475
        # TODO: Should we add a StaticTuple barrier here? It would be nice to
 
1476
        #       force callers to use StaticTuple, because there will often be
 
1477
        #       lots of keys passed in here. And even if we cast it locally,
 
1478
        #       that just meanst that we will have *both* a StaticTuple and a
 
1479
        #       tuple() in memory, referring to the same object. (so a net
 
1480
        #       increase in memory, not a decrease.)
 
1481
        self._store = store
 
1482
        self._new_root_keys = new_root_keys
 
1483
        self._old_root_keys = old_root_keys
 
1484
        self._pb = pb
 
1485
        # All uninteresting chks that we have seen. By the time they are added
 
1486
        # here, they should be either fully ignored, or queued up for
 
1487
        # processing
 
1488
        # TODO: This might grow to a large size if there are lots of merge
 
1489
        #       parents, etc. However, it probably doesn't scale to O(history)
 
1490
        #       like _processed_new_refs does.
 
1491
        self._all_old_chks = set(self._old_root_keys)
 
1492
        # All items that we have seen from the old_root_keys
 
1493
        self._all_old_items = set()
 
1494
        # These are interesting items which were either read, or already in the
 
1495
        # interesting queue (so we don't need to walk them again)
 
1496
        # TODO: processed_new_refs becomes O(all_chks), consider switching to
 
1497
        #       SimpleSet here.
 
1498
        self._processed_new_refs = set()
 
1499
        self._search_key_func = search_key_func
 
1500
 
 
1501
        # The uninteresting and interesting nodes to be searched
 
1502
        self._old_queue = []
 
1503
        self._new_queue = []
 
1504
        # Holds the (key, value) items found when processing the root nodes,
 
1505
        # waiting for the uninteresting nodes to be walked
 
1506
        self._new_item_queue = []
 
1507
        self._state = None
 
1508
 
 
1509
    def _read_nodes_from_store(self, keys):
 
1510
        # We chose not to use _get_cache(), because we think in
 
1511
        # terms of records to be yielded. Also, we expect to touch each page
 
1512
        # only 1 time during this code. (We may want to evaluate saving the
 
1513
        # raw bytes into the page cache, which would allow a working tree
 
1514
        # update after the fetch to not have to read the bytes again.)
 
1515
        as_st = StaticTuple.from_sequence
 
1516
        stream = self._store.get_record_stream(keys, 'unordered', True)
 
1517
        for record in stream:
 
1518
            if self._pb is not None:
 
1519
                self._pb.tick()
 
1520
            if record.storage_kind == 'absent':
 
1521
                raise errors.NoSuchRevision(self._store, record.key)
 
1522
            bytes = record.get_bytes_as('fulltext')
 
1523
            node = _deserialise(bytes, record.key,
 
1524
                                search_key_func=self._search_key_func)
 
1525
            if type(node) is InternalNode:
 
1526
                # Note we don't have to do node.refs() because we know that
 
1527
                # there are no children that have been pushed into this node
 
1528
                # Note: Using as_st() here seemed to save 1.2MB, which would
 
1529
                #       indicate that we keep 100k prefix_refs around while
 
1530
                #       processing. They *should* be shorter lived than that...
 
1531
                #       It does cost us ~10s of processing time
 
1532
                #prefix_refs = [as_st(item) for item in node._items.iteritems()]
 
1533
                prefix_refs = node._items.items()
 
1534
                items = []
 
1535
            else:
 
1536
                prefix_refs = []
 
1537
                # Note: We don't use a StaticTuple here. Profiling showed a
 
1538
                #       minor memory improvement (0.8MB out of 335MB peak 0.2%)
 
1539
                #       But a significant slowdown (15s / 145s, or 10%)
 
1540
                items = node._items.items()
 
1541
            yield record, node, prefix_refs, items
 
1542
 
 
1543
    def _read_old_roots(self):
 
1544
        old_chks_to_enqueue = []
 
1545
        all_old_chks = self._all_old_chks
 
1546
        for record, node, prefix_refs, items in \
 
1547
                self._read_nodes_from_store(self._old_root_keys):
 
1548
            # Uninteresting node
 
1549
            prefix_refs = [p_r for p_r in prefix_refs
 
1550
                                if p_r[1] not in all_old_chks]
 
1551
            new_refs = [p_r[1] for p_r in prefix_refs]
 
1552
            all_old_chks.update(new_refs)
 
1553
            # TODO: This might be a good time to turn items into StaticTuple
 
1554
            #       instances and possibly intern them. However, this does not
 
1555
            #       impact 'initial branch' performance, so I'm not worrying
 
1556
            #       about this yet
 
1557
            self._all_old_items.update(items)
 
1558
            # Queue up the uninteresting references
 
1559
            # Don't actually put them in the 'to-read' queue until we have
 
1560
            # finished checking the interesting references
 
1561
            old_chks_to_enqueue.extend(prefix_refs)
 
1562
        return old_chks_to_enqueue
 
1563
 
 
1564
    def _enqueue_old(self, new_prefixes, old_chks_to_enqueue):
 
1565
        # At this point, we have read all the uninteresting and interesting
 
1566
        # items, so we can queue up the uninteresting stuff, knowing that we've
 
1567
        # handled the interesting ones
 
1568
        for prefix, ref in old_chks_to_enqueue:
 
1569
            not_interesting = True
 
1570
            for i in xrange(len(prefix), 0, -1):
 
1571
                if prefix[:i] in new_prefixes:
 
1572
                    not_interesting = False
 
1573
                    break
 
1574
            if not_interesting:
 
1575
                # This prefix is not part of the remaining 'interesting set'
 
1576
                continue
 
1577
            self._old_queue.append(ref)
 
1578
 
 
1579
    def _read_all_roots(self):
 
1580
        """Read the root pages.
 
1581
 
 
1582
        This is structured as a generator, so that the root records can be
 
1583
        yielded up to whoever needs them without any buffering.
 
1584
        """
 
1585
        # This is the bootstrap phase
 
1586
        if not self._old_root_keys:
 
1587
            # With no old_root_keys we can just shortcut and be ready
 
1588
            # for _flush_new_queue
 
1589
            self._new_queue = list(self._new_root_keys)
 
1590
            return
 
1591
        old_chks_to_enqueue = self._read_old_roots()
 
1592
        # filter out any root keys that are already known to be uninteresting
 
1593
        new_keys = set(self._new_root_keys).difference(self._all_old_chks)
 
1594
        # These are prefixes that are present in new_keys that we are
 
1595
        # thinking to yield
 
1596
        new_prefixes = set()
 
1597
        # We are about to yield all of these, so we don't want them getting
 
1598
        # added a second time
 
1599
        processed_new_refs = self._processed_new_refs
 
1600
        processed_new_refs.update(new_keys)
 
1601
        for record, node, prefix_refs, items in \
 
1602
                self._read_nodes_from_store(new_keys):
 
1603
            # At this level, we now know all the uninteresting references
 
1604
            # So we filter and queue up whatever is remaining
 
1605
            prefix_refs = [p_r for p_r in prefix_refs
 
1606
                           if p_r[1] not in self._all_old_chks
 
1607
                              and p_r[1] not in processed_new_refs]
 
1608
            refs = [p_r[1] for p_r in prefix_refs]
 
1609
            new_prefixes.update([p_r[0] for p_r in prefix_refs])
 
1610
            self._new_queue.extend(refs)
 
1611
            # TODO: We can potentially get multiple items here, however the
 
1612
            #       current design allows for this, as callers will do the work
 
1613
            #       to make the results unique. We might profile whether we
 
1614
            #       gain anything by ensuring unique return values for items
 
1615
            # TODO: This might be a good time to cast to StaticTuple, as
 
1616
            #       self._new_item_queue will hold the contents of multiple
 
1617
            #       records for an extended lifetime
 
1618
            new_items = [item for item in items
 
1619
                               if item not in self._all_old_items]
 
1620
            self._new_item_queue.extend(new_items)
 
1621
            new_prefixes.update([self._search_key_func(item[0])
 
1622
                                 for item in new_items])
 
1623
            processed_new_refs.update(refs)
 
1624
            yield record
 
1625
        # For new_prefixes we have the full length prefixes queued up.
 
1626
        # However, we also need possible prefixes. (If we have a known ref to
 
1627
        # 'ab', then we also need to include 'a'.) So expand the
 
1628
        # new_prefixes to include all shorter prefixes
 
1629
        for prefix in list(new_prefixes):
 
1630
            new_prefixes.update([prefix[:i] for i in xrange(1, len(prefix))])
 
1631
        self._enqueue_old(new_prefixes, old_chks_to_enqueue)
 
1632
 
 
1633
    def _flush_new_queue(self):
 
1634
        # No need to maintain the heap invariant anymore, just pull things out
 
1635
        # and process them
 
1636
        refs = set(self._new_queue)
 
1637
        self._new_queue = []
 
1638
        # First pass, flush all interesting items and convert to using direct refs
 
1639
        all_old_chks = self._all_old_chks
 
1640
        processed_new_refs = self._processed_new_refs
 
1641
        all_old_items = self._all_old_items
 
1642
        new_items = [item for item in self._new_item_queue
 
1643
                           if item not in all_old_items]
 
1644
        self._new_item_queue = []
 
1645
        if new_items:
 
1646
            yield None, new_items
 
1647
        refs = refs.difference(all_old_chks)
 
1648
        processed_new_refs.update(refs)
 
1649
        while refs:
 
1650
            # TODO: Using a SimpleSet for self._processed_new_refs and
 
1651
            #       saved as much as 10MB of peak memory. However, it requires
 
1652
            #       implementing a non-pyrex version.
 
1653
            next_refs = set()
 
1654
            next_refs_update = next_refs.update
 
1655
            # Inlining _read_nodes_from_store improves 'bzr branch bzr.dev'
 
1656
            # from 1m54s to 1m51s. Consider it.
 
1657
            for record, _, p_refs, items in self._read_nodes_from_store(refs):
 
1658
                if all_old_items:
 
1659
                    # using the 'if' check saves about 145s => 141s, when
 
1660
                    # streaming initial branch of Launchpad data.
 
1661
                    items = [item for item in items
 
1662
                             if item not in all_old_items]
 
1663
                yield record, items
 
1664
                next_refs_update([p_r[1] for p_r in p_refs])
 
1665
                del p_refs
 
1666
            # set1.difference(set/dict) walks all of set1, and checks if it
 
1667
            # exists in 'other'.
 
1668
            # set1.difference(iterable) walks all of iterable, and does a
 
1669
            # 'difference_update' on a clone of set1. Pick wisely based on the
 
1670
            # expected sizes of objects.
 
1671
            # in our case it is expected that 'new_refs' will always be quite
 
1672
            # small.
 
1673
            next_refs = next_refs.difference(all_old_chks)
 
1674
            next_refs = next_refs.difference(processed_new_refs)
 
1675
            processed_new_refs.update(next_refs)
 
1676
            refs = next_refs
 
1677
 
 
1678
    def _process_next_old(self):
 
1679
        # Since we don't filter uninteresting any further than during
 
1680
        # _read_all_roots, process the whole queue in a single pass.
 
1681
        refs = self._old_queue
 
1682
        self._old_queue = []
 
1683
        all_old_chks = self._all_old_chks
 
1684
        for record, _, prefix_refs, items in self._read_nodes_from_store(refs):
 
1685
            # TODO: Use StaticTuple here?
 
1686
            self._all_old_items.update(items)
 
1687
            refs = [r for _,r in prefix_refs if r not in all_old_chks]
 
1688
            self._old_queue.extend(refs)
 
1689
            all_old_chks.update(refs)
 
1690
 
 
1691
    def _process_queues(self):
 
1692
        while self._old_queue:
 
1693
            self._process_next_old()
 
1694
        return self._flush_new_queue()
 
1695
 
 
1696
    def process(self):
 
1697
        for record in self._read_all_roots():
 
1698
            yield record, []
 
1699
        for record, items in self._process_queues():
 
1700
            yield record, items
 
1701
 
 
1702
 
 
1703
def iter_interesting_nodes(store, interesting_root_keys,
 
1704
                           uninteresting_root_keys, pb=None):
 
1705
    """Given root keys, find interesting nodes.
 
1706
 
 
1707
    Evaluate nodes referenced by interesting_root_keys. Ones that are also
 
1708
    referenced from uninteresting_root_keys are not considered interesting.
 
1709
 
 
1710
    :param interesting_root_keys: keys which should be part of the
 
1711
        "interesting" nodes (which will be yielded)
 
1712
    :param uninteresting_root_keys: keys which should be filtered out of the
 
1713
        result set.
 
1714
    :return: Yield
 
1715
        (interesting record, {interesting key:values})
 
1716
    """
 
1717
    iterator = CHKMapDifference(store, interesting_root_keys,
 
1718
                                uninteresting_root_keys,
 
1719
                                search_key_func=store._search_key_func,
 
1720
                                pb=pb)
 
1721
    return iterator.process()
 
1722
 
 
1723
 
 
1724
try:
 
1725
    from bzrlib._chk_map_pyx import (
 
1726
        _bytes_to_text_key,
 
1727
        _search_key_16,
 
1728
        _search_key_255,
 
1729
        _deserialise_leaf_node,
 
1730
        _deserialise_internal_node,
 
1731
        )
 
1732
except ImportError, e:
 
1733
    osutils.failed_to_load_extension(e)
 
1734
    from bzrlib._chk_map_py import (
 
1735
        _bytes_to_text_key,
 
1736
        _search_key_16,
 
1737
        _search_key_255,
 
1738
        _deserialise_leaf_node,
 
1739
        _deserialise_internal_node,
 
1740
        )
 
1741
search_key_registry.register('hash-16-way', _search_key_16)
 
1742
search_key_registry.register('hash-255-way', _search_key_255)
 
1743
 
 
1744
 
 
1745
def _check_key(key):
 
1746
    """Helper function to assert that a key is properly formatted.
 
1747
 
 
1748
    This generally shouldn't be used in production code, but it can be helpful
 
1749
    to debug problems.
 
1750
    """
 
1751
    if type(key) is not StaticTuple:
 
1752
        raise TypeError('key %r is not StaticTuple but %s' % (key, type(key)))
 
1753
    if len(key) != 1:
 
1754
        raise ValueError('key %r should have length 1, not %d' % (key, len(key),))
 
1755
    if type(key[0]) is not str:
 
1756
        raise TypeError('key %r should hold a str, not %r'
 
1757
                        % (key, type(key[0])))
 
1758
    if not key[0].startswith('sha1:'):
 
1759
        raise ValueError('key %r should point to a sha1:' % (key,))
 
1760
 
 
1761