69
72
class GraphIndexBuilder(object):
70
73
"""A builder that can build a GraphIndex.
72
The resulting graph has the structure:
75
The resulting graph has the structure::
74
_SIGNATURE OPTIONS NODES NEWLINE
75
_SIGNATURE := 'Bazaar Graph Index 1' NEWLINE
76
OPTIONS := 'node_ref_lists=' DIGITS NEWLINE
78
NODE := KEY NULL ABSENT? NULL REFERENCES NULL VALUE NEWLINE
79
KEY := Not-whitespace-utf8
81
REFERENCES := REFERENCE_LIST (TAB REFERENCE_LIST){node_ref_lists - 1}
82
REFERENCE_LIST := (REFERENCE (CR REFERENCE)*)?
83
REFERENCE := DIGITS ; digits is the byte offset in the index of the
85
VALUE := no-newline-no-null-bytes
77
_SIGNATURE OPTIONS NODES NEWLINE
78
_SIGNATURE := 'Bazaar Graph Index 1' NEWLINE
79
OPTIONS := 'node_ref_lists=' DIGITS NEWLINE
81
NODE := KEY NULL ABSENT? NULL REFERENCES NULL VALUE NEWLINE
82
KEY := Not-whitespace-utf8
84
REFERENCES := REFERENCE_LIST (TAB REFERENCE_LIST){node_ref_lists - 1}
85
REFERENCE_LIST := (REFERENCE (CR REFERENCE)*)?
86
REFERENCE := DIGITS ; digits is the byte offset in the index of the
88
VALUE := no-newline-no-null-bytes
88
91
def __init__(self, reference_lists=0, key_elements=1):
184
187
:param value: The value associate with this key. Must not contain
185
188
newlines or null characters.
186
189
:return: (node_refs, absent_references)
187
node_refs basically a packed form of 'references' where all
189
absent_references reference keys that are not in self._nodes.
190
This may contain duplicates if the same key is
191
referenced in multiple lists.
191
* node_refs: basically a packed form of 'references' where all
193
* absent_references: reference keys that are not in self._nodes.
194
This may contain duplicates if the same key is referenced in
193
197
as_st = StaticTuple.from_sequence
194
198
self._check_key(key)
382
391
suitable for production use. :XXX
385
def __init__(self, transport, name, size, unlimited_cache=False):
394
def __init__(self, transport, name, size, unlimited_cache=False, offset=0):
386
395
"""Open an index called name on transport.
388
397
:param transport: A bzrlib.transport.Transport.
441
453
# We already did this
443
455
if 'index' in debug.debug_flags:
444
mutter('Reading entire index %s', self._transport.abspath(self._name))
456
trace.mutter('Reading entire index %s',
457
self._transport.abspath(self._name))
445
458
if stream is None:
446
459
stream = self._transport.get(self._name)
460
if self._base_offset != 0:
461
# This is wasteful, but it is better than dealing with
462
# adjusting all the offsets, etc.
463
stream = StringIO(stream.read()[self._base_offset:])
447
464
self._read_prefix(stream)
448
465
self._expected_elements = 3 + self._key_length
1190
1208
self._buffer_all()
1211
base_offset = self._base_offset
1212
if base_offset != 0:
1213
# Rewrite the ranges for the offset
1214
readv_ranges = [(start+base_offset, size)
1215
for start, size in readv_ranges]
1193
1216
readv_data = self._transport.readv(self._name, readv_ranges, True,
1217
self._size + self._base_offset)
1196
1219
for offset, data in readv_data:
1220
offset -= base_offset
1197
1221
self._bytes_read += len(data)
1223
# transport.readv() expanded to extra data which isn't part of
1225
data = data[-offset:]
1198
1227
if offset == 0 and len(data) == self._size:
1199
1228
# We read the whole range, most likely because the
1200
1229
# Transport upcast our readv ranges into one long request
1229
1258
Queries against the combined index will be made against the first index,
1230
and then the second and so on. The order of index's can thus influence
1259
and then the second and so on. The order of indices can thus influence
1231
1260
performance significantly. For example, if one index is on local disk and a
1232
1261
second on a remote server, the local disk index should be before the other
1233
1262
in the index list.
1264
Also, queries tend to need results from the same indices as previous
1265
queries. So the indices will be reordered after every query to put the
1266
indices that had the result(s) of that query first (while otherwise
1267
preserving the relative ordering).
1236
1270
def __init__(self, indices, reload_func=None):
1244
1278
self._indices = indices
1245
1279
self._reload_func = reload_func
1280
# Sibling indices are other CombinedGraphIndex that we should call
1281
# _move_to_front_by_name on when we auto-reorder ourself.
1282
self._sibling_indices = []
1283
# A list of names that corresponds to the instances in self._indices,
1284
# so _index_names[0] is always the name for _indices[0], etc. Sibling
1285
# indices must all use the same set of names as each other.
1286
self._index_names = [None] * len(self._indices)
1247
1288
def __repr__(self):
1248
1289
return "%s(%s)" % (
1257
1298
def get_parent_map(self, keys):
1258
1299
"""See graph.StackedParentsProvider.get_parent_map"""
1259
1300
search_keys = set(keys)
1260
if NULL_REVISION in search_keys:
1261
search_keys.discard(NULL_REVISION)
1262
found_parents = {NULL_REVISION:[]}
1301
if _mod_revision.NULL_REVISION in search_keys:
1302
search_keys.discard(_mod_revision.NULL_REVISION)
1303
found_parents = {_mod_revision.NULL_REVISION:[]}
1264
1305
found_parents = {}
1265
1306
for index, key, value, refs in self.iter_entries(search_keys):
1266
1307
parents = refs[0]
1267
1308
if not parents:
1268
parents = (NULL_REVISION,)
1309
parents = (_mod_revision.NULL_REVISION,)
1269
1310
found_parents[key] = parents
1270
1311
return found_parents
1272
1313
has_key = _has_key_from_parent_map
1274
def insert_index(self, pos, index):
1315
def insert_index(self, pos, index, name=None):
1275
1316
"""Insert a new index in the list of indices to query.
1277
1318
:param pos: The position to insert the index.
1278
1319
:param index: The index to insert.
1320
:param name: a name for this index, e.g. a pack name. These names can
1321
be used to reflect index reorderings to related CombinedGraphIndex
1322
instances that use the same names. (see set_sibling_indices)
1280
1324
self._indices.insert(pos, index)
1325
self._index_names.insert(pos, name)
1282
1327
def iter_all_entries(self):
1283
1328
"""Iterate over all keys within the index
1308
1353
value and are only reported once.
1310
1355
:param keys: An iterable providing the keys to be retrieved.
1311
:return: An iterable of (index, key, reference_lists, value). There is no
1312
defined order for the result iteration - it will be in the most
1356
:return: An iterable of (index, key, reference_lists, value). There is
1357
no defined order for the result iteration - it will be in the most
1313
1358
efficient order for the index.
1315
1360
keys = set(keys)
1318
1364
for index in self._indices:
1321
1368
for node in index.iter_entries(keys):
1322
1369
keys.remove(node[1])
1373
hit_indices.append(index)
1325
1375
except errors.NoSuchFile:
1326
1376
self._reload_or_raise()
1377
self._move_to_front(hit_indices)
1328
1379
def iter_entries_prefix(self, keys):
1329
1380
"""Iterate over keys within the index using prefix matching.
1351
1402
seen_keys = set()
1354
1406
for index in self._indices:
1355
1408
for node in index.iter_entries_prefix(keys):
1356
1409
if node[1] in seen_keys:
1358
1411
seen_keys.add(node[1])
1415
hit_indices.append(index)
1361
1417
except errors.NoSuchFile:
1362
1418
self._reload_or_raise()
1419
self._move_to_front(hit_indices)
1421
def _move_to_front(self, hit_indices):
1422
"""Rearrange self._indices so that hit_indices are first.
1424
Order is maintained as much as possible, e.g. the first unhit index
1425
will be the first index in _indices after the hit_indices, and the
1426
hit_indices will be present in exactly the order they are passed to
1429
_move_to_front propagates to all objects in self._sibling_indices by
1430
calling _move_to_front_by_name.
1432
if self._indices[:len(hit_indices)] == hit_indices:
1433
# The 'hit_indices' are already at the front (and in the same
1434
# order), no need to re-order
1436
hit_names = self._move_to_front_by_index(hit_indices)
1437
for sibling_idx in self._sibling_indices:
1438
sibling_idx._move_to_front_by_name(hit_names)
1440
def _move_to_front_by_index(self, hit_indices):
1441
"""Core logic for _move_to_front.
1443
Returns a list of names corresponding to the hit_indices param.
1445
indices_info = zip(self._index_names, self._indices)
1446
if 'index' in debug.debug_flags:
1447
trace.mutter('CombinedGraphIndex reordering: currently %r, '
1448
'promoting %r', indices_info, hit_indices)
1451
new_hit_indices = []
1454
for offset, (name, idx) in enumerate(indices_info):
1455
if idx in hit_indices:
1456
hit_names.append(name)
1457
new_hit_indices.append(idx)
1458
if len(new_hit_indices) == len(hit_indices):
1459
# We've found all of the hit entries, everything else is
1461
unhit_names.extend(self._index_names[offset+1:])
1462
unhit_indices.extend(self._indices[offset+1:])
1465
unhit_names.append(name)
1466
unhit_indices.append(idx)
1468
self._indices = new_hit_indices + unhit_indices
1469
self._index_names = hit_names + unhit_names
1470
if 'index' in debug.debug_flags:
1471
trace.mutter('CombinedGraphIndex reordered: %r', self._indices)
1474
def _move_to_front_by_name(self, hit_names):
1475
"""Moves indices named by 'hit_names' to front of the search order, as
1476
described in _move_to_front.
1478
# Translate names to index instances, and then call
1479
# _move_to_front_by_index.
1480
indices_info = zip(self._index_names, self._indices)
1482
for name, idx in indices_info:
1483
if name in hit_names:
1484
hit_indices.append(idx)
1485
self._move_to_front_by_index(hit_indices)
1364
1487
def find_ancestry(self, keys, ref_list_num):
1365
1488
"""Find the complete ancestry for the given set of keys.