-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
blob compression + composition version
- Loading branch information
1 parent
09cb473
commit 17795ca
Showing
18 changed files
with
569 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and Open Component Model contributors. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package blobaccess | ||
|
||
import ( | ||
"bytes" | ||
"compress/gzip" | ||
"io" | ||
"sync" | ||
|
||
"github.com/opencontainers/go-digest" | ||
|
||
"github.com/open-component-model/ocm/pkg/common/accessio/blobaccess/spi" | ||
compression2 "github.com/open-component-model/ocm/pkg/common/compression" | ||
"github.com/open-component-model/ocm/pkg/errors" | ||
"github.com/open-component-model/ocm/pkg/mime" | ||
) | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
type compression struct { | ||
blob BlobAccess | ||
} | ||
|
||
var _ spi.BlobAccessBase = (*compression)(nil) | ||
|
||
func (c *compression) Close() error { | ||
return c.blob.Close() | ||
} | ||
|
||
func (c *compression) Get() ([]byte, error) { | ||
r, err := c.blob.Reader() | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer r.Close() | ||
rr, _, err := compression2.AutoDecompress(r) | ||
if err != nil { | ||
return nil, err | ||
} | ||
buf := bytes.NewBuffer(nil) | ||
|
||
w := gzip.NewWriter(buf) | ||
_, err = io.Copy(w, rr) | ||
w.Close() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return buf.Bytes(), nil | ||
} | ||
|
||
type reader struct { | ||
wait sync.WaitGroup | ||
io.ReadCloser | ||
err error | ||
} | ||
|
||
func (r *reader) Close() error { | ||
err := r.ReadCloser.Close() | ||
r.wait.Wait() | ||
return errors.Join(err, r.err) | ||
} | ||
|
||
func (c *compression) Reader() (io.ReadCloser, error) { | ||
r, err := c.blob.Reader() | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer r.Close() | ||
rr, _, err := compression2.AutoDecompress(r) | ||
if err != nil { | ||
return nil, err | ||
} | ||
pr, pw := io.Pipe() | ||
cw := gzip.NewWriter(pw) | ||
|
||
outr := &reader{ReadCloser: pr} | ||
outr.wait.Add(1) | ||
|
||
go func() { | ||
_, err := io.Copy(cw, rr) | ||
outr.err = errors.Join(err, cw.Close(), pw.Close()) | ||
outr.wait.Done() | ||
}() | ||
return outr, nil | ||
} | ||
|
||
func (c *compression) Digest() digest.Digest { | ||
return BLOB_UNKNOWN_DIGEST | ||
} | ||
|
||
func (c *compression) MimeType() string { | ||
m := c.blob.MimeType() | ||
if mime.IsGZip(m) { | ||
return m | ||
} | ||
return m + "+gzip" | ||
} | ||
|
||
func (c *compression) DigestKnown() bool { | ||
return false | ||
} | ||
|
||
func (c *compression) Size() int64 { | ||
return BLOB_UNKNOWN_SIZE | ||
} | ||
|
||
func WithCompression(blob BlobAccess) (BlobAccess, error) { | ||
b, err := blob.Dup() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return spi.NewBlobAccessForBase(&compression{ | ||
blob: b, | ||
}), nil | ||
} | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
type decompression struct { | ||
blob BlobAccess | ||
} | ||
|
||
var _ spi.BlobAccessBase = (*decompression)(nil) | ||
|
||
func (c *decompression) Close() error { | ||
return c.blob.Close() | ||
} | ||
|
||
func (c *decompression) Get() ([]byte, error) { | ||
r, err := c.blob.Reader() | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer r.Close() | ||
rr, _, err := compression2.AutoDecompress(r) | ||
if err != nil { | ||
return nil, err | ||
} | ||
buf := bytes.NewBuffer(nil) | ||
_, err = io.Copy(buf, rr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return buf.Bytes(), nil | ||
} | ||
|
||
func (c *decompression) Reader() (io.ReadCloser, error) { | ||
r, err := c.blob.Reader() | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer r.Close() | ||
rr, _, err := compression2.AutoDecompress(r) | ||
return rr, err | ||
} | ||
|
||
func (c *decompression) Digest() digest.Digest { | ||
return BLOB_UNKNOWN_DIGEST | ||
} | ||
|
||
func (c *decompression) MimeType() string { | ||
m := c.blob.MimeType() | ||
if !mime.IsGZip(m) { | ||
return m | ||
} | ||
return m[:len(m)-5] | ||
} | ||
|
||
func (c *decompression) DigestKnown() bool { | ||
return false | ||
} | ||
|
||
func (c *decompression) Size() int64 { | ||
return BLOB_UNKNOWN_SIZE | ||
} | ||
|
||
func WithDecompression(blob BlobAccess) (BlobAccess, error) { | ||
b, err := blob.Dup() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return spi.NewBlobAccessForBase(&decompression{ | ||
blob: b, | ||
}), 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,85 @@ | ||
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and Open Component Model contributors. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package blobaccess_test | ||
|
||
import ( | ||
"bytes" | ||
"compress/gzip" | ||
"io" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
. "github.com/open-component-model/ocm/pkg/testutils" | ||
|
||
"github.com/open-component-model/ocm/pkg/common/accessio/blobaccess" | ||
"github.com/open-component-model/ocm/pkg/mime" | ||
) | ||
|
||
var _ = Describe("temp file management", func() { | ||
|
||
Context("compress", func() { | ||
It("compress access", func() { | ||
blob := blobaccess.ForString(mime.MIME_TEXT, "testdata") | ||
defer blob.Close() | ||
|
||
comp := Must(blobaccess.WithCompression(blob)) | ||
defer comp.Close() | ||
|
||
Expect(comp.MimeType()).To(Equal(mime.MIME_TEXT + "+gzip")) | ||
data := Must(comp.Get()) | ||
Expect(len(data)).To(Not(Equal(8))) | ||
|
||
uncomp := Must(io.ReadAll(Must(gzip.NewReader(bytes.NewReader(data))))) | ||
Expect(string(uncomp)).To(Equal("testdata")) | ||
}) | ||
|
||
It("compress reader access", func() { | ||
blob := blobaccess.ForString(mime.MIME_TEXT, "testdata") | ||
defer blob.Close() | ||
|
||
comp := Must(blobaccess.WithCompression(blob)) | ||
defer comp.Close() | ||
|
||
r := Must(comp.Reader()) | ||
data := Must(io.ReadAll(r)) | ||
Expect(len(data)).To(Not(Equal(8))) | ||
|
||
uncomp := Must(io.ReadAll(Must(gzip.NewReader(bytes.NewReader(data))))) | ||
Expect(string(uncomp)).To(Equal("testdata")) | ||
}) | ||
}) | ||
|
||
Context("uncompress", func() { | ||
buf := bytes.NewBuffer(nil) | ||
cw := gzip.NewWriter(buf) | ||
MustBeSuccessful(io.WriteString(cw, "testdata")) | ||
cw.Close() | ||
|
||
It("uncompress access", func() { | ||
blob := blobaccess.ForData(mime.MIME_TEXT+"+gzip", buf.Bytes()) | ||
defer blob.Close() | ||
|
||
comp := Must(blobaccess.WithDecompression(blob)) | ||
defer comp.Close() | ||
Expect(comp.MimeType()).To(Equal(mime.MIME_TEXT)) | ||
|
||
data := Must(comp.Get()) | ||
Expect(string(data)).To(Equal("testdata")) | ||
}) | ||
|
||
It("compress reader access", func() { | ||
blob := blobaccess.ForData(mime.MIME_TEXT+"+gzip", buf.Bytes()) | ||
defer blob.Close() | ||
|
||
comp := Must(blobaccess.WithDecompression(blob)) | ||
defer comp.Close() | ||
|
||
r := Must(comp.Reader()) | ||
data := Must(io.ReadAll(r)) | ||
Expect(string(data)).To(Equal("testdata")) | ||
}) | ||
}) | ||
|
||
}) |
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
Oops, something went wrong.