lz4

package module
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 29, 2021 License: BSD-3-Clause Imports: 7 Imported by: 1

README

golz4

Godoc license CircleCI

Golang interface to LZ4 compression.

Forked from github.com/cloudflare/golz4 but with significant differences:

  • input/output arg order has been swapped to follow Go convention, ie Compress(in, out) -> Compress(out, in)
  • builds against the liblz4 that it detects using pkgconfig

Benchmark

BenchmarkCompress-4                 	 4002601	       284 ns/op	 151.65 MB/s	       0 B/op	       0 allocs/op
BenchmarkCompressUncompress-4       	14668696	        75.6 ns/op	 568.66 MB/s	       0 B/op	       0 allocs/op
BenchmarkStreamCompress-4           	     306	   4032676 ns/op	2600.20 MB/s	23627182 B/op	     643 allocs/op
BenchmarkStreamCompressReader-4     	    1617	    700174 ns/op	14975.94 MB/s	    7856 B/op	     163 allocs/op
BenchmarkStreamUncompress-4         	     100	  10385084 ns/op	1009.69 MB/s	22283872 B/op	     485 allocs/op
BenchmarkStreamDecompressReader-4   	     236	   5060225 ns/op	2072.19 MB/s	    8557 B/op	     324 allocs/op`

Building

Building golz4 requires that lz4 library be available.

On Debian or Ubuntu, this is as easy as running

$ sudo apt-get install liblz4-dev

On MacOS

$ brew install lz4

If the library version provided for your OS is too old and does not include a liblz4.pc pkg-config file, the upstream documentation describes how to build and install from source.

NOTE: if lz4 is not installed in standard directories, setting PKG_CONFIG_PATH environment variable with the directory containing the liblz4.pc file will help.

Documentation

Overview

Package lz4 implements compression using lz4.c and lz4hc.c

Copyright (c) 2016 Datadog Copyright (c) 2013 CloudFlare, Inc.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compress

func Compress(out, in []byte) (outSize int, err error)

Compress compresses in and puts the content in out. len(out) should have enough space for the compressed data (use CompressBound to calculate). Returns the number of bytes in the out slice.

func CompressAllocHdr

func CompressAllocHdr(in []byte) (out []byte, err error)

CompressAllocHdr is like Compress, but allocates the out slice itself and automatically resizes it to the proper size of the compressed output. This can be more convenient to use if you are in a situation where you cannot reuse buffers.

func CompressBound

func CompressBound(in []byte) int

CompressBound calculates the size of the output buffer needed by Compress. This is based on the following macro:

#define LZ4_COMPRESSBOUND(isize)

((unsigned int)(isize) > (unsigned int)LZ4_MAX_INPUT_SIZE ? 0 : (isize) + ((isize)/255) + 16)

func CompressBoundHdr

func CompressBoundHdr(in []byte) int

CompressBoundHdr returns the upper bounds of the size of the compressed byte plus space for a length header.

func CompressHC

func CompressHC(out, in []byte) (int, error)

CompressHC compresses in and puts the content in out. len(out) should have enough space for the compressed data (use CompressBound to calculate). Returns the number of bytes in the out slice. Determines the compression level automatically.

func CompressHCHdr

func CompressHCHdr(out, in []byte) (count int, err error)

CompressHCHdr implements high-compression ratio compression.

func CompressHCLevel

func CompressHCLevel(out, in []byte, level int) (outSize int, err error)

CompressHCLevel compresses in at the given compression level and puts the content in out. len(out) should have enough space for the compressed data (use CompressBound to calculate). Returns the number of bytes in the out slice. To automatically choose the compression level, use 0. Otherwise, use any value in the inclusive range 1 (worst) through 16 (best). Most applications will prefer CompressHC.

func CompressHCLevelHdr

func CompressHCLevelHdr(out, in []byte, level int) (count int, err error)

CompressHCLevelHdr implements high-compression ratio compression.

func CompressHdr

func CompressHdr(out, in []byte) (count int, err error)

CompressHdr compresses in to out. It returns the number of bytes written to out and any errors that may have been encountered. This version adds a 4-byte little endian "header" indicating the length of the original message so that it may be decompressed successfully later.

func NewDecompressReader added in v1.1.0

func NewDecompressReader(r io.Reader) io.ReadCloser

NewDecompressReader creates a new io.ReadCloser. This function mirrors the behavior of NewReader but provides better performance. It is the caller's responsibility to call Close on the ReadCloser when done. If this is not done, underlying objects in the lz4 library will not be freed.

func NewReader deprecated

func NewReader(r io.Reader) io.ReadCloser

NewReader creates a new io.ReadCloser. Reads from the returned ReadCloser read and decompress data from r. It is the caller's responsibility to call Close on the ReadCloser when done. If this is not done, underlying objects in the lz4 library will not be freed.

Deprecated: Use NewDecompressReader instead. It can decompress the output of NewWriter, but uses fewer allocations.

func Uncompress

func Uncompress(out, in []byte) (outSize int, err error)

Uncompress with a known output size. len(out) should be equal to the length of the uncompressed out.

func UncompressAllocHdr

func UncompressAllocHdr(out, in []byte) ([]byte, error)

UncompressAllocHdr uncompresses the stream from in into out if out has enough space. Otherwise, a new slice is allocated automatically and returned. This function uses the "length header" to determine how much space is necessary for the result message, which CloudFlare's implementation doesn't have.

func UncompressHdr

func UncompressHdr(out, in []byte) error

UncompressHdr uncompresses in into out. Out must have enough space allocated for the uncompressed message.

Types

type CompressReader added in v1.1.0

type CompressReader struct {
	// contains filtered or unexported fields
}

CompressReader reads input and creates an io.ReadCloser for reading compressed output

func NewCompressReader added in v1.1.0

func NewCompressReader(r io.Reader) *CompressReader

NewCompressReader creates a new io.ReadCloser. Reads from the returned ReadCloser read and compress data from r. It is the caller's responsibility to call Close on the ReadCloser when done. If this is not done, underlying objects in the lz4 library will not be freed. The compressed output must be decompressed using NewDecompressReader.

func (*CompressReader) Close added in v1.1.0

func (r *CompressReader) Close() error

Close releases all the resources occupied by Reader. r cannot be used after the release.

func (*CompressReader) Read added in v1.1.0

func (r *CompressReader) Read(dst []byte) (int, error)

Read compresses data from the underlyingReader into dst.

type DecompressReader added in v1.1.0

type DecompressReader struct {
	// contains filtered or unexported fields
}

DecompressReader is an io.ReadCloser that decompresses when read from.

func (*DecompressReader) Close added in v1.1.0

func (r *DecompressReader) Close() error

Close releases all the resources occupied by r. r cannot be used after the release.

func (*DecompressReader) Read added in v1.1.0

func (r *DecompressReader) Read(dst []byte) (int, error)

Read decompresses data from the underlying reader into `dst`.

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

Writer is an io.WriteCloser that lz4 compress its input.

func NewWriter

func NewWriter(w io.Writer) *Writer

NewWriter creates a new Writer. Writes to the writer will be written in compressed form to w.

func (*Writer) Close

func (w *Writer) Close() error

Close releases all the resources occupied by Writer. w cannot be used after the release.

func (*Writer) Write

func (w *Writer) Write(src []byte) (int, error)

Write writes a compressed form of src to the underlying io.Writer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL