Documentation ¶
Overview ¶
Package gozstd is Go wrapper for zstd.
Gozstd is used in https://github.com/VictoriaMetrics/VictoriaMetrics .
Index ¶
- Constants
- func BuildDict(samples [][]byte, desiredDictLen int) []byte
- func Compress(dst, src []byte) []byte
- func CompressDict(dst, src []byte, cd *CDict) []byte
- func CompressLevel(dst, src []byte, compressionLevel int) []byte
- func Decompress(dst, src []byte) ([]byte, error)
- func DecompressDict(dst, src []byte, dd *DDict) ([]byte, error)
- func StreamCompress(dst io.Writer, src io.Reader) error
- func StreamCompressDict(dst io.Writer, src io.Reader, cd *CDict) error
- func StreamCompressLevel(dst io.Writer, src io.Reader, compressionLevel int) error
- func StreamDecompress(dst io.Writer, src io.Reader) error
- func StreamDecompressDict(dst io.Writer, src io.Reader, dd *DDict) error
- type CDict
- type DDict
- type Reader
- type Writer
- func (zw *Writer) Close() error
- func (zw *Writer) Flush() error
- func (zw *Writer) ReadFrom(r io.Reader) (int64, error)
- func (zw *Writer) Release()
- func (zw *Writer) Reset(w io.Writer, cd *CDict, compressionLevel int)
- func (zw *Writer) ResetWriterParams(w io.Writer, params *WriterParams)
- func (zw *Writer) Write(p []byte) (int, error)
- type WriterParams
Examples ¶
Constants ¶
const ( // WindowLogMin is the minimum value of the windowLog parameter. WindowLogMin = 10 // from zstd.h // WindowLogMax32 is the maximum value of the windowLog parameter on 32-bit architectures. WindowLogMax32 = 30 // from zstd.h // WindowLogMax64 is the maximum value of the windowLog parameter on 64-bit architectures. WindowLogMax64 = 31 // from zstd.h // DefaultWindowLog is the default value of the windowLog parameter. DefaultWindowLog = 0 )
const DefaultCompressionLevel = 3 // Obtained from ZSTD_CLEVEL_DEFAULT.
DefaultCompressionLevel is the default compression level.
Variables ¶
This section is empty.
Functions ¶
func BuildDict ¶
BuildDict returns dictionary built from the given samples.
The resulting dictionary size will be close to desiredDictLen.
The returned dictionary may be passed to NewCDict* and NewDDict.
Example ¶
// Collect samples for the dictionary. var samples [][]byte for i := 0; i < 1000; i++ { sample := fmt.Sprintf("this is a dict sample number %d", i) samples = append(samples, []byte(sample)) } // Build a dictionary with the desired size of 8Kb. dict := BuildDict(samples, 8*1024) // Now the dict may be used for compression/decompression. // Create CDict from the dict. cd, err := NewCDict(dict) if err != nil { log.Fatalf("cannot create CDict: %s", err) } defer cd.Release() // Compress multiple blocks with the same CDict. var compressedBlocks [][]byte for i := 0; i < 3; i++ { plainData := fmt.Sprintf("this is line %d for dict compression", i) compressedData := CompressDict(nil, []byte(plainData), cd) compressedBlocks = append(compressedBlocks, compressedData) } // The compressedData must be decompressed with the same dict. // Create DDict from the dict. dd, err := NewDDict(dict) if err != nil { log.Fatalf("cannot create DDict: %s", err) } defer dd.Release() // Decompress multiple blocks with the same DDict. for _, compressedData := range compressedBlocks { decompressedData, err := DecompressDict(nil, compressedData, dd) if err != nil { log.Fatalf("cannot decompress data: %s", err) } fmt.Printf("%s\n", decompressedData) }
Output: this is line 0 for dict compression this is line 1 for dict compression this is line 2 for dict compression
func Compress ¶
Compress appends compressed src to dst and returns the result.
Example (NoAllocs) ¶
data := []byte("foo bar baz") // Compressed data will be put into cbuf. var cbuf []byte for i := 0; i < 5; i++ { // Compress re-uses cbuf for the compressed data. cbuf = Compress(cbuf[:0], data) decompressedData, err := Decompress(nil, cbuf) if err != nil { log.Fatalf("cannot decompress data: %s", err) } fmt.Printf("%d. %s\n", i, decompressedData) }
Output: 0. foo bar baz 1. foo bar baz 2. foo bar baz 3. foo bar baz 4. foo bar baz
Example (Simple) ¶
data := []byte("foo bar baz") // Compress and decompress data into new buffers. compressedData := Compress(nil, data) decompressedData, err := Decompress(nil, compressedData) if err != nil { log.Fatalf("cannot decompress data: %s", err) } fmt.Printf("%s", decompressedData)
Output: foo bar baz
func CompressDict ¶
CompressDict appends compressed src to dst and returns the result.
The given dictionary is used for the compression.
func CompressLevel ¶
CompressLevel appends compressed src to dst and returns the result.
The given compressionLevel is used for the compression.
func Decompress ¶
Decompress appends decompressed src to dst and returns the result.
Example (NoAllocs) ¶
data := []byte("foo bar baz") compressedData := Compress(nil, data) // Decompressed data will be put into dbuf. var dbuf []byte for i := 0; i < 5; i++ { // Decompress re-uses dbuf for the decompressed data. var err error dbuf, err = Decompress(dbuf[:0], compressedData) if err != nil { log.Fatalf("cannot decompress data: %s", err) } fmt.Printf("%d. %s\n", i, dbuf) }
Output: 0. foo bar baz 1. foo bar baz 2. foo bar baz 3. foo bar baz 4. foo bar baz
Example (Simple) ¶
data := []byte("foo bar baz") // Compress and decompress data into new buffers. compressedData := Compress(nil, data) decompressedData, err := Decompress(nil, compressedData) if err != nil { log.Fatalf("cannot decompress data: %s", err) } fmt.Printf("%s", decompressedData)
Output: foo bar baz
func DecompressDict ¶
DecompressDict appends decompressed src to dst and returns the result.
The given dictionary dd is used for the decompression.
func StreamCompress ¶
StreamCompress compresses src into dst.
This function doesn't work with interactive network streams, since data read from src may be buffered before passing to dst for performance reasons. Use Writer.Flush for interactive network streams.
func StreamCompressDict ¶
StreamCompressDict compresses src into dst using the given dict cd.
This function doesn't work with interactive network streams, since data read from src may be buffered before passing to dst for performance reasons. Use Writer.Flush for interactive network streams.
func StreamCompressLevel ¶
StreamCompressLevel compresses src into dst using the given compressionLevel.
This function doesn't work with interactive network streams, since data read from src may be buffered before passing to dst for performance reasons. Use Writer.Flush for interactive network streams.
func StreamDecompress ¶
StreamDecompress decompresses src into dst.
This function doesn't work with interactive network streams, since data read from src may be buffered before passing to dst for performance reasons. Use Reader for interactive network streams.
func StreamDecompressDict ¶
StreamDecompressDict decompresses src into dst using the given dictionary dd.
This function doesn't work with interactive network streams, since data read from src may be buffered before passing to dst for performance reasons. Use Reader for interactive network streams.
Types ¶
type CDict ¶
type CDict struct {
// contains filtered or unexported fields
}
CDict is a dictionary used for compression.
A single CDict may be re-used in concurrently running goroutines.
func NewCDict ¶
NewCDict creates new CDict from the given dict.
Call Release when the returned dict is no longer used.
func NewCDictLevel ¶
NewCDictLevel creates new CDict from the given dict using the given compressionLevel.
Call Release when the returned dict is no longer used.
type DDict ¶
type DDict struct {
// contains filtered or unexported fields
}
DDict is a dictionary used for decompression.
A single DDict may be re-used in concurrently running goroutines.
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Reader implements zstd reader.
Example ¶
// Compress the data. compressedData := Compress(nil, []byte("line 0\nline 1\nline 2")) // Read it via Reader. r := bytes.NewReader(compressedData) zr := NewReader(r) defer zr.Release() var a []int for i := 0; i < 3; i++ { var n int if _, err := fmt.Fscanf(zr, "line %d\n", &n); err != nil { log.Fatalf("cannot read line: %s", err) } a = append(a, n) } // Make sure there are no data left in zr. buf := make([]byte, 1) if _, err := zr.Read(buf); err != io.EOF { log.Fatalf("unexpected error; got %v; want %v", err, io.EOF) } fmt.Println(a)
Output: [0 1 2]
func NewReader ¶
NewReader returns new zstd reader reading compressed data from r.
Call Release when the Reader is no longer needed.
func NewReaderDict ¶
NewReaderDict returns new zstd reader reading compressed data from r using the given DDict.
Call Release when the Reader is no longer needed.
func (*Reader) Release ¶
func (zr *Reader) Release()
Release releases all the resources occupied by zr.
zr cannot be used after the release.
func (*Reader) Reset ¶
Reset resets zr to read from r using the given dictionary dd.
Example ¶
zr := NewReader(nil) defer zr.Release() // Read from different sources using the same Reader. for i := 0; i < 3; i++ { compressedData := Compress(nil, []byte(fmt.Sprintf("line %d", i))) r := bytes.NewReader(compressedData) zr.Reset(r, nil) data, err := ioutil.ReadAll(zr) if err != nil { log.Fatalf("unexpected error when reading compressed data: %s", err) } fmt.Printf("%s\n", data) }
Output: line 0 line 1 line 2
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
Writer implements zstd writer.
Example ¶
// Compress data to bb. var bb bytes.Buffer zw := NewWriter(&bb) defer zw.Release() for i := 0; i < 3; i++ { fmt.Fprintf(zw, "line %d\n", i) } if err := zw.Close(); err != nil { log.Fatalf("cannot close writer: %s", err) } // Decompress the data and verify it is valid. plainData, err := Decompress(nil, bb.Bytes()) fmt.Printf("err: %v\n%s", err, plainData)
Output: err: <nil> line 0 line 1 line 2
func NewWriter ¶
NewWriter returns new zstd writer writing compressed data to w.
The returned writer must be closed with Close call in order to finalize the compressed stream.
Call Release when the Writer is no longer needed.
func NewWriterDict ¶
NewWriterDict returns new zstd writer writing compressed data to w using the given cd.
The returned writer must be closed with Close call in order to finalize the compressed stream.
Call Release when the Writer is no longer needed.
func NewWriterLevel ¶
NewWriterLevel returns new zstd writer writing compressed data to w at the given compression level.
The returned writer must be closed with Close call in order to finalize the compressed stream.
Call Release when the Writer is no longer needed.
func NewWriterParams ¶
func NewWriterParams(w io.Writer, params *WriterParams) *Writer
NewWriterParams returns new zstd writer writing compressed data to w using the given set of parameters.
The returned writer must be closed with Close call in order to finalize the compressed stream.
Call Release when the Writer is no longer needed.
func (*Writer) Close ¶
Close finalizes the compressed stream and flushes all the compressed data to the underlying writer.
It doesn't close the underlying writer passed to New* functions.
func (*Writer) Flush ¶
Flush flushes the remaining data from zw to the underlying writer.
Example ¶
var bb bytes.Buffer zw := NewWriter(&bb) defer zw.Release() // Write some data to zw. data := []byte("some data\nto compress") if _, err := zw.Write(data); err != nil { log.Fatalf("cannot write data to zw: %s", err) } // Verify the data is cached in zw and isn't propagated to bb. if bb.Len() > 0 { log.Fatalf("%d bytes unexpectedly propagated to bb", bb.Len()) } // Flush the compressed data to bb. if err := zw.Flush(); err != nil { log.Fatalf("cannot flush compressed data: %s", err) } // Verify the compressed data is propagated to bb. if bb.Len() == 0 { log.Fatalf("the compressed data isn't propagated to bb") } // Try reading the compressed data with reader. zr := NewReader(&bb) defer zr.Release() buf := make([]byte, len(data)) if _, err := io.ReadFull(zr, buf); err != nil { log.Fatalf("cannot read the compressed data: %s", err) } fmt.Printf("%s", buf)
Output: some data to compress
func (*Writer) ReadFrom ¶
ReadFrom reads all the data from r and writes it to zw.
Returns the number of bytes read from r.
ReadFrom may not flush the compressed data to the underlying writer due to performance reasons. Call Flush or Close when the compressed data must propagate to the underlying writer.
func (*Writer) Release ¶
func (zw *Writer) Release()
Release releases all the resources occupied by zw.
zw cannot be used after the release.
func (*Writer) Reset ¶
Reset resets zw to write to w using the given dictionary cd and the given compressionLevel. Use ResetWriterParams if you wish to change other parameters that were set via WriterParams.
Example ¶
zw := NewWriter(nil) defer zw.Release() // Write to different destinations using the same Writer. for i := 0; i < 3; i++ { var bb bytes.Buffer zw.Reset(&bb, nil, DefaultCompressionLevel) if _, err := zw.Write([]byte(fmt.Sprintf("line %d", i))); err != nil { log.Fatalf("unexpected error when writing data: %s", err) } if err := zw.Close(); err != nil { log.Fatalf("unexpected error when closing zw: %s", err) } // Decompress the compressed data. plainData, err := Decompress(nil, bb.Bytes()) if err != nil { log.Fatalf("unexpected error when decompressing data: %s", err) } fmt.Printf("%s\n", plainData) }
Output: line 0 line 1 line 2
func (*Writer) ResetWriterParams ¶
func (zw *Writer) ResetWriterParams(w io.Writer, params *WriterParams)
ResetWriterParams resets zw to write to w using the given set of parameters.
type WriterParams ¶
type WriterParams struct { // Compression level. Special value 0 means 'default compression level'. CompressionLevel int // WindowLog. Must be clamped between WindowLogMin and WindowLogMin32/64. // Special value 0 means 'use default windowLog'. // // Note: enabling log distance matching increases memory usage for both // compressor and decompressor. When set to a value greater than 27, the // decompressor requires special treatment. WindowLog int // Dict is optional dictionary used for compression. Dict *CDict }
A WriterParams allows users to specify compression parameters by calling NewWriterParams.
Calling NewWriterParams with a nil WriterParams is equivalent to calling NewWriter.
Example ¶
// Compress data to bb. var bb bytes.Buffer zw := NewWriterParams(&bb, &WriterParams{ CompressionLevel: 10, WindowLog: 14, }) defer zw.Release() for i := 0; i < 3; i++ { fmt.Fprintf(zw, "line %d\n", i) } if err := zw.Close(); err != nil { log.Fatalf("cannot close writer: %s", err) } // Decompress the data and verify it is valid. plainData, err := Decompress(nil, bb.Bytes()) fmt.Printf("err: %v\n%s", err, plainData)
Output: err: <nil> line 0 line 1 line 2