feat: replace fs scans with bbolt for fast, persistent metadata management

Signed-off-by: skidoodle <contact@albert.lol>
This commit is contained in:
2026-01-18 20:27:33 +01:00
parent 5a3846266e
commit 954aec6d8e
11 changed files with 289 additions and 30 deletions
+41 -8
View File
@@ -1,20 +1,27 @@
package app
import (
"encoding/json"
"os"
"path/filepath"
"testing"
"time"
"go.etcd.io/bbolt"
)
func TestCleanup_AbandonedMerge(t *testing.T) {
tmpDir := t.TempDir()
tmpStorage := filepath.Join(tmpDir, "tmp")
tmpStorage := filepath.Join(tmpDir, TempDirName)
os.MkdirAll(tmpStorage, 0700)
db, _ := InitDB(tmpDir)
defer db.Close()
app := &App{
Conf: Config{StorageDir: tmpDir},
Conf: Config{StorageDir: tmpDir},
Logger: discardLogger(),
DB: db,
}
abandonedFile := filepath.Join(tmpStorage, "m_abandoned_upload_id")
@@ -36,12 +43,16 @@ func TestCleanup_AbandonedMerge(t *testing.T) {
func TestCleanup_AbandonedChunks(t *testing.T) {
tmpDir := t.TempDir()
tmpStorage := filepath.Join(tmpDir, "tmp")
tmpStorage := filepath.Join(tmpDir, TempDirName)
os.MkdirAll(tmpStorage, 0700)
db, _ := InitDB(tmpDir)
defer db.Close()
app := &App{
Conf: Config{StorageDir: tmpDir},
Conf: Config{StorageDir: tmpDir},
Logger: discardLogger(),
DB: db,
}
chunkDir := filepath.Join(tmpStorage, "some_upload_id")
@@ -60,26 +71,48 @@ func TestCleanup_AbandonedChunks(t *testing.T) {
func TestCleanup_ExpiredStorage(t *testing.T) {
storageDir := t.TempDir()
db, _ := InitDB(storageDir)
defer db.Close()
app := &App{
Conf: Config{
StorageDir: storageDir,
MaxMB: 100,
},
Logger: discardLogger(),
DB: db,
}
filename := "large_file_id"
path := filepath.Join(storageDir, filename)
f, _ := os.Create(path)
f.Truncate(100 * MegaByte) // Max size
f.Truncate(100 * MegaByte)
f.Close()
oldTime := time.Now().Add(-MinRetention - time.Hour)
os.Chtimes(path, oldTime, oldTime)
expiredMeta := FileMeta{
ID: filename,
Size: 100 * MegaByte,
CreatedAt: time.Now().Add(-MinRetention - 2*time.Hour),
ExpiresAt: time.Now().Add(-time.Hour),
}
app.CleanStorage(storageDir)
app.DB.Update(func(tx *bbolt.Tx) error {
b := tx.Bucket([]byte(DBBucketName))
data, _ := json.Marshal(expiredMeta)
return b.Put([]byte(filename), data)
})
app.CleanStorage()
if _, err := os.Stat(path); !os.IsNotExist(err) {
t.Error("Cleanup failed to remove expired large file")
}
app.DB.View(func(tx *bbolt.Tx) error {
b := tx.Bucket([]byte(DBBucketName))
if v := b.Get([]byte(filename)); v != nil {
t.Error("Cleanup failed to remove metadata")
}
return nil
})
}