/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_groupcompress.py

  • Committer: John Arbash Meinel
  • Date: 2009-03-17 03:43:24 UTC
  • mto: (3735.2.156 brisbane-core)
  • mto: This revision was merged to the branch mainline in revision 4280.
  • Revision ID: john@arbash-meinel.com-20090317034324-1nqpftq6na4rz8ft
Change the GroupCompressBlock code to allow not recording 'end'.

As long as we know what gc block we need to read, we don't care about how long
a record is. As it is already recorded in the data stream.
We already have read the whole compressed block into memory, so we aren't saving
a round trip. Though if we want trivial stripping of unused content,
we may want to keep the extra data in the index.
It still is good to have the functionality available to the extract() function.

Show diffs side-by-side

added added

removed removed

Lines of Context:
187
187
 
188
188
class TestGroupCompressBlock(tests.TestCase):
189
189
 
 
190
    def make_block(self, key_to_text):
 
191
        """Create a GroupCompressBlock, filling it with the given texts."""
 
192
        compressor = groupcompress.GroupCompressor()
 
193
        start = 0
 
194
        for key in sorted(key_to_text):
 
195
            compressor.compress(key, key_to_text[key], None)
 
196
        entries = compressor._block._entries
 
197
        raw_bytes = compressor.flush()
 
198
        return entries, groupcompress.GroupCompressBlock.from_bytes(raw_bytes)
 
199
 
190
200
    def test_from_empty_bytes(self):
191
201
        self.assertRaises(ValueError,
192
202
                          groupcompress.GroupCompressBlock.from_bytes, '')
308
318
                             'length:100\n'
309
319
                             '\n', raw_bytes)
310
320
 
 
321
    def test_extract_no_end(self):
 
322
        # We should be able to extract a record, even if we only know the start
 
323
        # of the bytes.
 
324
        texts = {
 
325
            ('key1',): 'text for key1\nhas bytes that are common\n',
 
326
            ('key2',): 'text for key2\nhas bytes that are common\n',
 
327
        }
 
328
        entries, block = self.make_block(texts)
 
329
        self.assertEqualDiff('text for key1\nhas bytes that are common\n',
 
330
                             block.extract(('key1',), entries[('key1',)].start,
 
331
                                           end=None)[1])
 
332
        self.assertEqualDiff('text for key2\nhas bytes that are common\n',
 
333
                             block.extract(('key2',), entries[('key2',)].start,
 
334
                                           end=None)[1])
 
335
 
311
336
    def test_partial_decomp(self):
312
337
        content_chunks = []
313
338
        # We need a sufficient amount of data so that zlib.decompress has
451
476
                   "with a reasonable amount of compressible bytes\n",
452
477
        ('key2',): "another text\n"
453
478
                   "with a reasonable amount of compressible bytes\n",
 
479
        ('key3',): "yet another text which won't be extracted\n"
 
480
                   "with a reasonable amount of compressible bytes\n",
 
481
        ('key4',): "this will be extracted\n"
 
482
                   "but references bytes from\n"
 
483
                   "yet another text which won't be extracted\n"
 
484
                   "with a reasonable amount of compressible bytes\n",
454
485
    }
455
486
    def make_block(self, key_to_text):
456
487
        """Create a GroupCompressBlock, filling it with the given texts."""
462
493
        raw_bytes = compressor.flush()
463
494
        return entries, groupcompress.GroupCompressBlock.from_bytes(raw_bytes)
464
495
 
 
496
    def add_key_to_manager(self, key, entries, block, manager):
 
497
        entry = entries[key]
 
498
        manager.add_factory(entry.key, (), entry.start, entry.end)
 
499
 
465
500
    def test_get_fulltexts(self):
466
501
        entries, block = self.make_block(self._texts)
467
502
        manager = groupcompress.LazyGroupContentManager(block)
468
 
        entry = entries[('key1',)]
469
 
        manager.add_factory(entry.key, (), entry.start, entry.end)
470
 
        entry = entries[('key2',)]
471
 
        manager.add_factory(entry.key, (), entry.start, entry.end)
472
 
        result_order = []
473
 
        for record in manager.get_record_stream():
474
 
            result_order.append(record.key)
475
 
            text = self._texts[record.key]
476
 
            self.assertEqual(text, record.get_bytes_as('fulltext'))
 
503
        self.add_key_to_manager(('key1',), entries, block, manager)
 
504
        self.add_key_to_manager(('key2',), entries, block, manager)
 
505
        result_order = []
 
506
        for record in manager.get_record_stream():
 
507
            result_order.append(record.key)
 
508
            text = self._texts[record.key]
 
509
            self.assertEqual(text, record.get_bytes_as('fulltext'))
 
510
        self.assertEqual([('key1',), ('key2',)], result_order)
 
511
 
 
512
        # If we build the manager in the opposite order, we should get them
 
513
        # back in the opposite order
 
514
        manager = groupcompress.LazyGroupContentManager(block)
 
515
        self.add_key_to_manager(('key2',), entries, block, manager)
 
516
        self.add_key_to_manager(('key1',), entries, block, manager)
 
517
        result_order = []
 
518
        for record in manager.get_record_stream():
 
519
            result_order.append(record.key)
 
520
            text = self._texts[record.key]
 
521
            self.assertEqual(text, record.get_bytes_as('fulltext'))
 
522
        self.assertEqual([('key2',), ('key1',)], result_order)
 
523
 
 
524
    def test__wire_bytes(self):
 
525
        entries, block = self.make_block(self._texts)