chore: rename VirtualPath to MountPath

This commit is contained in:
Noah Hsu
2022-07-12 14:11:37 +08:00
parent fbc858b43c
commit 2bff656f00
11 changed files with 59 additions and 59 deletions

View File

@ -24,7 +24,7 @@ var filesCache = cache.NewMemCache(cache.WithShards[[]model.Obj](64))
var filesG singleflight.Group[[]model.Obj]
func ClearCache(storage driver.Driver, path string) {
key := stdpath.Join(storage.GetStorage().VirtualPath, path)
key := stdpath.Join(storage.GetStorage().MountPath, path)
filesCache.Del(key)
}
@ -42,7 +42,7 @@ func List(ctx context.Context, storage driver.Driver, path string, refresh ...bo
if storage.Config().NoCache {
return storage.List(ctx, dir)
}
key := stdpath.Join(storage.GetStorage().VirtualPath, path)
key := stdpath.Join(storage.GetStorage().MountPath, path)
if len(refresh) == 0 || !refresh[0] {
if files, ok := filesCache.Get(key); ok {
return files, nil
@ -132,7 +132,7 @@ func Link(ctx context.Context, storage driver.Driver, path string, args model.Li
if file.IsDir() {
return nil, nil, errors.WithStack(errs.NotFile)
}
key := stdpath.Join(storage.GetStorage().VirtualPath, path)
key := stdpath.Join(storage.GetStorage().MountPath, path)
if link, ok := linkCache.Get(key); ok {
return link, file, nil
}
@ -253,7 +253,7 @@ func Put(ctx context.Context, storage driver.Driver, dstDirPath string, file mod
log.Debugf("put file [%s] done", file.GetName())
if err == nil {
// clear cache
key := stdpath.Join(storage.GetStorage().VirtualPath, dstDirPath)
key := stdpath.Join(storage.GetStorage().MountPath, dstDirPath)
filesCache.Del(key)
}
return err

View File

@ -31,8 +31,8 @@ func GetStorageAndActualPath(rawPath string) (driver.Driver, string, error) {
if storage == nil {
return nil, "", errors.Errorf("can't find storage with rawPath: %s", rawPath)
}
log.Debugln("use storage: ", storage.GetStorage().VirtualPath)
virtualPath := utils.GetActualVirtualPath(storage.GetStorage().VirtualPath)
log.Debugln("use storage: ", storage.GetStorage().MountPath)
virtualPath := utils.GetActualVirtualPath(storage.GetStorage().MountPath)
actualPath := strings.TrimPrefix(rawPath, virtualPath)
actualPath = ActualPath(storage.GetAddition(), actualPath)
return storage, actualPath, nil

View File

@ -32,7 +32,7 @@ func GetStorageByVirtualPath(virtualPath string) (driver.Driver, error) {
// then instantiate corresponding driver and save it in memory
func CreateStorage(ctx context.Context, storage model.Storage) error {
storage.Modified = time.Now()
storage.VirtualPath = utils.StandardizePath(storage.VirtualPath)
storage.MountPath = utils.StandardizePath(storage.MountPath)
var err error
// check driver first
driverName := storage.Driver
@ -52,7 +52,7 @@ func CreateStorage(ctx context.Context, storage model.Storage) error {
return errors.WithMessage(err, "failed init storage but storage is already created")
}
log.Debugf("storage %+v is created", storageDriver)
storagesMap.Store(storage.VirtualPath, storageDriver)
storagesMap.Store(storage.MountPath, storageDriver)
return nil
}
@ -68,15 +68,15 @@ func UpdateStorage(ctx context.Context, storage model.Storage) error {
return errors.Errorf("driver cannot be changed")
}
storage.Modified = time.Now()
storage.VirtualPath = utils.StandardizePath(storage.VirtualPath)
storage.MountPath = utils.StandardizePath(storage.MountPath)
err = db.UpdateStorage(&storage)
if err != nil {
return errors.WithMessage(err, "failed update storage in database")
}
storageDriver, err := GetStorageByVirtualPath(oldStorage.VirtualPath)
if oldStorage.VirtualPath != storage.VirtualPath {
storageDriver, err := GetStorageByVirtualPath(oldStorage.MountPath)
if oldStorage.MountPath != storage.MountPath {
// virtual path renamed, need to drop the storage
storagesMap.Delete(oldStorage.VirtualPath)
storagesMap.Delete(oldStorage.MountPath)
}
if err != nil {
return errors.WithMessage(err, "failed get storage driver")
@ -89,7 +89,7 @@ func UpdateStorage(ctx context.Context, storage model.Storage) error {
if err != nil {
return errors.WithMessage(err, "failed init storage")
}
storagesMap.Store(storage.VirtualPath, storageDriver)
storagesMap.Store(storage.MountPath, storageDriver)
return nil
}
@ -98,7 +98,7 @@ func DeleteStorageById(ctx context.Context, id uint) error {
if err != nil {
return errors.WithMessage(err, "failed get storage")
}
storageDriver, err := GetStorageByVirtualPath(storage.VirtualPath)
storageDriver, err := GetStorageByVirtualPath(storage.MountPath)
if err != nil {
return errors.WithMessage(err, "failed get storage driver")
}
@ -111,7 +111,7 @@ func DeleteStorageById(ctx context.Context, id uint) error {
return errors.WithMessage(err, "failed delete storage in database")
}
// delete the storage in the memory
storagesMap.Delete(storage.VirtualPath)
storagesMap.Delete(storage.MountPath)
return nil
}
@ -145,7 +145,7 @@ func getStoragesByPath(path string) []driver.Driver {
storages := make([]driver.Driver, 0)
curSlashCount := 0
storagesMap.Range(func(key string, value driver.Driver) bool {
virtualPath := utils.GetActualVirtualPath(value.GetStorage().VirtualPath)
virtualPath := utils.GetActualVirtualPath(value.GetStorage().MountPath)
if virtualPath == "/" {
virtualPath = ""
}
@ -167,7 +167,7 @@ func getStoragesByPath(path string) []driver.Driver {
})
// make sure the order is the same for same input
sort.Slice(storages, func(i, j int) bool {
return storages[i].GetStorage().VirtualPath < storages[j].GetStorage().VirtualPath
return storages[i].GetStorage().MountPath < storages[j].GetStorage().MountPath
})
return storages
}
@ -180,7 +180,7 @@ func GetStorageVirtualFilesByPath(prefix string) []model.Obj {
storages := storagesMap.Values()
sort.Slice(storages, func(i, j int) bool {
if storages[i].GetStorage().Index == storages[j].GetStorage().Index {
return storages[i].GetStorage().VirtualPath < storages[j].GetStorage().VirtualPath
return storages[i].GetStorage().MountPath < storages[j].GetStorage().MountPath
}
return storages[i].GetStorage().Index < storages[j].GetStorage().Index
})
@ -192,10 +192,10 @@ func GetStorageVirtualFilesByPath(prefix string) []model.Obj {
for _, v := range storages {
// TODO should save a balanced storage
// balance storage
if utils.IsBalance(v.GetStorage().VirtualPath) {
if utils.IsBalance(v.GetStorage().MountPath) {
continue
}
virtualPath := v.GetStorage().VirtualPath
virtualPath := v.GetStorage().MountPath
if len(virtualPath) <= len(prefix) {
continue
}
@ -230,7 +230,7 @@ func GetBalancedStorage(path string) driver.Driver {
case 1:
return storages[0]
default:
virtualPath := utils.GetActualVirtualPath(storages[0].GetStorage().VirtualPath)
virtualPath := utils.GetActualVirtualPath(storages[0].GetStorage().MountPath)
cur, ok := balanceMap.Load(virtualPath)
i := 0
if ok {

View File

@ -25,9 +25,9 @@ func TestCreateStorage(t *testing.T) {
storage model.Storage
isErr bool
}{
{storage: model.Storage{Driver: "Local", VirtualPath: "/local", Addition: `{"root_folder":"."}`}, isErr: false},
{storage: model.Storage{Driver: "Local", VirtualPath: "/local", Addition: `{"root_folder":"."}`}, isErr: true},
{storage: model.Storage{Driver: "None", VirtualPath: "/none", Addition: `{"root_folder":"."}`}, isErr: true},
{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},
}
for _, storage := range storages {
err := operations.CreateStorage(context.Background(), storage.storage)
@ -59,22 +59,22 @@ func TestGetStorageVirtualFilesByPath(t *testing.T) {
func TestGetBalancedStorage(t *testing.T) {
setupStorages(t)
storage := operations.GetBalancedStorage("/a/d/e")
if storage.GetStorage().VirtualPath != "/a/d/e" {
t.Errorf("expected: /a/d/e, got: %+v", storage.GetStorage().VirtualPath)
if storage.GetStorage().MountPath != "/a/d/e" {
t.Errorf("expected: /a/d/e, got: %+v", storage.GetStorage().MountPath)
}
storage = operations.GetBalancedStorage("/a/d/e")
if storage.GetStorage().VirtualPath != "/a/d/e.balance" {
t.Errorf("expected: /a/d/e.balance, got: %+v", storage.GetStorage().VirtualPath)
if storage.GetStorage().MountPath != "/a/d/e.balance" {
t.Errorf("expected: /a/d/e.balance, got: %+v", storage.GetStorage().MountPath)
}
}
func setupStorages(t *testing.T) {
var storages = []model.Storage{
{Driver: "Local", VirtualPath: "/a/b", Index: 0, Addition: `{"root_folder":"."}`},
{Driver: "Local", VirtualPath: "/a/c", Index: 1, Addition: `{"root_folder":"."}`},
{Driver: "Local", VirtualPath: "/a/d", Index: 2, Addition: `{"root_folder":"."}`},
{Driver: "Local", VirtualPath: "/a/d/e", Index: 3, Addition: `{"root_folder":"."}`},
{Driver: "Local", VirtualPath: "/a/d/e.balance", Index: 4, Addition: `{"root_folder":"."}`},
{Driver: "Local", MountPath: "/a/b", Index: 0, Addition: `{"root_folder":"."}`},
{Driver: "Local", MountPath: "/a/c", Index: 1, Addition: `{"root_folder":"."}`},
{Driver: "Local", MountPath: "/a/d", Index: 2, Addition: `{"root_folder":"."}`},
{Driver: "Local", MountPath: "/a/d/e", Index: 3, Addition: `{"root_folder":"."}`},
{Driver: "Local", MountPath: "/a/d/e.balance", Index: 4, Addition: `{"root_folder":"."}`},
}
for _, storage := range storages {
err := operations.CreateStorage(context.Background(), storage)