From 20ff614aa93923dbb80ba7ca777bedfd2d2bda11 Mon Sep 17 00:00:00 2001 From: "mykyta.oleksiienko" Date: Tue, 25 Mar 2025 12:32:38 +0200 Subject: [PATCH] Move the snappy compressor into a separate go package Currently, Snappy is downloaded and included even when users only require LZ4 compression. To streamline the driver and reduce unnecessary dependency overhead for users who don't utilize Snappy, move the Snappy compressor into its own separate package. This will allow users to include only the compression dependencies they need. Patch by Mykyta Oleksiienko; reviewed by Joao Reis CASSGO-33 --- CHANGELOG.md | 2 ++ common_test.go | 3 +- compressor.go | 27 ------------------ snappy/compressor.go | 28 +++++++++++++++++++ .../compressor_test.go | 2 +- 5 files changed, 33 insertions(+), 29 deletions(-) create mode 100644 snappy/compressor.go rename compressor_test.go => snappy/compressor_test.go (99%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 37ae55e3e..5c75450b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +- Moved the Snappy compressor into its own separate package (CASSGO-33) + - Move lz4 compressor to lz4 package within the gocql module (CASSGO-32) - Don't restrict server authenticator unless PasswordAuthentictor.AllowedAuthenticators is provided (CASSGO-19) diff --git a/common_test.go b/common_test.go index e420bbf16..25f4fc640 100644 --- a/common_test.go +++ b/common_test.go @@ -36,6 +36,7 @@ import ( "time" "github.com/gocql/gocql/lz4" + "github.com/gocql/gocql/snappy" ) var ( @@ -112,7 +113,7 @@ func createCluster(opts ...func(*ClusterConfig)) *ClusterConfig { switch *flagCompressTest { case "snappy": - cluster.Compressor = &SnappyCompressor{} + cluster.Compressor = &snappy.SnappyCompressor{} case "lz4": cluster.Compressor = &lz4.LZ4Compressor{} case "no-compression": diff --git a/compressor.go b/compressor.go index c1b7b2b6f..a4c305b7e 100644 --- a/compressor.go +++ b/compressor.go @@ -24,8 +24,6 @@ package gocql -import "github.com/golang/snappy" - type Compressor interface { Name() string @@ -47,28 +45,3 @@ type Compressor interface { // It returns a new byte slice that is the result of the append operation. AppendDecompressed(dst, src []byte, decompressedLength uint32) ([]byte, error) } - -// SnappyCompressor implements the Compressor interface and can be used to -// compress incoming and outgoing frames. The snappy compression algorithm -// aims for very high speeds and reasonable compression. -type SnappyCompressor struct{} - -func (s SnappyCompressor) Name() string { - return "snappy" -} - -func (s SnappyCompressor) AppendCompressedWithLength(dst, src []byte) ([]byte, error) { - return snappy.Encode(dst, src), nil -} - -func (s SnappyCompressor) AppendDecompressedWithLength(dst, src []byte) ([]byte, error) { - return snappy.Decode(dst, src) -} - -func (s SnappyCompressor) AppendCompressed(dst, src []byte) ([]byte, error) { - panic("SnappyCompressor.AppendCompressed is not supported") -} - -func (s SnappyCompressor) AppendDecompressed(dst, src []byte, decompressedLength uint32) ([]byte, error) { - panic("SnappyCompressor.AppendDecompressed is not supported") -} diff --git a/snappy/compressor.go b/snappy/compressor.go new file mode 100644 index 000000000..faec4a722 --- /dev/null +++ b/snappy/compressor.go @@ -0,0 +1,28 @@ +package snappy + +import "github.com/golang/snappy" + +// SnappyCompressor implements the Compressor interface and can be used to +// compress incoming and outgoing frames. The snappy compression algorithm +// aims for very high speeds and reasonable compression. +type SnappyCompressor struct{} + +func (s SnappyCompressor) Name() string { + return "snappy" +} + +func (s SnappyCompressor) AppendCompressedWithLength(dst, src []byte) ([]byte, error) { + return snappy.Encode(dst, src), nil +} + +func (s SnappyCompressor) AppendDecompressedWithLength(dst, src []byte) ([]byte, error) { + return snappy.Decode(dst, src) +} + +func (s SnappyCompressor) AppendCompressed(dst, src []byte) ([]byte, error) { + panic("SnappyCompressor.AppendCompressed is not supported") +} + +func (s SnappyCompressor) AppendDecompressed(dst, src []byte, decompressedLength uint32) ([]byte, error) { + panic("SnappyCompressor.AppendDecompressed is not supported") +} diff --git a/compressor_test.go b/snappy/compressor_test.go similarity index 99% rename from compressor_test.go rename to snappy/compressor_test.go index d2d2de040..3efe3fa70 100644 --- a/compressor_test.go +++ b/snappy/compressor_test.go @@ -22,7 +22,7 @@ * See the NOTICE file distributed with this work for additional information. */ -package gocql +package snappy import ( "bytes"