Example #1
0
    def test_compression_is_stable(self):
        data = json.dumps({"foo": "bar", "dog": "cat"}).encode("utf-8")

        data = snappy.compress(data)
        data = snappy.decompress(data)
        data = snappy.compress(data)
        data = snappy.decompress(data)

        self.assertEqual(
            json.loads(data.decode("utf-8")),
            {"foo": "bar", "dog": "cat"}
        )
Example #2
0
    def test_compression_is_stable(self):
        data = json.dumps({"foo": "bar", "dog": "cat"}).encode("utf-8")

        data = snappy.compress(data)
        data = snappy.decompress(data)
        data = snappy.compress(data)
        data = snappy.decompress(data)

        self.assertEqual(json.loads(data.decode("utf-8")), {
            "foo": "bar",
            "dog": "cat"
        })
Example #3
0
    def parse(cls, buff, offset):
        """
        Given a buffer and offset, returns the parsed `Message` and new offset.

        Parses via the basic ``Part`` parse method, but with added
        decompression support.

        If a parsed message's attributes denote that compression has been used,
        the value is run through the corresponding ``decompress()`` method.
        """
        message, offset = super(Message, cls).parse(buff, offset)

        # the compression scheme is stored in the lowest 2 bits of 'attributes'
        compression = message.attributes & 0b00000011

        if not compression:
            return message, offset

        if compression == GZIP:
            message.value = gzip.decompress(message.value)
        elif compression == SNAPPY:
            message.value = snappy.decompress(message.value)

        return message, offset
Example #4
0
    def parse(cls, buff, offset):
        """
        Given a buffer and offset, returns the parsed `Message` and new offset.

        Parses via the basic ``Part`` parse method, but with added
        decompression support.

        If a parsed message's attributes denote that compression has been used,
        the value is run through the corresponding ``decompress()`` method.
        """
        message, offset = super(Message, cls).parse(buff, offset)

        # the compression scheme is stored in the lowest 2 bits of 'attributes'
        compression = message.attributes & 0b00000011

        if not compression:
            return message, offset

        if compression == GZIP:
            message.value = gzip.decompress(message.value)
        elif compression == SNAPPY:
            message.value = snappy.decompress(message.value)

        return message, offset