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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user