Browse Source

Merge pull request #1182 from retailnext/zstd-build-constraints

Fix build without cgo
Vlad Gorodetsky 6 years ago
parent
commit
f1aa91b290
4 changed files with 34 additions and 6 deletions
  1. 2 3
      message.go
  2. 2 3
      record_batch.go
  3. 13 0
      zstd_cgo.go
  4. 17 0
      zstd_fallback.go

+ 2 - 3
message.go

@@ -7,7 +7,6 @@ import (
 	"io/ioutil"
 	"time"
 
-	"github.com/DataDog/zstd"
 	"github.com/eapache/go-xerial-snappy"
 	"github.com/pierrec/lz4"
 )
@@ -116,7 +115,7 @@ func (m *Message) encode(pe packetEncoder) error {
 			m.compressedCache = buf.Bytes()
 			payload = m.compressedCache
 		case CompressionZSTD:
-			c, err := zstd.CompressLevel(nil, m.Value, m.CompressionLevel)
+			c, err := zstdCompressLevel(nil, m.Value, m.CompressionLevel)
 			if err != nil {
 				return err
 			}
@@ -219,7 +218,7 @@ func (m *Message) decode(pd packetDecoder) (err error) {
 		if m.Value == nil {
 			break
 		}
-		if m.Value, err = zstd.Decompress(nil, m.Value); err != nil {
+		if m.Value, err = zstdDecompress(nil, m.Value); err != nil {
 			return err
 		}
 		if err := m.decodeSet(); err != nil {

+ 2 - 3
record_batch.go

@@ -7,7 +7,6 @@ import (
 	"io/ioutil"
 	"time"
 
-	"github.com/DataDog/zstd"
 	"github.com/eapache/go-xerial-snappy"
 	"github.com/pierrec/lz4"
 )
@@ -195,7 +194,7 @@ func (b *RecordBatch) decode(pd packetDecoder) (err error) {
 			return err
 		}
 	case CompressionZSTD:
-		if recBuffer, err = zstd.Decompress(nil, recBuffer); err != nil {
+		if recBuffer, err = zstdDecompress(nil, recBuffer); err != nil {
 			return err
 		}
 	default:
@@ -254,7 +253,7 @@ func (b *RecordBatch) encodeRecords(pe packetEncoder) error {
 		}
 		b.compressedRecords = buf.Bytes()
 	case CompressionZSTD:
-		c, err := zstd.CompressLevel(nil, raw, b.CompressionLevel)
+		c, err := zstdCompressLevel(nil, raw, b.CompressionLevel)
 		if err != nil {
 			return err
 		}

+ 13 - 0
zstd_cgo.go

@@ -0,0 +1,13 @@
+// +build cgo
+
+package sarama
+
+import "github.com/DataDog/zstd"
+
+func zstdDecompress(dst, src []byte) ([]byte, error) {
+	return zstd.Decompress(dst, src)
+}
+
+func zstdCompressLevel(dst, src []byte, level int) ([]byte, error) {
+	return zstd.CompressLevel(dst, src, level)
+}

+ 17 - 0
zstd_fallback.go

@@ -0,0 +1,17 @@
+// +build !cgo
+
+package sarama
+
+import (
+	"errors"
+)
+
+var errZstdCgo = errors.New("zstd compression requires building with cgo enabled")
+
+func zstdDecompress(dst, src []byte) ([]byte, error) {
+	return nil, errZstdCgo
+}
+
+func zstdCompressLevel(dst, src []byte, level int) ([]byte, error) {
+	return nil, errZstdCgo
+}