Skip to content

Commit

Permalink
snappy: configurable compression level
Browse files Browse the repository at this point in the history
  • Loading branch information
chriso committed Oct 22, 2022
1 parent 40898d3 commit cd7d6f4
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 cd7d6f4

Please sign in to comment.