Skip to content

Commit

Permalink
fix(index): update indexes in database
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Dec 5, 2022
1 parent dda1da4 commit f1a9b68
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
15 changes: 12 additions & 3 deletions internal/db/searchnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package db

import (
"fmt"
"path"

"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/pkg/errors"
)

Expand All @@ -15,10 +17,17 @@ func BatchCreateSearchNodes(nodes *[]model.SearchNode) error {
return db.CreateInBatches(nodes, 1000).Error
}

func DeleteSearchNodesByParent(parent string) error {
return db.Where(fmt.Sprintf("%s LIKE ?",
columnName("path")), fmt.Sprintf("%s%%", parent)).
func DeleteSearchNodesByParent(prefix string) error {
err := db.Where(fmt.Sprintf("%s LIKE ?",
columnName("parent")), fmt.Sprintf("%s%%", prefix)).
Delete(&model.SearchNode{}).Error
if err != nil {
return err
}
dir, name := path.Split(prefix)
return db.Where(fmt.Sprintf("%s = ? AND %s = ?",
columnName("parent"), columnName("name")),
utils.StandardizePath(dir), name).Delete(&model.SearchNode{}).Error
}

func ClearSearchNodes() error {
Expand Down
4 changes: 2 additions & 2 deletions internal/op/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ func List(ctx context.Context, storage driver.Driver, path string, args model.Li
return nil, errors.Wrapf(err, "failed to list objs")
}
// call hooks
go func() {
go func(reqPath string, files []model.Obj) {
for _, hook := range objsUpdateHooks {
hook(args.ReqPath, files)
}
}()
}(args.ReqPath, files)
if !storage.Config().NoCache {
if len(files) > 0 {
log.Debugf("set cache: %s => %+v", key, files)
Expand Down
2 changes: 2 additions & 0 deletions internal/search/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ func Update(parent string, objs []model.Obj) {
toAdd := now.Difference(old)
for i := range nodes {
if toDelete.Contains(nodes[i].Name) {
log.Debugf("delete index: %s", path.Join(parent, nodes[i].Name))
err = instance.Del(ctx, path.Join(parent, nodes[i].Name))
if err != nil {
log.Errorf("update search index error while del old node: %+v", err)
Expand All @@ -196,6 +197,7 @@ func Update(parent string, objs []model.Obj) {
}
for i := range objs {
if toAdd.Contains(objs[i].GetName()) {
log.Debugf("add index: %s", path.Join(parent, objs[i].GetName()))
err = Index(ctx, parent, objs[i])
if err != nil {
log.Errorf("update search index error while index new node: %+v", err)
Expand Down

0 comments on commit f1a9b68

Please sign in to comment.