Skip to content

Commit

Permalink
Merge pull request apache#2 from merlimat/lz4-go
Browse files Browse the repository at this point in the history
Use pure Go LZ4 compression
  • Loading branch information
merlimat authored May 14, 2019
2 parents 9c5d509 + a913fc6 commit b7700c0
Showing 1 changed file with 35 additions and 5 deletions.
40 changes: 35 additions & 5 deletions pulsar/internal/compression/lz4.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
package compression

import (
"github.com/cloudflare/golz4"
"github.com/pierrec/lz4"
)

type lz4Provider struct {
Expand All @@ -31,17 +31,47 @@ func NewLz4Provider() Provider {
}

func (lz4Provider) Compress(data []byte) []byte {
maxSize := lz4.CompressBound(data)
const tableSize = 1 << 16
hashTable := make([]int, tableSize)

maxSize := lz4.CompressBlockBound(len(data))
compressed := make([]byte, maxSize)
size, err := lz4.Compress(data, compressed)
size, err := lz4.CompressBlock(data, compressed, hashTable)
if err != nil {
panic("Failed to compress")
}
return compressed[:size]

if size == 0 {
// The data block was not compressed. Just repeat it with
// the block header flag to signal it's not compressed
headerSize := writeSize(len(data), compressed)
copy(compressed[headerSize:], data)
return compressed[:len(data)+headerSize]
} else {
return compressed[:size]
}
}

// Write the encoded size for the uncompressed payload
func writeSize(size int, dst []byte) int {
if size < 0xF {
dst[0] |= byte(size << 4)
return 1
} else {
dst[0] |= 0xF0
l := size - 0xF
i := 1
for ; l >= 0xFF; l -= 0xFF {
dst[i] = 0xFF
i++
}
dst[i] = byte(l)
return i + 1
}
}

func (lz4Provider) Decompress(compressedData []byte, originalSize int) ([]byte, error) {
uncompressed := make([]byte, originalSize)
err := lz4.Uncompress(compressedData, uncompressed)
_, err := lz4.UncompressBlock(compressedData, uncompressed)
return uncompressed, err
}

0 comments on commit b7700c0

Please sign in to comment.