1
# Copyright (C) 2008-2011 Canonical Ltd
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.
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.
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
17
"""Persistent maps from tuple_of_strings->string using CHK stores.
19
Overview and current status:
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.
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.
36
Densely packed upper nodes.
43
from bzrlib import lazy_import
44
lazy_import.lazy_import(globals(), """
56
from bzrlib.static_tuple import StaticTuple
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
65
# - we avoid locking on every cache lookup.
66
_thread_caches = threading.local()
68
_thread_caches.page_cache = None
71
"""Get the per-thread page cache.
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.
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
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
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)
99
search_key_registry = registry.Registry()
100
search_key_registry.register('plain', _search_key_plain)
103
class CHKMap(object):
104
"""A persistent map from string to string backed by a CHK store."""
106
__slots__ = ('_store', '_root_node', '_search_key_func')
108
def __init__(self, store, root_key, search_key_func=None):
109
"""Create a CHKMap object.
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
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
119
if search_key_func is None:
120
search_key_func = _search_key_plain
121
self._search_key_func = search_key_func
123
self._root_node = LeafNode(search_key_func=search_key_func)
125
self._root_node = self._node_key(root_key)
127
def apply_delta(self, delta):
128
"""Apply a delta to the map.
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.
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))
142
raise errors.InconsistentDeltaDelta(delta,
143
"New items are already in the map %r." % existing_new)
145
for old, new, value in delta:
146
if old is not None and old != new:
147
self.unmap(old, check_remap=False)
149
for old, new, value in delta:
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)
162
def _get_node(self, node):
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
169
:param node: A tuple key or node object.
170
:return: A node object.
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)
179
def _read_bytes(self, key):
181
return _get_cache()[key]
183
stream = self._store.get_record_stream([key], 'unordered', True)
184
bytes = stream.next().get_bytes_as('fulltext')
185
_get_cache()[key] = bytes
188
def _dump_tree(self, include_keys=False):
189
"""Return the tree in a string representation."""
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)
196
def _dump_tree_node(self, node, prefix, indent, include_keys=True):
197
"""For this node and all children, generate a string representation."""
202
node_key = node.key()
203
if node_key is not None:
204
key_str = ' %s' % (node_key[0],)
207
result.append('%s%r %s%s' % (indent, prefix, node.__class__.__name__,
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))
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))
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.
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
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
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
238
:return: The root chk of the resulting CHKMap.
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'
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
255
for key, value in initial_value.items():
256
delta.append((None, key, value))
257
root_key = result.apply_delta(delta)
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()
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))
288
def iter_changes(self, basis):
289
"""Iterate over the changes between basis and self.
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
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.
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
317
if self._node_key(self._root_node) == self._node_key(basis._root_node):
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.)
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))
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
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)
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)
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
391
while key_path is not None:
392
key, key_path = key_path
393
if key in excluded_keys:
398
while self_pending or basis_pending:
401
# self is exhausted: output remainder of basis
402
for prefix, key, node, path in basis_pending:
403
if check_excluded(path):
405
node = basis._get_node(node)
408
yield (key, node, None)
410
# subtree - fastpath the entire thing.
411
for key, value in node.iteritems(basis._store):
412
yield (key, value, None)
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):
419
node = self._get_node(node)
422
yield (key, None, node)
424
# subtree - fastpath the entire thing.
425
for key, value in node.iteritems(self._store):
426
yield (key, None, value)
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]:
434
prefix, key, node, path = heapq.heappop(self_pending)
435
if check_excluded(path):
439
yield (key, None, node)
441
process_node(node, path, self, self_pending)
443
elif self_pending[0][0] > basis_pending[0][0]:
445
prefix, key, node, path = heapq.heappop(basis_pending)
446
if check_excluded(path):
450
yield (key, node, None)
452
process_node(node, path, basis, basis_pending)
455
# common prefix: possibly expand both
456
if self_pending[0][1] is None:
461
if basis_pending[0][1] is None:
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])
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)
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
486
self_prefix, _, self_node, self_path = heapq.heappop(
488
basis_prefix, _, basis_node, basis_path = heapq.heappop(
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)
498
prefix, key, node, path = heapq.heappop(self_pending)
499
if check_excluded(path):
501
process_node(node, path, self, self_pending)
503
prefix, key, node, path = heapq.heappop(basis_pending)
504
if check_excluded(path):
506
process_node(node, path, basis, basis_pending)
509
def iteritems(self, key_filter=None):
510
"""Iterate over the entire CHKMap's contents."""
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)
518
"""Return the key for this map."""
519
if type(self._root_node) is StaticTuple:
520
return self._root_node
522
return self._root_node._key
526
return len(self._root_node)
528
def map(self, key, value):
529
"""Map a key tuple to value.
531
:param key: A key to map.
532
:param value: The value to assign to key.
534
key = StaticTuple.from_sequence(key)
535
# Need a root object.
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]
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)
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:
557
def unmap(self, key, check_remap=True):
558
"""remove key from the map."""
559
key = StaticTuple.from_sequence(key)
561
if type(self._root_node) is InternalNode:
562
unmapped = self._root_node.unmap(self._store, key,
563
check_remap=check_remap)
565
unmapped = self._root_node.unmap(self._store, key)
566
self._root_node = unmapped
568
def _check_remap(self):
569
"""Check if nodes can be collapsed."""
571
if type(self._root_node) is InternalNode:
572
self._root_node = self._root_node._check_remap(self._store)
575
"""Save the map completely.
577
:return: The key of the root node.
579
if type(self._root_node) is StaticTuple:
581
return self._root_node
582
keys = list(self._root_node.serialise(self._store))
587
"""Base class defining the protocol for CHK Map nodes.
589
:ivar _raw_size: The total size of the serialized key:value data, before
590
adding the header bytes, and without prefix compression.
593
__slots__ = ('_key', '_len', '_maximum_size', '_key_width',
594
'_raw_size', '_items', '_search_prefix', '_search_key_func'
597
def __init__(self, key_width=1):
600
:param key_width: The width of keys for this node.
603
# Current number of elements
605
self._maximum_size = 0
606
self._key_width = key_width
607
# current size in bytes
609
# The pointers/values this node has - meaning defined by child classes.
611
# The common search prefix
612
self._search_prefix = None
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)
629
def maximum_size(self):
630
"""What is the upper limit for adding references to a node."""
631
return self._maximum_size
633
def set_maximum_size(self, new_size):
634
"""Set the size threshold for nodes.
636
:param new_size: The size at which no data is added to a node. 0 for
639
self._maximum_size = new_size
642
def common_prefix(cls, prefix, key):
643
"""Given 2 strings, return the longest prefix common to both.
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
649
if key.startswith(prefix):
652
# Is there a better way to do this?
653
for pos, (left, right) in enumerate(zip(prefix, key)):
657
common = prefix[:pos+1]
661
def common_prefix_for_keys(cls, keys):
662
"""Given a list of keys, find their common prefix.
664
:param keys: An iterable of strings.
665
:return: The longest common prefix of all keys.
669
if common_prefix is None:
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
680
# Singleton indicating we have not computed _search_prefix yet
683
class LeafNode(Node):
684
"""A node containing actual key:value pairs.
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
691
__slots__ = ('_common_serialised_prefix',)
693
def __init__(self, search_key_func=None):
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
700
self._search_key_func = search_key_func
703
items_str = str(sorted(self._items))
704
if len(items_str) > 20:
705
items_str = items_str[:16] + '...]'
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)
711
def _current_size(self):
712
"""Answer the current serialised size of this node.
714
This differs from self._raw_size in that it includes the bytes used for
717
if self._common_serialised_prefix is None:
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
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
734
def deserialise(klass, bytes, key, search_key_func=None):
735
"""Deserialise bytes, with key key, into a LeafNode.
737
:param bytes: The bytes of the node.
738
:param key: The key that the serialised node has.
740
key = static_tuple.expect_static_tuple(key)
741
return _deserialise_leaf_node(bytes, key,
742
search_key_func=search_key_func)
744
def iteritems(self, store, key_filter=None):
745
"""Iterate over items in the node.
747
:param key_filter: A filter to apply to the node. It should be a
748
list/set/dict or similar repeatedly iterable container.
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>
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
760
yield key, self._items[key]
762
# This key is not present in this map, continue
765
# Short items, we need to match based on a prefix
766
length_filter = filters.setdefault(len(key), set())
767
length_filter.add(key)
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:
776
for item in self._items.iteritems():
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
786
def _search_key(self, key):
787
return self._search_key_func(key)
789
def _map_no_split(self, key, value):
790
"""Map a key to a value.
792
This assumes either the key does not already exist, or you have already
793
removed its size and length from self.
795
:return: True if adding this node should cause us to split.
797
self._items[key] = value
798
self._raw_size += self._key_value_len(key, value)
800
serialised_key = self._serialise_key(key)
801
if self._common_serialised_prefix is None:
802
self._common_serialised_prefix = serialised_key
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
812
self._search_prefix = self.common_prefix(
813
self._search_prefix, search_key)
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()):
825
def _split(self, store):
826
"""We have overflowed.
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.
831
:return: (common_serialised_prefix, [(node_serialised_prefix, node)])
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
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
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
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
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()
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])
879
if self._map_no_split(key, value):
880
return self._split(store)
882
if self._search_prefix is _unknown:
883
raise AssertionError('%r must be known' % self._search_prefix)
884
return self._search_prefix, [("", self)]
886
_serialise_key = '\x00'.join
888
def serialise(self, store):
889
"""Serialise the LeafNode to store.
891
:param store: A VersionedFiles honouring the CHK extensions.
892
:return: An iterable of the keys inserted by this operation.
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:
900
if len(self._items) != 0:
901
raise AssertionError('If _common_serialised_prefix is None'
902
' we should have no items')
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),
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)
926
"""Return the references to other CHK's held by this node."""
929
def _compute_search_prefix(self):
930
"""Determine the common search prefix for all keys in this node.
932
:return: A bytestring of the longest search key prefix that is
933
unique within this node.
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
939
def _are_search_keys_identical(self):
940
"""Check to see if the search keys for all entries are the same.
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.
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:
955
def _compute_serialised_prefix(self):
956
"""Determine the common prefix for serialised keys in this node.
958
:return: A bytestring of the longest serialised key prefix that is
959
unique within this node.
961
serialised_keys = [self._serialise_key(key) for key in self._items]
962
self._common_serialised_prefix = self.common_prefix_for_keys(
964
return self._common_serialised_prefix
966
def unmap(self, store, key):
967
"""Unmap key from the node."""
969
self._raw_size -= self._key_value_len(key, self._items[key])
971
trace.mutter("key %s not found in %r", key, self._items)
976
# Recompute from scratch
977
self._compute_search_prefix()
978
self._compute_serialised_prefix()
982
class InternalNode(Node):
983
"""A node that contains references to other nodes.
985
An InternalNode is responsible for mapping search key prefixes to child
988
:ivar _items: serialised_key => node dictionary. node may be a tuple,
989
LeafNode or InternalNode.
992
__slots__ = ('_node_width',)
994
def __init__(self, prefix='', search_key_func=None):
996
# The size of an internalnode with default values and no children.
997
# How many octets key prefixes within this node are.
999
self._search_prefix = prefix
1000
if search_key_func is None:
1001
self._search_key_func = _search_key_plain
1003
self._search_key_func = search_key_func
1005
def add_node(self, prefix, node):
1006
"""Add a child node with prefix prefix, and node node.
1008
:param prefix: The search key prefix for node.
1009
:param node: The node being added.
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
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)))
1034
def deserialise(klass, bytes, key, search_key_func=None):
1035
"""Deserialise bytes to an InternalNode, with key key.
1037
:param bytes: The bytes of the node.
1038
:param key: The key that the serialised node has.
1039
:return: An InternalNode instance.
1041
key = static_tuple.expect_static_tuple(key)
1042
return _deserialise_internal_node(bytes, key,
1043
search_key_func=search_key_func)
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):
1050
def _iter_nodes(self, store, key_filter=None, batch_size=None):
1051
"""Iterate over node objects which match key_filter.
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
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.)
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
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
1071
for prefix, node in self._items.iteritems():
1072
if node.__class__ is StaticTuple:
1073
keys[node] = (prefix, 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.
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:
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
1102
node = self._items[search_prefix]
1104
# A given key can only match 1 child node, if it isn't
1105
# there, then we can just return nothing
1107
if node.__class__ is StaticTuple:
1108
keys[node] = (search_prefix, [key])
1110
# This is loaded, and the only thing that can match,
1115
# First, convert all keys into a list of search prefixes
1116
# Aggregate common prefixes, and track the keys they come from
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)
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
1132
search_prefixes = length_filters[self._node_width]
1133
for search_prefix in search_prefixes:
1135
node = self._items[search_prefix]
1137
# We can ignore this one
1139
node_key_filter = prefix_to_keys[search_prefix]
1140
if node.__class__ is StaticTuple:
1141
keys[node] = (search_prefix, node_key_filter)
1143
yield node, node_key_filter
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)
1158
yield node, node_key_filter
1160
# Look in the page cache for some more bytes
1164
bytes = _get_cache()[key]
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
1173
yield node, node_key_filter
1174
for key in found_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:
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,
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],
1219
return new_parent.map(store, key, value)
1220
children = [node for node, _
1221
in self._iter_nodes(store, key_filter=[key])]
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()
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
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)
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):
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)
1275
return self._search_prefix, [("", self)]
1277
def _new_child(self, search_key, klass):
1278
"""Create a new child node of type 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
1286
def serialise(self, store):
1287
"""Serialise the node to store.
1289
:param store: A VersionedFiles honouring the CHK extensions.
1290
:return: An iterable of the keys inserted by this operation.
1292
for node in self._items.itervalues():
1293
if type(node) is StaticTuple:
1294
# Never deserialised.
1296
if node._key is not None:
1299
for key in node.serialise(store):
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:
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))
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
1328
return (self._search_key_func(key) + '\x00'*self._node_width)[:self._node_width]
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]
1334
def _split(self, offset):
1335
"""Split this node into smaller nodes starting at offset.
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.
1341
if offset >= self._node_width:
1342
for node in self._items.values():
1343
for result in node._split(offset):
1346
for key, node in self._items.items():
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.")
1354
for value in self._items.itervalues():
1355
if type(value) is StaticTuple:
1358
refs.append(value.key())
1361
def _compute_search_prefix(self, extra_key=None):
1362
"""Return the unique key prefix for this node.
1364
:return: A bytestring of the longest search key prefix that is
1365
unique within this node.
1367
self._search_prefix = self.common_prefix_for_keys(self._items)
1368
return self._search_prefix
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])]
1381
unmapped = child.unmap(store, key)
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]
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:
1397
return self._check_remap(store)
1401
def _check_remap(self, store):
1402
"""Check if all keys contained by children fit in a single LeafNode.
1404
:param store: A store to use for reading more nodes
1405
:return: Either self, or a new LeafNode which should replace self.
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.
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
1443
for key, value in node._items.iteritems():
1444
if new_leaf._map_no_split(key, value):
1446
trace.mutter("remap generated a new LeafNode")
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)
1458
raise AssertionError("Unknown node type.")
1462
class CHKMapDifference(object):
1463
"""Iterate the stored pages and key,value pairs for (new - old).
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
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.
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.)
1482
self._new_root_keys = new_root_keys
1483
self._old_root_keys = old_root_keys
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
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
1498
self._processed_new_refs = set()
1499
self._search_key_func = search_key_func
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 = []
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:
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()
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
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
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
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
1575
# This prefix is not part of the remaining 'interesting set'
1577
self._old_queue.append(ref)
1579
def _read_all_roots(self):
1580
"""Read the root pages.
1582
This is structured as a generator, so that the root records can be
1583
yielded up to whoever needs them without any buffering.
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)
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
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)
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)
1633
def _flush_new_queue(self):
1634
# No need to maintain the heap invariant anymore, just pull things out
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 = []
1646
yield None, new_items
1647
refs = refs.difference(all_old_chks)
1648
processed_new_refs.update(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.
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):
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]
1664
next_refs_update([p_r[1] for p_r in 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
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)
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)
1691
def _process_queues(self):
1692
while self._old_queue:
1693
self._process_next_old()
1694
return self._flush_new_queue()
1697
for record in self._read_all_roots():
1699
for record, items in self._process_queues():
1703
def iter_interesting_nodes(store, interesting_root_keys,
1704
uninteresting_root_keys, pb=None):
1705
"""Given root keys, find interesting nodes.
1707
Evaluate nodes referenced by interesting_root_keys. Ones that are also
1708
referenced from uninteresting_root_keys are not considered interesting.
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
1715
(interesting record, {interesting key:values})
1717
iterator = CHKMapDifference(store, interesting_root_keys,
1718
uninteresting_root_keys,
1719
search_key_func=store._search_key_func,
1721
return iterator.process()
1725
from bzrlib._chk_map_pyx import (
1729
_deserialise_leaf_node,
1730
_deserialise_internal_node,
1732
except ImportError, e:
1733
osutils.failed_to_load_extension(e)
1734
from bzrlib._chk_map_py import (
1738
_deserialise_leaf_node,
1739
_deserialise_internal_node,
1741
search_key_registry.register('hash-16-way', _search_key_16)
1742
search_key_registry.register('hash-255-way', _search_key_255)
1745
def _check_key(key):
1746
"""Helper function to assert that a key is properly formatted.
1748
This generally shouldn't be used in production code, but it can be helpful
1751
if type(key) is not StaticTuple:
1752
raise TypeError('key %r is not StaticTuple but %s' % (key, type(key)))
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,))