|
@@ -3,11 +3,18 @@ package lz4
|
|
|
import (
|
|
import (
|
|
|
"encoding/binary"
|
|
"encoding/binary"
|
|
|
"fmt"
|
|
"fmt"
|
|
|
|
|
+ "github.com/pierrec/lz4/internal/xxh32"
|
|
|
"io"
|
|
"io"
|
|
|
-
|
|
|
|
|
- "github.com/pierrec/lz4/v3/internal/xxh32"
|
|
|
|
|
|
|
+ "runtime"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
|
|
+// zResult contains the results of compressing a block.
|
|
|
|
|
+type zResult struct {
|
|
|
|
|
+ size uint32 // Block header
|
|
|
|
|
+ data []byte // Compressed data
|
|
|
|
|
+ checksum uint32 // Data checksum
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
// Writer implements the LZ4 frame encoder.
|
|
// Writer implements the LZ4 frame encoder.
|
|
|
type Writer struct {
|
|
type Writer struct {
|
|
|
Header
|
|
Header
|
|
@@ -18,10 +25,13 @@ type Writer struct {
|
|
|
buf [19]byte // magic number(4) + header(flags(2)+[Size(8)+DictID(4)]+checksum(1)) does not exceed 19 bytes
|
|
buf [19]byte // magic number(4) + header(flags(2)+[Size(8)+DictID(4)]+checksum(1)) does not exceed 19 bytes
|
|
|
dst io.Writer // Destination.
|
|
dst io.Writer // Destination.
|
|
|
checksum xxh32.XXHZero // Frame checksum.
|
|
checksum xxh32.XXHZero // Frame checksum.
|
|
|
- zdata []byte // Compressed data.
|
|
|
|
|
- data []byte // Data to be compressed.
|
|
|
|
|
|
|
+ data []byte // Data to be compressed + buffer for compressed data.
|
|
|
idx int // Index into data.
|
|
idx int // Index into data.
|
|
|
hashtable [winSize]int // Hash table used in CompressBlock().
|
|
hashtable [winSize]int // Hash table used in CompressBlock().
|
|
|
|
|
+
|
|
|
|
|
+ // For concurrency.
|
|
|
|
|
+ c chan chan zResult // Channel for block compression goroutines and writer goroutine.
|
|
|
|
|
+ err error // Any error encountered while writing to the underlying destination.
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// NewWriter returns a new LZ4 frame encoder.
|
|
// NewWriter returns a new LZ4 frame encoder.
|
|
@@ -29,30 +39,86 @@ type Writer struct {
|
|
|
// The supplied Header is checked at the first Write.
|
|
// The supplied Header is checked at the first Write.
|
|
|
// It is ok to change it before the first Write but then not until a Reset() is performed.
|
|
// It is ok to change it before the first Write but then not until a Reset() is performed.
|
|
|
func NewWriter(dst io.Writer) *Writer {
|
|
func NewWriter(dst io.Writer) *Writer {
|
|
|
- return &Writer{dst: dst}
|
|
|
|
|
|
|
+ z := new(Writer)
|
|
|
|
|
+ z.Reset(dst)
|
|
|
|
|
+ return z
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// WithConcurrency sets the number of concurrent go routines used for compression.
|
|
|
|
|
+// A negative value sets the concurrency to GOMAXPROCS.
|
|
|
|
|
+func (z *Writer) WithConcurrency(n int) *Writer {
|
|
|
|
|
+ switch {
|
|
|
|
|
+ case n == 0 || n == 1:
|
|
|
|
|
+ z.c = nil
|
|
|
|
|
+ return z
|
|
|
|
|
+ case n < 0:
|
|
|
|
|
+ n = runtime.GOMAXPROCS(0)
|
|
|
|
|
+ }
|
|
|
|
|
+ z.c = make(chan chan zResult, n)
|
|
|
|
|
+ // Writer goroutine managing concurrent block compression goroutines.
|
|
|
|
|
+ go func() {
|
|
|
|
|
+ // Process next block compression item.
|
|
|
|
|
+ for c := range z.c {
|
|
|
|
|
+ // Read the next compressed block result.
|
|
|
|
|
+ // Waiting here ensures that the blocks are output in the order they were sent.
|
|
|
|
|
+ res := <-c
|
|
|
|
|
+ n := len(res.data)
|
|
|
|
|
+ if n == 0 {
|
|
|
|
|
+ // Notify the block compression routine that we are done with its result.
|
|
|
|
|
+ // This is used when a sentinel block is sent to terminate the compression.
|
|
|
|
|
+ close(c)
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+ // Write the block.
|
|
|
|
|
+ if err := z.writeUint32(res.size); err != nil && z.err == nil {
|
|
|
|
|
+ z.err = err
|
|
|
|
|
+ }
|
|
|
|
|
+ if _, err := z.dst.Write(res.data); err != nil && z.err == nil {
|
|
|
|
|
+ z.err = err
|
|
|
|
|
+ }
|
|
|
|
|
+ if z.BlockChecksum {
|
|
|
|
|
+ if err := z.writeUint32(res.checksum); err != nil && z.err == nil {
|
|
|
|
|
+ z.err = err
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if h := z.OnBlockDone; h != nil {
|
|
|
|
|
+ h(n)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }()
|
|
|
|
|
+ return z
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// newBuffers instantiates new buffers which size matches the one in Header.
|
|
|
|
|
+// The returned buffers are for decompression and compression respectively.
|
|
|
|
|
+func (z *Writer) newBuffers() {
|
|
|
|
|
+ bSize := z.Header.BlockMaxSize
|
|
|
|
|
+ idx := blockSizeValueToIndex(bSize) - 4
|
|
|
|
|
+ buf := bsMapValue[idx].Get().([]byte)
|
|
|
|
|
+ z.data = buf[:bSize] // Uncompressed buffer is the first half.
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// freeBuffers puts the writer's buffers back to the pool.
|
|
|
|
|
+func (z *Writer) freeBuffers() {
|
|
|
|
|
+ // Put the buffer back into the pool, if any.
|
|
|
|
|
+ putBuffer(z.Header.BlockMaxSize, z.data)
|
|
|
|
|
+ z.data = nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// writeHeader builds and writes the header (magic+header) to the underlying io.Writer.
|
|
// writeHeader builds and writes the header (magic+header) to the underlying io.Writer.
|
|
|
func (z *Writer) writeHeader() error {
|
|
func (z *Writer) writeHeader() error {
|
|
|
// Default to 4Mb if BlockMaxSize is not set.
|
|
// Default to 4Mb if BlockMaxSize is not set.
|
|
|
if z.Header.BlockMaxSize == 0 {
|
|
if z.Header.BlockMaxSize == 0 {
|
|
|
- z.Header.BlockMaxSize = bsMapID[7]
|
|
|
|
|
|
|
+ z.Header.BlockMaxSize = blockSize4M
|
|
|
}
|
|
}
|
|
|
// The only option that needs to be validated.
|
|
// The only option that needs to be validated.
|
|
|
bSize := z.Header.BlockMaxSize
|
|
bSize := z.Header.BlockMaxSize
|
|
|
- bSizeID, ok := bsMapValue[bSize]
|
|
|
|
|
- if !ok {
|
|
|
|
|
|
|
+ if !isValidBlockSize(z.Header.BlockMaxSize) {
|
|
|
return fmt.Errorf("lz4: invalid block max size: %d", bSize)
|
|
return fmt.Errorf("lz4: invalid block max size: %d", bSize)
|
|
|
}
|
|
}
|
|
|
// Allocate the compressed/uncompressed buffers.
|
|
// Allocate the compressed/uncompressed buffers.
|
|
|
// The compressed buffer cannot exceed the uncompressed one.
|
|
// The compressed buffer cannot exceed the uncompressed one.
|
|
|
- if cap(z.zdata) < bSize {
|
|
|
|
|
- // Only allocate if there is not enough capacity.
|
|
|
|
|
- // Allocate both buffers at once.
|
|
|
|
|
- z.zdata = make([]byte, 2*bSize)
|
|
|
|
|
- }
|
|
|
|
|
- z.data = z.zdata[:bSize] // Uncompressed buffer is the first half.
|
|
|
|
|
- z.zdata = z.zdata[:cap(z.zdata)][bSize:] // Compressed buffer is the second half.
|
|
|
|
|
|
|
+ z.newBuffers()
|
|
|
z.idx = 0
|
|
z.idx = 0
|
|
|
|
|
|
|
|
// Size is optional.
|
|
// Size is optional.
|
|
@@ -72,7 +138,7 @@ func (z *Writer) writeHeader() error {
|
|
|
flg |= 1 << 2
|
|
flg |= 1 << 2
|
|
|
}
|
|
}
|
|
|
buf[4] = flg
|
|
buf[4] = flg
|
|
|
- buf[5] = bSizeID << 4
|
|
|
|
|
|
|
+ buf[5] = blockSizeValueToIndex(z.Header.BlockMaxSize) << 4
|
|
|
|
|
|
|
|
// Current buffer size: magic(4) + flags(1) + block max size (1).
|
|
// Current buffer size: magic(4) + flags(1) + block max size (1).
|
|
|
n := 6
|
|
n := 6
|
|
@@ -155,58 +221,90 @@ func (z *Writer) compressBlock(data []byte) error {
|
|
|
z.checksum.Write(data)
|
|
z.checksum.Write(data)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // The compressed block size cannot exceed the input's.
|
|
|
|
|
- var zn int
|
|
|
|
|
- var err error
|
|
|
|
|
|
|
+ zdata := z.data[z.Header.BlockMaxSize:cap(z.data)]
|
|
|
|
|
+ if z.c == nil {
|
|
|
|
|
+ // The compressed block size cannot exceed the input's.
|
|
|
|
|
+ var zn int
|
|
|
|
|
|
|
|
- if level := z.Header.CompressionLevel; level != 0 {
|
|
|
|
|
- zn, err = CompressBlockHC(data, z.zdata, level)
|
|
|
|
|
- } else {
|
|
|
|
|
- zn, err = CompressBlock(data, z.zdata, z.hashtable[:])
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if level := z.Header.CompressionLevel; level != 0 {
|
|
|
|
|
+ zn = compressBlockHC(data, zdata, level)
|
|
|
|
|
+ } else {
|
|
|
|
|
+ zn = compressBlock(data, zdata, z.hashtable[:])
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- var zdata []byte
|
|
|
|
|
- var bLen uint32
|
|
|
|
|
- if debugFlag {
|
|
|
|
|
- debug("block compression %d => %d", len(data), zn)
|
|
|
|
|
- }
|
|
|
|
|
- if err == nil && zn > 0 && zn < len(data) {
|
|
|
|
|
- // Compressible and compressed size smaller than uncompressed: ok!
|
|
|
|
|
- bLen = uint32(zn)
|
|
|
|
|
- zdata = z.zdata[:zn]
|
|
|
|
|
- } else {
|
|
|
|
|
- // Uncompressed block.
|
|
|
|
|
- bLen = uint32(len(data)) | compressedBlockFlag
|
|
|
|
|
- zdata = data
|
|
|
|
|
- }
|
|
|
|
|
- if debugFlag {
|
|
|
|
|
- debug("block compression to be written len=%d data len=%d", bLen, len(zdata))
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ var bLen uint32
|
|
|
|
|
+ if debugFlag {
|
|
|
|
|
+ debug("block compression %d => %d", len(data), zn)
|
|
|
|
|
+ }
|
|
|
|
|
+ if zn > 0 && zn < len(data) {
|
|
|
|
|
+ // Compressible and compressed size smaller than uncompressed: ok!
|
|
|
|
|
+ bLen = uint32(zn)
|
|
|
|
|
+ zdata = zdata[:zn]
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // Uncompressed block.
|
|
|
|
|
+ bLen = uint32(len(data)) | compressedBlockFlag
|
|
|
|
|
+ zdata = data
|
|
|
|
|
+ }
|
|
|
|
|
+ if debugFlag {
|
|
|
|
|
+ debug("block compression to be written len=%d data len=%d", bLen, len(zdata))
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- // Write the block.
|
|
|
|
|
- if err := z.writeUint32(bLen); err != nil {
|
|
|
|
|
- return err
|
|
|
|
|
- }
|
|
|
|
|
- written, err := z.dst.Write(zdata)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return err
|
|
|
|
|
- }
|
|
|
|
|
- if h := z.OnBlockDone; h != nil {
|
|
|
|
|
- h(written)
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ // Write the block.
|
|
|
|
|
+ if err := z.writeUint32(bLen); err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+ written, err := z.dst.Write(zdata)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+ if h := z.OnBlockDone; h != nil {
|
|
|
|
|
+ h(written)
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- if !z.BlockChecksum {
|
|
|
|
|
|
|
+ if !z.BlockChecksum {
|
|
|
|
|
+ if debugFlag {
|
|
|
|
|
+ debug("current frame checksum %x", z.checksum.Sum32())
|
|
|
|
|
+ }
|
|
|
|
|
+ return nil
|
|
|
|
|
+ }
|
|
|
|
|
+ checksum := xxh32.ChecksumZero(zdata)
|
|
|
if debugFlag {
|
|
if debugFlag {
|
|
|
- debug("current frame checksum %x", z.checksum.Sum32())
|
|
|
|
|
|
|
+ debug("block checksum %x", checksum)
|
|
|
|
|
+ defer func() { debug("current frame checksum %x", z.checksum.Sum32()) }()
|
|
|
}
|
|
}
|
|
|
- return nil
|
|
|
|
|
|
|
+ return z.writeUint32(checksum)
|
|
|
}
|
|
}
|
|
|
- checksum := xxh32.ChecksumZero(zdata)
|
|
|
|
|
- if debugFlag {
|
|
|
|
|
- debug("block checksum %x", checksum)
|
|
|
|
|
- defer func() { debug("current frame checksum %x", z.checksum.Sum32()) }()
|
|
|
|
|
- }
|
|
|
|
|
- return z.writeUint32(checksum)
|
|
|
|
|
|
|
+
|
|
|
|
|
+ odata := z.data
|
|
|
|
|
+ z.newBuffers()
|
|
|
|
|
+ c := make(chan zResult)
|
|
|
|
|
+ z.c <- c // Send now to guarantee order
|
|
|
|
|
+ go func(header Header) {
|
|
|
|
|
+ // The compressed block size cannot exceed the input's.
|
|
|
|
|
+ var zn int
|
|
|
|
|
+ if level := header.CompressionLevel; level != 0 {
|
|
|
|
|
+ zn = compressBlockHC(data, zdata, level)
|
|
|
|
|
+ } else {
|
|
|
|
|
+ var hashTable [winSize]int
|
|
|
|
|
+ zn = compressBlock(data, zdata, hashTable[:])
|
|
|
|
|
+ }
|
|
|
|
|
+ var res zResult
|
|
|
|
|
+ if zn > 0 && zn < len(data) {
|
|
|
|
|
+ // Compressible and compressed size smaller than uncompressed: ok!
|
|
|
|
|
+ res.size = uint32(zn)
|
|
|
|
|
+ res.data = zdata[:zn]
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // Uncompressed block.
|
|
|
|
|
+ res.size = uint32(len(data)) | compressedBlockFlag
|
|
|
|
|
+ res.data = data
|
|
|
|
|
+ }
|
|
|
|
|
+ if header.BlockChecksum {
|
|
|
|
|
+ res.checksum = xxh32.ChecksumZero(res.data)
|
|
|
|
|
+ }
|
|
|
|
|
+ c <- res
|
|
|
|
|
+ putBuffer(header.BlockMaxSize, odata)
|
|
|
|
|
+ }(z.Header)
|
|
|
|
|
+ return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Flush flushes any pending compressed data to the underlying writer.
|
|
// Flush flushes any pending compressed data to the underlying writer.
|
|
@@ -227,6 +325,21 @@ func (z *Writer) Flush() error {
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+func (z *Writer) close() error {
|
|
|
|
|
+ if z.c == nil {
|
|
|
|
|
+ return nil
|
|
|
|
|
+ }
|
|
|
|
|
+ // Send a sentinel block (no data to compress) to terminate the writer main goroutine.
|
|
|
|
|
+ c := make(chan zResult)
|
|
|
|
|
+ z.c <- c
|
|
|
|
|
+ c <- zResult{}
|
|
|
|
|
+ // Wait for the main goroutine to complete.
|
|
|
|
|
+ <-c
|
|
|
|
|
+ // At this point the main goroutine has shut down or is about to return.
|
|
|
|
|
+ z.c = nil
|
|
|
|
|
+ return z.err
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
// Close closes the Writer, flushing any unwritten data to the underlying io.Writer, but does not close the underlying io.Writer.
|
|
// Close closes the Writer, flushing any unwritten data to the underlying io.Writer, but does not close the underlying io.Writer.
|
|
|
func (z *Writer) Close() error {
|
|
func (z *Writer) Close() error {
|
|
|
if !z.Header.done {
|
|
if !z.Header.done {
|
|
@@ -237,6 +350,10 @@ func (z *Writer) Close() error {
|
|
|
if err := z.Flush(); err != nil {
|
|
if err := z.Flush(); err != nil {
|
|
|
return err
|
|
return err
|
|
|
}
|
|
}
|
|
|
|
|
+ if err := z.close(); err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+ z.freeBuffers()
|
|
|
|
|
|
|
|
if debugFlag {
|
|
if debugFlag {
|
|
|
debug("writing last empty block")
|
|
debug("writing last empty block")
|
|
@@ -258,12 +375,14 @@ func (z *Writer) Close() error {
|
|
|
// initial state from NewWriter, but instead writing to w.
|
|
// initial state from NewWriter, but instead writing to w.
|
|
|
// No access to the underlying io.Writer is performed.
|
|
// No access to the underlying io.Writer is performed.
|
|
|
func (z *Writer) Reset(w io.Writer) {
|
|
func (z *Writer) Reset(w io.Writer) {
|
|
|
|
|
+ n := cap(z.c)
|
|
|
|
|
+ _ = z.close()
|
|
|
|
|
+ z.freeBuffers()
|
|
|
z.Header = Header{}
|
|
z.Header = Header{}
|
|
|
z.dst = w
|
|
z.dst = w
|
|
|
z.checksum.Reset()
|
|
z.checksum.Reset()
|
|
|
- z.zdata = z.zdata[:0]
|
|
|
|
|
- z.data = z.data[:0]
|
|
|
|
|
z.idx = 0
|
|
z.idx = 0
|
|
|
|
|
+ z.WithConcurrency(n)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// writeUint32 writes a uint32 to the underlying writer.
|
|
// writeUint32 writes a uint32 to the underlying writer.
|