mirror of
https://github.com/skidoodle/safebin.git
synced 2026-04-28 03:07:41 +02:00
feat: replace fs scans with bbolt for fast, persistent metadata management
Signed-off-by: skidoodle <contact@albert.lol>
This commit is contained in:
@@ -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
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user