~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-14 15:43:09 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-20090314154309-4l8c12173z21vmfz
Change the byte representation of a groupcompress block.
We now include the length of the compressed and uncompressed content.
The important bit is the length of uncompressed content, as it allows
to potentially pre-allocate the decomp buffer. But more important than
that, is that it allows us to know how much of the content we will
be processing during 'extract', just from the index entries and that
header block.

Show diffs side-by-side

added added

removed removed

Lines of Context:
190
190
                          groupcompress.GroupCompressBlock.from_bytes, '')
191
191
 
192
192
    def test_from_minimal_bytes(self):
193
 
        block = groupcompress.GroupCompressBlock.from_bytes('gcb1z\n0\n0\n')
 
193
        block = groupcompress.GroupCompressBlock.from_bytes(
 
194
            'gcb1z\n0\n0\n0\n0\n')
194
195
        self.assertIsInstance(block, groupcompress.GroupCompressBlock)
195
196
        self.assertEqual({}, block._entries)
 
197
        self.assertIs(None, block._content)
196
198
 
197
199
    def test_from_bytes(self):
198
 
        z_header_bytes = (
199
 
            'gcb1z\n' # group compress block v1 plain
200
 
            '76\n' # Length of zlib bytes
201
 
            '183\n' # Length of all meta-info
202
 
            + zlib.compress(
203
 
            'key:bing\n'
 
200
        header = ('key:bing\n'
204
201
            'sha1:abcdabcdabcdabcdabcdabcdabcdabcdabcdabcd\n'
205
202
            'type:fulltext\n'
206
203
            'start:100\n'
211
208
            'type:fulltext\n'
212
209
            'start:0\n'
213
210
            'length:100\n'
214
 
            '\n'))
 
211
            '\n')
 
212
        z_header = zlib.compress(header)
 
213
        content = ('a tiny bit of content\n')
 
214
        z_content = zlib.compress(content)
 
215
        z_bytes = (
 
216
            'gcb1z\n' # group compress block v1 plain
 
217
            '%d\n' # Length of zlib bytes
 
218
            '%d\n' # Length of all meta-info
 
219
            '%d\n' # Length of compressed content
 
220
            '%d\n' # Length of uncompressed content
 
221
            '%s'   # Compressed header
 
222
            '%s'   # Compressed content
 
223
            ) % (len(z_header), len(header),
 
224
                 len(z_content), len(content),
 
225
                 z_header, z_content)
215
226
        block = groupcompress.GroupCompressBlock.from_bytes(
216
 
            z_header_bytes)
217
 
        self.assertIs(None, block._content)
 
227
            z_bytes)
218
228
        self.assertIsInstance(block, groupcompress.GroupCompressBlock)
219
229
        self.assertEqual([('bing',), ('foo', 'bar')], sorted(block._entries))
220
230
        bing = block._entries[('bing',)]
229
239
        self.assertEqual('abcd'*10, foobar.sha1)
230
240
        self.assertEqual(0, foobar.start)
231
241
        self.assertEqual(100, foobar.length)
 
242
        self.assertEqual(content, block._content)
232
243
 
233
244
    def test_add_entry(self):
234
245
        gcb = groupcompress.GroupCompressBlock()
244
255
        gcb = groupcompress.GroupCompressBlock()
245
256
        gcb.add_entry(('foo', 'bar'), 'fulltext', 'abcd'*10, 0, 100)
246
257
        gcb.add_entry(('bing',), 'fulltext', 'abcd'*10, 100, 100)
247
 
        bytes = gcb.to_bytes()
248
 
        self.assertStartsWith(bytes,
249
 
                              'gcb1z\n' # group compress block v1 zlib
250
 
                              '76\n' # Length of compressed bytes
251
 
                              '183\n' # Length of all meta-info
252
 
                             )
253
 
        remaining_bytes = bytes[13:]
 
258
        bytes = gcb.to_bytes('this is some content\n'
 
259
                             'this content will be compressed\n')
 
260
        expected_header =('gcb1z\n' # group compress block v1 zlib
 
261
                          '76\n' # Length of compressed bytes
 
262
                          '183\n' # Length of uncompressed meta-info
 
263
                          '50\n' # Length of compressed content
 
264
                          '53\n' # Length of uncompressed content
 
265
                         )
 
266
        self.assertStartsWith(bytes, expected_header)
 
267
        remaining_bytes = bytes[len(expected_header):]
254
268
        raw_bytes = zlib.decompress(remaining_bytes)
255
269
        self.assertEqualDiff('key:bing\n'
256
270
                             'sha1:abcdabcdabcdabcdabcdabcdabcdabcdabcdabcd\n'