334
300
'data aa', ([('agoodkey', ), ('that is a bad key', )], ))
335
301
# and if there is more than one list it should be getting checked
337
builder = index.GraphIndexBuilder(reference_lists=2)
303
builder = GraphIndexBuilder(reference_lists=2)
338
304
self.assertRaises(errors.BadIndexKey, builder.add_node, ('akey', ),
339
305
'data aa', ([], ['a bad key']))
341
307
def test_add_duplicate_key(self):
342
builder = index.GraphIndexBuilder()
308
builder = GraphIndexBuilder()
343
309
builder.add_node(('key', ), 'data')
344
self.assertRaises(errors.BadIndexDuplicateKey,
345
builder.add_node, ('key', ), 'data')
310
self.assertRaises(errors.BadIndexDuplicateKey, builder.add_node, ('key', ),
347
313
def test_add_duplicate_key_2_elements(self):
348
builder = index.GraphIndexBuilder(key_elements=2)
314
builder = GraphIndexBuilder(key_elements=2)
349
315
builder.add_node(('key', 'key'), 'data')
350
316
self.assertRaises(errors.BadIndexDuplicateKey, builder.add_node,
351
317
('key', 'key'), 'data')
353
319
def test_add_key_after_referencing_key(self):
354
builder = index.GraphIndexBuilder(reference_lists=1)
320
builder = GraphIndexBuilder(reference_lists=1)
355
321
builder.add_node(('key', ), 'data', ([('reference', )], ))
356
322
builder.add_node(('reference', ), 'data', ([],))
358
324
def test_add_key_after_referencing_key_2_elements(self):
359
builder = index.GraphIndexBuilder(reference_lists=1, key_elements=2)
325
builder = GraphIndexBuilder(reference_lists=1, key_elements=2)
360
326
builder.add_node(('k', 'ey'), 'data', ([('reference', 'tokey')], ))
361
327
builder.add_node(('reference', 'tokey'), 'data', ([],))
363
def test_set_optimize(self):
364
builder = index.GraphIndexBuilder(reference_lists=1, key_elements=2)
365
builder.set_optimize(for_size=True)
366
self.assertTrue(builder._optimize_for_size)
367
builder.set_optimize(for_size=False)
368
self.assertFalse(builder._optimize_for_size)
371
class TestGraphIndex(tests.TestCaseWithMemoryTransport):
373
def make_key(self, number):
374
return (str(number) + 'X'*100,)
376
def make_value(self, number):
377
return str(number) + 'Y'*100
379
def make_nodes(self, count=64):
380
# generate a big enough index that we only read some of it on a typical
383
for counter in range(count):
384
nodes.append((self.make_key(counter), self.make_value(counter), ()))
330
class TestGraphIndex(TestCaseWithMemoryTransport):
387
332
def make_index(self, ref_lists=0, key_elements=1, nodes=[]):
388
builder = index.GraphIndexBuilder(ref_lists, key_elements=key_elements)
389
for key, value, references in nodes:
390
builder.add_node(key, value, references)
333
builder = GraphIndexBuilder(ref_lists, key_elements=key_elements)
334
for node, value, references in nodes:
335
builder.add_node(node, value, references)
391
336
stream = builder.finish()
392
trans = transport.get_transport('trace+' + self.get_url())
393
size = trans.put_file('index', stream)
394
return index.GraphIndex(trans, 'index', size)
396
def make_index_with_offset(self, ref_lists=0, key_elements=1, nodes=[],
398
builder = index.GraphIndexBuilder(ref_lists, key_elements=key_elements)
399
for key, value, references in nodes:
400
builder.add_node(key, value, references)
401
content = builder.finish().read()
403
337
trans = self.get_transport()
404
trans.put_bytes('index', (' '*offset) + content)
405
return index.GraphIndex(trans, 'index', size, offset=offset)
407
def test_clear_cache(self):
408
index = self.make_index()
409
# For now, we just want to make sure the api is available. As this is
410
# old code, we don't really worry if it *does* anything.
338
trans.put_file('index', stream)
339
return GraphIndex(trans, 'index')
413
341
def test_open_bad_index_no_error(self):
414
342
trans = self.get_transport()
415
343
trans.put_bytes('name', "not an index\n")
416
idx = index.GraphIndex(trans, 'name', 13)
418
def test_with_offset(self):
419
nodes = self.make_nodes(200)
420
idx = self.make_index_with_offset(offset=1234567, nodes=nodes)
421
self.assertEqual(200, idx.key_count())
423
def test_buffer_all_with_offset(self):
424
nodes = self.make_nodes(200)
425
idx = self.make_index_with_offset(offset=1234567, nodes=nodes)
427
self.assertEqual(200, idx.key_count())
429
def test_side_effect_buffering_with_offset(self):
430
nodes = self.make_nodes(20)
431
index = self.make_index_with_offset(offset=1234567, nodes=nodes)
432
index._transport.recommended_page_size = lambda:64*1024
433
subset_nodes = [nodes[0][0], nodes[10][0], nodes[19][0]]
434
entries = [n[1] for n in index.iter_entries(subset_nodes)]
435
self.assertEqual(sorted(subset_nodes), sorted(entries))
436
self.assertEqual(20, index.key_count())
438
def test_open_sets_parsed_map_empty(self):
439
index = self.make_index()
440
self.assertEqual([], index._parsed_byte_map)
441
self.assertEqual([], index._parsed_key_map)
443
def test_key_count_buffers(self):
444
index = self.make_index(nodes=self.make_nodes(2))
445
# reset the transport log
446
del index._transport._activity[:]
447
self.assertEqual(2, index.key_count())
448
# We should have requested reading the header bytes
450
('readv', 'index', [(0, 200)], True, index._size),
452
index._transport._activity)
453
# And that should have been enough to trigger reading the whole index
455
self.assertIsNot(None, index._nodes)
457
def test_lookup_key_via_location_buffers(self):
458
index = self.make_index()
459
# reset the transport log
460
del index._transport._activity[:]
461
# do a _lookup_keys_via_location call for the middle of the file, which
462
# is what bisection uses.
463
result = index._lookup_keys_via_location(
464
[(index._size // 2, ('missing', ))])
465
# this should have asked for a readv request, with adjust_for_latency,
466
# and two regions: the header, and half-way into the file.
468
('readv', 'index', [(30, 30), (0, 200)], True, 60),
470
index._transport._activity)
471
# and the result should be that the key cannot be present, because this
472
# is a trivial index.
473
self.assertEqual([((index._size // 2, ('missing', )), False)],
475
# And this should have caused the file to be fully buffered
476
self.assertIsNot(None, index._nodes)
477
self.assertEqual([], index._parsed_byte_map)
479
def test_first_lookup_key_via_location(self):
480
# We need enough data so that the _HEADER_READV doesn't consume the
481
# whole file. We always read 800 bytes for every key, and the local
482
# transport natural expansion is 4096 bytes. So we have to have >8192
483
# bytes or we will trigger "buffer_all".
484
# We also want the 'missing' key to fall within the range that *did*
487
index = self.make_index(nodes=self.make_nodes(64))
488
# reset the transport log
489
del index._transport._activity[:]
490
# do a _lookup_keys_via_location call for the middle of the file, which
491
# is what bisection uses.
492
start_lookup = index._size // 2
493
result = index._lookup_keys_via_location(
494
[(start_lookup, ('40missing', ))])
495
# this should have asked for a readv request, with adjust_for_latency,
496
# and two regions: the header, and half-way into the file.
499
[(start_lookup, 800), (0, 200)], True, index._size),
501
index._transport._activity)
502
# and the result should be that the key cannot be present, because this
503
# is a trivial index.
504
self.assertEqual([((start_lookup, ('40missing', )), False)],
506
# And this should not have caused the file to be fully buffered
507
self.assertIs(None, index._nodes)
508
# And the regions of the file that have been parsed should be in the
509
# parsed_byte_map and the parsed_key_map
510
self.assertEqual([(0, 4008), (5046, 8996)], index._parsed_byte_map)
511
self.assertEqual([(None, self.make_key(26)),
512
(self.make_key(31), self.make_key(48))],
513
index._parsed_key_map)
515
def test_parsing_non_adjacent_data_trims(self):
516
index = self.make_index(nodes=self.make_nodes(64))
517
result = index._lookup_keys_via_location(
518
[(index._size // 2, ('40', ))])
519
# and the result should be that the key cannot be present, because key is
520
# in the middle of the observed data from a 4K read - the smallest transport
521
# will do today with this api.
522
self.assertEqual([((index._size // 2, ('40', )), False)],
524
# and we should have a parse map that includes the header and the
525
# region that was parsed after trimming.
526
self.assertEqual([(0, 4008), (5046, 8996)], index._parsed_byte_map)
527
self.assertEqual([(None, self.make_key(26)),
528
(self.make_key(31), self.make_key(48))],
529
index._parsed_key_map)
531
def test_parsing_data_handles_parsed_contained_regions(self):
532
# the following patten creates a parsed region that is wholly within a
533
# single result from the readv layer:
534
# .... single-read (readv-minimum-size) ...
535
# which then trims the start and end so the parsed size is < readv
537
# then a dual lookup (or a reference lookup for that matter) which
538
# abuts or overlaps the parsed region on both sides will need to
539
# discard the data in the middle, but parse the end as well.
541
# we test this by doing a single lookup to seed the data, then
542
# a lookup for two keys that are present, and adjacent -
543
# we except both to be found, and the parsed byte map to include the
544
# locations of both keys.
545
index = self.make_index(nodes=self.make_nodes(128))
546
result = index._lookup_keys_via_location(
547
[(index._size // 2, ('40', ))])
548
# and we should have a parse map that includes the header and the
549
# region that was parsed after trimming.
550
self.assertEqual([(0, 4045), (11759, 15707)], index._parsed_byte_map)
551
self.assertEqual([(None, self.make_key(116)),
552
(self.make_key(35), self.make_key(51))],
553
index._parsed_key_map)
554
# now ask for two keys, right before and after the parsed region
555
result = index._lookup_keys_via_location(
556
[(11450, self.make_key(34)), (15707, self.make_key(52))])
558
((11450, self.make_key(34)),
559
(index, self.make_key(34), self.make_value(34))),
560
((15707, self.make_key(52)),
561
(index, self.make_key(52), self.make_value(52))),
564
self.assertEqual([(0, 4045), (9889, 17993)], index._parsed_byte_map)
566
def test_lookup_missing_key_answers_without_io_when_map_permits(self):
567
# generate a big enough index that we only read some of it on a typical
569
index = self.make_index(nodes=self.make_nodes(64))
570
# lookup the keys in the middle of the file
571
result =index._lookup_keys_via_location(
572
[(index._size // 2, ('40', ))])
573
# check the parse map, this determines the test validity
574
self.assertEqual([(0, 4008), (5046, 8996)], index._parsed_byte_map)
575
self.assertEqual([(None, self.make_key(26)),
576
(self.make_key(31), self.make_key(48))],
577
index._parsed_key_map)
578
# reset the transport log
579
del index._transport._activity[:]
580
# now looking up a key in the portion of the file already parsed should
581
# not create a new transport request, and should return False (cannot
582
# be in the index) - even when the byte location we ask for is outside
584
result = index._lookup_keys_via_location(
586
self.assertEqual([((4000, ('40', )), False)],
588
self.assertEqual([], index._transport._activity)
590
def test_lookup_present_key_answers_without_io_when_map_permits(self):
591
# generate a big enough index that we only read some of it on a typical
593
index = self.make_index(nodes=self.make_nodes(64))
594
# lookup the keys in the middle of the file
595
result =index._lookup_keys_via_location(
596
[(index._size // 2, ('40', ))])
597
# check the parse map, this determines the test validity
598
self.assertEqual([(0, 4008), (5046, 8996)], index._parsed_byte_map)
599
self.assertEqual([(None, self.make_key(26)),
600
(self.make_key(31), self.make_key(48))],
601
index._parsed_key_map)
602
# reset the transport log
603
del index._transport._activity[:]
604
# now looking up a key in the portion of the file already parsed should
605
# not create a new transport request, and should return False (cannot
606
# be in the index) - even when the byte location we ask for is outside
609
result = index._lookup_keys_via_location([(4000, self.make_key(40))])
611
[((4000, self.make_key(40)),
612
(index, self.make_key(40), self.make_value(40)))],
614
self.assertEqual([], index._transport._activity)
616
def test_lookup_key_below_probed_area(self):
617
# generate a big enough index that we only read some of it on a typical
619
index = self.make_index(nodes=self.make_nodes(64))
620
# ask for the key in the middle, but a key that is located in the
621
# unparsed region before the middle.
622
result =index._lookup_keys_via_location(
623
[(index._size // 2, ('30', ))])
624
# check the parse map, this determines the test validity
625
self.assertEqual([(0, 4008), (5046, 8996)], index._parsed_byte_map)
626
self.assertEqual([(None, self.make_key(26)),
627
(self.make_key(31), self.make_key(48))],
628
index._parsed_key_map)
629
self.assertEqual([((index._size // 2, ('30', )), -1)],
632
def test_lookup_key_above_probed_area(self):
633
# generate a big enough index that we only read some of it on a typical
635
index = self.make_index(nodes=self.make_nodes(64))
636
# ask for the key in the middle, but a key that is located in the
637
# unparsed region after the middle.
638
result =index._lookup_keys_via_location(
639
[(index._size // 2, ('50', ))])
640
# check the parse map, this determines the test validity
641
self.assertEqual([(0, 4008), (5046, 8996)], index._parsed_byte_map)
642
self.assertEqual([(None, self.make_key(26)),
643
(self.make_key(31), self.make_key(48))],
644
index._parsed_key_map)
645
self.assertEqual([((index._size // 2, ('50', )), +1)],
648
def test_lookup_key_resolves_references(self):
649
# generate a big enough index that we only read some of it on a typical
652
for counter in range(99):
653
nodes.append((self.make_key(counter), self.make_value(counter),
654
((self.make_key(counter + 20),),) ))
655
index = self.make_index(ref_lists=1, nodes=nodes)
656
# lookup a key in the middle that does not exist, so that when we can
657
# check that the referred-to-keys are not accessed automatically.
658
index_size = index._size
659
index_center = index_size // 2
660
result = index._lookup_keys_via_location(
661
[(index_center, ('40', ))])
662
# check the parse map - only the start and middle should have been
664
self.assertEqual([(0, 4027), (10198, 14028)], index._parsed_byte_map)
665
self.assertEqual([(None, self.make_key(17)),
666
(self.make_key(44), self.make_key(5))],
667
index._parsed_key_map)
668
# and check the transport activity likewise.
670
[('readv', 'index', [(index_center, 800), (0, 200)], True,
672
index._transport._activity)
673
# reset the transport log for testing the reference lookup
674
del index._transport._activity[:]
675
# now looking up a key in the portion of the file already parsed should
676
# only perform IO to resolve its key references.
677
result = index._lookup_keys_via_location([(11000, self.make_key(45))])
679
[((11000, self.make_key(45)),
680
(index, self.make_key(45), self.make_value(45),
681
((self.make_key(65),),)))],
683
self.assertEqual([('readv', 'index', [(15093, 800)], True, index_size)],
684
index._transport._activity)
686
def test_lookup_key_can_buffer_all(self):
688
for counter in range(64):
689
nodes.append((self.make_key(counter), self.make_value(counter),
690
((self.make_key(counter + 20),),) ))
691
index = self.make_index(ref_lists=1, nodes=nodes)
692
# lookup a key in the middle that does not exist, so that when we can
693
# check that the referred-to-keys are not accessed automatically.
694
index_size = index._size
695
index_center = index_size // 2
696
result = index._lookup_keys_via_location([(index_center, ('40', ))])
697
# check the parse map - only the start and middle should have been
699
self.assertEqual([(0, 3890), (6444, 10274)], index._parsed_byte_map)
700
self.assertEqual([(None, self.make_key(25)),
701
(self.make_key(37), self.make_key(52))],
702
index._parsed_key_map)
703
# and check the transport activity likewise.
705
[('readv', 'index', [(index_center, 800), (0, 200)], True,
707
index._transport._activity)
708
# reset the transport log for testing the reference lookup
709
del index._transport._activity[:]
710
# now looking up a key in the portion of the file already parsed should
711
# only perform IO to resolve its key references.
712
result = index._lookup_keys_via_location([(7000, self.make_key(40))])
714
[((7000, self.make_key(40)),
715
(index, self.make_key(40), self.make_value(40),
716
((self.make_key(60),),)))],
718
# Resolving the references would have required more data read, and we
719
# are already above the 50% threshold, so it triggered a _buffer_all
720
self.assertEqual([('get', 'index')], index._transport._activity)
344
index = GraphIndex(trans, 'name')
722
346
def test_iter_all_entries_empty(self):
723
347
index = self.make_index()
969
494
index = self.make_index(nodes=[(('key', ), 'value', ())])
972
# XXX: external_references tests are duplicated in test_btree_index. We
973
# probably should have per_graph_index tests...
974
def test_external_references_no_refs(self):
975
index = self.make_index(ref_lists=0, nodes=[])
976
self.assertRaises(ValueError, index.external_references, 0)
978
def test_external_references_no_results(self):
979
index = self.make_index(ref_lists=1, nodes=[
980
(('key',), 'value', ([],))])
981
self.assertEqual(set(), index.external_references(0))
983
def test_external_references_missing_ref(self):
984
missing_key = ('missing',)
985
index = self.make_index(ref_lists=1, nodes=[
986
(('key',), 'value', ([missing_key],))])
987
self.assertEqual(set([missing_key]), index.external_references(0))
989
def test_external_references_multiple_ref_lists(self):
990
missing_key = ('missing',)
991
index = self.make_index(ref_lists=2, nodes=[
992
(('key',), 'value', ([], [missing_key]))])
993
self.assertEqual(set([]), index.external_references(0))
994
self.assertEqual(set([missing_key]), index.external_references(1))
996
def test_external_references_two_records(self):
997
index = self.make_index(ref_lists=1, nodes=[
998
(('key-1',), 'value', ([('key-2',)],)),
999
(('key-2',), 'value', ([],)),
1001
self.assertEqual(set([]), index.external_references(0))
1003
def test__find_ancestors(self):
1006
index = self.make_index(ref_lists=1, key_elements=1, nodes=[
1007
(key1, 'value', ([key2],)),
1008
(key2, 'value', ([],)),
1011
missing_keys = set()
1012
search_keys = index._find_ancestors([key1], 0, parent_map, missing_keys)
1013
self.assertEqual({key1: (key2,)}, parent_map)
1014
self.assertEqual(set(), missing_keys)
1015
self.assertEqual(set([key2]), search_keys)
1016
search_keys = index._find_ancestors(search_keys, 0, parent_map,
1018
self.assertEqual({key1: (key2,), key2: ()}, parent_map)
1019
self.assertEqual(set(), missing_keys)
1020
self.assertEqual(set(), search_keys)
1022
def test__find_ancestors_w_missing(self):
1026
index = self.make_index(ref_lists=1, key_elements=1, nodes=[
1027
(key1, 'value', ([key2],)),
1028
(key2, 'value', ([],)),
1031
missing_keys = set()
1032
search_keys = index._find_ancestors([key2, key3], 0, parent_map,
1034
self.assertEqual({key2: ()}, parent_map)
1035
self.assertEqual(set([key3]), missing_keys)
1036
self.assertEqual(set(), search_keys)
1038
def test__find_ancestors_dont_search_known(self):
1042
index = self.make_index(ref_lists=1, key_elements=1, nodes=[
1043
(key1, 'value', ([key2],)),
1044
(key2, 'value', ([key3],)),
1045
(key3, 'value', ([],)),
1047
# We already know about key2, so we won't try to search for key3
1048
parent_map = {key2: (key3,)}
1049
missing_keys = set()
1050
search_keys = index._find_ancestors([key1], 0, parent_map,
1052
self.assertEqual({key1: (key2,), key2: (key3,)}, parent_map)
1053
self.assertEqual(set(), missing_keys)
1054
self.assertEqual(set(), search_keys)
1056
def test_supports_unlimited_cache(self):
1057
builder = index.GraphIndexBuilder(0, key_elements=1)
1058
stream = builder.finish()
1059
trans = transport.get_transport(self.get_url())
1060
size = trans.put_file('index', stream)
1061
# It doesn't matter what unlimited_cache does here, just that it can be
1063
idx = index.GraphIndex(trans, 'index', size, unlimited_cache=True)
1066
class TestCombinedGraphIndex(tests.TestCaseWithMemoryTransport):
498
class TestCombinedGraphIndex(TestCaseWithMemoryTransport):
1068
500
def make_index(self, name, ref_lists=0, key_elements=1, nodes=[]):
1069
builder = index.GraphIndexBuilder(ref_lists, key_elements=key_elements)
1070
for key, value, references in nodes:
1071
builder.add_node(key, value, references)
501
builder = GraphIndexBuilder(ref_lists, key_elements=key_elements)
502
for node, value, references in nodes:
503
builder.add_node(node, value, references)
1072
504
stream = builder.finish()
1073
505
trans = self.get_transport()
1074
size = trans.put_file(name, stream)
1075
return index.GraphIndex(trans, name, size)
1077
def make_combined_index_with_missing(self, missing=['1', '2']):
1078
"""Create a CombinedGraphIndex which will have missing indexes.
1080
This creates a CGI which thinks it has 2 indexes, however they have
1081
been deleted. If CGI._reload_func() is called, then it will repopulate
1084
:param missing: The underlying indexes to delete
1085
:return: (CombinedGraphIndex, reload_counter)
1087
idx1 = self.make_index('1', nodes=[(('1',), '', ())])
1088
idx2 = self.make_index('2', nodes=[(('2',), '', ())])
1089
idx3 = self.make_index('3', nodes=[
1093
# total_reloads, num_changed, num_unchanged
1094
reload_counter = [0, 0, 0]
1096
reload_counter[0] += 1
1097
new_indices = [idx3]
1098
if idx._indices == new_indices:
1099
reload_counter[2] += 1
1101
reload_counter[1] += 1
1102
idx._indices[:] = new_indices
1104
idx = index.CombinedGraphIndex([idx1, idx2], reload_func=reload)
1105
trans = self.get_transport()
1106
for fname in missing:
1108
return idx, reload_counter
506
trans.put_file(name, stream)
507
return GraphIndex(trans, name)
1110
509
def test_open_missing_index_no_error(self):
1111
510
trans = self.get_transport()
1112
idx1 = index.GraphIndex(trans, 'missing', 100)
1113
idx = index.CombinedGraphIndex([idx1])
511
index1 = GraphIndex(trans, 'missing')
512
index = CombinedGraphIndex([index1])
1115
514
def test_add_index(self):
1116
idx = index.CombinedGraphIndex([])
1117
idx1 = self.make_index('name', 0, nodes=[(('key', ), '', ())])
1118
idx.insert_index(0, idx1)
1119
self.assertEqual([(idx1, ('key', ), '')],
1120
list(idx.iter_all_entries()))
1122
def test_clear_cache(self):
1125
class ClearCacheProxy(object):
1127
def __init__(self, index):
1130
def __getattr__(self, name):
1131
return getattr(self._index)
1133
def clear_cache(self):
1134
log.append(self._index)
1135
return self._index.clear_cache()
1137
idx = index.CombinedGraphIndex([])
1138
idx1 = self.make_index('name', 0, nodes=[(('key', ), '', ())])
1139
idx.insert_index(0, ClearCacheProxy(idx1))
1140
idx2 = self.make_index('name', 0, nodes=[(('key', ), '', ())])
1141
idx.insert_index(1, ClearCacheProxy(idx2))
1142
# CombinedGraphIndex should call 'clear_cache()' on all children
1144
self.assertEqual(sorted([idx1, idx2]), sorted(log))
515
index = CombinedGraphIndex([])
516
index1 = self.make_index('name', 0, nodes=[(('key', ), '', ())])
517
index.insert_index(0, index1)
518
self.assertEqual([(index1, ('key', ), '')], list(index.iter_all_entries()))
1146
520
def test_iter_all_entries_empty(self):
1147
idx = index.CombinedGraphIndex([])
1148
self.assertEqual([], list(idx.iter_all_entries()))
521
index = CombinedGraphIndex([])
522
self.assertEqual([], list(index.iter_all_entries()))
1150
524
def test_iter_all_entries_children_empty(self):
1151
idx1 = self.make_index('name')
1152
idx = index.CombinedGraphIndex([idx1])
1153
self.assertEqual([], list(idx.iter_all_entries()))
525
index1 = self.make_index('name')
526
index = CombinedGraphIndex([index1])
527
self.assertEqual([], list(index.iter_all_entries()))
1155
529
def test_iter_all_entries_simple(self):
1156
idx1 = self.make_index('name', nodes=[(('name', ), 'data', ())])
1157
idx = index.CombinedGraphIndex([idx1])
1158
self.assertEqual([(idx1, ('name', ), 'data')],
1159
list(idx.iter_all_entries()))
530
index1 = self.make_index('name', nodes=[(('name', ), 'data', ())])
531
index = CombinedGraphIndex([index1])
532
self.assertEqual([(index1, ('name', ), 'data')],
533
list(index.iter_all_entries()))
1161
535
def test_iter_all_entries_two_indices(self):
1162
idx1 = self.make_index('name1', nodes=[(('name', ), 'data', ())])
1163
idx2 = self.make_index('name2', nodes=[(('2', ), '', ())])
1164
idx = index.CombinedGraphIndex([idx1, idx2])
1165
self.assertEqual([(idx1, ('name', ), 'data'),
1166
(idx2, ('2', ), '')],
1167
list(idx.iter_all_entries()))
536
index1 = self.make_index('name1', nodes=[(('name', ), 'data', ())])
537
index2 = self.make_index('name2', nodes=[(('2', ), '', ())])
538
index = CombinedGraphIndex([index1, index2])
539
self.assertEqual([(index1, ('name', ), 'data'),
540
(index2, ('2', ), '')],
541
list(index.iter_all_entries()))
1169
543
def test_iter_entries_two_indices_dup_key(self):
1170
idx1 = self.make_index('name1', nodes=[(('name', ), 'data', ())])
1171
idx2 = self.make_index('name2', nodes=[(('name', ), 'data', ())])
1172
idx = index.CombinedGraphIndex([idx1, idx2])
1173
self.assertEqual([(idx1, ('name', ), 'data')],
1174
list(idx.iter_entries([('name', )])))
544
index1 = self.make_index('name1', nodes=[(('name', ), 'data', ())])
545
index2 = self.make_index('name2', nodes=[(('name', ), 'data', ())])
546
index = CombinedGraphIndex([index1, index2])
547
self.assertEqual([(index1, ('name', ), 'data')],
548
list(index.iter_entries([('name', )])))
1176
550
def test_iter_all_entries_two_indices_dup_key(self):
1177
idx1 = self.make_index('name1', nodes=[(('name', ), 'data', ())])
1178
idx2 = self.make_index('name2', nodes=[(('name', ), 'data', ())])
1179
idx = index.CombinedGraphIndex([idx1, idx2])
1180
self.assertEqual([(idx1, ('name', ), 'data')],
1181
list(idx.iter_all_entries()))
551
index1 = self.make_index('name1', nodes=[(('name', ), 'data', ())])
552
index2 = self.make_index('name2', nodes=[(('name', ), 'data', ())])
553
index = CombinedGraphIndex([index1, index2])
554
self.assertEqual([(index1, ('name', ), 'data')],
555
list(index.iter_all_entries()))
1183
557
def test_iter_key_prefix_2_key_element_refs(self):
1184
idx1 = self.make_index('1', 1, key_elements=2, nodes=[
1185
(('name', 'fin1'), 'data', ([('ref', 'erence')], ))])
1186
idx2 = self.make_index('2', 1, key_elements=2, nodes=[
1187
(('name', 'fin2'), 'beta', ([], )),
1188
(('ref', 'erence'), 'refdata', ([], ))])
1189
idx = index.CombinedGraphIndex([idx1, idx2])
1190
self.assertEqual(set([(idx1, ('name', 'fin1'), 'data',
1191
((('ref', 'erence'),),)),
1192
(idx2, ('ref', 'erence'), 'refdata', ((), ))]),
1193
set(idx.iter_entries_prefix([('name', 'fin1'),
1194
('ref', 'erence')])))
1195
self.assertEqual(set([(idx1, ('name', 'fin1'), 'data',
1196
((('ref', 'erence'),),)),
1197
(idx2, ('name', 'fin2'), 'beta', ((), ))]),
1198
set(idx.iter_entries_prefix([('name', None)])))
558
index1 = self.make_index('1', 1, key_elements=2, nodes=[
559
(('name', 'fin1'), 'data', ([('ref', 'erence')], ))])
560
index2 = self.make_index('2', 1, key_elements=2, nodes=[
561
(('name', 'fin2'), 'beta', ([], )),
562
(('ref', 'erence'), 'refdata', ([], ))])
563
index = CombinedGraphIndex([index1, index2])
564
self.assertEqual(set([(index1, ('name', 'fin1'), 'data', ((('ref', 'erence'),),)),
565
(index2, ('ref', 'erence'), 'refdata', ((), ))]),
566
set(index.iter_entries_prefix([('name', 'fin1'), ('ref', 'erence')])))
567
self.assertEqual(set([(index1, ('name', 'fin1'), 'data', ((('ref', 'erence'),),)),
568
(index2, ('name', 'fin2'), 'beta', ((), ))]),
569
set(index.iter_entries_prefix([('name', None)])))
1200
571
def test_iter_nothing_empty(self):
1201
idx = index.CombinedGraphIndex([])
1202
self.assertEqual([], list(idx.iter_entries([])))
572
index = CombinedGraphIndex([])
573
self.assertEqual([], list(index.iter_entries([])))
1204
575
def test_iter_nothing_children_empty(self):
1205
idx1 = self.make_index('name')
1206
idx = index.CombinedGraphIndex([idx1])
1207
self.assertEqual([], list(idx.iter_entries([])))
576
index1 = self.make_index('name')
577
index = CombinedGraphIndex([index1])
578
self.assertEqual([], list(index.iter_entries([])))
1209
580
def test_iter_all_keys(self):
1210
idx1 = self.make_index('1', 1, nodes=[(('name', ), 'data',
1212
idx2 = self.make_index('2', 1, nodes=[(('ref', ), 'refdata', ((), ))])
1213
idx = index.CombinedGraphIndex([idx1, idx2])
1214
self.assertEqual(set([(idx1, ('name', ), 'data', ((('ref', ), ), )),
1215
(idx2, ('ref', ), 'refdata', ((), ))]),
1216
set(idx.iter_entries([('name', ), ('ref', )])))
581
index1 = self.make_index('1', 1, nodes=[
582
(('name', ), 'data', ([('ref', )], ))])
583
index2 = self.make_index('2', 1, nodes=[
584
(('ref', ), 'refdata', ((), ))])
585
index = CombinedGraphIndex([index1, index2])
586
self.assertEqual(set([(index1, ('name', ), 'data', ((('ref', ), ), )),
587
(index2, ('ref', ), 'refdata', ((), ))]),
588
set(index.iter_entries([('name', ), ('ref', )])))
1218
590
def test_iter_all_keys_dup_entry(self):
1219
idx1 = self.make_index('1', 1, nodes=[(('name', ), 'data',
1221
(('ref', ), 'refdata', ([], ))])
1222
idx2 = self.make_index('2', 1, nodes=[(('ref', ), 'refdata', ([], ))])
1223
idx = index.CombinedGraphIndex([idx1, idx2])
1224
self.assertEqual(set([(idx1, ('name', ), 'data', ((('ref',),),)),
1225
(idx1, ('ref', ), 'refdata', ((), ))]),
1226
set(idx.iter_entries([('name', ), ('ref', )])))
591
index1 = self.make_index('1', 1, nodes=[
592
(('name', ), 'data', ([('ref', )], )),
593
(('ref', ), 'refdata', ([], ))])
594
index2 = self.make_index('2', 1, nodes=[
595
(('ref', ), 'refdata', ([], ))])
596
index = CombinedGraphIndex([index1, index2])
597
self.assertEqual(set([(index1, ('name', ), 'data', ((('ref',),),)),
598
(index1, ('ref', ), 'refdata', ((), ))]),
599
set(index.iter_entries([('name', ), ('ref', )])))
1228
601
def test_iter_missing_entry_empty(self):
1229
idx = index.CombinedGraphIndex([])
1230
self.assertEqual([], list(idx.iter_entries([('a', )])))
602
index = CombinedGraphIndex([])
603
self.assertEqual([], list(index.iter_entries([('a', )])))
1232
605
def test_iter_missing_entry_one_index(self):
1233
idx1 = self.make_index('1')
1234
idx = index.CombinedGraphIndex([idx1])
1235
self.assertEqual([], list(idx.iter_entries([('a', )])))
606
index1 = self.make_index('1')
607
index = CombinedGraphIndex([index1])
608
self.assertEqual([], list(index.iter_entries([('a', )])))
1237
610
def test_iter_missing_entry_two_index(self):
1238
idx1 = self.make_index('1')
1239
idx2 = self.make_index('2')
1240
idx = index.CombinedGraphIndex([idx1, idx2])
1241
self.assertEqual([], list(idx.iter_entries([('a', )])))
611
index1 = self.make_index('1')
612
index2 = self.make_index('2')
613
index = CombinedGraphIndex([index1, index2])
614
self.assertEqual([], list(index.iter_entries([('a', )])))
1243
616
def test_iter_entry_present_one_index_only(self):
1244
idx1 = self.make_index('1', nodes=[(('key', ), '', ())])
1245
idx2 = self.make_index('2', nodes=[])
1246
idx = index.CombinedGraphIndex([idx1, idx2])
1247
self.assertEqual([(idx1, ('key', ), '')],
1248
list(idx.iter_entries([('key', )])))
617
index1 = self.make_index('1', nodes=[(('key', ), '', ())])
618
index2 = self.make_index('2', nodes=[])
619
index = CombinedGraphIndex([index1, index2])
620
self.assertEqual([(index1, ('key', ), '')],
621
list(index.iter_entries([('key', )])))
1249
622
# and in the other direction
1250
idx = index.CombinedGraphIndex([idx2, idx1])
1251
self.assertEqual([(idx1, ('key', ), '')],
1252
list(idx.iter_entries([('key', )])))
1254
def test_key_count_empty(self):
1255
idx1 = self.make_index('1', nodes=[])
1256
idx2 = self.make_index('2', nodes=[])
1257
idx = index.CombinedGraphIndex([idx1, idx2])
1258
self.assertEqual(0, idx.key_count())
1260
def test_key_count_sums_index_keys(self):
1261
idx1 = self.make_index('1', nodes=[
1264
idx2 = self.make_index('2', nodes=[(('1',), '', ())])
1265
idx = index.CombinedGraphIndex([idx1, idx2])
1266
self.assertEqual(3, idx.key_count())
623
index = CombinedGraphIndex([index2, index1])
624
self.assertEqual([(index1, ('key', ), '')],
625
list(index.iter_entries([('key', )])))
1268
627
def test_validate_bad_child_index_errors(self):
1269
628
trans = self.get_transport()
1270
629
trans.put_bytes('name', "not an index\n")
1271
idx1 = index.GraphIndex(trans, 'name', 13)
1272
idx = index.CombinedGraphIndex([idx1])
1273
self.assertRaises(errors.BadIndexFormatSignature, idx.validate)
630
index1 = GraphIndex(trans, 'name')
631
index = CombinedGraphIndex([index1])
632
self.assertRaises(errors.BadIndexFormatSignature, index.validate)
1275
634
def test_validate_empty(self):
1276
idx = index.CombinedGraphIndex([])
1279
def test_key_count_reloads(self):
1280
idx, reload_counter = self.make_combined_index_with_missing()
1281
self.assertEqual(2, idx.key_count())
1282
self.assertEqual([1, 1, 0], reload_counter)
1284
def test_key_count_no_reload(self):
1285
idx, reload_counter = self.make_combined_index_with_missing()
1286
idx._reload_func = None
1287
# Without a _reload_func we just raise the exception
1288
self.assertRaises(errors.NoSuchFile, idx.key_count)
1290
def test_key_count_reloads_and_fails(self):
1291
# We have deleted all underlying indexes, so we will try to reload, but
1292
# still fail. This is mostly to test we don't get stuck in an infinite
1293
# loop trying to reload
1294
idx, reload_counter = self.make_combined_index_with_missing(
1296
self.assertRaises(errors.NoSuchFile, idx.key_count)
1297
self.assertEqual([2, 1, 1], reload_counter)
1299
def test_iter_entries_reloads(self):
1300
index, reload_counter = self.make_combined_index_with_missing()
1301
result = list(index.iter_entries([('1',), ('2',), ('3',)]))
1302
index3 = index._indices[0]
1303
self.assertEqual([(index3, ('1',), ''), (index3, ('2',), '')],
1305
self.assertEqual([1, 1, 0], reload_counter)
1307
def test_iter_entries_reloads_midway(self):
1308
# The first index still looks present, so we get interrupted mid-way
1310
index, reload_counter = self.make_combined_index_with_missing(['2'])
1311
index1, index2 = index._indices
1312
result = list(index.iter_entries([('1',), ('2',), ('3',)]))
1313
index3 = index._indices[0]
1314
# We had already yielded '1', so we just go on to the next, we should
1315
# not yield '1' twice.
1316
self.assertEqual([(index1, ('1',), ''), (index3, ('2',), '')],
1318
self.assertEqual([1, 1, 0], reload_counter)
1320
def test_iter_entries_no_reload(self):
1321
index, reload_counter = self.make_combined_index_with_missing()
1322
index._reload_func = None
1323
# Without a _reload_func we just raise the exception
1324
self.assertListRaises(errors.NoSuchFile, index.iter_entries, [('3',)])
1326
def test_iter_entries_reloads_and_fails(self):
1327
index, reload_counter = self.make_combined_index_with_missing(
1329
self.assertListRaises(errors.NoSuchFile, index.iter_entries, [('3',)])
1330
self.assertEqual([2, 1, 1], reload_counter)
1332
def test_iter_all_entries_reloads(self):
1333
index, reload_counter = self.make_combined_index_with_missing()
1334
result = list(index.iter_all_entries())
1335
index3 = index._indices[0]
1336
self.assertEqual([(index3, ('1',), ''), (index3, ('2',), '')],
1338
self.assertEqual([1, 1, 0], reload_counter)
1340
def test_iter_all_entries_reloads_midway(self):
1341
index, reload_counter = self.make_combined_index_with_missing(['2'])
1342
index1, index2 = index._indices
1343
result = list(index.iter_all_entries())
1344
index3 = index._indices[0]
1345
# We had already yielded '1', so we just go on to the next, we should
1346
# not yield '1' twice.
1347
self.assertEqual([(index1, ('1',), ''), (index3, ('2',), '')],
1349
self.assertEqual([1, 1, 0], reload_counter)
1351
def test_iter_all_entries_no_reload(self):
1352
index, reload_counter = self.make_combined_index_with_missing()
1353
index._reload_func = None
1354
self.assertListRaises(errors.NoSuchFile, index.iter_all_entries)
1356
def test_iter_all_entries_reloads_and_fails(self):
1357
index, reload_counter = self.make_combined_index_with_missing(
1359
self.assertListRaises(errors.NoSuchFile, index.iter_all_entries)
1361
def test_iter_entries_prefix_reloads(self):
1362
index, reload_counter = self.make_combined_index_with_missing()
1363
result = list(index.iter_entries_prefix([('1',)]))
1364
index3 = index._indices[0]
1365
self.assertEqual([(index3, ('1',), '')], result)
1366
self.assertEqual([1, 1, 0], reload_counter)
1368
def test_iter_entries_prefix_reloads_midway(self):
1369
index, reload_counter = self.make_combined_index_with_missing(['2'])
1370
index1, index2 = index._indices
1371
result = list(index.iter_entries_prefix([('1',)]))
1372
index3 = index._indices[0]
1373
# We had already yielded '1', so we just go on to the next, we should
1374
# not yield '1' twice.
1375
self.assertEqual([(index1, ('1',), '')], result)
1376
self.assertEqual([1, 1, 0], reload_counter)
1378
def test_iter_entries_prefix_no_reload(self):
1379
index, reload_counter = self.make_combined_index_with_missing()
1380
index._reload_func = None
1381
self.assertListRaises(errors.NoSuchFile, index.iter_entries_prefix,
1384
def test_iter_entries_prefix_reloads_and_fails(self):
1385
index, reload_counter = self.make_combined_index_with_missing(
1387
self.assertListRaises(errors.NoSuchFile, index.iter_entries_prefix,
1391
def make_index_with_simple_nodes(self, name, num_nodes=1):
1392
"""Make an index named after 'name', with keys named after 'name' too.
1394
Nodes will have a value of '' and no references.
1397
(('index-%s-key-%s' % (name, n),), '', ())
1398
for n in range(1, num_nodes+1)]
1399
return self.make_index('index-%s' % name, 0, nodes=nodes)
1401
def test_reorder_after_iter_entries(self):
1402
# Four indices: [key1] in idx1, [key2,key3] in idx2, [] in idx3,
1404
idx = index.CombinedGraphIndex([])
1405
idx.insert_index(0, self.make_index_with_simple_nodes('1'), '1')
1406
idx.insert_index(1, self.make_index_with_simple_nodes('2'), '2')
1407
idx.insert_index(2, self.make_index_with_simple_nodes('3'), '3')
1408
idx.insert_index(3, self.make_index_with_simple_nodes('4'), '4')
1409
idx1, idx2, idx3, idx4 = idx._indices
1410
# Query a key from idx4 and idx2.
1411
self.assertLength(2, list(idx.iter_entries(
1412
[('index-4-key-1',), ('index-2-key-1',)])))
1413
# Now idx2 and idx4 should be moved to the front (and idx1 should
1414
# still be before idx3).
1415
self.assertEqual([idx2, idx4, idx1, idx3], idx._indices)
1416
self.assertEqual(['2', '4', '1', '3'], idx._index_names)
1418
def test_reorder_propagates_to_siblings(self):
1419
# Two CombinedGraphIndex objects, with the same number of indicies with
1421
cgi1 = index.CombinedGraphIndex([])
1422
cgi2 = index.CombinedGraphIndex([])
1423
cgi1.insert_index(0, self.make_index_with_simple_nodes('1-1'), 'one')
1424
cgi1.insert_index(1, self.make_index_with_simple_nodes('1-2'), 'two')
1425
cgi2.insert_index(0, self.make_index_with_simple_nodes('2-1'), 'one')
1426
cgi2.insert_index(1, self.make_index_with_simple_nodes('2-2'), 'two')
1427
index2_1, index2_2 = cgi2._indices
1428
cgi1.set_sibling_indices([cgi2])
1429
# Trigger a reordering in cgi1. cgi2 will be reordered as well.
1430
list(cgi1.iter_entries([('index-1-2-key-1',)]))
1431
self.assertEqual([index2_2, index2_1], cgi2._indices)
1432
self.assertEqual(['two', 'one'], cgi2._index_names)
1434
def test_validate_reloads(self):
1435
idx, reload_counter = self.make_combined_index_with_missing()
1437
self.assertEqual([1, 1, 0], reload_counter)
1439
def test_validate_reloads_midway(self):
1440
idx, reload_counter = self.make_combined_index_with_missing(['2'])
1443
def test_validate_no_reload(self):
1444
idx, reload_counter = self.make_combined_index_with_missing()
1445
idx._reload_func = None
1446
self.assertRaises(errors.NoSuchFile, idx.validate)
1448
def test_validate_reloads_and_fails(self):
1449
idx, reload_counter = self.make_combined_index_with_missing(
1451
self.assertRaises(errors.NoSuchFile, idx.validate)
1453
def test_find_ancestors_across_indexes(self):
1458
index1 = self.make_index('12', ref_lists=1, nodes=[
1459
(key1, 'value', ([],)),
1460
(key2, 'value', ([key1],)),
1462
index2 = self.make_index('34', ref_lists=1, nodes=[
1463
(key3, 'value', ([key2],)),
1464
(key4, 'value', ([key3],)),
1466
c_index = index.CombinedGraphIndex([index1, index2])
1467
parent_map, missing_keys = c_index.find_ancestry([key1], 0)
1468
self.assertEqual({key1: ()}, parent_map)
1469
self.assertEqual(set(), missing_keys)
1470
# Now look for a key from index2 which requires us to find the key in
1471
# the second index, and then continue searching for parents in the
1473
parent_map, missing_keys = c_index.find_ancestry([key3], 0)
1474
self.assertEqual({key1: (), key2: (key1,), key3: (key2,)}, parent_map)
1475
self.assertEqual(set(), missing_keys)
1477
def test_find_ancestors_missing_keys(self):
1482
index1 = self.make_index('12', ref_lists=1, nodes=[
1483
(key1, 'value', ([],)),
1484
(key2, 'value', ([key1],)),
1486
index2 = self.make_index('34', ref_lists=1, nodes=[
1487
(key3, 'value', ([key2],)),
1489
c_index = index.CombinedGraphIndex([index1, index2])
1490
# Searching for a key which is actually not present at all should
1491
# eventually converge
1492
parent_map, missing_keys = c_index.find_ancestry([key4], 0)
1493
self.assertEqual({}, parent_map)
1494
self.assertEqual(set([key4]), missing_keys)
1496
def test_find_ancestors_no_indexes(self):
1497
c_index = index.CombinedGraphIndex([])
1499
parent_map, missing_keys = c_index.find_ancestry([key1], 0)
1500
self.assertEqual({}, parent_map)
1501
self.assertEqual(set([key1]), missing_keys)
1503
def test_find_ancestors_ghost_parent(self):
1508
index1 = self.make_index('12', ref_lists=1, nodes=[
1509
(key1, 'value', ([],)),
1510
(key2, 'value', ([key1],)),
1512
index2 = self.make_index('34', ref_lists=1, nodes=[
1513
(key4, 'value', ([key2, key3],)),
1515
c_index = index.CombinedGraphIndex([index1, index2])
1516
# Searching for a key which is actually not present at all should
1517
# eventually converge
1518
parent_map, missing_keys = c_index.find_ancestry([key4], 0)
1519
self.assertEqual({key4: (key2, key3), key2: (key1,), key1: ()},
1521
self.assertEqual(set([key3]), missing_keys)
1523
def test__find_ancestors_empty_index(self):
1524
idx = self.make_index('test', ref_lists=1, key_elements=1, nodes=[])
1526
missing_keys = set()
1527
search_keys = idx._find_ancestors([('one',), ('two',)], 0, parent_map,
1529
self.assertEqual(set(), search_keys)
1530
self.assertEqual({}, parent_map)
1531
self.assertEqual(set([('one',), ('two',)]), missing_keys)
1534
class TestInMemoryGraphIndex(tests.TestCaseWithMemoryTransport):
635
index = CombinedGraphIndex([])
639
class TestInMemoryGraphIndex(TestCaseWithMemoryTransport):
1536
641
def make_index(self, ref_lists=0, key_elements=1, nodes=[]):
1537
result = index.InMemoryGraphIndex(ref_lists, key_elements=key_elements)
642
result = InMemoryGraphIndex(ref_lists, key_elements=key_elements)
1538
643
result.add_nodes(nodes)