607
614
for key, value, references in nodes:
608
615
builder.add_node(key, value, references)
609
616
stream = builder.finish()
610
trans = transport.get_transport('trace+' + self.get_url())
617
trans = get_transport('trace+' + self.get_url())
611
618
size = trans.put_file('index', stream)
612
619
return btree_index.BTreeGraphIndex(trans, 'index', size)
614
def make_index_with_offset(self, ref_lists=1, key_elements=1, nodes=[],
616
builder = btree_index.BTreeBuilder(key_elements=key_elements,
617
reference_lists=ref_lists)
618
builder.add_nodes(nodes)
619
transport = self.get_transport('')
620
# NamedTemporaryFile dies on builder.finish().read(). weird.
621
temp_file = builder.finish()
622
content = temp_file.read()
625
transport.put_bytes('index', (' '*offset)+content)
626
return btree_index.BTreeGraphIndex(transport, 'index', size=size,
629
621
def test_clear_cache(self):
630
622
nodes = self.make_nodes(160, 2, 2)
631
623
index = self.make_index(ref_lists=2, key_elements=2, nodes=nodes)
648
640
self.assertEqual(0, len(index._leaf_node_cache))
650
642
def test_trivial_constructor(self):
651
t = transport.get_transport('trace+' + self.get_url(''))
652
index = btree_index.BTreeGraphIndex(t, 'index', None)
643
transport = get_transport('trace+' + self.get_url(''))
644
index = btree_index.BTreeGraphIndex(transport, 'index', None)
653
645
# Checks the page size at load, but that isn't logged yet.
654
self.assertEqual([], t._activity)
646
self.assertEqual([], transport._activity)
656
648
def test_with_size_constructor(self):
657
t = transport.get_transport('trace+' + self.get_url(''))
658
index = btree_index.BTreeGraphIndex(t, 'index', 1)
649
transport = get_transport('trace+' + self.get_url(''))
650
index = btree_index.BTreeGraphIndex(transport, 'index', 1)
659
651
# Checks the page size at load, but that isn't logged yet.
660
self.assertEqual([], t._activity)
652
self.assertEqual([], transport._activity)
662
654
def test_empty_key_count_no_size(self):
663
655
builder = btree_index.BTreeBuilder(key_elements=1, reference_lists=0)
664
t = transport.get_transport('trace+' + self.get_url(''))
665
t.put_file('index', builder.finish())
666
index = btree_index.BTreeGraphIndex(t, 'index', None)
668
self.assertEqual([], t._activity)
656
transport = get_transport('trace+' + self.get_url(''))
657
transport.put_file('index', builder.finish())
658
index = btree_index.BTreeGraphIndex(transport, 'index', None)
659
del transport._activity[:]
660
self.assertEqual([], transport._activity)
669
661
self.assertEqual(0, index.key_count())
670
662
# The entire index should have been requested (as we generally have the
671
663
# size available, and doing many small readvs is inappropriate).
672
664
# We can't tell how much was actually read here, but - check the code.
673
self.assertEqual([('get', 'index')], t._activity)
665
self.assertEqual([('get', 'index')], transport._activity)
675
667
def test_empty_key_count(self):
676
668
builder = btree_index.BTreeBuilder(key_elements=1, reference_lists=0)
677
t = transport.get_transport('trace+' + self.get_url(''))
678
size = t.put_file('index', builder.finish())
669
transport = get_transport('trace+' + self.get_url(''))
670
size = transport.put_file('index', builder.finish())
679
671
self.assertEqual(72, size)
680
index = btree_index.BTreeGraphIndex(t, 'index', size)
682
self.assertEqual([], t._activity)
672
index = btree_index.BTreeGraphIndex(transport, 'index', size)
673
del transport._activity[:]
674
self.assertEqual([], transport._activity)
683
675
self.assertEqual(0, index.key_count())
684
676
# The entire index should have been read, as 4K > size
685
677
self.assertEqual([('readv', 'index', [(0, 72)], False, None)],
688
680
def test_non_empty_key_count_2_2(self):
689
681
builder = btree_index.BTreeBuilder(key_elements=2, reference_lists=2)
690
682
nodes = self.make_nodes(35, 2, 2)
691
683
for node in nodes:
692
684
builder.add_node(*node)
693
t = transport.get_transport('trace+' + self.get_url(''))
694
size = t.put_file('index', builder.finish())
695
index = btree_index.BTreeGraphIndex(t, 'index', size)
697
self.assertEqual([], t._activity)
685
transport = get_transport('trace+' + self.get_url(''))
686
size = transport.put_file('index', builder.finish())
687
index = btree_index.BTreeGraphIndex(transport, 'index', size)
688
del transport._activity[:]
689
self.assertEqual([], transport._activity)
698
690
self.assertEqual(70, index.key_count())
699
691
# The entire index should have been read, as it is one page long.
700
692
self.assertEqual([('readv', 'index', [(0, size)], False, None)],
702
694
self.assertEqual(1173, size)
704
def test_with_offset_no_size(self):
705
index = self.make_index_with_offset(key_elements=1, ref_lists=1,
707
nodes=self.make_nodes(200, 1, 1))
708
index._size = None # throw away the size info
709
self.assertEqual(200, index.key_count())
711
def test_with_small_offset(self):
712
index = self.make_index_with_offset(key_elements=1, ref_lists=1,
714
nodes=self.make_nodes(200, 1, 1))
715
self.assertEqual(200, index.key_count())
717
def test_with_large_offset(self):
718
index = self.make_index_with_offset(key_elements=1, ref_lists=1,
720
nodes=self.make_nodes(200, 1, 1))
721
self.assertEqual(200, index.key_count())
723
696
def test__read_nodes_no_size_one_page_reads_once(self):
724
697
self.make_index(nodes=[(('key',), 'value', ())])
725
trans = transport.get_transport('trace+' + self.get_url())
698
trans = get_transport('trace+' + self.get_url())
726
699
index = btree_index.BTreeGraphIndex(trans, 'index', None)
727
700
del trans._activity[:]
728
701
nodes = dict(index._read_nodes([0]))
729
702
self.assertEqual([0], nodes.keys())
731
self.assertEqual([('key',)], node.all_keys())
704
self.assertEqual([('key',)], node.keys.keys())
732
705
self.assertEqual([('get', 'index')], trans._activity)
734
707
def test__read_nodes_no_size_multiple_pages(self):
747
720
nodes = self.make_nodes(160, 2, 2)
748
721
for node in nodes:
749
722
builder.add_node(*node)
750
t = transport.get_transport('trace+' + self.get_url(''))
751
size = t.put_file('index', builder.finish())
723
transport = get_transport('trace+' + self.get_url(''))
724
size = transport.put_file('index', builder.finish())
752
725
self.assertEqual(17692, size)
753
index = btree_index.BTreeGraphIndex(t, 'index', size)
755
self.assertEqual([], t._activity)
726
index = btree_index.BTreeGraphIndex(transport, 'index', size)
727
del transport._activity[:]
728
self.assertEqual([], transport._activity)
756
729
self.assertEqual(320, index.key_count())
757
730
# The entire index should not have been read.
758
731
self.assertEqual([('readv', 'index', [(0, 4096)], False, None)],
761
734
def test_validate_one_page(self):
762
735
builder = btree_index.BTreeBuilder(key_elements=2, reference_lists=2)
763
736
nodes = self.make_nodes(45, 2, 2)
764
737
for node in nodes:
765
738
builder.add_node(*node)
766
t = transport.get_transport('trace+' + self.get_url(''))
767
size = t.put_file('index', builder.finish())
768
index = btree_index.BTreeGraphIndex(t, 'index', size)
770
self.assertEqual([], t._activity)
739
transport = get_transport('trace+' + self.get_url(''))
740
size = transport.put_file('index', builder.finish())
741
index = btree_index.BTreeGraphIndex(transport, 'index', size)
742
del transport._activity[:]
743
self.assertEqual([], transport._activity)
772
745
# The entire index should have been read linearly.
773
746
self.assertEqual([('readv', 'index', [(0, size)], False, None)],
775
748
self.assertEqual(1488, size)
777
750
def test_validate_two_pages(self):
779
752
nodes = self.make_nodes(80, 2, 2)
780
753
for node in nodes:
781
754
builder.add_node(*node)
782
t = transport.get_transport('trace+' + self.get_url(''))
783
size = t.put_file('index', builder.finish())
755
transport = get_transport('trace+' + self.get_url(''))
756
size = transport.put_file('index', builder.finish())
784
757
# Root page, 2 leaf pages
785
758
self.assertEqual(9339, size)
786
index = btree_index.BTreeGraphIndex(t, 'index', size)
788
self.assertEqual([], t._activity)
759
index = btree_index.BTreeGraphIndex(transport, 'index', size)
760
del transport._activity[:]
761
self.assertEqual([], transport._activity)
790
763
# The entire index should have been read linearly.
792
[('readv', 'index', [(0, 4096)], False, None),
793
('readv', 'index', [(4096, 4096), (8192, 1147)], False, None)],
764
self.assertEqual([('readv', 'index', [(0, 4096)], False, None),
765
('readv', 'index', [(4096, 4096), (8192, 1147)], False, None)],
795
767
# XXX: TODO: write some badly-ordered nodes, and some pointers-to-wrong
796
768
# node and make validate find them.
798
770
def test_eq_ne(self):
799
771
# two indices are equal when constructed with the same parameters:
800
t1 = transport.get_transport('trace+' + self.get_url(''))
801
t2 = transport.get_transport(self.get_url(''))
803
btree_index.BTreeGraphIndex(t1, 'index', None) ==
804
btree_index.BTreeGraphIndex(t1, 'index', None))
806
btree_index.BTreeGraphIndex(t1, 'index', 20) ==
807
btree_index.BTreeGraphIndex(t1, 'index', 20))
809
btree_index.BTreeGraphIndex(t1, 'index', 20) ==
810
btree_index.BTreeGraphIndex(t2, 'index', 20))
812
btree_index.BTreeGraphIndex(t1, 'inde1', 20) ==
813
btree_index.BTreeGraphIndex(t1, 'inde2', 20))
815
btree_index.BTreeGraphIndex(t1, 'index', 10) ==
816
btree_index.BTreeGraphIndex(t1, 'index', 20))
818
btree_index.BTreeGraphIndex(t1, 'index', None) !=
819
btree_index.BTreeGraphIndex(t1, 'index', None))
821
btree_index.BTreeGraphIndex(t1, 'index', 20) !=
822
btree_index.BTreeGraphIndex(t1, 'index', 20))
824
btree_index.BTreeGraphIndex(t1, 'index', 20) !=
825
btree_index.BTreeGraphIndex(t2, 'index', 20))
827
btree_index.BTreeGraphIndex(t1, 'inde1', 20) !=
828
btree_index.BTreeGraphIndex(t1, 'inde2', 20))
830
btree_index.BTreeGraphIndex(t1, 'index', 10) !=
831
btree_index.BTreeGraphIndex(t1, 'index', 20))
772
transport1 = get_transport('trace+' + self.get_url(''))
773
transport2 = get_transport(self.get_url(''))
775
btree_index.BTreeGraphIndex(transport1, 'index', None) ==
776
btree_index.BTreeGraphIndex(transport1, 'index', None))
778
btree_index.BTreeGraphIndex(transport1, 'index', 20) ==
779
btree_index.BTreeGraphIndex(transport1, 'index', 20))
781
btree_index.BTreeGraphIndex(transport1, 'index', 20) ==
782
btree_index.BTreeGraphIndex(transport2, 'index', 20))
784
btree_index.BTreeGraphIndex(transport1, 'inde1', 20) ==
785
btree_index.BTreeGraphIndex(transport1, 'inde2', 20))
787
btree_index.BTreeGraphIndex(transport1, 'index', 10) ==
788
btree_index.BTreeGraphIndex(transport1, 'index', 20))
790
btree_index.BTreeGraphIndex(transport1, 'index', None) !=
791
btree_index.BTreeGraphIndex(transport1, 'index', None))
793
btree_index.BTreeGraphIndex(transport1, 'index', 20) !=
794
btree_index.BTreeGraphIndex(transport1, 'index', 20))
796
btree_index.BTreeGraphIndex(transport1, 'index', 20) !=
797
btree_index.BTreeGraphIndex(transport2, 'index', 20))
799
btree_index.BTreeGraphIndex(transport1, 'inde1', 20) !=
800
btree_index.BTreeGraphIndex(transport1, 'inde2', 20))
802
btree_index.BTreeGraphIndex(transport1, 'index', 10) !=
803
btree_index.BTreeGraphIndex(transport1, 'index', 20))
833
805
def test_iter_all_only_root_no_size(self):
834
806
self.make_index(nodes=[(('key',), 'value', ())])
835
t = transport.get_transport('trace+' + self.get_url(''))
836
index = btree_index.BTreeGraphIndex(t, 'index', None)
807
trans = get_transport('trace+' + self.get_url(''))
808
index = btree_index.BTreeGraphIndex(trans, 'index', None)
809
del trans._activity[:]
838
810
self.assertEqual([(('key',), 'value')],
839
811
[x[1:] for x in index.iter_all_entries()])
840
self.assertEqual([('get', 'index')], t._activity)
812
self.assertEqual([('get', 'index')], trans._activity)
842
814
def test_iter_all_entries_reads(self):
843
815
# iterating all entries reads the header, then does a linear