40
from __future__ import absolute_import
43
from bzrlib import lazy_import
44
lazy_import.lazy_import(globals(), """
57
from bzrlib.static_tuple import StaticTuple
53
from ..sixish import (
57
from ..sixish import PY3
58
from ..static_tuple import StaticTuple
60
61
# If each line is 50 bytes, and you have 255 internal pages, with 255-way fan
90
91
_INTERESTING_NEW_SIZE = 50
91
92
# If a ChildNode shrinks by more than this amount, we check for a remap
92
93
_INTERESTING_SHRINKAGE_LIMIT = 20
93
# If we delete more than this many nodes applying a delta, we check for a remap
94
_INTERESTING_DELETES_LIMIT = 5
97
96
def _search_key_plain(key):
98
97
"""Map the key tuple into a search string that just uses the key bytes."""
99
return '\x00'.join(key)
98
return b'\x00'.join(key)
102
101
search_key_registry = registry.Registry()
103
search_key_registry.register('plain', _search_key_plain)
102
search_key_registry.register(b'plain', _search_key_plain)
106
105
class CHKMap(object):
135
134
into the map; if old_key is not None, then the old mapping
136
135
of old_key is removed.
139
138
# Check preconditions first.
140
139
as_st = StaticTuple.from_sequence
141
new_items = set([as_st(key) for (old, key, value) in delta
142
if key is not None and old is None])
140
new_items = {as_st(key) for (old, key, value) in delta
141
if key is not None and old is None}
143
142
existing_new = list(self.iteritems(key_filter=new_items))
145
144
raise errors.InconsistentDeltaDelta(delta,
148
147
for old, new, value in delta:
149
148
if old is not None and old != new:
150
149
self.unmap(old, check_remap=False)
152
151
for old, new, value in delta:
153
152
if new is not None:
154
153
self.map(new, value)
155
if delete_count > _INTERESTING_DELETES_LIMIT:
156
trace.mutter("checking remap as %d deletions", delete_count)
157
155
self._check_remap()
158
156
return self._save()
160
158
def _ensure_root(self):
161
159
"""Ensure that the root node is an object not a key."""
162
if type(self._root_node) is StaticTuple:
160
if isinstance(self._root_node, StaticTuple):
163
161
# Demand-load the root
164
162
self._root_node = self._get_node(self._root_node)
173
171
:param node: A tuple key or node object.
174
172
:return: A node object.
176
if type(node) is StaticTuple:
174
if isinstance(node, StaticTuple):
177
175
bytes = self._read_bytes(node)
178
176
return _deserialise(bytes, node,
179
177
search_key_func=self._search_key_func)
185
183
return _get_cache()[key]
187
185
stream = self._store.get_record_stream([key], 'unordered', True)
188
bytes = stream.next().get_bytes_as('fulltext')
186
bytes = next(stream).get_bytes_as('fulltext')
189
187
_get_cache()[key] = bytes
192
def _dump_tree(self, include_keys=False):
190
def _dump_tree(self, include_keys=False, encoding='utf-8'):
193
191
"""Return the tree in a string representation."""
194
192
self._ensure_root()
195
res = self._dump_tree_node(self._root_node, prefix='', indent='',
196
include_keys=include_keys)
194
decode = lambda x: x.decode(encoding)
197
res = self._dump_tree_node(self._root_node, prefix=b'', indent='',
198
decode=decode, include_keys=include_keys)
197
199
res.append('') # Give a trailing '\n'
198
200
return '\n'.join(res)
200
def _dump_tree_node(self, node, prefix, indent, include_keys=True):
202
def _dump_tree_node(self, node, prefix, indent, decode, include_keys=True):
201
203
"""For this node and all children, generate a string representation."""
203
205
if not include_keys:
206
208
node_key = node.key()
207
209
if node_key is not None:
208
key_str = ' %s' % (node_key[0],)
210
key_str = ' %s' % (decode(node_key[0]),)
210
212
key_str = ' None'
211
result.append('%s%r %s%s' % (indent, prefix, node.__class__.__name__,
213
if type(node) is InternalNode:
213
result.append('%s%r %s%s' % (indent, decode(prefix), node.__class__.__name__,
215
if isinstance(node, InternalNode):
214
216
# Trigger all child nodes to get loaded
215
217
list(node._iter_nodes(self._store))
216
for prefix, sub in sorted(node._items.iteritems()):
218
for prefix, sub in sorted(viewitems(node._items)):
217
219
result.extend(self._dump_tree_node(sub, prefix, indent + ' ',
218
include_keys=include_keys))
220
decode=decode, include_keys=include_keys))
220
for key, value in sorted(node._items.iteritems()):
222
for key, value in sorted(viewitems(node._items)):
221
223
# Don't use prefix nor indent here to line up when used in
222
224
# tests in conjunction with assertEqualDiff
223
result.append(' %r %r' % (tuple(key), value))
225
result.append(' %r %r' % (
226
tuple([decode(ke) for ke in key]), decode(value)))
244
247
root_key = klass._create_directly(store, initial_value,
245
248
maximum_size=maximum_size, key_width=key_width,
246
249
search_key_func=search_key_func)
247
if type(root_key) is not StaticTuple:
250
if not isinstance(root_key, StaticTuple):
248
251
raise AssertionError('we got a %s instead of a StaticTuple'
249
252
% (type(root_key),))
256
259
result._root_node.set_maximum_size(maximum_size)
257
260
result._root_node._key_width = key_width
259
for key, value in initial_value.items():
262
for key, value in viewitems(initial_value):
260
263
delta.append((None, key, value))
261
264
root_key = result.apply_delta(delta)
268
271
node.set_maximum_size(maximum_size)
269
272
node._key_width = key_width
270
273
as_st = StaticTuple.from_sequence
271
node._items = dict([(as_st(key), val) for key, val
272
in initial_value.iteritems()])
273
node._raw_size = sum([node._key_value_len(key, value)
274
for key,value in node._items.iteritems()])
274
node._items = dict((as_st(key), val)
275
for key, val in viewitems(initial_value))
276
node._raw_size = sum(node._key_value_len(key, value)
277
for key, value in viewitems(node._items))
275
278
node._len = len(node._items)
276
279
node._compute_search_prefix()
277
280
node._compute_serialised_prefix()
332
335
def process_node(node, path, a_map, pending):
333
336
# take a node and expand it
334
337
node = a_map._get_node(node)
335
if type(node) == LeafNode:
338
if isinstance(node, LeafNode):
336
339
path = (node._key, path)
337
for key, value in node._items.items():
340
for key, value in viewitems(node._items):
338
341
# For a LeafNode, the key is a serialized_key, rather than
339
342
# a search_key, but the heap is using search_keys
340
343
search_key = node._search_key_func(key)
343
346
# type(node) == InternalNode
344
347
path = (node._key, path)
345
for prefix, child in node._items.items():
348
for prefix, child in viewitems(node._items):
346
349
heapq.heappush(pending, (prefix, None, child, path))
347
350
def process_common_internal_nodes(self_node, basis_node):
348
self_items = set(self_node._items.items())
349
basis_items = set(basis_node._items.items())
351
self_items = set(viewitems(self_node._items))
352
basis_items = set(viewitems(basis_node._items))
350
353
path = (self_node._key, None)
351
354
for prefix, child in self_items - basis_items:
352
355
heapq.heappush(self_pending, (prefix, None, child, path))
354
357
for prefix, child in basis_items - self_items:
355
358
heapq.heappush(basis_pending, (prefix, None, child, path))
356
359
def process_common_leaf_nodes(self_node, basis_node):
357
self_items = set(self_node._items.items())
358
basis_items = set(basis_node._items.items())
360
self_items = set(viewitems(self_node._items))
361
basis_items = set(viewitems(basis_node._items))
359
362
path = (self_node._key, None)
360
363
for key, value in self_items - basis_items:
361
364
prefix = self._search_key_func(key)
371
374
self_node = self._get_node(self_node)
372
375
basis_node = basis._get_node(basis_node)
373
if (type(self_node) == InternalNode
374
and type(basis_node) == InternalNode):
376
if (isinstance(self_node, InternalNode)
377
and isinstance(basis_node, InternalNode)):
375
378
# Matching internal nodes
376
379
process_common_internal_nodes(self_node, basis_node)
377
elif (type(self_node) == LeafNode
378
and type(basis_node) == LeafNode):
380
elif (isinstance(self_node, LeafNode)
381
and isinstance(basis_node, LeafNode)):
379
382
process_common_leaf_nodes(self_node, basis_node)
381
384
process_node(self_node, self_path, self, self_pending)
390
393
# A better implementation would probably have a reverse map
391
394
# back to the children of a node, and jump straight to it when
392
395
# a common node is detected, the proceed to remove the already
393
# pending children. bzrlib.graph has a searcher module with a
396
# pending children. breezy.graph has a searcher module with a
394
397
# similar problem.
395
398
while key_path is not None:
396
399
key, key_path = key_path
552
555
def _node_key(self, node):
553
556
"""Get the key for a node whether it's a tuple or node."""
554
if type(node) is tuple:
557
if isinstance(node, tuple):
555
558
node = StaticTuple.from_sequence(node)
556
if type(node) is StaticTuple:
559
if isinstance(node, StaticTuple):
562
565
"""remove key from the map."""
563
566
key = StaticTuple.from_sequence(key)
564
567
self._ensure_root()
565
if type(self._root_node) is InternalNode:
568
if isinstance(self._root_node, InternalNode):
566
569
unmapped = self._root_node.unmap(self._store, key,
567
570
check_remap=check_remap)
572
575
def _check_remap(self):
573
576
"""Check if nodes can be collapsed."""
574
577
self._ensure_root()
575
if type(self._root_node) is InternalNode:
576
self._root_node._check_remap(self._store)
578
if isinstance(self._root_node, InternalNode):
579
self._root_node = self._root_node._check_remap(self._store)
579
582
"""Save the map completely.
581
584
:return: The key of the root node.
583
if type(self._root_node) is StaticTuple:
586
if isinstance(self._root_node, StaticTuple):
585
588
return self._root_node
586
589
keys = list(self._root_node.serialise(self._store))
769
772
# Short items, we need to match based on a prefix
770
length_filter = filters.setdefault(len(key), set())
771
length_filter.add(key)
773
filters.setdefault(len(key), set()).add(key)
773
filters = filters.items()
774
for item in self._items.iteritems():
775
for length, length_filter in filters:
775
filters_itemview = viewitems(filters)
776
for item in viewitems(self._items):
777
for length, length_filter in filters_itemview:
776
778
if item[0][:length] in length_filter:
780
for item in self._items.iteritems():
782
for item in viewitems(self._items):
783
785
def _key_value_len(self, key, value):
784
786
# TODO: Should probably be done without actually joining the key, but
785
787
# then that can be done via the C extension
786
788
return (len(self._serialise_key(key)) + 1
787
+ len(str(value.count('\n'))) + 1
789
+ len(b'%d' % value.count(b'\n')) + 1
788
790
+ len(value) + 1)
790
792
def _search_key(self, key):
839
841
common_prefix = self._search_prefix
840
842
split_at = len(common_prefix) + 1
842
for key, value in self._items.iteritems():
844
for key, value in viewitems(self._items):
843
845
search_key = self._search_key(key)
844
846
prefix = search_key[:split_at]
845
847
# TODO: Generally only 1 key can be exactly the right length,
851
853
# may get a '\00' node anywhere, but won't have keys of
852
854
# different lengths.
853
855
if len(prefix) < split_at:
854
prefix += '\x00'*(split_at - len(prefix))
856
prefix += b'\x00'*(split_at - len(prefix))
855
857
if prefix not in result:
856
858
node = LeafNode(search_key_func=self._search_key_func)
857
859
node.set_maximum_size(self._maximum_size)
872
874
for split, node in node_details:
873
875
new_node.add_node(split, node)
874
876
result[prefix] = new_node
875
return common_prefix, result.items()
877
return common_prefix, list(viewitems(result))
877
879
def map(self, store, key, value):
878
880
"""Map key to value."""
886
888
if self._search_prefix is _unknown:
887
889
raise AssertionError('%r must be known' % self._search_prefix)
888
return self._search_prefix, [("", self)]
890
return self._search_prefix, [(b"", self)]
890
_serialise_key = '\x00'.join
892
_serialise_key = b'\x00'.join
892
894
def serialise(self, store):
893
895
"""Serialise the LeafNode to store.
895
897
:param store: A VersionedFiles honouring the CHK extensions.
896
898
:return: An iterable of the keys inserted by this operation.
898
lines = ["chkleaf:\n"]
899
lines.append("%d\n" % self._maximum_size)
900
lines.append("%d\n" % self._key_width)
901
lines.append("%d\n" % self._len)
900
lines = [b"chkleaf:\n"]
901
lines.append(b"%d\n" % self._maximum_size)
902
lines.append(b"%d\n" % self._key_width)
903
lines.append(b"%d\n" % self._len)
902
904
if self._common_serialised_prefix is None:
904
906
if len(self._items) != 0:
905
907
raise AssertionError('If _common_serialised_prefix is None'
906
908
' we should have no items')
908
lines.append('%s\n' % (self._common_serialised_prefix,))
910
lines.append(b'%s\n' % (self._common_serialised_prefix,))
909
911
prefix_len = len(self._common_serialised_prefix)
910
for key, value in sorted(self._items.items()):
912
for key, value in sorted(viewitems(self._items)):
911
913
# Always add a final newline
912
value_lines = osutils.chunks_to_lines([value + '\n'])
913
serialized = "%s\x00%s\n" % (self._serialise_key(key),
914
value_lines = osutils.chunks_to_lines([value + b'\n'])
915
serialized = b"%s\x00%d\n" % (self._serialise_key(key),
914
916
len(value_lines))
915
917
if not serialized.startswith(self._common_serialised_prefix):
916
918
raise AssertionError('We thought the common prefix was %r'
919
921
lines.append(serialized[prefix_len:])
920
922
lines.extend(value_lines)
921
923
sha1, _, _ = store.add_lines((None,), (), lines)
922
self._key = StaticTuple("sha1:" + sha1,).intern()
923
bytes = ''.join(lines)
924
if len(bytes) != self._current_size():
924
self._key = StaticTuple(b"sha1:" + sha1,).intern()
925
data = b''.join(lines)
926
if len(data) != self._current_size():
925
927
raise AssertionError('Invalid _current_size')
926
_get_cache().add(self._key, bytes)
928
_get_cache()[self._key] = data
927
929
return [self._key]
996
998
__slots__ = ('_node_width',)
998
def __init__(self, prefix='', search_key_func=None):
1000
def __init__(self, prefix=b'', search_key_func=None):
999
1001
Node.__init__(self)
1000
1002
# The size of an internalnode with default values and no children.
1001
1003
# How many octets key prefixes within this node are.
1016
1018
raise AssertionError("_search_prefix should not be None")
1017
1019
if not prefix.startswith(self._search_prefix):
1018
1020
raise AssertionError("prefixes mismatch: %s must start with %s"
1019
% (prefix,self._search_prefix))
1021
% (prefix, self._search_prefix))
1020
1022
if len(prefix) != len(self._search_prefix) + 1:
1021
1023
raise AssertionError("prefix wrong length: len(%s) is not %d" %
1022
1024
(prefix, len(self._search_prefix) + 1))
1072
1074
# yielding all nodes, yield whatever we have, and queue up a read
1073
1075
# for whatever we are missing
1074
1076
shortcut = True
1075
for prefix, node in self._items.iteritems():
1077
for prefix, node in viewitems(self._items):
1076
1078
if node.__class__ is StaticTuple:
1077
1079
keys[node] = (prefix, None)
1149
1151
# The slow way. We walk every item in self._items, and check to
1150
1152
# see if there are any matches
1151
length_filters = length_filters.items()
1152
for prefix, node in self._items.iteritems():
1153
length_filters_itemview = viewitems(length_filters)
1154
for prefix, node in viewitems(self._items):
1153
1155
node_key_filter = []
1154
for length, length_filter in length_filters:
1156
for length, length_filter in length_filters_itemview:
1155
1157
sub_prefix = prefix[:length]
1156
1158
if sub_prefix in length_filter:
1157
1159
node_key_filter.extend(prefix_to_keys[sub_prefix])
1196
1198
prefix, node_key_filter = keys[record.key]
1197
1199
node_and_filters.append((node, node_key_filter))
1198
1200
self._items[prefix] = node
1199
_get_cache().add(record.key, bytes)
1201
_get_cache()[record.key] = bytes
1200
1202
for info in node_and_filters:
1221
1223
new_parent.add_node(self._search_prefix[:len(new_prefix)+1],
1223
1225
return new_parent.map(store, key, value)
1224
children = [node for node, _
1225
in self._iter_nodes(store, key_filter=[key])]
1226
children = [node for node, _ in self._iter_nodes(store, key_filter=[key])]
1227
1228
child = children[0]
1229
1230
# new child needed:
1230
1231
child = self._new_child(search_key, LeafNode)
1231
1232
old_len = len(child)
1232
if type(child) is LeafNode:
1233
if isinstance(child, LeafNode):
1233
1234
old_size = child._current_size()
1235
1236
old_size = None
1241
1242
self._items[search_key] = child
1242
1243
self._key = None
1243
1244
new_node = self
1244
if type(child) is LeafNode:
1245
if isinstance(child, LeafNode):
1245
1246
if old_size is None:
1246
1247
# The old node was an InternalNode which means it has now
1247
1248
# collapsed, so we need to check if it will chain to a
1265
1266
new_node = self._check_remap(store)
1266
1267
if new_node._search_prefix is None:
1267
1268
raise AssertionError("_search_prefix should not be None")
1268
return new_node._search_prefix, [('', new_node)]
1269
return new_node._search_prefix, [(b'', new_node)]
1269
1270
# child has overflown - create a new intermediate node.
1270
1271
# XXX: This is where we might want to try and expand our depth
1271
1272
# to refer to more bytes of every child (which would give us
1276
1277
child.add_node(split, node)
1277
1278
self._len = self._len - old_len + len(child)
1278
1279
self._key = None
1279
return self._search_prefix, [("", self)]
1280
return self._search_prefix, [(b"", self)]
1281
1282
def _new_child(self, search_key, klass):
1282
1283
"""Create a new child node of type klass."""
1293
1294
:param store: A VersionedFiles honouring the CHK extensions.
1294
1295
:return: An iterable of the keys inserted by this operation.
1296
for node in self._items.itervalues():
1297
if type(node) is StaticTuple:
1297
for node in viewvalues(self._items):
1298
if isinstance(node, StaticTuple):
1298
1299
# Never deserialised.
1300
1301
if node._key is not None:
1303
1304
for key in node.serialise(store):
1305
lines = ["chknode:\n"]
1306
lines.append("%d\n" % self._maximum_size)
1307
lines.append("%d\n" % self._key_width)
1308
lines.append("%d\n" % self._len)
1306
lines = [b"chknode:\n"]
1307
lines.append(b"%d\n" % self._maximum_size)
1308
lines.append(b"%d\n" % self._key_width)
1309
lines.append(b"%d\n" % self._len)
1309
1310
if self._search_prefix is None:
1310
1311
raise AssertionError("_search_prefix should not be None")
1311
lines.append('%s\n' % (self._search_prefix,))
1312
lines.append(b'%s\n' % (self._search_prefix,))
1312
1313
prefix_len = len(self._search_prefix)
1313
for prefix, node in sorted(self._items.items()):
1314
if type(node) is StaticTuple:
1314
for prefix, node in sorted(viewitems(self._items)):
1315
if isinstance(node, StaticTuple):
1317
1318
key = node._key[0]
1318
serialised = "%s\x00%s\n" % (prefix, key)
1319
serialised = b"%s\x00%s\n" % (prefix, key)
1319
1320
if not serialised.startswith(self._search_prefix):
1320
1321
raise AssertionError("prefixes mismatch: %s must start with %s"
1321
1322
% (serialised, self._search_prefix))
1322
1323
lines.append(serialised[prefix_len:])
1323
1324
sha1, _, _ = store.add_lines((None,), (), lines)
1324
self._key = StaticTuple("sha1:" + sha1,).intern()
1325
_get_cache().add(self._key, ''.join(lines))
1325
self._key = StaticTuple(b"sha1:" + sha1,).intern()
1326
_get_cache()[self._key] = b''.join(lines)
1326
1327
yield self._key
1328
1329
def _search_key(self, key):
1329
1330
"""Return the serialised key for key in this node."""
1330
1331
# search keys are fixed width. All will be self._node_width wide, so we
1331
1332
# pad as necessary.
1332
return (self._search_key_func(key) + '\x00'*self._node_width)[:self._node_width]
1333
return (self._search_key_func(key) + b'\x00'*self._node_width)[:self._node_width]
1334
1335
def _search_prefix_filter(self, key):
1335
1336
"""Serialise key for use as a prefix filter in iteritems."""
1343
1344
prefix for reaching node.
1345
1346
if offset >= self._node_width:
1346
for node in self._items.values():
1347
for node in valueview(self._items):
1347
1348
for result in node._split(offset):
1350
for key, node in self._items.items():
1353
1351
def refs(self):
1354
1352
"""Return the references to other CHK's held by this node."""
1355
1353
if self._key is None:
1356
1354
raise AssertionError("unserialised nodes have no refs.")
1358
for value in self._items.itervalues():
1359
if type(value) is StaticTuple:
1356
for value in viewvalues(self._items):
1357
if isinstance(value, StaticTuple):
1360
1358
refs.append(value)
1362
1360
refs.append(value.key())
1372
1370
return self._search_prefix
1374
1372
def unmap(self, store, key, check_remap=True):
1375
"""Remove key from this node and it's children."""
1373
"""Remove key from this node and its children."""
1376
1374
if not len(self._items):
1377
1375
raise AssertionError("can't unmap in an empty InternalNode.")
1378
1376
children = [node for node, _
1394
1392
self._items[search_key] = unmapped
1395
1393
if len(self._items) == 1:
1396
1394
# this node is no longer needed:
1397
return self._items.values()[0]
1398
if type(unmapped) is InternalNode:
1395
return list(viewvalues(self._items))[0]
1396
if isinstance(unmapped, InternalNode):
1400
1398
if check_remap:
1401
1399
return self._check_remap(store)
1441
1439
# c) With 255-way fan out, we don't want to read all 255 and destroy
1442
1440
# the page cache, just to determine that we really don't need it.
1443
1441
for node, _ in self._iter_nodes(store, batch_size=16):
1444
if type(node) is InternalNode:
1442
if isinstance(node, InternalNode):
1445
1443
# Without looking at any leaf nodes, we are sure
1447
for key, value in node._items.iteritems():
1445
for key, value in viewitems(node._items):
1448
1446
if new_leaf._map_no_split(key, value):
1450
1448
trace.mutter("remap generated a new LeafNode")
1451
1449
return new_leaf
1454
def _deserialise(bytes, key, search_key_func):
1452
def _deserialise(data, key, search_key_func):
1455
1453
"""Helper for repositorydetails - convert bytes to a node."""
1456
if bytes.startswith("chkleaf:\n"):
1457
node = LeafNode.deserialise(bytes, key, search_key_func=search_key_func)
1458
elif bytes.startswith("chknode:\n"):
1459
node = InternalNode.deserialise(bytes, key,
1454
if data.startswith(b"chkleaf:\n"):
1455
node = LeafNode.deserialise(data, key, search_key_func=search_key_func)
1456
elif data.startswith(b"chknode:\n"):
1457
node = InternalNode.deserialise(data, key,
1460
1458
search_key_func=search_key_func)
1462
1460
raise AssertionError("Unknown node type.")
1526
1524
bytes = record.get_bytes_as('fulltext')
1527
1525
node = _deserialise(bytes, record.key,
1528
1526
search_key_func=self._search_key_func)
1529
if type(node) is InternalNode:
1527
if isinstance(node, InternalNode):
1530
1528
# Note we don't have to do node.refs() because we know that
1531
1529
# there are no children that have been pushed into this node
1532
1530
# Note: Using as_st() here seemed to save 1.2MB, which would
1533
1531
# indicate that we keep 100k prefix_refs around while
1534
1532
# processing. They *should* be shorter lived than that...
1535
1533
# It does cost us ~10s of processing time
1536
#prefix_refs = [as_st(item) for item in node._items.iteritems()]
1537
prefix_refs = node._items.items()
1534
prefix_refs = list(viewitems(node._items))
1540
1537
prefix_refs = []
1541
1538
# Note: We don't use a StaticTuple here. Profiling showed a
1542
1539
# minor memory improvement (0.8MB out of 335MB peak 0.2%)
1543
1540
# But a significant slowdown (15s / 145s, or 10%)
1544
items = node._items.items()
1541
items = list(viewitems(node._items))
1545
1542
yield record, node, prefix_refs, items
1547
1544
def _read_old_roots(self):
1571
1568
# handled the interesting ones
1572
1569
for prefix, ref in old_chks_to_enqueue:
1573
1570
not_interesting = True
1574
for i in xrange(len(prefix), 0, -1):
1571
for i in range(len(prefix), 0, -1):
1575
1572
if prefix[:i] in new_prefixes:
1576
1573
not_interesting = False
1631
1628
# 'ab', then we also need to include 'a'.) So expand the
1632
1629
# new_prefixes to include all shorter prefixes
1633
1630
for prefix in list(new_prefixes):
1634
new_prefixes.update([prefix[:i] for i in xrange(1, len(prefix))])
1631
new_prefixes.update([prefix[:i] for i in range(1, len(prefix))])
1635
1632
self._enqueue_old(new_prefixes, old_chks_to_enqueue)
1637
1634
def _flush_new_queue(self):
1688
1685
for record, _, prefix_refs, items in self._read_nodes_from_store(refs):
1689
1686
# TODO: Use StaticTuple here?
1690
1687
self._all_old_items.update(items)
1691
refs = [r for _,r in prefix_refs if r not in all_old_chks]
1688
refs = [r for _, r in prefix_refs if r not in all_old_chks]
1692
1689
self._old_queue.extend(refs)
1693
1690
all_old_chks.update(refs)
1729
from bzrlib._chk_map_pyx import (
1726
from ._chk_map_pyx import (
1730
1728
_search_key_16,
1731
1729
_search_key_255,
1732
1730
_deserialise_leaf_node,
1733
1731
_deserialise_internal_node,
1735
except ImportError, e:
1733
except ImportError as e:
1736
1734
osutils.failed_to_load_extension(e)
1737
from bzrlib._chk_map_py import (
1735
from ._chk_map_py import (
1738
1737
_search_key_16,
1739
1738
_search_key_255,
1740
1739
_deserialise_leaf_node,
1741
1740
_deserialise_internal_node,
1743
search_key_registry.register('hash-16-way', _search_key_16)
1744
search_key_registry.register('hash-255-way', _search_key_255)
1742
search_key_registry.register(b'hash-16-way', _search_key_16)
1743
search_key_registry.register(b'hash-255-way', _search_key_255)
1747
1746
def _check_key(key):
1750
1749
This generally shouldn't be used in production code, but it can be helpful
1751
1750
to debug problems.
1753
if type(key) is not StaticTuple:
1752
if not isinstance(key, StaticTuple):
1754
1753
raise TypeError('key %r is not StaticTuple but %s' % (key, type(key)))
1755
1754
if len(key) != 1:
1756
1755
raise ValueError('key %r should have length 1, not %d' % (key, len(key),))
1757
if type(key[0]) is not str:
1756
if not isinstance(key[0], str):
1758
1757
raise TypeError('key %r should hold a str, not %r'
1759
1758
% (key, type(key[0])))
1760
1759
if not key[0].startswith('sha1:'):