internal/pkg: return reader for files
All checks were successful
Test / Create distribution (push) Successful in 47s
Test / Sandbox (push) Successful in 2m51s
Test / ShareFS (push) Successful in 4m41s
Test / Sandbox (race detector) (push) Successful in 5m17s
Test / Hpkg (push) Successful in 5m31s
Test / Hakurei (race detector) (push) Successful in 7m28s
Test / Hakurei (push) Successful in 3m48s
Test / Flake checks (push) Successful in 1m42s
All checks were successful
Test / Create distribution (push) Successful in 47s
Test / Sandbox (push) Successful in 2m51s
Test / ShareFS (push) Successful in 4m41s
Test / Sandbox (race detector) (push) Successful in 5m17s
Test / Hpkg (push) Successful in 5m31s
Test / Hakurei (race detector) (push) Successful in 7m28s
Test / Hakurei (push) Successful in 3m48s
Test / Flake checks (push) Successful in 1m42s
This improves efficiency for cache hits. Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
@@ -87,10 +87,11 @@ func (c *CureContext) Cure(a Artifact) (
|
||||
return c.cache.Cure(a)
|
||||
}
|
||||
|
||||
// LoadData tries to load [File] from [Cache], and if that fails, obtains it via
|
||||
// [File.Data] instead. Notably, it does not cure [File].
|
||||
func (c *CureContext) LoadData(f File) (data []byte, err error) {
|
||||
return c.cache.loadData(f)
|
||||
// OpenFile tries to load [File] from [Cache], and if that fails, obtains it via
|
||||
// [File.Data] instead. Notably, it does not cure [File]. If err is nil, the
|
||||
// caller is responsible for closing the resulting [io.ReadCloser].
|
||||
func (c *CureContext) OpenFile(f File) (r io.ReadCloser, err error) {
|
||||
return c.cache.openFile(f)
|
||||
}
|
||||
|
||||
// An Artifact is a read-only reference to a piece of data that may be created
|
||||
@@ -555,9 +556,8 @@ func (c *Cache) finaliseIdent(
|
||||
close(done)
|
||||
}
|
||||
|
||||
// loadData provides [CureContext.LoadData] for [Artifact.Cure].
|
||||
func (c *Cache) loadData(f File) (data []byte, err error) {
|
||||
var r *os.File
|
||||
// openFile provides [CureContext.OpenFile] for [Artifact.Cure].
|
||||
func (c *Cache) openFile(f File) (r io.ReadCloser, err error) {
|
||||
if kc, ok := f.(KnownChecksum); ok {
|
||||
c.checksumMu.RLock()
|
||||
r, err = os.Open(c.base.Append(
|
||||
@@ -578,13 +578,11 @@ func (c *Cache) loadData(f File) (data []byte, err error) {
|
||||
if !errors.Is(err, os.ErrNotExist) {
|
||||
return
|
||||
}
|
||||
return f.Data()
|
||||
}
|
||||
|
||||
data, err = io.ReadAll(r)
|
||||
closeErr := r.Close()
|
||||
if err == nil {
|
||||
err = closeErr
|
||||
var data []byte
|
||||
if data, err = f.Data(); err != nil {
|
||||
return
|
||||
}
|
||||
r = io.NopCloser(bytes.NewReader(data))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"os"
|
||||
@@ -536,10 +537,14 @@ func TestCache(t *testing.T) {
|
||||
fs.ModeDir | 0500,
|
||||
)},
|
||||
|
||||
{"loadData directory", overrideIdent{pkg.ID{0xff, 3}, stubArtifact{
|
||||
{"openFile directory", overrideIdent{pkg.ID{0xff, 3}, stubArtifact{
|
||||
kind: pkg.KindTar,
|
||||
cure: func(c *pkg.CureContext) error {
|
||||
_, err := c.LoadData(overrideChecksumFile{checksum: wantChecksum})
|
||||
r, err := c.OpenFile(overrideChecksumFile{checksum: wantChecksum})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
_, err = io.ReadAll(r)
|
||||
return err
|
||||
},
|
||||
}}, nil, pkg.Checksum{}, &os.PathError{
|
||||
|
||||
@@ -2,7 +2,6 @@ package pkg
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"bytes"
|
||||
"compress/bzip2"
|
||||
"compress/gzip"
|
||||
"context"
|
||||
@@ -78,12 +77,17 @@ func (a *tarArtifact) Cure(c *CureContext) (err error) {
|
||||
var tr io.ReadCloser
|
||||
|
||||
{
|
||||
var data []byte
|
||||
data, err = c.LoadData(a.f)
|
||||
if err != nil {
|
||||
|
||||
if tr, err = c.OpenFile(a.f); err != nil {
|
||||
return
|
||||
}
|
||||
tr = io.NopCloser(bytes.NewReader(data))
|
||||
defer func(f io.ReadCloser) {
|
||||
closeErr := f.Close()
|
||||
if err == nil {
|
||||
err = closeErr
|
||||
}
|
||||
}(tr)
|
||||
tr = io.NopCloser(tr)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
|
||||
Reference in New Issue
Block a user