feat: customize index max depth

Because some driver's issue may cause infinite loop
pull/3078/head
Noah Hsu 2023-01-17 17:33:18 +08:00
parent 9c7e451c03
commit 26fe0a7684
7 changed files with 19 additions and 21 deletions

View File

@ -146,6 +146,7 @@ func InitialSettings() []model.SettingItem {
{Key: conf.SearchIndex, Value: "none", Type: conf.TypeSelect, Options: "database,database_non_full_text,bleve,none", Group: model.INDEX}, {Key: conf.SearchIndex, Value: "none", Type: conf.TypeSelect, Options: "database,database_non_full_text,bleve,none", Group: model.INDEX},
{Key: conf.AutoUpdateIndex, Value: "false", Type: conf.TypeBool, Group: model.INDEX}, {Key: conf.AutoUpdateIndex, Value: "false", Type: conf.TypeBool, Group: model.INDEX},
{Key: conf.IgnorePaths, Value: "", Type: conf.TypeText, Group: model.INDEX, Flag: model.PRIVATE, Help: `one path per line`}, {Key: conf.IgnorePaths, Value: "", Type: conf.TypeText, Group: model.INDEX, Flag: model.PRIVATE, Help: `one path per line`},
{Key: conf.MaxIndexDepth, Value: "20", Type: conf.TypeNumber, Group: model.INDEX, Flag: model.PRIVATE, Help: `max depth of index`},
{Key: conf.IndexProgress, Value: "{}", Type: conf.TypeText, Group: model.SINGLE, Flag: model.PRIVATE}, {Key: conf.IndexProgress, Value: "{}", Type: conf.TypeText, Group: model.SINGLE, Flag: model.PRIVATE},
// GitHub settings // GitHub settings

View File

@ -44,8 +44,8 @@ const (
// index // index
SearchIndex = "search_index" SearchIndex = "search_index"
AutoUpdateIndex = "auto_update_index" AutoUpdateIndex = "auto_update_index"
IndexPaths = "index_paths"
IgnorePaths = "ignore_paths" IgnorePaths = "ignore_paths"
MaxIndexDepth = "max_index_depth"
// aria2 // aria2
Aria2Uri = "aria2_uri" Aria2Uri = "aria2_uri"

View File

@ -2,7 +2,7 @@ package db
import ( import (
"fmt" "fmt"
"path" stdpath "path"
"strings" "strings"
"github.com/alist-org/alist/v3/internal/conf" "github.com/alist-org/alist/v3/internal/conf"
@ -29,13 +29,13 @@ func BatchCreateSearchNodes(nodes *[]model.SearchNode) error {
return db.CreateInBatches(nodes, 1000).Error return db.CreateInBatches(nodes, 1000).Error
} }
func DeleteSearchNodesByParent(prefix string) error { func DeleteSearchNodesByParent(path string) error {
prefix = utils.FixAndCleanPath(prefix) path = utils.FixAndCleanPath(path)
err := db.Where(whereInParent(prefix)).Delete(&model.SearchNode{}).Error err := db.Where(whereInParent(path)).Delete(&model.SearchNode{}).Error
if err != nil { if err != nil {
return err return err
} }
dir, name := path.Split(prefix) dir, name := stdpath.Split(path)
return db.Where(fmt.Sprintf("%s = ? AND %s = ?", return db.Where(fmt.Sprintf("%s = ? AND %s = ?",
columnName("parent"), columnName("name")), columnName("parent"), columnName("name")),
dir, name).Delete(&model.SearchNode{}).Error dir, name).Delete(&model.SearchNode{}).Error

View File

@ -217,10 +217,11 @@ func Update(parent string, objs []model.Obj) {
} }
// build index if it's a folder // build index if it's a folder
if objs[i].IsDir() { if objs[i].IsDir() {
dir := path.Join(parent, objs[i].GetName())
err = BuildIndex(ctx, err = BuildIndex(ctx,
[]string{path.Join(parent, objs[i].GetName())}, []string{dir},
conf.SlicesMap[conf.IgnorePaths], conf.SlicesMap[conf.IgnorePaths],
-1, false) setting.GetInt(conf.MaxIndexDepth, 20)-strings.Count(dir, "/"), false)
if err != nil { if err != nil {
log.Errorf("update search index error while build index: %+v", err) log.Errorf("update search index error while build index: %+v", err)
return return

View File

@ -30,8 +30,8 @@ func (D DB) Get(ctx context.Context, parent string) ([]model.SearchNode, error)
return db.GetSearchNodesByParent(parent) return db.GetSearchNodesByParent(parent)
} }
func (D DB) Del(ctx context.Context, prefix string) error { func (D DB) Del(ctx context.Context, path string) error {
return db.DeleteSearchNodesByParent(prefix) return db.DeleteSearchNodesByParent(path)
} }
func (D DB) Release(ctx context.Context) error { func (D DB) Release(ctx context.Context) error {

View File

@ -30,8 +30,8 @@ func (D DB) Get(ctx context.Context, parent string) ([]model.SearchNode, error)
return db.GetSearchNodesByParent(parent) return db.GetSearchNodesByParent(parent)
} }
func (D DB) Del(ctx context.Context, prefix string) error { func (D DB) Del(ctx context.Context, path string) error {
return db.DeleteSearchNodesByParent(prefix) return db.DeleteSearchNodesByParent(path)
} }
func (D DB) Release(ctx context.Context) error { func (D DB) Release(ctx context.Context) error {

View File

@ -6,23 +6,19 @@ import (
"github.com/alist-org/alist/v3/internal/conf" "github.com/alist-org/alist/v3/internal/conf"
"github.com/alist-org/alist/v3/internal/model" "github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/search" "github.com/alist-org/alist/v3/internal/search"
"github.com/alist-org/alist/v3/internal/setting"
"github.com/alist-org/alist/v3/server/common" "github.com/alist-org/alist/v3/server/common"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
type BuildOrUpdateIndexReq struct { type UpdateIndexReq struct {
Paths []string `json:"paths"` Paths []string `json:"paths"`
MaxDepth int `json:"max_depth"` MaxDepth int `json:"max_depth"`
//IgnorePaths []string `json:"ignore_paths"` //IgnorePaths []string `json:"ignore_paths"`
} }
func BuildIndex(c *gin.Context) { func BuildIndex(c *gin.Context) {
var req BuildOrUpdateIndexReq
if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400)
return
}
if search.Running.Load() { if search.Running.Load() {
common.ErrorStrResp(c, "index is running", 400) common.ErrorStrResp(c, "index is running", 400)
return return
@ -34,8 +30,8 @@ func BuildIndex(c *gin.Context) {
log.Errorf("clear index error: %+v", err) log.Errorf("clear index error: %+v", err)
return return
} }
err = search.BuildIndex(context.Background(), req.Paths, err = search.BuildIndex(context.Background(), []string{"/"},
conf.SlicesMap[conf.IgnorePaths], req.MaxDepth, true) conf.SlicesMap[conf.IgnorePaths], setting.GetInt(conf.MaxIndexDepth, 20), true)
if err != nil { if err != nil {
log.Errorf("build index error: %+v", err) log.Errorf("build index error: %+v", err)
} }
@ -44,7 +40,7 @@ func BuildIndex(c *gin.Context) {
} }
func UpdateIndex(c *gin.Context) { func UpdateIndex(c *gin.Context) {
var req BuildOrUpdateIndexReq var req UpdateIndexReq
if err := c.ShouldBind(&req); err != nil { if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400) common.ErrorResp(c, err, 400)
return return