github.com/parquet-go/parquet-go@v0.20.0/compress/brotli/brotli.go (about)

     1  // Package brotli implements the BROTLI parquet compression codec.
     2  package brotli
     3  
     4  import (
     5  	"io"
     6  
     7  	"github.com/andybalholm/brotli"
     8  	"github.com/parquet-go/parquet-go/compress"
     9  	"github.com/parquet-go/parquet-go/format"
    10  )
    11  
    12  const (
    13  	DefaultQuality = 0
    14  	DefaultLGWin   = 0
    15  )
    16  
    17  type Codec struct {
    18  	// Quality controls the compression-speed vs compression-density trade-offs.
    19  	// The higher the quality, the slower the compression. Range is 0 to 11.
    20  	Quality int
    21  	// LGWin is the base 2 logarithm of the sliding window size.
    22  	// Range is 10 to 24. 0 indicates automatic configuration based on Quality.
    23  	LGWin int
    24  
    25  	r compress.Decompressor
    26  	w compress.Compressor
    27  }
    28  
    29  func (c *Codec) String() string {
    30  	return "BROTLI"
    31  }
    32  
    33  func (c *Codec) CompressionCodec() format.CompressionCodec {
    34  	return format.Brotli
    35  }
    36  
    37  func (c *Codec) Encode(dst, src []byte) ([]byte, error) {
    38  	return c.w.Encode(dst, src, func(w io.Writer) (compress.Writer, error) {
    39  		return brotli.NewWriterOptions(w, brotli.WriterOptions{
    40  			Quality: c.Quality,
    41  			LGWin:   c.LGWin,
    42  		}), nil
    43  	})
    44  }
    45  
    46  func (c *Codec) Decode(dst, src []byte) ([]byte, error) {
    47  	return c.r.Decode(dst, src, func(r io.Reader) (compress.Reader, error) {
    48  		return reader{brotli.NewReader(r)}, nil
    49  	})
    50  }
    51  
    52  type reader struct{ *brotli.Reader }
    53  
    54  func (reader) Close() error { return nil }