refactor: split the db package hook and cache to the op package (#2747)

* refactor:separate the setting method from the db package to the op package and add the cache

* refactor:separate the meta method from the db package to the op package

* fix:setting not load database data

* refactor:separate the user method from the db package to the op package

* refactor:remove user JoinPath error

* fix:op package user cache

* refactor:fs package list method

* fix:tile virtual paths (close #2743)

* Revert "refactor:remove user JoinPath error"

This reverts commit 4e20daaf9e700da047000d4fd4900abbe05c3848.

* clean path directly may lead to unknown behavior

* fix: The path of the meta passed in must be prefix of reqPath

* chore: rename all virtualPath to mountPath

* fix: `getStoragesByPath` and `GetStorageVirtualFilesByPath`

is_sub_path:

/a/b isn't subpath of /a/bc

* fix: don't save setting if hook error

Co-authored-by: Noah Hsu <i@nn.ci>
This commit is contained in:
foxxorcat
2022-12-18 19:51:20 +08:00
committed by GitHub
parent f38f4f401b
commit 6024e8d832
39 changed files with 741 additions and 543 deletions

View File

@ -4,10 +4,12 @@ import (
"context"
"testing"
"github.com/alist-org/alist/v3/internal/conf"
"github.com/alist-org/alist/v3/internal/db"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/op"
"github.com/alist-org/alist/v3/pkg/utils"
mapset "github.com/deckarep/golang-set/v2"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
@ -17,6 +19,7 @@ func init() {
if err != nil {
panic("failed to connect database")
}
conf.Conf = conf.DefaultConfig()
db.Init(dB)
}
@ -25,9 +28,9 @@ func TestCreateStorage(t *testing.T) {
storage model.Storage
isErr bool
}{
{storage: model.Storage{Driver: "Local", MountPath: "/local", Addition: `{"root_folder":"."}`}, isErr: false},
{storage: model.Storage{Driver: "Local", MountPath: "/local", Addition: `{"root_folder":"."}`}, isErr: true},
{storage: model.Storage{Driver: "None", MountPath: "/none", Addition: `{"root_folder":"."}`}, isErr: true},
{storage: model.Storage{Driver: "Local", MountPath: "/local", Addition: `{"root_folder_path":"."}`}, isErr: false},
{storage: model.Storage{Driver: "Local", MountPath: "/local", Addition: `{"root_folder_path":"."}`}, isErr: true},
{storage: model.Storage{Driver: "None", MountPath: "/none", Addition: `{"root_folder_path":"."}`}, isErr: true},
}
for _, storage := range storages {
_, err := op.CreateStorage(context.Background(), storage.storage)
@ -58,23 +61,26 @@ func TestGetStorageVirtualFilesByPath(t *testing.T) {
func TestGetBalancedStorage(t *testing.T) {
setupStorages(t)
storage := op.GetBalancedStorage("/a/d/e")
if storage.GetStorage().MountPath != "/a/d/e" {
t.Errorf("expected: /a/d/e, got: %+v", storage.GetStorage().MountPath)
set := mapset.NewSet[string]()
for i := 0; i < 5; i++ {
storage := op.GetBalancedStorage("/a/d/e1")
set.Add(storage.GetStorage().MountPath)
}
storage = op.GetBalancedStorage("/a/d/e")
if storage.GetStorage().MountPath != "/a/d/e.balance" {
t.Errorf("expected: /a/d/e.balance, got: %+v", storage.GetStorage().MountPath)
expected := mapset.NewSet([]string{"/a/d/e1", "/a/d/e1.balance"}...)
if !expected.Equal(set) {
t.Errorf("expected: %+v, got: %+v", expected, set)
}
}
func setupStorages(t *testing.T) {
var storages = []model.Storage{
{Driver: "Local", MountPath: "/a/b", Order: 0, Addition: `{"root_folder":"."}`},
{Driver: "Local", MountPath: "/a/c", Order: 1, Addition: `{"root_folder":"."}`},
{Driver: "Local", MountPath: "/a/d", Order: 2, Addition: `{"root_folder":"."}`},
{Driver: "Local", MountPath: "/a/d/e", Order: 3, Addition: `{"root_folder":"."}`},
{Driver: "Local", MountPath: "/a/d/e.balance", Order: 4, Addition: `{"root_folder":"."}`},
{Driver: "Local", MountPath: "/a/b", Order: 0, Addition: `{"root_folder_path":"."}`},
{Driver: "Local", MountPath: "/adc", Order: 0, Addition: `{"root_folder_path":"."}`},
{Driver: "Local", MountPath: "/a/c", Order: 1, Addition: `{"root_folder_path":"."}`},
{Driver: "Local", MountPath: "/a/d", Order: 2, Addition: `{"root_folder_path":"."}`},
{Driver: "Local", MountPath: "/a/d/e1", Order: 3, Addition: `{"root_folder_path":"."}`},
{Driver: "Local", MountPath: "/a/d/e", Order: 4, Addition: `{"root_folder_path":"."}`},
{Driver: "Local", MountPath: "/a/d/e1.balance", Order: 4, Addition: `{"root_folder_path":"."}`},
}
for _, storage := range storages {
_, err := op.CreateStorage(context.Background(), storage)