feat: optimize database search (#2687)
* feat: remove index on `SearchNode.Name` As we do not use s% on name column, index there does not work * fix: init index after init data Or on the first run, it will log 'init index error: readObjectStart: expect { or n, but found , error found in #0 byte of ...||..., bigger context ...||...' * fix: match parent more precisely It will match `/a/bc` if we search in `/a/b` originally. But it is not backward compatible by adding a suffix `/` to all the data in parent field
This commit is contained in:
parent
a2e8e96c71
commit
179d285564
@ -15,8 +15,8 @@ func Init() {
|
|||||||
bootstrap.InitConfig()
|
bootstrap.InitConfig()
|
||||||
bootstrap.Log()
|
bootstrap.Log()
|
||||||
bootstrap.InitDB()
|
bootstrap.InitDB()
|
||||||
bootstrap.InitIndex()
|
|
||||||
data.InitData()
|
data.InitData()
|
||||||
|
bootstrap.InitIndex()
|
||||||
}
|
}
|
||||||
|
|
||||||
var pid = -1
|
var pid = -1
|
||||||
|
@ -8,8 +8,16 @@ import (
|
|||||||
"github.com/alist-org/alist/v3/internal/model"
|
"github.com/alist-org/alist/v3/internal/model"
|
||||||
"github.com/alist-org/alist/v3/pkg/utils"
|
"github.com/alist-org/alist/v3/pkg/utils"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func whereInParent(parent string) *gorm.DB {
|
||||||
|
return db.Where(fmt.Sprintf("%s LIKE ?", columnName("parent")),
|
||||||
|
fmt.Sprintf("%s/%%", parent)).
|
||||||
|
Or(fmt.Sprintf("%s = ?", columnName("parent")),
|
||||||
|
fmt.Sprintf("%s%%", parent))
|
||||||
|
}
|
||||||
|
|
||||||
func CreateSearchNode(node *model.SearchNode) error {
|
func CreateSearchNode(node *model.SearchNode) error {
|
||||||
return db.Create(node).Error
|
return db.Create(node).Error
|
||||||
}
|
}
|
||||||
@ -19,9 +27,7 @@ func BatchCreateSearchNodes(nodes *[]model.SearchNode) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func DeleteSearchNodesByParent(prefix string) error {
|
func DeleteSearchNodesByParent(prefix string) error {
|
||||||
err := db.Where(fmt.Sprintf("%s LIKE ?",
|
err := db.Where(whereInParent(prefix)).Delete(&model.SearchNode{}).Error
|
||||||
columnName("parent")), fmt.Sprintf("%s%%", prefix)).
|
|
||||||
Delete(&model.SearchNode{}).Error
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -51,9 +57,7 @@ func SearchNode(req model.SearchReq) ([]model.SearchNode, int64, error) {
|
|||||||
fmt.Sprintf("%s LIKE ?", columnName("name")),
|
fmt.Sprintf("%s LIKE ?", columnName("name")),
|
||||||
fmt.Sprintf("%%%s%%", keyword))
|
fmt.Sprintf("%%%s%%", keyword))
|
||||||
}
|
}
|
||||||
searchDB := db.Model(&model.SearchNode{}).Where(
|
searchDB := db.Model(&model.SearchNode{}).Where(whereInParent(req.Parent)).Where(keywordsClause)
|
||||||
fmt.Sprintf("%s LIKE ?", columnName("parent")),
|
|
||||||
fmt.Sprintf("%s%%", req.Parent)).Where(keywordsClause)
|
|
||||||
var count int64
|
var count int64
|
||||||
if err := searchDB.Count(&count).Error; err != nil {
|
if err := searchDB.Count(&count).Error; err != nil {
|
||||||
return nil, 0, errors.Wrapf(err, "failed get users count")
|
return nil, 0, errors.Wrapf(err, "failed get users count")
|
||||||
|
@ -20,7 +20,7 @@ type SearchReq struct {
|
|||||||
|
|
||||||
type SearchNode struct {
|
type SearchNode struct {
|
||||||
Parent string `json:"parent" gorm:"index"`
|
Parent string `json:"parent" gorm:"index"`
|
||||||
Name string `json:"name" gorm:"index"`
|
Name string `json:"name"`
|
||||||
IsDir bool `json:"is_dir"`
|
IsDir bool `json:"is_dir"`
|
||||||
Size int64 `json:"size"`
|
Size int64 `json:"size"`
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user