Keep cached sources and variants in one FileStore type (closes #318)
check / check (push) Canceled after 0s

imgcache.FileStore is a directory of files named by a hex key, at
<ab>/<cd>/<key>. It writes a file under a temp name, syncs it and renames
it into place. It opens, reports the size of and deletes the file for a
key. The cache keeps its sources in one FileStore and its variants in
another, so ContentStorage and VariantStorage are gone. FileStore has no
Exists: the reconciliation pass checks a variant's file with Size, and the
tests look for the file on disk. Cache.Variant reads the content type from
the file's first bytes itself. storage.go is now filestore.go, and the
unused testdata/red.avif is gone.

Model: opus-5-5
This commit was merged in pull request #335.
This commit is contained in:
2026-10-26 20:24:33 +01:00
parent 17376d885b
commit ac850823f4
12 changed files with 496 additions and 535 deletions
+21 -16
View File
@@ -209,7 +209,8 @@ func assertNoDanglingReferences(t *testing.T, cache *Cache) {
t.Fatalf("failed to scan source_key: %v", err)
}
if !cache.srcContent.Exists(imagerequest.SourceKey(key)) {
_, err = os.Stat(cache.srcContent.keyToPath(key))
if err != nil {
t.Errorf("source_content references content %s but the file is missing",
key)
}
@@ -236,7 +237,8 @@ func assertNoDanglingReferences(t *testing.T, cache *Cache) {
t.Fatalf("failed to scan cache_key: %v", err)
}
if !cache.variants.Exists(imagerequest.VariantKey(key)) {
_, err = os.Stat(cache.variants.keyToPath(key))
if err != nil {
t.Errorf("variant_content references key %s but the file is missing", key)
}
}
@@ -334,7 +336,8 @@ func TestEvictToLimitEvictsLeastRecentlyUsedFirst(t *testing.T) {
t.Errorf("usage after eviction = %d, want <= %d", usage, limit)
}
if cache.variants.Exists(keys[0]) {
_, err = os.Stat(cache.variants.keyToPath(string(keys[0])))
if err == nil {
t.Errorf("least recently used variant %s must be evicted", keys[0])
}
@@ -345,7 +348,8 @@ func TestEvictToLimitEvictsLeastRecentlyUsedFirst(t *testing.T) {
}
for _, key := range keys[1:] {
if !cache.variants.Exists(key) {
_, err = os.Stat(cache.variants.keyToPath(string(key)))
if err != nil {
t.Errorf("more recently used variant %s must survive eviction", key)
}
}
@@ -373,7 +377,8 @@ func TestEvictionKeepsEverythingWhenUnderLimit(t *testing.T) {
}
for _, key := range keys {
if !cache.srcContent.Exists(key) {
_, err = os.Stat(cache.srcContent.keyToPath(string(key)))
if err != nil {
t.Errorf("source %s must not be evicted while usage is under the limit", key)
}
@@ -384,7 +389,8 @@ func TestEvictionKeepsEverythingWhenUnderLimit(t *testing.T) {
}
}
if !cache.variants.Exists(testVariantKeyOne) {
_, err = os.Stat(cache.variants.keyToPath(string(testVariantKeyOne)))
if err != nil {
t.Error("variant must not be evicted while usage is under the limit")
}
@@ -586,7 +592,7 @@ func TestEvictionRunsOnPeriodicSchedule(t *testing.T) {
for i, key := range keys {
content := bytes.Repeat([]byte{fills[i]}, 1000)
_, err = cache.variants.Store(key, bytes.NewReader(content))
err = cache.variants.Write(string(key), content)
if err != nil {
t.Fatalf("failed to store variant file: %v", err)
}
@@ -605,7 +611,8 @@ func TestEvictionRunsOnPeriodicSchedule(t *testing.T) {
stored = 0
for _, key := range keys {
if cache.variants.Exists(key) {
_, err = os.Stat(cache.variants.keyToPath(string(key)))
if err == nil {
stored++
}
}
@@ -637,9 +644,7 @@ func TestStartEvictionReconcilesAccountingWithDisk(t *testing.T) {
// feature existed) must be adopted into the accounting.
untracked := bytes.Repeat([]byte{0x31}, 1000)
_, err := cache.variants.Store(
testVariantKeyOne, bytes.NewReader(untracked),
)
err := cache.variants.Write(string(testVariantKeyOne), untracked)
if err != nil {
t.Fatalf("failed to store untracked variant file: %v", err)
}
@@ -738,9 +743,7 @@ func TestPeriodicReconciliationAdoptsFileThatAppearsAfterStartup(t *testing.T) {
// insert had failed and only the file write had succeeded.
untracked := bytes.Repeat([]byte{0x41}, 900)
_, err = cache.variants.Store(
"aabbccdd0099", bytes.NewReader(untracked),
)
err = cache.variants.Write("aabbccdd0099", untracked)
if err != nil {
t.Fatalf("failed to store untracked variant file: %v", err)
}
@@ -1046,13 +1049,15 @@ func assertOnlyOldestSourceEvicted(
) {
t.Helper()
if cache.srcContent.Exists(keys[0]) {
_, err := os.Stat(cache.srcContent.keyToPath(string(keys[0])))
if err == nil {
t.Errorf("source %s, evicted when the context was cancelled, "+
"is still on disk", keys[0])
}
for _, key := range keys[1:] {
if !cache.srcContent.Exists(key) {
_, err = os.Stat(cache.srcContent.keyToPath(string(key)))
if err != nil {
t.Errorf("source %s was evicted after the context was cancelled", key)
}