-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add compress package & the ability to compress steps in a resol…
…ution Signed-off-by: Simon Delberghe <open-source@orandin.fr>
- Loading branch information
Showing
16 changed files
with
282 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package compress | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
) | ||
|
||
var ( | ||
compressions = map[string]Compression{} | ||
compressionsMut sync.Mutex | ||
) | ||
|
||
type Compression interface { | ||
Compress([]byte) ([]byte, error) | ||
Decompress([]byte) ([]byte, error) | ||
} | ||
|
||
// RegisterAlgorithm registers a custom compression algorithm. | ||
func RegisterAlgorithm(name string, c Compression) error { | ||
if c == nil { | ||
return nil | ||
} | ||
compressionsMut.Lock() | ||
defer compressionsMut.Unlock() | ||
_, found := compressions[name] | ||
if found { | ||
return fmt.Errorf("conflicting compression key compressions: %s", name) | ||
} | ||
compressions[name] = c | ||
return nil | ||
} | ||
|
||
func Get(name string) (Compression, error) { | ||
compressionsMut.Lock() | ||
defer compressionsMut.Unlock() | ||
|
||
c, ok := compressions[name] | ||
if !ok { | ||
return nil, fmt.Errorf("%s compression algorithm not found", name) | ||
} | ||
return c, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package gzip | ||
|
||
import ( | ||
"bytes" | ||
"compress/gzip" | ||
"io" | ||
|
||
"github.com/ovh/utask/pkg/compress" | ||
) | ||
|
||
const AlgorithmName = "gzip" | ||
|
||
type gzipCompression struct{} | ||
|
||
// New returns a new compress.Compression with gzip as the compression algorithm. | ||
func New() compress.Compression { | ||
return &gzipCompression{} | ||
} | ||
|
||
// Compress transforms data into a compressed form. | ||
func (c *gzipCompression) Compress(data []byte) ([]byte, error) { | ||
var buf bytes.Buffer | ||
zw := gzip.NewWriter(&buf) | ||
|
||
if _, err := zw.Write(data); err != nil { | ||
_ = zw.Close() | ||
return nil, err | ||
} | ||
|
||
// Need to close the gzip writer before accessing the underlying bytes | ||
_ = zw.Close() | ||
return buf.Bytes(), nil | ||
} | ||
|
||
// Decompress transforms compressed form into an uncompressed form. | ||
func (c *gzipCompression) Decompress(data []byte) ([]byte, error) { | ||
zr, err := gzip.NewReader(bytes.NewReader(data)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer func() { _ = zr.Close() }() | ||
|
||
return io.ReadAll(zr) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package gzip_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/ovh/utask/pkg/compress/gzip" | ||
"github.com/ovh/utask/pkg/compress/tests" | ||
) | ||
|
||
func TestCompression(t *testing.T) { | ||
tests.CompressionTests(t, gzip.New()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package init | ||
|
||
import ( | ||
"github.com/ovh/utask/pkg/compress" | ||
"github.com/ovh/utask/pkg/compress/gzip" | ||
"github.com/ovh/utask/pkg/compress/noop" | ||
) | ||
|
||
// Register registers default compression algorithms. | ||
func Register() error { | ||
noopCompress := noop.New() | ||
|
||
for name, c := range map[string]compress.Compression{ | ||
"": noopCompress, // to ensure backwards compatibility | ||
noop.AlgorithmName: noopCompress, | ||
gzip.AlgorithmName: gzip.New(), | ||
} { | ||
if err := compress.RegisterAlgorithm(name, c); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package noop | ||
|
||
import "github.com/ovh/utask/pkg/compress" | ||
|
||
const AlgorithmName = "noop" | ||
|
||
type noneCompression struct{} | ||
|
||
// New returns a new compress.Compression with no compression algorithm. | ||
func New() compress.Compression { | ||
return &noneCompression{} | ||
} | ||
|
||
func (c *noneCompression) Compress(data []byte) ([]byte, error) { | ||
return data, nil | ||
} | ||
|
||
func (c *noneCompression) Decompress(data []byte) ([]byte, error) { | ||
return data, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package noop_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/ovh/utask/pkg/compress/noop" | ||
"github.com/ovh/utask/pkg/compress/tests" | ||
) | ||
|
||
func TestCompression(t *testing.T) { | ||
tests.CompressionTests(t, noop.New()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package tests | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/ovh/utask/pkg/compress" | ||
) | ||
|
||
// CompressionTests executes a range of tests to test a `Compression` module. | ||
func CompressionTests(t *testing.T, c compress.Compression) { | ||
tests := []struct { | ||
name string | ||
want string | ||
}{ | ||
{name: "Hello world", want: "Hello world!"}, | ||
{name: "Empty string", want: " "}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := c.Compress([]byte(tt.want)) | ||
require.Nilf(t, err, "Compress(): %s", err) | ||
|
||
got, err = c.Decompress(got) | ||
|
||
require.Nilf(t, err, "Decompress(): %s", err) | ||
assert.Equal(t, tt.want, string(got)) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.