Przeglądaj źródła

Initial import from https://github.com/Shopify/sarama/

Evan Huus 9 lat temu
rodzic
commit
d7f579c9fc
3 zmienionych plików z 68 dodań i 1 usunięć
  1. 9 1
      README.md
  2. 43 0
      snappy.go
  3. 16 0
      snappy_test.go

+ 9 - 1
README.md

@@ -1,2 +1,10 @@
 # go-xerial-snappy
-Xerial-compatible Snappy framing support for golang
+Xerial-compatible Snappy framing support for golang.
+
+Packages using Xerial for snappy encoding use a framing format incompatible with
+basically everything else in existence. This package wraps Go's built-in snappy
+package to support it.
+
+Apps that use this format include Apache Kafka (see
+https://github.com/dpkp/kafka-python/issues/126#issuecomment-35478921 for
+details).

+ 43 - 0
snappy.go

@@ -0,0 +1,43 @@
+package snappy
+
+import (
+	"bytes"
+	"encoding/binary"
+
+	master "github.com/golang/snappy"
+)
+
+var xerialHeader = []byte{130, 83, 78, 65, 80, 80, 89, 0}
+
+// Encode encodes data as snappy with no framing header.
+func Encode(src []byte) []byte {
+	return master.Encode(nil, src)
+}
+
+// Decode decodes snappy data whether it is traditional unframed
+// or includes the xerial framing format.
+func Decode(src []byte) ([]byte, error) {
+	if !bytes.Equal(src[:8], xerialHeader) {
+		return master.Decode(nil, src)
+	}
+
+	var (
+		pos   = uint32(16)
+		max   = uint32(len(src))
+		dst   = make([]byte, 0, len(src))
+		chunk []byte
+		err   error
+	)
+	for pos < max {
+		size := binary.BigEndian.Uint32(src[pos : pos+4])
+		pos += 4
+
+		chunk, err = master.Decode(chunk, src[pos:pos+size])
+		if err != nil {
+			return nil, err
+		}
+		pos += size
+		dst = append(dst, chunk...)
+	}
+	return dst, nil
+}

Plik diff jest za duży
+ 16 - 0
snappy_test.go


Niektóre pliki nie zostały wyświetlone z powodu dużej ilości zmienionych plików