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

@ -1,52 +1,16 @@
package db
import (
stdpath "path"
"time"
"github.com/Xhofe/go-cache"
"github.com/alist-org/alist/v3/internal/errs"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/pkg/singleflight"
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/pkg/errors"
"gorm.io/gorm"
)
var metaCache = cache.NewMemCache(cache.WithShards[*model.Meta](2))
// metaG maybe not needed
var metaG singleflight.Group[*model.Meta]
func GetNearestMeta(path string) (*model.Meta, error) {
path = utils.FixAndCleanPath(path)
meta, err := GetMetaByPath(path)
if err == nil {
return meta, nil
}
if errors.Cause(err) != gorm.ErrRecordNotFound {
return nil, err
}
if path == "/" {
return nil, errors.WithStack(errs.MetaNotFound)
}
return GetNearestMeta(stdpath.Dir(path))
}
func GetMetaByPath(path string) (*model.Meta, error) {
meta, ok := metaCache.Get(path)
if ok {
return meta, nil
meta := model.Meta{Path: path}
if err := db.Where(meta).First(&meta).Error; err != nil {
return nil, errors.Wrapf(err, "failed select meta")
}
meta, err, _ := metaG.Do(path, func() (*model.Meta, error) {
meta := model.Meta{Path: path}
if err := db.Where(meta).First(&meta).Error; err != nil {
return nil, errors.Wrapf(err, "failed select meta")
}
metaCache.Set(path, &meta, cache.WithEx[*model.Meta](time.Hour))
return &meta, nil
})
return meta, err
return &meta, nil
}
func GetMetaById(id uint) (*model.Meta, error) {
@ -62,32 +26,20 @@ func CreateMeta(u *model.Meta) error {
}
func UpdateMeta(u *model.Meta) error {
old, err := GetMetaById(u.ID)
if err != nil {
return err
}
metaCache.Del(old.Path)
return errors.WithStack(db.Save(u).Error)
}
func GetMetas(pageIndex, pageSize int) ([]model.Meta, int64, error) {
func GetMetas(pageIndex, pageSize int) (metas []model.Meta, count int64, err error) {
metaDB := db.Model(&model.Meta{})
var count int64
if err := metaDB.Count(&count).Error; err != nil {
if err = metaDB.Count(&count).Error; err != nil {
return nil, 0, errors.Wrapf(err, "failed get metas count")
}
var metas []model.Meta
if err := metaDB.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&metas).Error; err != nil {
if err = metaDB.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&metas).Error; err != nil {
return nil, 0, errors.Wrapf(err, "failed get find metas")
}
return metas, count, nil
}
func DeleteMetaById(id uint) error {
old, err := GetMetaById(id)
if err != nil {
return err
}
metaCache.Del(old.Path)
return errors.WithStack(db.Delete(&model.Meta{}, id).Error)
}