54
52
_newline_null_re = re.compile('[\n\0]')
57
def _has_key_from_parent_map(self, key):
58
"""Check if this index has one key.
60
If it's possible to check for multiple keys at once through
61
calling get_parent_map that should be faster.
63
return (key in self.get_parent_map([key]))
66
def _missing_keys_from_parent_map(self, keys):
67
return set(keys) - set(self.get_parent_map(keys))
70
55
class GraphIndexBuilder(object):
71
56
"""A builder that can build a GraphIndex.
73
The resulting graph has the structure::
75
_SIGNATURE OPTIONS NODES NEWLINE
76
_SIGNATURE := 'Bazaar Graph Index 1' NEWLINE
77
OPTIONS := 'node_ref_lists=' DIGITS NEWLINE
79
NODE := KEY NULL ABSENT? NULL REFERENCES NULL VALUE NEWLINE
80
KEY := Not-whitespace-utf8
82
REFERENCES := REFERENCE_LIST (TAB REFERENCE_LIST){node_ref_lists - 1}
83
REFERENCE_LIST := (REFERENCE (CR REFERENCE)*)?
84
REFERENCE := DIGITS ; digits is the byte offset in the index of the
86
VALUE := no-newline-no-null-bytes
58
The resulting graph has the structure:
60
_SIGNATURE OPTIONS NODES NEWLINE
61
_SIGNATURE := 'Bazaar Graph Index 1' NEWLINE
62
OPTIONS := 'node_ref_lists=' DIGITS NEWLINE
64
NODE := KEY NULL ABSENT? NULL REFERENCES NULL VALUE NEWLINE
65
KEY := Not-whitespace-utf8
67
REFERENCES := REFERENCE_LIST (TAB REFERENCE_LIST){node_ref_lists - 1}
68
REFERENCE_LIST := (REFERENCE (CR REFERENCE)*)?
69
REFERENCE := DIGITS ; digits is the byte offset in the index of the
71
VALUE := no-newline-no-null-bytes
89
74
def __init__(self, reference_lists=0, key_elements=1):
336
278
(len(result.getvalue()), expected_bytes))
339
def set_optimize(self, for_size=None, combine_backing_indices=None):
340
"""Change how the builder tries to optimize the result.
342
:param for_size: Tell the builder to try and make the index as small as
344
:param combine_backing_indices: If the builder spills to disk to save
345
memory, should the on-disk indices be combined. Set to True if you
346
are going to be probing the index, but to False if you are not. (If
347
you are not querying, then the time spent combining is wasted.)
350
# GraphIndexBuilder itself doesn't pay attention to the flag yet, but
352
if for_size is not None:
353
self._optimize_for_size = for_size
354
if combine_backing_indices is not None:
355
self._combine_backing_indices = combine_backing_indices
357
def find_ancestry(self, keys, ref_list_num):
358
"""See CombinedGraphIndex.find_ancestry()"""
364
for _, key, value, ref_lists in self.iter_entries(pending):
365
parent_keys = ref_lists[ref_list_num]
366
parent_map[key] = parent_keys
367
next_pending.update([p for p in parent_keys if p not in
369
missing_keys.update(pending.difference(parent_map))
370
pending = next_pending
371
return parent_map, missing_keys
374
282
class GraphIndex(object):
375
283
"""An index for data with embedded graphs.
377
285
The index maps keys to a list of key reference lists, and a value.
378
286
Each node has the same number of key reference lists. Each key reference
379
287
list can be empty or an arbitrary length. The value is an opaque NULL
380
terminated string without any newlines. The storage of the index is
288
terminated string without any newlines. The storage of the index is
381
289
hidden in the interface: keys and key references are always tuples of
382
290
bytestrings, never the internal representation (e.g. dictionary offsets).
484
376
node_value = value
485
377
self._nodes[key] = node_value
378
if self._key_length > 1:
379
# TODO: We may want to do this lazily, but if we are calling
380
# _buffer_all, we are likely to be doing
381
# iter_entries_prefix
382
key_dict = self._nodes_by_key
383
if self.node_ref_lists:
384
key_value = key, node_value[0], node_value[1]
386
key_value = key, node_value
387
# For a key of (foo, bar, baz) create
388
# _nodes_by_key[foo][bar][baz] = key_value
389
for subkey in key[:-1]:
390
key_dict = key_dict.setdefault(subkey, {})
391
key_dict[key[-1]] = key_value
486
392
# cache the keys for quick set intersections
393
self._keys = set(self._nodes)
487
394
if trailers != 1:
488
395
# there must be one line - the empty trailer line.
489
396
raise errors.BadIndexData(self)
491
def clear_cache(self):
492
"""Clear out any cached/memoized values.
494
This can be called at any time, but generally it is used when we have
495
extracted some information, but don't expect to be requesting any more
499
def external_references(self, ref_list_num):
500
"""Return references that are not present in this index.
503
if ref_list_num + 1 > self.node_ref_lists:
504
raise ValueError('No ref list %d, index has %d ref lists'
505
% (ref_list_num, self.node_ref_lists))
508
for key, (value, ref_lists) in nodes.iteritems():
509
ref_list = ref_lists[ref_list_num]
510
refs.update([ref for ref in ref_list if ref not in nodes])
513
def _get_nodes_by_key(self):
514
if self._nodes_by_key is None:
516
if self.node_ref_lists:
517
for key, (value, references) in self._nodes.iteritems():
518
key_dict = nodes_by_key
519
for subkey in key[:-1]:
520
key_dict = key_dict.setdefault(subkey, {})
521
key_dict[key[-1]] = key, value, references
523
for key, value in self._nodes.iteritems():
524
key_dict = nodes_by_key
525
for subkey in key[:-1]:
526
key_dict = key_dict.setdefault(subkey, {})
527
key_dict[key[-1]] = key, value
528
self._nodes_by_key = nodes_by_key
529
return self._nodes_by_key
531
398
def iter_all_entries(self):
532
399
"""Iterate over all keys within the index.
841
686
if self._bisect_nodes is None:
842
687
readv_ranges.append(_HEADER_READV)
843
688
self._read_and_parse(readv_ranges)
845
if self._nodes is not None:
846
# _read_and_parse triggered a _buffer_all because we requested the
848
for location, key in location_keys:
849
if key not in self._nodes: # not present
850
result.append(((location, key), False))
851
elif self.node_ref_lists:
852
value, refs = self._nodes[key]
853
result.append(((location, key),
854
(self, key, value, refs)))
856
result.append(((location, key),
857
(self, key, self._nodes[key])))
859
689
# generate results:
860
690
# - figure out <, >, missing, present
861
691
# - result present references so we can return them.
862
693
# keys that we cannot answer until we resolve references
863
694
pending_references = []
864
695
pending_locations = set()
1194
1019
self._parsed_key_map.insert(index + 1, new_key)
1196
1021
def _read_and_parse(self, readv_ranges):
1197
"""Read the ranges and parse the resulting data.
1022
"""Read the the ranges and parse the resulting data.
1199
1024
:param readv_ranges: A prepared readv range list.
1201
if not readv_ranges:
1203
if self._nodes is None and self._bytes_read * 2 >= self._size:
1204
# We've already read more than 50% of the file and we are about to
1205
# request more data, just _buffer_all() and be done
1209
base_offset = self._base_offset
1210
if base_offset != 0:
1211
# Rewrite the ranges for the offset
1212
readv_ranges = [(start+base_offset, size)
1213
for start, size in readv_ranges]
1214
readv_data = self._transport.readv(self._name, readv_ranges, True,
1215
self._size + self._base_offset)
1217
for offset, data in readv_data:
1218
offset -= base_offset
1219
self._bytes_read += len(data)
1221
# transport.readv() expanded to extra data which isn't part of
1223
data = data[-offset:]
1225
if offset == 0 and len(data) == self._size:
1226
# We read the whole range, most likely because the
1227
# Transport upcast our readv ranges into one long request
1228
# for enough total data to grab the whole index.
1229
self._buffer_all(StringIO(data))
1231
if self._bisect_nodes is None:
1232
# this must be the start
1233
if not (offset == 0):
1234
raise AssertionError()
1235
offset, data = self._parse_header_from_bytes(data)
1236
# print readv_ranges, "[%d:%d]" % (offset, offset + len(data))
1237
self._parse_region(offset, data)
1027
readv_data = self._transport.readv(self._name, readv_ranges, True,
1030
for offset, data in readv_data:
1031
if self._bisect_nodes is None:
1032
# this must be the start
1033
if not (offset == 0):
1034
raise AssertionError()
1035
offset, data = self._parse_header_from_bytes(data)
1036
# print readv_ranges, "[%d:%d]" % (offset, offset + len(data))
1037
self._parse_region(offset, data)
1239
1039
def _signature(self):
1240
1040
"""The file signature for this index type."""
1250
1050
class CombinedGraphIndex(object):
1251
1051
"""A GraphIndex made up from smaller GraphIndices.
1253
1053
The backing indices must implement GraphIndex, and are presumed to be
1256
1056
Queries against the combined index will be made against the first index,
1257
and then the second and so on. The order of indices can thus influence
1057
and then the second and so on. The order of index's can thus influence
1258
1058
performance significantly. For example, if one index is on local disk and a
1259
1059
second on a remote server, the local disk index should be before the other
1260
1060
in the index list.
1262
Also, queries tend to need results from the same indices as previous
1263
queries. So the indices will be reordered after every query to put the
1264
indices that had the result(s) of that query first (while otherwise
1265
preserving the relative ordering).
1268
def __init__(self, indices, reload_func=None):
1063
def __init__(self, indices):
1269
1064
"""Create a CombinedGraphIndex backed by indices.
1271
1066
:param indices: An ordered list of indices to query for data.
1272
:param reload_func: A function to call if we find we are missing an
1273
index. Should have the form reload_func() => True/False to indicate
1274
if reloading actually changed anything.
1276
1068
self._indices = indices
1277
self._reload_func = reload_func
1278
# Sibling indices are other CombinedGraphIndex that we should call
1279
# _move_to_front_by_name on when we auto-reorder ourself.
1280
self._sibling_indices = []
1281
# A list of names that corresponds to the instances in self._indices,
1282
# so _index_names[0] is always the name for _indices[0], etc. Sibling
1283
# indices must all use the same set of names as each other.
1284
self._index_names = [None] * len(self._indices)
1286
1070
def __repr__(self):
1287
1071
return "%s(%s)" % (
1288
1072
self.__class__.__name__,
1289
1073
', '.join(map(repr, self._indices)))
1291
def clear_cache(self):
1292
"""See GraphIndex.clear_cache()"""
1293
for index in self._indices:
1075
@symbol_versioning.deprecated_method(symbol_versioning.one_one)
1076
def get_parents(self, revision_ids):
1077
"""See graph._StackedParentsProvider.get_parents.
1079
This implementation thunks the graph.Graph.get_parents api across to
1082
:param revision_ids: An iterable of graph keys for this graph.
1083
:return: A list of parent details for each key in revision_ids.
1084
Each parent details will be one of:
1085
* None when the key was missing
1086
* (NULL_REVISION,) when the key has no parents.
1087
* (parent_key, parent_key...) otherwise.
1089
parent_map = self.get_parent_map(revision_ids)
1090
return [parent_map.get(r, None) for r in revision_ids]
1296
1092
def get_parent_map(self, keys):
1297
"""See graph.StackedParentsProvider.get_parent_map"""
1093
"""See graph._StackedParentsProvider.get_parent_map"""
1298
1094
search_keys = set(keys)
1299
if _mod_revision.NULL_REVISION in search_keys:
1300
search_keys.discard(_mod_revision.NULL_REVISION)
1301
found_parents = {_mod_revision.NULL_REVISION:[]}
1095
if NULL_REVISION in search_keys:
1096
search_keys.discard(NULL_REVISION)
1097
found_parents = {NULL_REVISION:[]}
1303
1099
found_parents = {}
1304
1100
for index, key, value, refs in self.iter_entries(search_keys):
1305
1101
parents = refs[0]
1306
1102
if not parents:
1307
parents = (_mod_revision.NULL_REVISION,)
1103
parents = (NULL_REVISION,)
1308
1104
found_parents[key] = parents
1309
1105
return found_parents
1311
has_key = _has_key_from_parent_map
1313
def insert_index(self, pos, index, name=None):
1107
def insert_index(self, pos, index):
1314
1108
"""Insert a new index in the list of indices to query.
1316
1110
:param pos: The position to insert the index.
1317
1111
:param index: The index to insert.
1318
:param name: a name for this index, e.g. a pack name. These names can
1319
be used to reflect index reorderings to related CombinedGraphIndex
1320
instances that use the same names. (see set_sibling_indices)
1322
1113
self._indices.insert(pos, index)
1323
self._index_names.insert(pos, name)
1325
1115
def iter_all_entries(self):
1326
1116
"""Iterate over all keys within the index
1400
1174
seen_keys = set()
1404
for index in self._indices:
1406
for node in index.iter_entries_prefix(keys):
1407
if node[1] in seen_keys:
1409
seen_keys.add(node[1])
1413
hit_indices.append(index)
1415
except errors.NoSuchFile:
1416
self._reload_or_raise()
1417
self._move_to_front(hit_indices)
1419
def _move_to_front(self, hit_indices):
1420
"""Rearrange self._indices so that hit_indices are first.
1422
Order is maintained as much as possible, e.g. the first unhit index
1423
will be the first index in _indices after the hit_indices, and the
1424
hit_indices will be present in exactly the order they are passed to
1427
_move_to_front propagates to all objects in self._sibling_indices by
1428
calling _move_to_front_by_name.
1430
if self._indices[:len(hit_indices)] == hit_indices:
1431
# The 'hit_indices' are already at the front (and in the same
1432
# order), no need to re-order
1434
hit_names = self._move_to_front_by_index(hit_indices)
1435
for sibling_idx in self._sibling_indices:
1436
sibling_idx._move_to_front_by_name(hit_names)
1438
def _move_to_front_by_index(self, hit_indices):
1439
"""Core logic for _move_to_front.
1441
Returns a list of names corresponding to the hit_indices param.
1443
indices_info = zip(self._index_names, self._indices)
1444
if 'index' in debug.debug_flags:
1445
trace.mutter('CombinedGraphIndex reordering: currently %r, '
1446
'promoting %r', indices_info, hit_indices)
1449
new_hit_indices = []
1452
for offset, (name, idx) in enumerate(indices_info):
1453
if idx in hit_indices:
1454
hit_names.append(name)
1455
new_hit_indices.append(idx)
1456
if len(new_hit_indices) == len(hit_indices):
1457
# We've found all of the hit entries, everything else is
1459
unhit_names.extend(self._index_names[offset+1:])
1460
unhit_indices.extend(self._indices[offset+1:])
1463
unhit_names.append(name)
1464
unhit_indices.append(idx)
1466
self._indices = new_hit_indices + unhit_indices
1467
self._index_names = hit_names + unhit_names
1468
if 'index' in debug.debug_flags:
1469
trace.mutter('CombinedGraphIndex reordered: %r', self._indices)
1472
def _move_to_front_by_name(self, hit_names):
1473
"""Moves indices named by 'hit_names' to front of the search order, as
1474
described in _move_to_front.
1476
# Translate names to index instances, and then call
1477
# _move_to_front_by_index.
1478
indices_info = zip(self._index_names, self._indices)
1480
for name, idx in indices_info:
1481
if name in hit_names:
1482
hit_indices.append(idx)
1483
self._move_to_front_by_index(hit_indices)
1485
def find_ancestry(self, keys, ref_list_num):
1486
"""Find the complete ancestry for the given set of keys.
1488
Note that this is a whole-ancestry request, so it should be used
1491
:param keys: An iterable of keys to look for
1492
:param ref_list_num: The reference list which references the parents
1494
:return: (parent_map, missing_keys)
1496
# XXX: make this call _move_to_front?
1497
missing_keys = set()
1499
keys_to_lookup = set(keys)
1501
while keys_to_lookup:
1502
# keys that *all* indexes claim are missing, stop searching them
1504
all_index_missing = None
1505
# print 'gen\tidx\tsub\tn_keys\tn_pmap\tn_miss'
1506
# print '%4d\t\t\t%4d\t%5d\t%5d' % (generation, len(keys_to_lookup),
1508
# len(missing_keys))
1509
for index_idx, index in enumerate(self._indices):
1510
# TODO: we should probably be doing something with
1511
# 'missing_keys' since we've already determined that
1512
# those revisions have not been found anywhere
1513
index_missing_keys = set()
1514
# Find all of the ancestry we can from this index
1515
# keep looking until the search_keys set is empty, which means
1516
# things we didn't find should be in index_missing_keys
1517
search_keys = keys_to_lookup
1519
# print ' \t%2d\t\t%4d\t%5d\t%5d' % (
1520
# index_idx, len(search_keys),
1521
# len(parent_map), len(index_missing_keys))
1524
# TODO: ref_list_num should really be a parameter, since
1525
# CombinedGraphIndex does not know what the ref lists
1527
search_keys = index._find_ancestors(search_keys,
1528
ref_list_num, parent_map, index_missing_keys)
1529
# print ' \t \t%2d\t%4d\t%5d\t%5d' % (
1530
# sub_generation, len(search_keys),
1531
# len(parent_map), len(index_missing_keys))
1532
# Now set whatever was missing to be searched in the next index
1533
keys_to_lookup = index_missing_keys
1534
if all_index_missing is None:
1535
all_index_missing = set(index_missing_keys)
1537
all_index_missing.intersection_update(index_missing_keys)
1538
if not keys_to_lookup:
1540
if all_index_missing is None:
1541
# There were no indexes, so all search keys are 'missing'
1542
missing_keys.update(keys_to_lookup)
1543
keys_to_lookup = None
1545
missing_keys.update(all_index_missing)
1546
keys_to_lookup.difference_update(all_index_missing)
1547
return parent_map, missing_keys
1175
for index in self._indices:
1176
for node in index.iter_entries_prefix(keys):
1177
if node[1] in seen_keys:
1179
seen_keys.add(node[1])
1549
1182
def key_count(self):
1550
1183
"""Return an estimate of the number of keys in this index.
1552
1185
For CombinedGraphIndex this is approximated by the sum of the keys of
1553
1186
the child indices. As child indices may have duplicate keys this can
1554
1187
have a maximum error of the number of child indices * largest number of
1555
1188
keys in any index.
1559
return sum((index.key_count() for index in self._indices), 0)
1560
except errors.NoSuchFile:
1561
self._reload_or_raise()
1563
missing_keys = _missing_keys_from_parent_map
1565
def _reload_or_raise(self):
1566
"""We just got a NoSuchFile exception.
1568
Try to reload the indices, if it fails, just raise the current
1571
if self._reload_func is None:
1573
exc_type, exc_value, exc_traceback = sys.exc_info()
1574
trace.mutter('Trying to reload after getting exception: %s',
1576
if not self._reload_func():
1577
# We tried to reload, but nothing changed, so we fail anyway
1578
trace.mutter('_reload_func indicated nothing has changed.'
1579
' Raising original exception.')
1580
raise exc_type, exc_value, exc_traceback
1582
def set_sibling_indices(self, sibling_combined_graph_indices):
1583
"""Set the CombinedGraphIndex objects to reorder after reordering self.
1585
self._sibling_indices = sibling_combined_graph_indices
1190
return sum((index.key_count() for index in self._indices), 0)
1587
1192
def validate(self):
1588
1193
"""Validate that everything in the index can be accessed."""
1591
for index in self._indices:
1594
except errors.NoSuchFile:
1595
self._reload_or_raise()
1194
for index in self._indices:
1598
1198
class InMemoryGraphIndex(GraphIndexBuilder):