~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_groupcompress.py

  • Committer: John Arbash Meinel
  • Date: 2009-03-27 22:05:37 UTC
  • mto: (3735.39.2 clean)
  • mto: This revision was merged to the branch mainline in revision 4280.
  • Revision ID: john@arbash-meinel.com-20090327220537-loj7fdr9hi360qc3
Get rid of the entries dict in GroupCompressBlock.
We weren't making use of it, and it was overhead to update it.
This simplifies the code a bit more.
The only difference now between the python and pyrex compressors
is the __init__ and _compress functions.
If it wasn't for circular import issues, I would be tempted
to move them one step further, into the _groupcompress_* modules.

Show diffs side-by-side

added added

removed removed

Lines of Context:
287
287
        start = 0
288
288
        for key in sorted(key_to_text):
289
289
            compressor.compress(key, key_to_text[key], None)
 
290
        locs = dict((key, (start, end)) for key, (start, _, end, _)
 
291
                    in compressor.labels_deltas.iteritems())
290
292
        block = compressor.flush()
291
 
        entries = block._entries
 
293
        raw_bytes = block.to_bytes()
292
294
        # Go through from_bytes(to_bytes()) so that we start with a compressed
293
295
        # content object
294
 
        return entries, groupcompress.GroupCompressBlock.from_bytes(
295
 
            block.to_bytes())
 
296
        return locs, groupcompress.GroupCompressBlock.from_bytes(raw_bytes)
296
297
 
297
298
    def test_from_empty_bytes(self):
298
299
        self.assertRaises(ValueError,
302
303
        block = groupcompress.GroupCompressBlock.from_bytes(
303
304
            'gcb1z\n0\n0\n')
304
305
        self.assertIsInstance(block, groupcompress.GroupCompressBlock)
305
 
        self.assertEqual({}, block._entries)
306
306
        self.assertIs(None, block._content)
307
307
        self.assertEqual('', block._z_content)
308
308
        block._ensure_content()
329
329
        self.assertEqual(z_content, block._z_content)
330
330
        self.assertEqual(content, block._content)
331
331
 
332
 
    def test_add_entry(self):
333
 
        gcb = groupcompress.GroupCompressBlock()
334
 
        e = gcb.add_entry(('foo', 'bar'), 'fulltext', 'abcd'*10, 0, 100)
335
 
        self.assertIsInstance(e, groupcompress.GroupCompressBlockEntry)
336
 
        self.assertEqual(('foo', 'bar'), e.key)
337
 
        self.assertEqual('fulltext', e.type)
338
 
        self.assertEqual('abcd'*10, e.sha1)
339
 
        self.assertEqual(0, e.start)
340
 
        self.assertEqual(100, e.length)
341
 
 
342
332
    def test_to_bytes(self):
343
333
        content = ('this is some content\n'
344
334
                   'this content will be compressed\n')
345
335
        gcb = groupcompress.GroupCompressBlock()
346
 
        gcb.add_entry(('foo', 'bar'), 'fulltext', 'abcd'*10, 0, 100)
347
 
        gcb.add_entry(('bing',), 'fulltext', 'abcd'*10, 100, 100)
348
336
        gcb.set_content(content)
349
337
        bytes = gcb.to_bytes()
350
338
        self.assertEqual(gcb._z_content_length, len(gcb._z_content))
612
600
        start = 0
613
601
        for key in sorted(key_to_text):
614
602
            compressor.compress(key, key_to_text[key], None)
 
603
        locs = dict((key, (start, end)) for key, (start, _, end, _)
 
604
                    in compressor.labels_deltas.iteritems())
615
605
        block = compressor.flush()
616
 
        entries = block._entries
617
606
        raw_bytes = block.to_bytes()
618
 
        return entries, groupcompress.GroupCompressBlock.from_bytes(raw_bytes)
 
607
        return locs, groupcompress.GroupCompressBlock.from_bytes(raw_bytes)
619
608
 
620
 
    def add_key_to_manager(self, key, entries, block, manager):
621
 
        entry = entries[key]
622
 
        manager.add_factory(entry.key, (), entry.start, entry.end)
 
609
    def add_key_to_manager(self, key, locations, block, manager):
 
610
        start, end = locations[key]
 
611
        manager.add_factory(key, (), start, end)
623
612
 
624
613
    def test_get_fulltexts(self):
625
 
        entries, block = self.make_block(self._texts)
 
614
        locations, block = self.make_block(self._texts)
626
615
        manager = groupcompress._LazyGroupContentManager(block)
627
 
        self.add_key_to_manager(('key1',), entries, block, manager)
628
 
        self.add_key_to_manager(('key2',), entries, block, manager)
 
616
        self.add_key_to_manager(('key1',), locations, block, manager)
 
617
        self.add_key_to_manager(('key2',), locations, block, manager)
629
618
        result_order = []
630
619
        for record in manager.get_record_stream():
631
620
            result_order.append(record.key)
636
625
        # If we build the manager in the opposite order, we should get them
637
626
        # back in the opposite order
638
627
        manager = groupcompress._LazyGroupContentManager(block)
639
 
        self.add_key_to_manager(('key2',), entries, block, manager)
640
 
        self.add_key_to_manager(('key1',), entries, block, manager)
 
628
        self.add_key_to_manager(('key2',), locations, block, manager)
 
629
        self.add_key_to_manager(('key1',), locations, block, manager)
641
630
        result_order = []
642
631
        for record in manager.get_record_stream():
643
632
            result_order.append(record.key)
646
635
        self.assertEqual([('key2',), ('key1',)], result_order)
647
636
 
648
637
    def test__wire_bytes_no_keys(self):
649
 
        entries, block = self.make_block(self._texts)
 
638
        locations, block = self.make_block(self._texts)
650
639
        manager = groupcompress._LazyGroupContentManager(block)
651
640
        wire_bytes = manager._wire_bytes()
652
641
        block_length = len(block.to_bytes())
665
654
                         wire_bytes)
666
655
 
667
656
    def test__wire_bytes(self):
668
 
        entries, block = self.make_block(self._texts)
 
657
        locations, block = self.make_block(self._texts)
669
658
        manager = groupcompress._LazyGroupContentManager(block)
670
 
        self.add_key_to_manager(('key1',), entries, block, manager)
671
 
        self.add_key_to_manager(('key4',), entries, block, manager)
 
659
        self.add_key_to_manager(('key1',), locations, block, manager)
 
660
        self.add_key_to_manager(('key4',), locations, block, manager)
672
661
        block_bytes = block.to_bytes()
673
662
        wire_bytes = manager._wire_bytes()
674
663
        (storage_kind, z_header_len, header_len,
683
672
        z_header = rest[:z_header_len]
684
673
        header = zlib.decompress(z_header)
685
674
        self.assertEqual(header_len, len(header))
686
 
        entry1 = entries[('key1',)]
687
 
        entry4 = entries[('key4',)]
 
675
        entry1 = locations[('key1',)]
 
676
        entry4 = locations[('key4',)]
688
677
        self.assertEqualDiff('key1\n'
689
678
                             '\n'  # no parents
690
679
                             '%d\n' # start offset
693
682
                             '\n'
694
683
                             '%d\n'
695
684
                             '%d\n'
696
 
                             % (entry1.start, entry1.end,
697
 
                                entry4.start, entry4.end),
 
685
                             % (entry1[0], entry1[1],
 
686
                                entry4[0], entry4[1]),
698
687
                            header)
699
688
        z_block = rest[z_header_len:]
700
689
        self.assertEqual(block_bytes, z_block)
701
690
 
702
691
    def test_from_bytes(self):
703
 
        entries, block = self.make_block(self._texts)
 
692
        locations, block = self.make_block(self._texts)
704
693
        manager = groupcompress._LazyGroupContentManager(block)
705
 
        self.add_key_to_manager(('key1',), entries, block, manager)
706
 
        self.add_key_to_manager(('key4',), entries, block, manager)
 
694
        self.add_key_to_manager(('key1',), locations, block, manager)
 
695
        self.add_key_to_manager(('key4',), locations, block, manager)
707
696
        wire_bytes = manager._wire_bytes()
708
697
        self.assertStartsWith(wire_bytes, 'groupcompress-block\n')
709
698
        manager = groupcompress._LazyGroupContentManager.from_bytes(wire_bytes)
718
707
        self.assertEqual([('key1',), ('key4',)], result_order)
719
708
 
720
709
    def test__check_rebuild_no_changes(self):
721
 
        entries, block = self.make_block(self._texts)
 
710
        locations, block = self.make_block(self._texts)
722
711
        manager = groupcompress._LazyGroupContentManager(block)
723
712
        # Request all the keys, which ensures that we won't rebuild
724
 
        self.add_key_to_manager(('key1',), entries, block, manager)
725
 
        self.add_key_to_manager(('key2',), entries, block, manager)
726
 
        self.add_key_to_manager(('key3',), entries, block, manager)
727
 
        self.add_key_to_manager(('key4',), entries, block, manager)
 
713
        self.add_key_to_manager(('key1',), locations, block, manager)
 
714
        self.add_key_to_manager(('key2',), locations, block, manager)
 
715
        self.add_key_to_manager(('key3',), locations, block, manager)
 
716
        self.add_key_to_manager(('key4',), locations, block, manager)
728
717
        manager._check_rebuild_block()
729
718
        self.assertIs(block, manager._block)
730
719
 
731
720
    def test__check_rebuild_only_one(self):
732
 
        entries, block = self.make_block(self._texts)
 
721
        locations, block = self.make_block(self._texts)
733
722
        manager = groupcompress._LazyGroupContentManager(block)
734
723
        # Request just the first key, which should trigger a 'strip' action
735
 
        self.add_key_to_manager(('key1',), entries, block, manager)
 
724
        self.add_key_to_manager(('key1',), locations, block, manager)
736
725
        manager._check_rebuild_block()
737
726
        self.assertIsNot(block, manager._block)
738
727
        self.assertTrue(block._content_length > manager._block._content_length)
744
733
                             record.get_bytes_as('fulltext'))
745
734
 
746
735
    def test__check_rebuild_middle(self):
747
 
        entries, block = self.make_block(self._texts)
 
736
        locations, block = self.make_block(self._texts)
748
737
        manager = groupcompress._LazyGroupContentManager(block)
749
738
        # Request a small key in the middle should trigger a 'rebuild'
750
 
        self.add_key_to_manager(('key4',), entries, block, manager)
 
739
        self.add_key_to_manager(('key4',), locations, block, manager)
751
740
        manager._check_rebuild_block()
752
741
        self.assertIsNot(block, manager._block)
753
742
        self.assertTrue(block._content_length > manager._block._content_length)