forked from apache/cassandra-gocql-driver
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add BlobCompressor to compress blob and text fields on fly
- Loading branch information
1 parent
6af144e
commit 6902f8c
Showing
5 changed files
with
172 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package lz4 | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
"fmt" | ||
|
||
"github.com/pierrec/lz4/v4" | ||
) | ||
|
||
type BlobCompressor struct { | ||
prefix []byte | ||
prefixPlusLen int | ||
limit int | ||
} | ||
|
||
var defaultPrefix = []byte{0x01, 0x11, 0x22, 0x33} | ||
|
||
func NewBlobCompressor(limit int) BlobCompressor { | ||
return BlobCompressor{ | ||
prefix: defaultPrefix, | ||
prefixPlusLen: len(defaultPrefix) + 4, | ||
limit: limit, | ||
} | ||
} | ||
|
||
func (c BlobCompressor) Compress(data []byte) ([]byte, error) { | ||
if len(data) < c.limit { | ||
return data, nil | ||
} | ||
buf := make([]byte, len(c.prefix)+lz4.CompressBlockBound(len(data)+4)) | ||
copy(buf, c.prefix) | ||
|
||
var compressor lz4.Compressor | ||
|
||
n, err := compressor.CompressBlock(data, buf[c.prefixPlusLen:]) | ||
// According to lz4.CompressBlock doc, it doesn't fail as long as the dst | ||
// buffer length is at least lz4.CompressBlockBound(len(data))) bytes, but | ||
// we check for error anyway just to be thorough. | ||
if err != nil { | ||
return nil, err | ||
} | ||
binary.BigEndian.PutUint32(buf[len(c.prefix):], uint32(len(data))) | ||
|
||
return buf[:c.prefixPlusLen+n], nil | ||
} | ||
|
||
func (c BlobCompressor) Decompress(data []byte) ([]byte, error) { | ||
if !bytes.HasPrefix(data, c.prefix) { | ||
return data, nil | ||
} | ||
if len(data) < 4+len(c.prefix) { | ||
return nil, fmt.Errorf("compressed data should be >4, got=%d", len(data)) | ||
} | ||
uncompressedLength := binary.BigEndian.Uint32(data[len(c.prefix):]) | ||
if uncompressedLength == 0 { | ||
return nil, nil | ||
} | ||
buf := make([]byte, uncompressedLength) | ||
n, err := lz4.UncompressBlock(data[c.prefixPlusLen:], buf) | ||
return buf[:n], err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters