Skip to content

Commit

Permalink
Merge pull request #1014 from segmentio/snappy-configurable-level
Browse files Browse the repository at this point in the history
compress/snappy: configurable compression level
  • Loading branch information
chriso committed Oct 26, 2022
2 parents 7501938 + cd7d6f4 commit c1240f0
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions compress/snappy/snappy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io"
"sync"

"github.com/klauspost/compress/s2"
"github.com/klauspost/compress/snappy"
)

Expand All @@ -16,6 +17,16 @@ const (
Unframed
)

// Compression level.
type Compression int

const (
DefaultCompression Compression = iota
FasterCompression
BetterCompression
BestCompression
)

var (
readerPool sync.Pool
writerPool sync.Pool
Expand All @@ -28,6 +39,9 @@ type Codec struct {
//
// Default to Framed.
Framing Framing

// Compression level.
Compression Compression
}

// Code implements the compress.Codec interface.
Expand Down Expand Up @@ -56,12 +70,19 @@ func (c *Codec) NewWriter(w io.Writer) io.WriteCloser {
if x != nil {
x.Reset(w)
} else {
x = &xerialWriter{
writer: w,
encode: snappy.Encode,
}
x = &xerialWriter{writer: w}
}
x.framed = c.Framing == Framed
switch c.Compression {
case FasterCompression:
x.encode = s2.EncodeSnappy
case BetterCompression:
x.encode = s2.EncodeSnappyBetter
case BestCompression:
x.encode = s2.EncodeSnappyBest
default:
x.encode = snappy.Encode // aka. s2.EncodeSnappyBetter
}
return &writer{xerialWriter: x}
}

Expand Down

0 comments on commit c1240f0

Please sign in to comment.