~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_btree_index.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-03-06 06:48:25 UTC
  • mfrom: (4070.8.6 debug-config)
  • Revision ID: pqm@pqm.ubuntu.com-20090306064825-kbpwggw21dygeix6
(mbp) debug_flags configuration option

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
#
17
17
 
18
18
"""Tests for btree indices."""
27
27
    )
28
28
from bzrlib.tests import (
29
29
    TestCaseWithTransport,
 
30
    TestScenarioApplier,
 
31
    adapt_tests,
30
32
    condition_isinstance,
31
 
    multiply_tests,
32
33
    split_suite_by_condition,
33
34
    )
34
35
from bzrlib.transport import get_transport
38
39
    # parameterise the TestBTreeNodes tests
39
40
    node_tests, others = split_suite_by_condition(standard_tests,
40
41
        condition_isinstance(TestBTreeNodes))
 
42
    applier = TestScenarioApplier()
41
43
    import bzrlib._btree_serializer_py as py_module
42
 
    scenarios = [('python', {'parse_btree': py_module})]
 
44
    applier.scenarios = [('python', {'parse_btree': py_module})]
43
45
    if CompiledBtreeParserFeature.available():
44
46
        # Is there a way to do this that gets missing feature failures rather
45
47
        # than no indication to the user?
46
48
        import bzrlib._btree_serializer_c as c_module
47
 
        scenarios.append(('C', {'parse_btree': c_module}))
48
 
    return multiply_tests(node_tests, scenarios, others)
 
49
        applier.scenarios.append(('C', {'parse_btree': c_module}))
 
50
    adapt_tests(node_tests, applier, others)
 
51
    return others
49
52
 
50
53
 
51
54
class _CompiledBtreeParserFeature(tests.Feature):
431
434
        self.assertEqual(sorted(nodes), nodes)
432
435
        self.assertEqual(16, len(nodes))
433
436
 
434
 
    def test_spill_index_stress_1_1_no_combine(self):
435
 
        builder = btree_index.BTreeBuilder(key_elements=1, spill_at=2)
436
 
        builder.set_optimize(for_size=False, combine_backing_indices=False)
437
 
        nodes = [node[0:2] for node in self.make_nodes(16, 1, 0)]
438
 
        builder.add_node(*nodes[0])
439
 
        # Test the parts of the index that take up memory are doing so
440
 
        # predictably.
441
 
        self.assertEqual(1, len(builder._nodes))
442
 
        self.assertEqual(1, len(builder._keys))
443
 
        self.assertIs(None, builder._nodes_by_key)
444
 
        builder.add_node(*nodes[1])
445
 
        self.assertEqual(0, len(builder._nodes))
446
 
        self.assertEqual(0, len(builder._keys))
447
 
        self.assertIs(None, builder._nodes_by_key)
448
 
        self.assertEqual(1, len(builder._backing_indices))
449
 
        self.assertEqual(2, builder._backing_indices[0].key_count())
450
 
        # now back to memory
451
 
        builder.add_node(*nodes[2])
452
 
        self.assertEqual(1, len(builder._nodes))
453
 
        self.assertEqual(1, len(builder._keys))
454
 
        self.assertIs(None, builder._nodes_by_key)
455
 
        # And spills to a second backing index but doesn't combine
456
 
        builder.add_node(*nodes[3])
457
 
        self.assertEqual(0, len(builder._nodes))
458
 
        self.assertEqual(0, len(builder._keys))
459
 
        self.assertIs(None, builder._nodes_by_key)
460
 
        self.assertEqual(2, len(builder._backing_indices))
461
 
        for backing_index in builder._backing_indices:
462
 
            self.assertEqual(2, backing_index.key_count())
463
 
        # The next spills to the 3rd slot
464
 
        builder.add_node(*nodes[4])
465
 
        builder.add_node(*nodes[5])
466
 
        self.assertEqual(0, len(builder._nodes))
467
 
        self.assertEqual(0, len(builder._keys))
468
 
        self.assertIs(None, builder._nodes_by_key)
469
 
        self.assertEqual(3, len(builder._backing_indices))
470
 
        for backing_index in builder._backing_indices:
471
 
            self.assertEqual(2, backing_index.key_count())
472
 
        # Now spill a few more, and check that we don't combine
473
 
        builder.add_node(*nodes[6])
474
 
        builder.add_node(*nodes[7])
475
 
        builder.add_node(*nodes[8])
476
 
        builder.add_node(*nodes[9])
477
 
        builder.add_node(*nodes[10])
478
 
        builder.add_node(*nodes[11])
479
 
        builder.add_node(*nodes[12])
480
 
        self.assertEqual(6, len(builder._backing_indices))
481
 
        for backing_index in builder._backing_indices:
482
 
            self.assertEqual(2, backing_index.key_count())
483
 
        # Test that memory and disk are both used for query methods; and that
484
 
        # None is skipped over happily.
485
 
        self.assertEqual([(builder,) + node for node in sorted(nodes[:13])],
486
 
            list(builder.iter_all_entries()))
487
 
        # Two nodes - one memory one disk
488
 
        self.assertEqual(set([(builder,) + node for node in nodes[11:13]]),
489
 
            set(builder.iter_entries([nodes[12][0], nodes[11][0]])))
490
 
        self.assertEqual(13, builder.key_count())
491
 
        self.assertEqual(set([(builder,) + node for node in nodes[11:13]]),
492
 
            set(builder.iter_entries_prefix([nodes[12][0], nodes[11][0]])))
493
 
        builder.add_node(*nodes[13])
494
 
        builder.add_node(*nodes[14])
495
 
        builder.add_node(*nodes[15])
496
 
        self.assertEqual(8, len(builder._backing_indices))
497
 
        for backing_index in builder._backing_indices:
498
 
            self.assertEqual(2, backing_index.key_count())
499
 
        # Now finish, and check we got a correctly ordered tree
500
 
        transport = self.get_transport('')
501
 
        size = transport.put_file('index', builder.finish())
502
 
        index = btree_index.BTreeGraphIndex(transport, 'index', size)
503
 
        nodes = list(index.iter_all_entries())
504
 
        self.assertEqual(sorted(nodes), nodes)
505
 
        self.assertEqual(16, len(nodes))
506
 
 
507
437
    def test_set_optimize(self):
508
438
        builder = btree_index.BTreeBuilder(key_elements=2, reference_lists=2)
509
439
        builder.set_optimize(for_size=True)
510
440
        self.assertTrue(builder._optimize_for_size)
511
441
        builder.set_optimize(for_size=False)
512
442
        self.assertFalse(builder._optimize_for_size)
513
 
        # test that we can set combine_backing_indices without effecting
514
 
        # _optimize_for_size
515
 
        obj = object()
516
 
        builder._optimize_for_size = obj
517
 
        builder.set_optimize(combine_backing_indices=False)
518
 
        self.assertFalse(builder._combine_backing_indices)
519
 
        self.assertIs(obj, builder._optimize_for_size)
520
 
        builder.set_optimize(combine_backing_indices=True)
521
 
        self.assertTrue(builder._combine_backing_indices)
522
 
        self.assertIs(obj, builder._optimize_for_size)
523
443
 
524
444
    def test_spill_index_stress_2_2(self):
525
445
        # test that references and longer keys don't confuse things.