~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_btree_index.py

  • Committer: John Arbash Meinel
  • Date: 2010-02-17 17:11:16 UTC
  • mfrom: (4797.2.17 2.1)
  • mto: (4797.2.18 2.1)
  • mto: This revision was merged to the branch mainline in revision 5055.
  • Revision ID: john@arbash-meinel.com-20100217171116-h7t9223ystbnx5h8
merge bzr.2.1 in preparation for NEWS entry.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008, 2009 Canonical Ltd
 
1
# Copyright (C) 2008, 2009, 2010 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
43
43
        condition_isinstance(TestBTreeNodes))
44
44
    import bzrlib._btree_serializer_py as py_module
45
45
    scenarios = [('python', {'parse_btree': py_module})]
46
 
    if CompiledBtreeParserFeature.available():
47
 
        # Is there a way to do this that gets missing feature failures rather
48
 
        # than no indication to the user?
49
 
        import bzrlib._btree_serializer_pyx as c_module
50
 
        scenarios.append(('C', {'parse_btree': c_module}))
 
46
    if compiled_btreeparser_feature.available():
 
47
        scenarios.append(('C', {'parse_btree':
 
48
                                compiled_btreeparser_feature.module}))
51
49
    return multiply_tests(node_tests, scenarios, others)
52
50
 
53
51
 
54
 
class _CompiledBtreeParserFeature(tests.Feature):
55
 
    def _probe(self):
56
 
        try:
57
 
            import bzrlib._btree_serializer_pyx
58
 
        except ImportError:
59
 
            return False
60
 
        return True
61
 
 
62
 
    def feature_name(self):
63
 
        return 'bzrlib._btree_serializer_pyx'
64
 
 
65
 
CompiledBtreeParserFeature = _CompiledBtreeParserFeature()
 
52
compiled_btreeparser_feature = tests.ModuleAvailableFeature(
 
53
                                'bzrlib._btree_serializer_pyx')
66
54
 
67
55
 
68
56
class BTreeTestCase(TestCaseWithTransport):
161
149
        temp_file = builder.finish()
162
150
        content = temp_file.read()
163
151
        del temp_file
164
 
        self.assertEqual(158, len(content))
 
152
        self.assertEqual(131, len(content))
165
153
        self.assertEqual(
166
154
            "B+Tree Graph Index 2\nnode_ref_lists=0\nkey_elements=1\nlen=5\n"
167
155
            "row_lengths=1\n",
185
173
        temp_file = builder.finish()
186
174
        content = temp_file.read()
187
175
        del temp_file
188
 
        self.assertEqual(264, len(content))
 
176
        self.assertEqual(238, len(content))
189
177
        self.assertEqual(
190
178
            "B+Tree Graph Index 2\nnode_ref_lists=2\nkey_elements=2\nlen=10\n"
191
179
            "row_lengths=1\n",
251
239
        temp_file = builder.finish()
252
240
        content = temp_file.read()
253
241
        del temp_file
254
 
        self.assertEqual(181, len(content))
 
242
        self.assertEqual(155, len(content))
255
243
        self.assertEqual(
256
244
            "B+Tree Graph Index 2\nnode_ref_lists=0\nkey_elements=1\nlen=10\n"
257
245
            "row_lengths=1\n",
359
347
        # Test the parts of the index that take up memory are doing so
360
348
        # predictably.
361
349
        self.assertEqual(1, len(builder._nodes))
362
 
        self.assertEqual(1, len(builder._keys))
363
350
        self.assertIs(None, builder._nodes_by_key)
364
351
        builder.add_node(*nodes[1])
365
352
        self.assertEqual(0, len(builder._nodes))
366
 
        self.assertEqual(0, len(builder._keys))
367
353
        self.assertIs(None, builder._nodes_by_key)
368
354
        self.assertEqual(1, len(builder._backing_indices))
369
355
        self.assertEqual(2, builder._backing_indices[0].key_count())
370
356
        # now back to memory
371
357
        builder.add_node(*nodes[2])
372
358
        self.assertEqual(1, len(builder._nodes))
373
 
        self.assertEqual(1, len(builder._keys))
374
359
        self.assertIs(None, builder._nodes_by_key)
375
360
        # And spills to a second backing index combing all
376
361
        builder.add_node(*nodes[3])
377
362
        self.assertEqual(0, len(builder._nodes))
378
 
        self.assertEqual(0, len(builder._keys))
379
363
        self.assertIs(None, builder._nodes_by_key)
380
364
        self.assertEqual(2, len(builder._backing_indices))
381
365
        self.assertEqual(None, builder._backing_indices[0])
384
368
        builder.add_node(*nodes[4])
385
369
        builder.add_node(*nodes[5])
386
370
        self.assertEqual(0, len(builder._nodes))
387
 
        self.assertEqual(0, len(builder._keys))
388
371
        self.assertIs(None, builder._nodes_by_key)
389
372
        self.assertEqual(2, len(builder._backing_indices))
390
373
        self.assertEqual(2, builder._backing_indices[0].key_count())
448
431
        # Test the parts of the index that take up memory are doing so
449
432
        # predictably.
450
433
        self.assertEqual(1, len(builder._nodes))
451
 
        self.assertEqual(1, len(builder._keys))
452
434
        self.assertIs(None, builder._nodes_by_key)
453
435
        builder.add_node(*nodes[1])
454
436
        self.assertEqual(0, len(builder._nodes))
455
 
        self.assertEqual(0, len(builder._keys))
456
437
        self.assertIs(None, builder._nodes_by_key)
457
438
        self.assertEqual(1, len(builder._backing_indices))
458
439
        self.assertEqual(2, builder._backing_indices[0].key_count())
459
440
        # now back to memory
460
441
        builder.add_node(*nodes[2])
461
442
        self.assertEqual(1, len(builder._nodes))
462
 
        self.assertEqual(1, len(builder._keys))
463
443
        self.assertIs(None, builder._nodes_by_key)
464
444
        # And spills to a second backing index but doesn't combine
465
445
        builder.add_node(*nodes[3])
466
446
        self.assertEqual(0, len(builder._nodes))
467
 
        self.assertEqual(0, len(builder._keys))
468
447
        self.assertIs(None, builder._nodes_by_key)
469
448
        self.assertEqual(2, len(builder._backing_indices))
470
449
        for backing_index in builder._backing_indices:
473
452
        builder.add_node(*nodes[4])
474
453
        builder.add_node(*nodes[5])
475
454
        self.assertEqual(0, len(builder._nodes))
476
 
        self.assertEqual(0, len(builder._keys))
477
455
        self.assertIs(None, builder._nodes_by_key)
478
456
        self.assertEqual(3, len(builder._backing_indices))
479
457
        for backing_index in builder._backing_indices:
538
516
        builder.add_node(*nodes[0])
539
517
        # Test the parts of the index that take up memory are doing so
540
518
        # predictably.
541
 
        self.assertEqual(1, len(builder._keys))
542
519
        self.assertEqual(1, len(builder._nodes))
543
520
        self.assertIs(None, builder._nodes_by_key)
544
521
        builder.add_node(*nodes[1])
545
 
        self.assertEqual(0, len(builder._keys))
546
522
        self.assertEqual(0, len(builder._nodes))
547
523
        self.assertIs(None, builder._nodes_by_key)
548
524
        self.assertEqual(1, len(builder._backing_indices))
551
527
        old = dict(builder._get_nodes_by_key()) #Build up the nodes by key dict
552
528
        builder.add_node(*nodes[2])
553
529
        self.assertEqual(1, len(builder._nodes))
554
 
        self.assertEqual(1, len(builder._keys))
555
530
        self.assertIsNot(None, builder._nodes_by_key)
556
531
        self.assertNotEqual({}, builder._nodes_by_key)
557
532
        # We should have a new entry
559
534
        # And spills to a second backing index combing all
560
535
        builder.add_node(*nodes[3])
561
536
        self.assertEqual(0, len(builder._nodes))
562
 
        self.assertEqual(0, len(builder._keys))
563
537
        self.assertIs(None, builder._nodes_by_key)
564
538
        self.assertEqual(2, len(builder._backing_indices))
565
539
        self.assertEqual(None, builder._backing_indices[0])
568
542
        builder.add_node(*nodes[4])
569
543
        builder.add_node(*nodes[5])
570
544
        self.assertEqual(0, len(builder._nodes))
571
 
        self.assertEqual(0, len(builder._keys))
572
545
        self.assertIs(None, builder._nodes_by_key)
573
546
        self.assertEqual(2, len(builder._backing_indices))
574
547
        self.assertEqual(2, builder._backing_indices[0].key_count())
718
691
        # The entire index should have been read, as it is one page long.
719
692
        self.assertEqual([('readv', 'index', [(0, size)], False, None)],
720
693
            transport._activity)
721
 
        self.assertEqual(1199, size)
 
694
        self.assertEqual(1173, size)
722
695
 
723
696
    def test__read_nodes_no_size_one_page_reads_once(self):
724
697
        self.make_index(nodes=[(('key',), 'value', ())])
772
745
        # The entire index should have been read linearly.
773
746
        self.assertEqual([('readv', 'index', [(0, size)], False, None)],
774
747
            transport._activity)
775
 
        self.assertEqual(1514, size)
 
748
        self.assertEqual(1488, size)
776
749
 
777
750
    def test_validate_two_pages(self):
778
751
        builder = btree_index.BTreeBuilder(key_elements=2, reference_lists=2)
1308
1281
    def test_exists(self):
1309
1282
        # This is just to let the user know if they don't have the feature
1310
1283
        # available
1311
 
        self.requireFeature(CompiledBtreeParserFeature)
 
1284
        self.requireFeature(compiled_btreeparser_feature)
1312
1285
 
1313
1286
 
1314
1287
class TestMultiBisectRight(tests.TestCase):