mirror of
https://github.com/AlistGo/alist.git
synced 2025-09-15 12:58:42 +08:00
* 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 4e20daaf9e.
* 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>
96 lines
2.0 KiB
Go
96 lines
2.0 KiB
Go
package search
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/alist-org/alist/v3/internal/conf"
|
|
"github.com/alist-org/alist/v3/internal/errs"
|
|
"github.com/alist-org/alist/v3/internal/model"
|
|
"github.com/alist-org/alist/v3/internal/op"
|
|
"github.com/alist-org/alist/v3/internal/search/searcher"
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
var instance searcher.Searcher = nil
|
|
|
|
// Init or reset index
|
|
func Init(mode string) error {
|
|
if instance != nil {
|
|
// unchanged, do nothing
|
|
if instance.Config().Name == mode {
|
|
return nil
|
|
}
|
|
err := instance.Release(context.Background())
|
|
if err != nil {
|
|
log.Errorf("release instance err: %+v", err)
|
|
}
|
|
instance = nil
|
|
}
|
|
if Running.Load() {
|
|
return fmt.Errorf("index is running")
|
|
}
|
|
if mode == "none" {
|
|
log.Warnf("not enable search")
|
|
return nil
|
|
}
|
|
s, ok := searcher.NewMap[mode]
|
|
if !ok {
|
|
return fmt.Errorf("not support index: %s", mode)
|
|
}
|
|
i, err := s()
|
|
if err != nil {
|
|
log.Errorf("init searcher error: %+v", err)
|
|
} else {
|
|
instance = i
|
|
}
|
|
return err
|
|
}
|
|
|
|
func Search(ctx context.Context, req model.SearchReq) ([]model.SearchNode, int64, error) {
|
|
return instance.Search(ctx, req)
|
|
}
|
|
|
|
func Index(ctx context.Context, parent string, obj model.Obj) error {
|
|
if instance == nil {
|
|
return errs.SearchNotAvailable
|
|
}
|
|
return instance.Index(ctx, model.SearchNode{
|
|
Parent: parent,
|
|
Name: obj.GetName(),
|
|
IsDir: obj.IsDir(),
|
|
Size: obj.GetSize(),
|
|
})
|
|
}
|
|
|
|
type ObjWithParent struct {
|
|
Parent string
|
|
model.Obj
|
|
}
|
|
|
|
func BatchIndex(ctx context.Context, objs []ObjWithParent) error {
|
|
if instance == nil {
|
|
return errs.SearchNotAvailable
|
|
}
|
|
if len(objs) == 0 {
|
|
return nil
|
|
}
|
|
var searchNodes []model.SearchNode
|
|
for i := range objs {
|
|
searchNodes = append(searchNodes, model.SearchNode{
|
|
Parent: objs[i].Parent,
|
|
Name: objs[i].GetName(),
|
|
IsDir: objs[i].IsDir(),
|
|
Size: objs[i].GetSize(),
|
|
})
|
|
}
|
|
return instance.BatchIndex(ctx, searchNodes)
|
|
}
|
|
|
|
func init() {
|
|
op.RegisterSettingItemHook(conf.SearchIndex, func(item *model.SettingItem) error {
|
|
log.Debugf("searcher init, mode: %s", item.Value)
|
|
return Init(item.Value)
|
|
})
|
|
}
|