644
659
# becuase without a 3-level index, we don't have any internal
646
661
self.assertEqual(internal_node_pre_clear,
647
index._internal_node_cache.keys())
662
set(index._internal_node_cache))
648
663
self.assertEqual(0, len(index._leaf_node_cache))
650
665
def test_trivial_constructor(self):
651
transport = get_transport('trace+' + self.get_url(''))
652
index = btree_index.BTreeGraphIndex(transport, 'index', None)
666
t = transport.get_transport_from_url('trace+' + self.get_url(''))
667
index = btree_index.BTreeGraphIndex(t, 'index', None)
653
668
# Checks the page size at load, but that isn't logged yet.
654
self.assertEqual([], transport._activity)
669
self.assertEqual([], t._activity)
656
671
def test_with_size_constructor(self):
657
transport = get_transport('trace+' + self.get_url(''))
658
index = btree_index.BTreeGraphIndex(transport, 'index', 1)
672
t = transport.get_transport_from_url('trace+' + self.get_url(''))
673
index = btree_index.BTreeGraphIndex(t, 'index', 1)
659
674
# Checks the page size at load, but that isn't logged yet.
660
self.assertEqual([], transport._activity)
675
self.assertEqual([], t._activity)
662
677
def test_empty_key_count_no_size(self):
663
678
builder = btree_index.BTreeBuilder(key_elements=1, reference_lists=0)
664
transport = get_transport('trace+' + self.get_url(''))
665
transport.put_file('index', builder.finish())
666
index = btree_index.BTreeGraphIndex(transport, 'index', None)
667
del transport._activity[:]
668
self.assertEqual([], transport._activity)
679
t = transport.get_transport_from_url('trace+' + self.get_url(''))
680
t.put_file('index', builder.finish())
681
index = btree_index.BTreeGraphIndex(t, 'index', None)
683
self.assertEqual([], t._activity)
669
684
self.assertEqual(0, index.key_count())
670
685
# The entire index should have been requested (as we generally have the
671
686
# size available, and doing many small readvs is inappropriate).
672
687
# We can't tell how much was actually read here, but - check the code.
673
self.assertEqual([('get', 'index')], transport._activity)
688
self.assertEqual([('get', 'index')], t._activity)
675
690
def test_empty_key_count(self):
676
691
builder = btree_index.BTreeBuilder(key_elements=1, reference_lists=0)
677
transport = get_transport('trace+' + self.get_url(''))
678
size = transport.put_file('index', builder.finish())
692
t = transport.get_transport_from_url('trace+' + self.get_url(''))
693
size = t.put_file('index', builder.finish())
679
694
self.assertEqual(72, size)
680
index = btree_index.BTreeGraphIndex(transport, 'index', size)
681
del transport._activity[:]
682
self.assertEqual([], transport._activity)
695
index = btree_index.BTreeGraphIndex(t, 'index', size)
697
self.assertEqual([], t._activity)
683
698
self.assertEqual(0, index.key_count())
684
699
# The entire index should have been read, as 4K > size
685
700
self.assertEqual([('readv', 'index', [(0, 72)], False, None)],
688
703
def test_non_empty_key_count_2_2(self):
689
704
builder = btree_index.BTreeBuilder(key_elements=2, reference_lists=2)
690
705
nodes = self.make_nodes(35, 2, 2)
691
706
for node in nodes:
692
707
builder.add_node(*node)
693
transport = get_transport('trace+' + self.get_url(''))
694
size = transport.put_file('index', builder.finish())
695
index = btree_index.BTreeGraphIndex(transport, 'index', size)
696
del transport._activity[:]
697
self.assertEqual([], transport._activity)
708
t = transport.get_transport_from_url('trace+' + self.get_url(''))
709
size = t.put_file('index', builder.finish())
710
index = btree_index.BTreeGraphIndex(t, 'index', size)
712
self.assertEqual([], t._activity)
698
713
self.assertEqual(70, index.key_count())
699
714
# The entire index should have been read, as it is one page long.
700
715
self.assertEqual([('readv', 'index', [(0, size)], False, None)],
702
self.assertEqual(1173, size)
717
self.assertEqualApproxCompressed(1173, size)
704
719
def test_with_offset_no_size(self):
705
720
index = self.make_index_with_offset(key_elements=1, ref_lists=1,
736
751
index.key_count()
737
752
num_pages = index._row_offsets[-1]
738
753
# Reopen with a traced transport and no size
739
trans = get_transport('trace+' + self.get_url())
754
trans = transport.get_transport_from_url('trace+' + self.get_url())
740
755
index = btree_index.BTreeGraphIndex(trans, 'index', None)
741
756
del trans._activity[:]
742
757
nodes = dict(index._read_nodes([0]))
743
self.assertEqual(range(num_pages), nodes.keys())
758
self.assertEqual(list(range(num_pages)), sorted(nodes))
745
760
def test_2_levels_key_count_2_2(self):
746
761
builder = btree_index.BTreeBuilder(key_elements=2, reference_lists=2)
747
762
nodes = self.make_nodes(160, 2, 2)
748
763
for node in nodes:
749
764
builder.add_node(*node)
750
transport = get_transport('trace+' + self.get_url(''))
751
size = transport.put_file('index', builder.finish())
752
self.assertEqual(17692, size)
753
index = btree_index.BTreeGraphIndex(transport, 'index', size)
754
del transport._activity[:]
755
self.assertEqual([], transport._activity)
765
t = transport.get_transport_from_url('trace+' + self.get_url(''))
766
size = t.put_file('index', builder.finish())
767
self.assertEqualApproxCompressed(17692, size)
768
index = btree_index.BTreeGraphIndex(t, 'index', size)
770
self.assertEqual([], t._activity)
756
771
self.assertEqual(320, index.key_count())
757
772
# The entire index should not have been read.
758
773
self.assertEqual([('readv', 'index', [(0, 4096)], False, None)],
761
776
def test_validate_one_page(self):
762
777
builder = btree_index.BTreeBuilder(key_elements=2, reference_lists=2)
763
778
nodes = self.make_nodes(45, 2, 2)
764
779
for node in nodes:
765
780
builder.add_node(*node)
766
transport = get_transport('trace+' + self.get_url(''))
767
size = transport.put_file('index', builder.finish())
768
index = btree_index.BTreeGraphIndex(transport, 'index', size)
769
del transport._activity[:]
770
self.assertEqual([], transport._activity)
781
t = transport.get_transport_from_url('trace+' + self.get_url(''))
782
size = t.put_file('index', builder.finish())
783
index = btree_index.BTreeGraphIndex(t, 'index', size)
785
self.assertEqual([], t._activity)
772
787
# The entire index should have been read linearly.
773
788
self.assertEqual([('readv', 'index', [(0, size)], False, None)],
775
self.assertEqual(1488, size)
790
self.assertEqualApproxCompressed(1488, size)
777
792
def test_validate_two_pages(self):
778
793
builder = btree_index.BTreeBuilder(key_elements=2, reference_lists=2)
779
794
nodes = self.make_nodes(80, 2, 2)
780
795
for node in nodes:
781
796
builder.add_node(*node)
782
transport = get_transport('trace+' + self.get_url(''))
783
size = transport.put_file('index', builder.finish())
797
t = transport.get_transport_from_url('trace+' + self.get_url(''))
798
size = t.put_file('index', builder.finish())
784
799
# Root page, 2 leaf pages
785
self.assertEqual(9339, size)
786
index = btree_index.BTreeGraphIndex(transport, 'index', size)
787
del transport._activity[:]
788
self.assertEqual([], transport._activity)
800
self.assertEqualApproxCompressed(9339, size)
801
index = btree_index.BTreeGraphIndex(t, 'index', size)
803
self.assertEqual([], t._activity)
805
rem = size - 8192 # Number of remaining bytes after second block
790
806
# The entire index should have been read linearly.
791
self.assertEqual([('readv', 'index', [(0, 4096)], False, None),
792
('readv', 'index', [(4096, 4096), (8192, 1147)], False, None)],
808
[('readv', 'index', [(0, 4096)], False, None),
809
('readv', 'index', [(4096, 4096), (8192, rem)], False, None)],
794
811
# XXX: TODO: write some badly-ordered nodes, and some pointers-to-wrong
795
812
# node and make validate find them.
797
814
def test_eq_ne(self):
798
815
# two indices are equal when constructed with the same parameters:
799
transport1 = get_transport('trace+' + self.get_url(''))
800
transport2 = get_transport(self.get_url(''))
802
btree_index.BTreeGraphIndex(transport1, 'index', None) ==
803
btree_index.BTreeGraphIndex(transport1, 'index', None))
805
btree_index.BTreeGraphIndex(transport1, 'index', 20) ==
806
btree_index.BTreeGraphIndex(transport1, 'index', 20))
808
btree_index.BTreeGraphIndex(transport1, 'index', 20) ==
809
btree_index.BTreeGraphIndex(transport2, 'index', 20))
811
btree_index.BTreeGraphIndex(transport1, 'inde1', 20) ==
812
btree_index.BTreeGraphIndex(transport1, 'inde2', 20))
814
btree_index.BTreeGraphIndex(transport1, 'index', 10) ==
815
btree_index.BTreeGraphIndex(transport1, 'index', 20))
817
btree_index.BTreeGraphIndex(transport1, 'index', None) !=
818
btree_index.BTreeGraphIndex(transport1, 'index', None))
820
btree_index.BTreeGraphIndex(transport1, 'index', 20) !=
821
btree_index.BTreeGraphIndex(transport1, 'index', 20))
823
btree_index.BTreeGraphIndex(transport1, 'index', 20) !=
824
btree_index.BTreeGraphIndex(transport2, 'index', 20))
826
btree_index.BTreeGraphIndex(transport1, 'inde1', 20) !=
827
btree_index.BTreeGraphIndex(transport1, 'inde2', 20))
829
btree_index.BTreeGraphIndex(transport1, 'index', 10) !=
830
btree_index.BTreeGraphIndex(transport1, 'index', 20))
816
t1 = transport.get_transport_from_url('trace+' + self.get_url(''))
817
t2 = self.get_transport()
819
btree_index.BTreeGraphIndex(t1, 'index', None) ==
820
btree_index.BTreeGraphIndex(t1, 'index', None))
822
btree_index.BTreeGraphIndex(t1, 'index', 20) ==
823
btree_index.BTreeGraphIndex(t1, 'index', 20))
825
btree_index.BTreeGraphIndex(t1, 'index', 20) ==
826
btree_index.BTreeGraphIndex(t2, 'index', 20))
828
btree_index.BTreeGraphIndex(t1, 'inde1', 20) ==
829
btree_index.BTreeGraphIndex(t1, 'inde2', 20))
831
btree_index.BTreeGraphIndex(t1, 'index', 10) ==
832
btree_index.BTreeGraphIndex(t1, 'index', 20))
834
btree_index.BTreeGraphIndex(t1, 'index', None) !=
835
btree_index.BTreeGraphIndex(t1, 'index', None))
837
btree_index.BTreeGraphIndex(t1, 'index', 20) !=
838
btree_index.BTreeGraphIndex(t1, 'index', 20))
840
btree_index.BTreeGraphIndex(t1, 'index', 20) !=
841
btree_index.BTreeGraphIndex(t2, 'index', 20))
843
btree_index.BTreeGraphIndex(t1, 'inde1', 20) !=
844
btree_index.BTreeGraphIndex(t1, 'inde2', 20))
846
btree_index.BTreeGraphIndex(t1, 'index', 10) !=
847
btree_index.BTreeGraphIndex(t1, 'index', 20))
849
def test_key_too_big(self):
850
# the size that matters here is the _compressed_ size of the key, so we can't
851
# do a simple character repeat.
852
bigKey = b''.join(b'%d' % n for n in range(btree_index._PAGE_SIZE))
853
self.assertRaises(errors.BadIndexKey,
855
nodes=[((bigKey,), b'value', ())])
832
857
def test_iter_all_only_root_no_size(self):
833
self.make_index(nodes=[(('key',), 'value', ())])
834
trans = get_transport('trace+' + self.get_url(''))
835
index = btree_index.BTreeGraphIndex(trans, 'index', None)
836
del trans._activity[:]
837
self.assertEqual([(('key',), 'value')],
858
self.make_index(nodes=[((b'key',), b'value', ())])
859
t = transport.get_transport_from_url('trace+' + self.get_url(''))
860
index = btree_index.BTreeGraphIndex(t, 'index', None)
862
self.assertEqual([((b'key',), b'value')],
838
863
[x[1:] for x in index.iter_all_entries()])
839
self.assertEqual([('get', 'index')], trans._activity)
864
self.assertEqual([('get', 'index')], t._activity)
841
866
def test_iter_all_entries_reads(self):
842
867
# iterating all entries reads the header, then does a linear
932
947
def test_iter_key_prefix_wrong_length(self):
933
948
index = self.make_index()
934
949
self.assertRaises(errors.BadIndexKey, list,
935
index.iter_entries_prefix([('foo', None)]))
950
index.iter_entries_prefix([(b'foo', None)]))
936
951
index = self.make_index(key_elements=2)
937
952
self.assertRaises(errors.BadIndexKey, list,
938
index.iter_entries_prefix([('foo', )]))
953
index.iter_entries_prefix([(b'foo', )]))
939
954
self.assertRaises(errors.BadIndexKey, list,
940
index.iter_entries_prefix([('foo', None, None)]))
955
index.iter_entries_prefix([(b'foo', None, None)]))
942
957
def test_iter_key_prefix_1_key_element_no_refs(self):
943
index = self.make_index( nodes=[
944
(('name', ), 'data', ()),
945
(('ref', ), 'refdata', ())])
946
self.assertEqual(set([(index, ('name', ), 'data'),
947
(index, ('ref', ), 'refdata')]),
948
set(index.iter_entries_prefix([('name', ), ('ref', )])))
958
index = self.make_index(nodes=[
959
((b'name', ), b'data', ()),
960
((b'ref', ), b'refdata', ())])
961
self.assertEqual({(index, (b'name', ), b'data'),
962
(index, (b'ref', ), b'refdata')},
963
set(index.iter_entries_prefix([(b'name', ), (b'ref', )])))
950
965
def test_iter_key_prefix_1_key_element_refs(self):
951
966
index = self.make_index(1, nodes=[
952
(('name', ), 'data', ([('ref', )], )),
953
(('ref', ), 'refdata', ([], ))])
954
self.assertEqual(set([(index, ('name', ), 'data', ((('ref',),),)),
955
(index, ('ref', ), 'refdata', ((), ))]),
956
set(index.iter_entries_prefix([('name', ), ('ref', )])))
967
((b'name', ), b'data', ([(b'ref', )], )),
968
((b'ref', ), b'refdata', ([], ))])
969
self.assertEqual({(index, (b'name', ), b'data', (((b'ref',),),)),
970
(index, (b'ref', ), b'refdata', ((), ))},
971
set(index.iter_entries_prefix([(b'name', ), (b'ref', )])))
958
973
def test_iter_key_prefix_2_key_element_no_refs(self):
959
974
index = self.make_index(key_elements=2, nodes=[
960
(('name', 'fin1'), 'data', ()),
961
(('name', 'fin2'), 'beta', ()),
962
(('ref', 'erence'), 'refdata', ())])
963
self.assertEqual(set([(index, ('name', 'fin1'), 'data'),
964
(index, ('ref', 'erence'), 'refdata')]),
965
set(index.iter_entries_prefix([('name', 'fin1'), ('ref', 'erence')])))
966
self.assertEqual(set([(index, ('name', 'fin1'), 'data'),
967
(index, ('name', 'fin2'), 'beta')]),
968
set(index.iter_entries_prefix([('name', None)])))
975
((b'name', b'fin1'), b'data', ()),
976
((b'name', b'fin2'), b'beta', ()),
977
((b'ref', b'erence'), b'refdata', ())])
978
self.assertEqual({(index, (b'name', b'fin1'), b'data'),
979
(index, (b'ref', b'erence'), b'refdata')},
980
set(index.iter_entries_prefix(
981
[(b'name', b'fin1'), (b'ref', b'erence')])))
982
self.assertEqual({(index, (b'name', b'fin1'), b'data'),
983
(index, (b'name', b'fin2'), b'beta')},
984
set(index.iter_entries_prefix([(b'name', None)])))
970
986
def test_iter_key_prefix_2_key_element_refs(self):
971
987
index = self.make_index(1, key_elements=2, nodes=[
972
(('name', 'fin1'), 'data', ([('ref', 'erence')], )),
973
(('name', 'fin2'), 'beta', ([], )),
974
(('ref', 'erence'), 'refdata', ([], ))])
975
self.assertEqual(set([(index, ('name', 'fin1'), 'data', ((('ref', 'erence'),),)),
976
(index, ('ref', 'erence'), 'refdata', ((), ))]),
977
set(index.iter_entries_prefix([('name', 'fin1'), ('ref', 'erence')])))
978
self.assertEqual(set([(index, ('name', 'fin1'), 'data', ((('ref', 'erence'),),)),
979
(index, ('name', 'fin2'), 'beta', ((), ))]),
980
set(index.iter_entries_prefix([('name', None)])))
988
((b'name', b'fin1'), b'data', ([(b'ref', b'erence')], )),
989
((b'name', b'fin2'), b'beta', ([], )),
990
((b'ref', b'erence'), b'refdata', ([], ))])
992
{(index, (b'name', b'fin1'), b'data', (((b'ref', b'erence'),),)),
993
(index, (b'ref', b'erence'), b'refdata', ((), ))},
994
set(index.iter_entries_prefix(
995
[(b'name', b'fin1'), (b'ref', b'erence')])))
997
{(index, (b'name', b'fin1'), b'data', (((b'ref', b'erence'),),)),
998
(index, (b'name', b'fin2'), b'beta', ((), ))},
999
set(index.iter_entries_prefix([(b'name', None)])))
982
1001
# XXX: external_references tests are duplicated in test_index. We
983
1002
# probably should have per_graph_index tests...
988
1007
def test_external_references_no_results(self):
989
1008
index = self.make_index(ref_lists=1, nodes=[
990
(('key',), 'value', ([],))])
1009
((b'key',), b'value', ([],))])
991
1010
self.assertEqual(set(), index.external_references(0))
993
1012
def test_external_references_missing_ref(self):
994
missing_key = ('missing',)
1013
missing_key = (b'missing',)
995
1014
index = self.make_index(ref_lists=1, nodes=[
996
(('key',), 'value', ([missing_key],))])
997
self.assertEqual(set([missing_key]), index.external_references(0))
1015
((b'key',), b'value', ([missing_key],))])
1016
self.assertEqual({missing_key}, index.external_references(0))
999
1018
def test_external_references_multiple_ref_lists(self):
1000
missing_key = ('missing',)
1019
missing_key = (b'missing',)
1001
1020
index = self.make_index(ref_lists=2, nodes=[
1002
(('key',), 'value', ([], [missing_key]))])
1021
((b'key',), b'value', ([], [missing_key]))])
1003
1022
self.assertEqual(set([]), index.external_references(0))
1004
self.assertEqual(set([missing_key]), index.external_references(1))
1023
self.assertEqual({missing_key}, index.external_references(1))
1006
1025
def test_external_references_two_records(self):
1007
1026
index = self.make_index(ref_lists=1, nodes=[
1008
(('key-1',), 'value', ([('key-2',)],)),
1009
(('key-2',), 'value', ([],)),
1027
((b'key-1',), b'value', ([(b'key-2',)],)),
1028
((b'key-2',), b'value', ([],)),
1011
1030
self.assertEqual(set([]), index.external_references(0))
1013
1032
def test__find_ancestors_one_page(self):
1016
1035
index = self.make_index(ref_lists=1, key_elements=1, nodes=[
1017
(key1, 'value', ([key2],)),
1018
(key2, 'value', ([],)),
1036
(key1, b'value', ([key2],)),
1037
(key2, b'value', ([],)),
1020
1039
parent_map = {}
1021
1040
missing_keys = set()
1185
1204
class TestBTreeNodes(BTreeTestCase):
1206
scenarios = btreeparser_scenarios()
1187
1208
def setUp(self):
1188
BTreeTestCase.setUp(self)
1209
super(TestBTreeNodes, self).setUp()
1189
1210
self.overrideAttr(btree_index, '_btree_serializer', self.parse_btree)
1191
1212
def test_LeafNode_1_0(self):
1192
node_bytes = ("type=leaf\n"
1193
"0000000000000000000000000000000000000000\x00\x00value:0\n"
1194
"1111111111111111111111111111111111111111\x00\x00value:1\n"
1195
"2222222222222222222222222222222222222222\x00\x00value:2\n"
1196
"3333333333333333333333333333333333333333\x00\x00value:3\n"
1197
"4444444444444444444444444444444444444444\x00\x00value:4\n")
1213
node_bytes = (b"type=leaf\n"
1214
b"0000000000000000000000000000000000000000\x00\x00value:0\n"
1215
b"1111111111111111111111111111111111111111\x00\x00value:1\n"
1216
b"2222222222222222222222222222222222222222\x00\x00value:2\n"
1217
b"3333333333333333333333333333333333333333\x00\x00value:3\n"
1218
b"4444444444444444444444444444444444444444\x00\x00value:4\n")
1198
1219
node = btree_index._LeafNode(node_bytes, 1, 0)
1199
1220
# We do direct access, or don't care about order, to leaf nodes most of
1200
1221
# the time, so a dict is useful:
1201
1222
self.assertEqual({
1202
("0000000000000000000000000000000000000000",): ("value:0", ()),
1203
("1111111111111111111111111111111111111111",): ("value:1", ()),
1204
("2222222222222222222222222222222222222222",): ("value:2", ()),
1205
("3333333333333333333333333333333333333333",): ("value:3", ()),
1206
("4444444444444444444444444444444444444444",): ("value:4", ()),
1223
(b"0000000000000000000000000000000000000000",): (b"value:0", ()),
1224
(b"1111111111111111111111111111111111111111",): (b"value:1", ()),
1225
(b"2222222222222222222222222222222222222222",): (b"value:2", ()),
1226
(b"3333333333333333333333333333333333333333",): (b"value:3", ()),
1227
(b"4444444444444444444444444444444444444444",): (b"value:4", ()),
1228
}, dict(node.all_items()))
1209
1230
def test_LeafNode_2_2(self):
1210
node_bytes = ("type=leaf\n"
1211
"00\x0000\x00\t00\x00ref00\x00value:0\n"
1212
"00\x0011\x0000\x00ref00\t00\x00ref00\r01\x00ref01\x00value:1\n"
1213
"11\x0033\x0011\x00ref22\t11\x00ref22\r11\x00ref22\x00value:3\n"
1214
"11\x0044\x00\t11\x00ref00\x00value:4\n"
1231
node_bytes = (b"type=leaf\n"
1232
b"00\x0000\x00\t00\x00ref00\x00value:0\n"
1233
b"00\x0011\x0000\x00ref00\t00\x00ref00\r01\x00ref01\x00value:1\n"
1234
b"11\x0033\x0011\x00ref22\t11\x00ref22\r11\x00ref22\x00value:3\n"
1235
b"11\x0044\x00\t11\x00ref00\x00value:4\n"
1217
1238
node = btree_index._LeafNode(node_bytes, 2, 2)
1218
1239
# We do direct access, or don't care about order, to leaf nodes most of
1219
1240
# the time, so a dict is useful:
1220
1241
self.assertEqual({
1221
('00', '00'): ('value:0', ((), (('00', 'ref00'),))),
1222
('00', '11'): ('value:1',
1223
((('00', 'ref00'),), (('00', 'ref00'), ('01', 'ref01')))),
1224
('11', '33'): ('value:3',
1225
((('11', 'ref22'),), (('11', 'ref22'), ('11', 'ref22')))),
1226
('11', '44'): ('value:4', ((), (('11', 'ref00'),)))
1242
(b'00', b'00'): (b'value:0', ((), ((b'00', b'ref00'),))),
1243
(b'00', b'11'): (b'value:1', (((b'00', b'ref00'),),
1244
((b'00', b'ref00'), (b'01', b'ref01')))),
1245
(b'11', b'33'): (b'value:3', (((b'11', b'ref22'),),
1246
((b'11', b'ref22'), (b'11', b'ref22')))),
1247
(b'11', b'44'): (b'value:4', ((), ((b'11', b'ref00'),)))
1248
}, dict(node.all_items()))
1229
1250
def test_InternalNode_1(self):
1230
node_bytes = ("type=internal\n"
1232
"0000000000000000000000000000000000000000\n"
1233
"1111111111111111111111111111111111111111\n"
1234
"2222222222222222222222222222222222222222\n"
1235
"3333333333333333333333333333333333333333\n"
1236
"4444444444444444444444444444444444444444\n"
1251
node_bytes = (b"type=internal\n"
1253
b"0000000000000000000000000000000000000000\n"
1254
b"1111111111111111111111111111111111111111\n"
1255
b"2222222222222222222222222222222222222222\n"
1256
b"3333333333333333333333333333333333333333\n"
1257
b"4444444444444444444444444444444444444444\n"
1238
1259
node = btree_index._InternalNode(node_bytes)
1239
1260
# We want to bisect to find the right children from this node, so a
1240
1261
# vector is most useful.
1241
1262
self.assertEqual([
1242
("0000000000000000000000000000000000000000",),
1243
("1111111111111111111111111111111111111111",),
1244
("2222222222222222222222222222222222222222",),
1245
("3333333333333333333333333333333333333333",),
1246
("4444444444444444444444444444444444444444",),
1263
(b"0000000000000000000000000000000000000000",),
1264
(b"1111111111111111111111111111111111111111",),
1265
(b"2222222222222222222222222222222222222222",),
1266
(b"3333333333333333333333333333333333333333",),
1267
(b"4444444444444444444444444444444444444444",),
1248
1269
self.assertEqual(1, node.offset)
1250
1271
def test_LeafNode_2_2(self):
1251
node_bytes = ("type=leaf\n"
1252
"00\x0000\x00\t00\x00ref00\x00value:0\n"
1253
"00\x0011\x0000\x00ref00\t00\x00ref00\r01\x00ref01\x00value:1\n"
1254
"11\x0033\x0011\x00ref22\t11\x00ref22\r11\x00ref22\x00value:3\n"
1255
"11\x0044\x00\t11\x00ref00\x00value:4\n"
1272
node_bytes = (b"type=leaf\n"
1273
b"00\x0000\x00\t00\x00ref00\x00value:0\n"
1274
b"00\x0011\x0000\x00ref00\t00\x00ref00\r01\x00ref01\x00value:1\n"
1275
b"11\x0033\x0011\x00ref22\t11\x00ref22\r11\x00ref22\x00value:3\n"
1276
b"11\x0044\x00\t11\x00ref00\x00value:4\n"
1258
1279
node = btree_index._LeafNode(node_bytes, 2, 2)
1259
1280
# We do direct access, or don't care about order, to leaf nodes most of
1260
1281
# the time, so a dict is useful:
1261
1282
self.assertEqual({
1262
('00', '00'): ('value:0', ((), (('00', 'ref00'),))),
1263
('00', '11'): ('value:1',
1264
((('00', 'ref00'),), (('00', 'ref00'), ('01', 'ref01')))),
1265
('11', '33'): ('value:3',
1266
((('11', 'ref22'),), (('11', 'ref22'), ('11', 'ref22')))),
1267
('11', '44'): ('value:4', ((), (('11', 'ref00'),)))
1283
(b'00', b'00'): (b'value:0', ((), ((b'00', b'ref00'),))),
1284
(b'00', b'11'): (b'value:1', (((b'00', b'ref00'),),
1285
((b'00', b'ref00'), (b'01', b'ref01')))),
1286
(b'11', b'33'): (b'value:3', (((b'11', b'ref22'),),
1287
((b'11', b'ref22'), (b'11', b'ref22')))),
1288
(b'11', b'44'): (b'value:4', ((), ((b'11', b'ref00'),)))
1289
}, dict(node.all_items()))
1270
1291
def assertFlattened(self, expected, key, value, refs):
1271
1292
flat_key, flat_line = self.parse_btree._flatten_node(
1272
1293
(None, key, value, refs), bool(refs))
1273
self.assertEqual('\x00'.join(key), flat_key)
1294
self.assertEqual(b'\x00'.join(key), flat_key)
1274
1295
self.assertEqual(expected, flat_line)
1276
1297
def test__flatten_node(self):
1277
self.assertFlattened('key\0\0value\n', ('key',), 'value', [])
1278
self.assertFlattened('key\0tuple\0\0value str\n',
1279
('key', 'tuple'), 'value str', [])
1280
self.assertFlattened('key\0tuple\0triple\0\0value str\n',
1281
('key', 'tuple', 'triple'), 'value str', [])
1282
self.assertFlattened('k\0t\0s\0ref\0value str\n',
1283
('k', 't', 's'), 'value str', [[('ref',)]])
1284
self.assertFlattened('key\0tuple\0ref\0key\0value str\n',
1285
('key', 'tuple'), 'value str', [[('ref', 'key')]])
1286
self.assertFlattened("00\x0000\x00\t00\x00ref00\x00value:0\n",
1287
('00', '00'), 'value:0', ((), (('00', 'ref00'),)))
1288
self.assertFlattened(
1289
"00\x0011\x0000\x00ref00\t00\x00ref00\r01\x00ref01\x00value:1\n",
1290
('00', '11'), 'value:1',
1291
((('00', 'ref00'),), (('00', 'ref00'), ('01', 'ref01'))))
1292
self.assertFlattened(
1293
"11\x0033\x0011\x00ref22\t11\x00ref22\r11\x00ref22\x00value:3\n",
1294
('11', '33'), 'value:3',
1295
((('11', 'ref22'),), (('11', 'ref22'), ('11', 'ref22'))))
1296
self.assertFlattened(
1297
"11\x0044\x00\t11\x00ref00\x00value:4\n",
1298
('11', '44'), 'value:4', ((), (('11', 'ref00'),)))
1298
self.assertFlattened(b'key\0\0value\n', (b'key',), b'value', [])
1299
self.assertFlattened(b'key\0tuple\0\0value str\n',
1300
(b'key', b'tuple'), b'value str', [])
1301
self.assertFlattened(b'key\0tuple\0triple\0\0value str\n',
1302
(b'key', b'tuple', b'triple'), b'value str', [])
1303
self.assertFlattened(b'k\0t\0s\0ref\0value str\n',
1304
(b'k', b't', b's'), b'value str', [[(b'ref',)]])
1305
self.assertFlattened(b'key\0tuple\0ref\0key\0value str\n',
1306
(b'key', b'tuple'), b'value str', [[(b'ref', b'key')]])
1307
self.assertFlattened(b"00\x0000\x00\t00\x00ref00\x00value:0\n",
1308
(b'00', b'00'), b'value:0', ((), ((b'00', b'ref00'),)))
1309
self.assertFlattened(
1310
b"00\x0011\x0000\x00ref00\t00\x00ref00\r01\x00ref01\x00value:1\n",
1311
(b'00', b'11'), b'value:1',
1312
(((b'00', b'ref00'),), ((b'00', b'ref00'), (b'01', b'ref01'))))
1313
self.assertFlattened(
1314
b"11\x0033\x0011\x00ref22\t11\x00ref22\r11\x00ref22\x00value:3\n",
1315
(b'11', b'33'), b'value:3',
1316
(((b'11', b'ref22'),), ((b'11', b'ref22'), (b'11', b'ref22'))))
1317
self.assertFlattened(
1318
b"11\x0044\x00\t11\x00ref00\x00value:4\n",
1319
(b'11', b'44'), b'value:4', ((), ((b'11', b'ref00'),)))
1301
1322
class TestCompiledBtree(tests.TestCase):