Skip to content

Commit 0d44add

Browse files
committed
compression: add Compressor.Algorithm() convenience method
1 parent 08c01fe commit 0d44add

File tree

7 files changed

+15
-4
lines changed

7 files changed

+15
-4
lines changed

internal/compression/compression.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ var (
7272
// Compressor is an interface for compressing data. An instance is associated
7373
// with a specific Setting.
7474
type Compressor interface {
75+
// Algorithm returns the algorithm used by this Compressor.
76+
Algorithm() Algorithm
77+
7578
// Compress a block, appending the compressed data to dst[:0].
7679
Compress(dst, src []byte) []byte
7780

internal/compression/minlz.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ type minlzCompressor struct {
1616

1717
var _ Compressor = (*minlzCompressor)(nil)
1818

19+
func (c *minlzCompressor) Algorithm() Algorithm { return MinLZ }
20+
1921
func (c *minlzCompressor) Compress(dst, src []byte) []byte {
2022
// MinLZ cannot encode blocks greater than 8MB. Fall back to Snappy in those
2123
// cases. Note that MinLZ can decode the Snappy compressed block.

internal/compression/noop.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ type noopCompressor struct{}
88

99
var _ Compressor = noopCompressor{}
1010

11+
func (noopCompressor) Algorithm() Algorithm { return NoCompression }
1112
func (noopCompressor) Compress(dst, src []byte) []byte {
1213
return append(dst[:0], src...)
1314
}

internal/compression/snappy.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ type snappyCompressor struct{}
1414

1515
var _ Compressor = snappyCompressor{}
1616

17+
func (snappyCompressor) Algorithm() Algorithm { return SnappyAlgorithm }
18+
1719
func (snappyCompressor) Compress(dst, src []byte) []byte {
1820
dst = dst[:cap(dst):cap(dst)]
1921
return snappy.Encode(dst, src)

internal/compression/zstd_cgo.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ var zstdCompressorPool = sync.Pool{
3939
// relies on CGo.
4040
const UseStandardZstdLib = true
4141

42+
func (z *zstdCompressor) Algorithm() Algorithm { return Zstd }
43+
4244
func (z *zstdCompressor) Compress(compressedBuf []byte, b []byte) []byte {
4345
if len(compressedBuf) < binary.MaxVarintLen64 {
4446
compressedBuf = append(compressedBuf, make([]byte, binary.MaxVarintLen64-len(compressedBuf))...)

internal/compression/zstd_nocgo.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ func getZstdCompressor(level int) *zstdCompressor {
3434
// relies on CGo.
3535
const UseStandardZstdLib = false
3636

37+
func (z *zstdCompressor) Algorithm() Algorithm { return Zstd }
38+
3739
func (z *zstdCompressor) Compress(compressedBuf, b []byte) []byte {
3840
if len(compressedBuf) < binary.MaxVarintLen64 {
3941
compressedBuf = append(compressedBuf, make([]byte, binary.MaxVarintLen64-len(compressedBuf))...)

sstable/block/compressor.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,8 @@ func (c *Compressor) Close() {
4848
//
4949
// In addition to the buffer, returns the algorithm that was used.
5050
func (c *Compressor) Compress(dst, src []byte, kind Kind) (CompressionIndicator, []byte) {
51-
setting := c.profile.DataBlocks
5251
compressor := c.dataBlocksCompressor
5352
if kind != blockkind.SSTableData && kind != blockkind.SSTableValue && kind != blockkind.BlobValue {
54-
setting = c.profile.OtherBlocks
5553
compressor = c.otherBlocksCompressor
5654
}
5755
out := compressor.Compress(dst, src)
@@ -62,11 +60,12 @@ func (c *Compressor) Compress(dst, src []byte, kind Kind) (CompressionIndicator,
6260
// after * 100
6361
// ----------- > 100 - MinReductionPercent
6462
// before
65-
if setting.Algorithm != compression.NoCompression &&
63+
algorithm := compressor.Algorithm()
64+
if algorithm != compression.NoCompression &&
6665
int64(len(out))*100 > int64(len(src))*int64(100-c.profile.MinReductionPercent) {
6766
return NoCompressionIndicator, append(out[:0], src...)
6867
}
69-
return compressionIndicatorFromAlgorithm(setting.Algorithm), out
68+
return compressionIndicatorFromAlgorithm(algorithm), out
7069
}
7170

7271
// NoopCompressor is a Compressor that does not compress data. It does not have

0 commit comments

Comments
 (0)