feat: Add lz4 compression support

This commit is contained in:
Felix Pojtinger
2021-11-30 18:35:28 +01:00
parent 9f343d105f
commit 7cd97c4d2d
6 changed files with 89 additions and 1 deletions
+68
View File
@@ -11,6 +11,7 @@ import (
"strconv"
"github.com/klauspost/pgzip"
"github.com/pierrec/lz4/v4"
"github.com/pojntfx/stfs/pkg/adapters"
"github.com/pojntfx/stfs/pkg/controllers"
"github.com/pojntfx/stfs/pkg/counters"
@@ -218,6 +219,40 @@ func archive(
hdr.Size = int64(fileSizeCounter.BytesRead)
hdr.Name += compressionFormatGZipSuffix
case compressionFormatLZ4Key:
// Get the compressed size for the header
file, err := os.Open(path)
if err != nil {
return err
}
fileSizeCounter := counters.CounterWriter{
Writer: io.Discard,
}
lz := lz4.NewWriter(&fileSizeCounter)
if err := lz.Apply(lz4.ConcurrencyOption(-1)); err != nil {
return err
}
if _, err := io.Copy(lz, file); err != nil {
return err
}
if err := lz.Close(); err != nil {
return err
}
if err := file.Close(); err != nil {
return err
}
if hdr.PAXRecords == nil {
hdr.PAXRecords = map[string]string{}
}
hdr.PAXRecords[pax.STFSRecordUncompressedSize] = strconv.Itoa(int(hdr.Size))
hdr.Size = int64(fileSizeCounter.BytesRead)
hdr.Name += compressionFormatLZ4Suffix
case compressionFormatNoneKey:
default:
return errUnsupportedCompressionFormat
@@ -284,6 +319,39 @@ func archive(
if err := file.Close(); err != nil {
return err
}
case compressionFormatLZ4Key:
// Compress and write the file
file, err := os.Open(path)
if err != nil {
return err
}
lz := lz4.NewWriter(tw)
if err := lz.Apply(lz4.ConcurrencyOption(-1)); err != nil {
return err
}
if _, err := io.Copy(lz, file); err != nil {
return err
}
if isRegular {
if _, err := io.Copy(lz, file); err != nil {
return err
}
} else {
buf := make([]byte, controllers.BlockSize*recordSize)
if _, err := io.CopyBuffer(lz, file, buf); err != nil {
return err
}
}
if err := lz.Close(); err != nil {
return err
}
if err := file.Close(); err != nil {
return err
}
case compressionFormatNoneKey:
// Write the file
file, err := os.Open(path)
+10
View File
@@ -9,6 +9,7 @@ import (
"path/filepath"
"github.com/klauspost/pgzip"
"github.com/pierrec/lz4/v4"
"github.com/pojntfx/stfs/pkg/controllers"
"github.com/pojntfx/stfs/pkg/formatting"
"github.com/spf13/cobra"
@@ -150,6 +151,15 @@ func restoreFromRecordAndBlock(
if _, err := io.Copy(dstFile, gz); err != nil {
return err
}
case compressionFormatLZ4Key:
lz := lz4.NewReader(tr)
if err := lz.Apply(lz4.ConcurrencyOption(-1)); err != nil {
return err
}
if _, err := io.Copy(dstFile, lz); err != nil {
return err
}
case compressionFormatNoneKey:
if _, err := io.Copy(dstFile, tr); err != nil {
return err
+4
View File
@@ -274,6 +274,10 @@ func indexHeader(
if hdr.FileInfo().Mode().IsRegular() {
hdr.Name = strings.TrimSuffix(hdr.Name, compressionFormatGZipSuffix)
}
case compressionFormatLZ4Key:
if hdr.FileInfo().Mode().IsRegular() {
hdr.Name = strings.TrimSuffix(hdr.Name, compressionFormatLZ4Suffix)
}
case compressionFormatNoneKey:
default:
return errUnsupportedCompressionFormat
+4 -1
View File
@@ -23,10 +23,13 @@ const (
compressionFormatGZipSuffix = ".gz"
compressionFormatParallelGZipKey = "parallelgzip"
compressionFormatLZ4Key = "lz4"
compressionFormatLZ4Suffix = ".lz4"
)
var (
knownCompressionFormats = []string{compressionFormatNoneKey, compressionFormatGZipKey, compressionFormatParallelGZipKey}
knownCompressionFormats = []string{compressionFormatNoneKey, compressionFormatGZipKey, compressionFormatParallelGZipKey, compressionFormatLZ4Key}
errUnknownCompressionFormat = errors.New("unknown compression format")
errUnsupportedCompressionFormat = errors.New("unsupported compression format")