diff --git a/kernel/api/setting.go b/kernel/api/setting.go index bc97a587f..0ff8fb322 100644 --- a/kernel/api/setting.go +++ b/kernel/api/setting.go @@ -91,6 +91,10 @@ func setEditor(c *gin.Context) { editor.KaTexMacros = "{}" } + oldVirtualBlockRef := model.Conf.Editor.VirtualBlockRef + oldVirtualBlockRefInclude := model.Conf.Editor.VirtualBlockRefInclude + oldVirtualBlockRefExclude := model.Conf.Editor.VirtualBlockRefExclude + model.Conf.Editor = editor model.Conf.Save() @@ -98,6 +102,12 @@ func setEditor(c *gin.Context) { model.ChangeHistoryTick(editor.GenerateHistoryInterval) } + if oldVirtualBlockRef != model.Conf.Editor.VirtualBlockRef || + oldVirtualBlockRefInclude != model.Conf.Editor.VirtualBlockRefInclude || + oldVirtualBlockRefExclude != model.Conf.Editor.VirtualBlockRefExclude { + model.ResetVirtualBlockRefCache() + } + ret.Data = model.Conf.Editor } @@ -232,7 +242,7 @@ func setSearch(c *gin.Context) { oldVirtualRefAnchor != s.VirtualRefAnchor || oldVirtualRefDoc != s.VirtualRefDoc || oldVirtualRefKeywordsLimit != s.VirtualRefKeywordsLimit { - model.CacheVirtualBlockRefJob() + model.ResetVirtualBlockRefCache() } ret.Data = s } diff --git a/kernel/model/virutalref.go b/kernel/model/virutalref.go index 2caf4d75a..c8401958d 100644 --- a/kernel/model/virutalref.go +++ b/kernel/model/virutalref.go @@ -18,8 +18,6 @@ package model import ( "bytes" - goahocorasick "github.com/anknown/ahocorasick" - "github.com/siyuan-note/logging" "regexp" "sort" "strings" @@ -29,6 +27,7 @@ import ( "github.com/88250/lute" "github.com/88250/lute/ast" "github.com/88250/lute/parse" + ahocorasick "github.com/BobuSumisu/aho-corasick" "github.com/dgraph-io/ristretto" "github.com/siyuan-note/siyuan/kernel/search" "github.com/siyuan-note/siyuan/kernel/sql" @@ -71,42 +70,22 @@ func putBlockVirtualRefKeywords(blockContent, blockID, docTitle string) (ret []s } contentTmp := blockContent - var keywordsTmp [][]rune + var keywordsTmp []string if !Conf.Search.CaseSensitive { contentTmp = strings.ToLower(blockContent) for _, keyword := range keywords { - keywordsTmp = append(keywordsTmp, []rune(strings.ToLower(keyword))) + keywordsTmp = append(keywordsTmp, strings.ToLower(keyword)) } } else { for _, keyword := range keywords { - keywordsTmp = append(keywordsTmp, []rune(keyword)) + keywordsTmp = append(keywordsTmp, keyword) } } - if 1024*1024 < len(contentTmp) { - m := goahocorasick.Machine{} - buildErr := m.Build(keywordsTmp) - if nil != buildErr { - logging.LogWarnf("build virtual ref keywords AC matcher failed: %s", buildErr) - for _, keywordRunes := range keywordsTmp { - keyword := string(keywordRunes) - if strings.Contains(contentTmp, keyword) { - ret = append(ret, keyword) - } - } - } else { - hits := m.MultiPatternSearch([]rune(contentTmp), false) - for _, hit := range hits { - ret = append(ret, string(hit.Word)) - } - } - } else { - for _, keywordRunes := range keywordsTmp { - keyword := string(keywordRunes) - if strings.Contains(contentTmp, keyword) { - ret = append(ret, keyword) - } - } + trie := ahocorasick.NewTrieBuilder().AddStrings(keywordsTmp).Build() + hits := trie.MatchString(contentTmp) + for _, hit := range hits { + ret = append(ret, hit.MatchString()) } if 1 > len(ret) { @@ -128,6 +107,11 @@ func CacheVirtualBlockRefJob() { virtualBlockRefCache.Set("virtual_ref", keywords, 1) } +func ResetVirtualBlockRefCache() { + virtualBlockRefCache.Clear() + CacheVirtualBlockRefJob() +} + func processVirtualRef(n *ast.Node, unlinks *[]*ast.Node, virtualBlockRefKeywords []string, refCount map[string]int, luteEngine *lute.Lute) bool { if !Conf.Editor.VirtualBlockRef { return false diff --git a/kernel/sql/block_query.go b/kernel/sql/block_query.go index a52c5d41f..d32450780 100644 --- a/kernel/sql/block_query.go +++ b/kernel/sql/block_query.go @@ -274,8 +274,8 @@ func queryDocIDsByTitle(title string, excludeIDs []string) (ret []string) { func queryDocTitles() (ret []string) { ret = []string{} - sqlStmt := "SELECT content FROM blocks WHERE type = 'd' LIMIT ?" - rows, err := query(sqlStmt, 10240) + sqlStmt := "SELECT content FROM blocks WHERE type = 'd'" + rows, err := query(sqlStmt) if nil != err { logging.LogErrorf("sql query [%s] failed: %s", sqlStmt, err) return