Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mandelsoft committed Nov 26, 2024
1 parent 0434710 commit c032dc6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
8 changes: 7 additions & 1 deletion api/utils/accessio/chunkedreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ import (
"github.com/mandelsoft/goutils/general"
)

// ChunkedReader splits a reader into several
// logical readers with a limited content size.
// Once the reader reaches its limits it provides
// a io.EOF.
// It can be continued by Calling Next, which returns
// whether a follow-up is required or not.
type ChunkedReader struct {
lock sync.Mutex
reader io.Reader
Expand All @@ -34,7 +40,7 @@ func (c *ChunkedReader) Read(p []byte) (n int, err error) {
c.lock.Lock()
defer c.lock.Unlock()

if c.read == c.size {
if c.read >= c.size {
return 0, io.EOF
}
if c.read+int64(len(p)) > c.size {
Expand Down
3 changes: 2 additions & 1 deletion api/utils/accessio/chunkedreader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"ocm.software/ocm/api/utils/accessio"
)

var _ = FDescribe("Test Environment", func() {
var _ = Describe("Test Environment", func() {
in := "12345678901234567890"
var buf *bytes.Buffer
var chunked *accessio.ChunkedReader
Expand Down
12 changes: 10 additions & 2 deletions api/utils/blobaccess/chunked/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"ocm.software/ocm/api/utils/mime"
)

func newChunck(r io.Reader, fss ...vfs.FileSystem) (bpi.BlobAccess, error) {
func newChunk(r io.Reader, fss ...vfs.FileSystem) (bpi.BlobAccess, error) {
t, err := blobaccess.NewTempFile("", "chunk-*", fss...)
if err != nil {
return nil, err
Expand All @@ -28,6 +28,8 @@ func newChunck(r io.Reader, fss ...vfs.FileSystem) (bpi.BlobAccess, error) {
return t.AsBlob(mime.MIME_OCTET), nil
}

// ChunkedBlobSource provides a sequence of
// bpi.BlobAccess objects.
type ChunkedBlobSource interface {
Next() (bpi.BlobAccess, error)
}
Expand All @@ -40,6 +42,12 @@ type chunkedAccess struct {
cont bool
}

// New provides a sequence of
// bpi.BlobAccess objects for a given io.Reader
// each with a limited size.
// The provided blobs are temporarily stored
// on the filesystem and can therefore be kept
// and accessed any number of times until they are closed.
func New(r io.Reader, chunksize int64, fss ...vfs.FileSystem) ChunkedBlobSource {
reader := accessio.NewChunkedReader(r, chunksize)
return &chunkedAccess{
Expand All @@ -63,5 +71,5 @@ func (r *chunkedAccess) Next() (bpi.BlobAccess, error) {
}
}
r.cont = true
return newChunck(r.reader, r.fs)
return newChunk(r.reader, r.fs)
}

0 comments on commit c032dc6

Please sign in to comment.