feat: lazy index creation on searcher init (#2962)
This commit is contained in:
parent
9398cdaac1
commit
0ad9e17196
@ -1,8 +1,7 @@
|
|||||||
package db
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
log "github.com/sirupsen/logrus"
|
||||||
"log"
|
|
||||||
|
|
||||||
"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"
|
||||||
@ -14,21 +13,6 @@ var db *gorm.DB
|
|||||||
func Init(d *gorm.DB) {
|
func Init(d *gorm.DB) {
|
||||||
db = d
|
db = d
|
||||||
err := AutoMigrate(new(model.Storage), new(model.User), new(model.Meta), new(model.SettingItem), new(model.SearchNode))
|
err := AutoMigrate(new(model.Storage), new(model.User), new(model.Meta), new(model.SettingItem), new(model.SearchNode))
|
||||||
switch conf.Conf.Database.Type {
|
|
||||||
case "sqlite3":
|
|
||||||
case "mysql":
|
|
||||||
if err == nil {
|
|
||||||
tableName := fmt.Sprintf("%ssearch_nodes", conf.Conf.Database.TablePrefix)
|
|
||||||
db.Exec(fmt.Sprintf("CREATE FULLTEXT INDEX idx_%s_name_fulltext ON %s(name);", tableName, tableName))
|
|
||||||
}
|
|
||||||
case "postgres":
|
|
||||||
if err == nil {
|
|
||||||
db.Exec("CREATE EXTENSION pg_trgm;")
|
|
||||||
db.Exec("CREATE EXTENSION btree_gin;")
|
|
||||||
tableName := fmt.Sprintf("%ssearch_nodes", conf.Conf.Database.TablePrefix)
|
|
||||||
db.Exec(fmt.Sprintf("CREATE INDEX idx_%s_name ON %s USING GIN (name);", tableName, tableName))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("failed migrate database: %s", err.Error())
|
log.Fatalf("failed migrate database: %s", err.Error())
|
||||||
}
|
}
|
||||||
@ -43,3 +27,7 @@ func AutoMigrate(dst ...interface{}) error {
|
|||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetDb() *gorm.DB {
|
||||||
|
return db;
|
||||||
|
}
|
||||||
|
@ -1,6 +1,13 @@
|
|||||||
package db
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
|
"github.com/alist-org/alist/v3/internal/conf"
|
||||||
|
"github.com/alist-org/alist/v3/internal/db"
|
||||||
"github.com/alist-org/alist/v3/internal/search/searcher"
|
"github.com/alist-org/alist/v3/internal/search/searcher"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -11,6 +18,25 @@ var config = searcher.Config{
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
searcher.RegisterSearcher(config, func() (searcher.Searcher, error) {
|
searcher.RegisterSearcher(config, func() (searcher.Searcher, error) {
|
||||||
|
db := db.GetDb()
|
||||||
|
switch conf.Conf.Database.Type {
|
||||||
|
case "mysql":
|
||||||
|
tableName := fmt.Sprintf("%ssearch_nodes", conf.Conf.Database.TablePrefix)
|
||||||
|
tx := db.Exec(fmt.Sprintf("CREATE FULLTEXT INDEX idx_%s_name_fulltext ON %s(name);", tableName, tableName))
|
||||||
|
if err := tx.Error; err != nil && !strings.Contains(err.Error(), "Error 1061 (42000)") { // duplicate error
|
||||||
|
log.Errorf("failed to create full text index: %v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
case "postgres":
|
||||||
|
db.Exec("CREATE EXTENSION pg_trgm;")
|
||||||
|
db.Exec("CREATE EXTENSION btree_gin;")
|
||||||
|
tableName := fmt.Sprintf("%ssearch_nodes", conf.Conf.Database.TablePrefix)
|
||||||
|
tx := db.Exec(fmt.Sprintf("CREATE INDEX idx_%s_name ON %s USING GIN (name);", tableName, tableName))
|
||||||
|
if err := tx.Error; err != nil && !strings.Contains(err.Error(), "SQLSTATE 42P07") {
|
||||||
|
log.Errorf("failed to create index using GIN: %v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
return &DB{}, nil
|
return &DB{}, nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user