compress.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package compress
  2. import (
  3. "io"
  4. "github.com/segmentio/kafka-go/compress/gzip"
  5. "github.com/segmentio/kafka-go/compress/lz4"
  6. "github.com/segmentio/kafka-go/compress/snappy"
  7. "github.com/segmentio/kafka-go/compress/zstd"
  8. )
  9. // Compression represents the the compression applied to a record set.
  10. type Compression int8
  11. const (
  12. Gzip Compression = 1
  13. Snappy Compression = 2
  14. Lz4 Compression = 3
  15. Zstd Compression = 4
  16. )
  17. func (c Compression) Codec() Codec {
  18. if i := int(c); i >= 0 && i < len(Codecs) {
  19. return Codecs[i]
  20. }
  21. return nil
  22. }
  23. func (c Compression) String() string {
  24. if codec := c.Codec(); codec != nil {
  25. return codec.Name()
  26. }
  27. return "uncompressed"
  28. }
  29. // Codec represents a compression codec to encode and decode the messages.
  30. // See : https://cwiki.apache.org/confluence/display/KAFKA/Compression
  31. //
  32. // A Codec must be safe for concurrent access by multiple go routines.
  33. type Codec interface {
  34. // Code returns the compression codec code
  35. Code() int8
  36. // Human-readable name for the codec.
  37. Name() string
  38. // Constructs a new reader which decompresses data from r.
  39. NewReader(r io.Reader) io.ReadCloser
  40. // Constructs a new writer which writes compressed data to w.
  41. NewWriter(w io.Writer) io.WriteCloser
  42. }
  43. var (
  44. // The global gzip codec installed on the Codecs table.
  45. GzipCodec gzip.Codec
  46. // The global snappy codec installed on the Codecs table.
  47. SnappyCodec snappy.Codec
  48. // The global lz4 codec installed on the Codecs table.
  49. Lz4Codec lz4.Codec
  50. // The global zstd codec installed on the Codecs table.
  51. ZstdCodec zstd.Codec
  52. // The global table of compression codecs supported by the kafka protocol.
  53. Codecs = [...]Codec{
  54. Gzip: &GzipCodec,
  55. Snappy: &SnappyCodec,
  56. Lz4: &Lz4Codec,
  57. Zstd: &ZstdCodec,
  58. }
  59. )