* feat: index paths as setting * feat: clear index (#2632) * feat: check indexMQ more frequently
This commit is contained in:
parent
1f7c1b4f43
commit
6f1aeb47fd
@ -132,6 +132,7 @@ func InitialSettings() []model.SettingItem {
|
|||||||
// single settings
|
// single settings
|
||||||
{Key: conf.Token, Value: token, Type: conf.TypeString, Group: model.SINGLE, Flag: model.PRIVATE},
|
{Key: conf.Token, Value: token, Type: conf.TypeString, Group: model.SINGLE, Flag: model.PRIVATE},
|
||||||
{Key: conf.SearchIndex, Value: "none", Type: conf.TypeSelect, Options: "database,bleve,none", Group: model.INDEX},
|
{Key: conf.SearchIndex, Value: "none", Type: conf.TypeSelect, Options: "database,bleve,none", Group: model.INDEX},
|
||||||
|
{Key: conf.IndexPaths, 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.IgnorePaths, Value: "", Type: conf.TypeText, Group: model.INDEX, Flag: model.PRIVATE, Help: `one path per line`},
|
||||||
{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},
|
||||||
}
|
}
|
||||||
|
@ -44,6 +44,7 @@ const (
|
|||||||
|
|
||||||
// index
|
// index
|
||||||
SearchIndex = "search_index"
|
SearchIndex = "search_index"
|
||||||
|
IndexPaths = "index_paths"
|
||||||
IgnorePaths = "ignore_paths"
|
IgnorePaths = "ignore_paths"
|
||||||
|
|
||||||
// aria2
|
// aria2
|
||||||
|
@ -33,10 +33,17 @@ func BuildIndex(ctx context.Context, indexPaths, ignorePaths []string, maxDepth
|
|||||||
Quit = make(chan struct{}, 1)
|
Quit = make(chan struct{}, 1)
|
||||||
indexMQ := mq.NewInMemoryMQ[ObjWithParent]()
|
indexMQ := mq.NewInMemoryMQ[ObjWithParent]()
|
||||||
go func() {
|
go func() {
|
||||||
ticker := time.NewTicker(5 * time.Second)
|
ticker := time.NewTicker(time.Second)
|
||||||
|
tickCount := 0
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
|
tickCount += 1
|
||||||
|
if indexMQ.Len() < 1000 && tickCount != 5 {
|
||||||
|
continue
|
||||||
|
} else if tickCount >= 5 {
|
||||||
|
tickCount = 0
|
||||||
|
}
|
||||||
log.Infof("index obj count: %d", objCount)
|
log.Infof("index obj count: %d", objCount)
|
||||||
indexMQ.ConsumeAll(func(messages []mq.Message[ObjWithParent]) {
|
indexMQ.ConsumeAll(func(messages []mq.Message[ObjWithParent]) {
|
||||||
if len(messages) != 0 {
|
if len(messages) != 0 {
|
||||||
@ -155,6 +162,10 @@ func Update(parent string, objs []model.Obj) {
|
|||||||
if instance == nil || !instance.Config().AutoUpdate || Running.Load() {
|
if instance == nil || !instance.Config().AutoUpdate || Running.Load() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
indexPaths := GetIndexPaths()
|
||||||
|
if !isIndexPath(parent, indexPaths) {
|
||||||
|
return
|
||||||
|
}
|
||||||
ignorePaths, err := GetIgnorePaths()
|
ignorePaths, err := GetIgnorePaths()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("update search index error while get ignore paths: %+v", err)
|
log.Errorf("update search index error while get ignore paths: %+v", err)
|
||||||
|
@ -35,6 +35,24 @@ func WriteProgress(progress *model.IndexProgress) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetIndexPaths() []string {
|
||||||
|
indexPaths := make([]string, 0)
|
||||||
|
customIndexPaths := setting.GetStr(conf.IndexPaths)
|
||||||
|
if customIndexPaths != "" {
|
||||||
|
indexPaths = append(indexPaths, strings.Split(customIndexPaths, "\n")...)
|
||||||
|
}
|
||||||
|
return indexPaths
|
||||||
|
}
|
||||||
|
|
||||||
|
func isIndexPath(path string, indexPaths []string) bool {
|
||||||
|
for _, indexPaths := range indexPaths {
|
||||||
|
if strings.HasPrefix(path, indexPaths) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
func GetIgnorePaths() ([]string, error) {
|
func GetIgnorePaths() ([]string, error) {
|
||||||
storages, err := db.GetEnabledStorages()
|
storages, err := db.GetEnabledStorages()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -18,6 +18,7 @@ type MQ[T any] interface {
|
|||||||
Consume(BasicConsumer[T])
|
Consume(BasicConsumer[T])
|
||||||
ConsumeAll(AllConsumer[T])
|
ConsumeAll(AllConsumer[T])
|
||||||
Clear()
|
Clear()
|
||||||
|
Len() int
|
||||||
}
|
}
|
||||||
|
|
||||||
type inMemoryMQ[T any] struct {
|
type inMemoryMQ[T any] struct {
|
||||||
@ -54,3 +55,7 @@ func (mq *inMemoryMQ[T]) Clear() {
|
|||||||
defer mq.Unlock()
|
defer mq.Unlock()
|
||||||
mq.queue.Clear()
|
mq.queue.Clear()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (mq *inMemoryMQ[T]) Len() int {
|
||||||
|
return mq.queue.Len()
|
||||||
|
}
|
||||||
|
@ -3,8 +3,10 @@ package handles
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
|
"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/server/common"
|
"github.com/alist-org/alist/v3/server/common"
|
||||||
|
mapset "github.com/deckarep/golang-set/v2"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
@ -26,6 +28,13 @@ func BuildIndex(c *gin.Context) {
|
|||||||
common.ErrorStrResp(c, "index is running", 400)
|
common.ErrorStrResp(c, "index is running", 400)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
indexPaths := search.GetIndexPaths()
|
||||||
|
indexPaths = append(indexPaths, req.Paths...)
|
||||||
|
indexPathsSet := mapset.NewSet[string]()
|
||||||
|
for _, indexPath := range indexPaths {
|
||||||
|
indexPathsSet.Add(indexPath)
|
||||||
|
}
|
||||||
|
indexPaths = indexPathsSet.ToSlice()
|
||||||
ignorePaths, err := search.GetIgnorePaths()
|
ignorePaths, err := search.GetIgnorePaths()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
common.ErrorResp(c, err, 500)
|
common.ErrorResp(c, err, 500)
|
||||||
@ -50,7 +59,7 @@ func BuildIndex(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
err = search.BuildIndex(context.Background(), req.Paths, ignorePaths, req.MaxDepth, true)
|
err = search.BuildIndex(context.Background(), indexPaths, ignorePaths, req.MaxDepth, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("build index error: %+v", err)
|
log.Errorf("build index error: %+v", err)
|
||||||
}
|
}
|
||||||
@ -67,6 +76,21 @@ func StopIndex(c *gin.Context) {
|
|||||||
common.SuccessResp(c)
|
common.SuccessResp(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ClearIndex(c *gin.Context) {
|
||||||
|
if search.Running.Load() {
|
||||||
|
common.ErrorStrResp(c, "index is running", 400)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
search.Clear(c)
|
||||||
|
search.WriteProgress(&model.IndexProgress{
|
||||||
|
ObjCount: 0,
|
||||||
|
IsDone: false,
|
||||||
|
LastDoneTime: nil,
|
||||||
|
Error: "",
|
||||||
|
})
|
||||||
|
common.SuccessResp(c)
|
||||||
|
}
|
||||||
|
|
||||||
func GetProgress(c *gin.Context) {
|
func GetProgress(c *gin.Context) {
|
||||||
progress, err := search.Progress()
|
progress, err := search.Progress()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -111,6 +111,7 @@ func admin(g *gin.RouterGroup) {
|
|||||||
index := g.Group("/index")
|
index := g.Group("/index")
|
||||||
index.POST("/build", middlewares.SearchIndex, handles.BuildIndex)
|
index.POST("/build", middlewares.SearchIndex, handles.BuildIndex)
|
||||||
index.POST("/stop", middlewares.SearchIndex, handles.StopIndex)
|
index.POST("/stop", middlewares.SearchIndex, handles.StopIndex)
|
||||||
|
index.POST("/clear", middlewares.SearchIndex, handles.ClearIndex)
|
||||||
index.GET("/progress", middlewares.SearchIndex, handles.GetProgress)
|
index.GET("/progress", middlewares.SearchIndex, handles.GetProgress)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user