An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:perf: Flush asset content indexing in bounded batches https://github.com/siyuan-note/siyuan/issues/20361
Daniel84beba6a8346
diff --git a/kernel/model/asset_content.go b/kernel/model/asset_content.goindex 43b55bbf07..539f598d99 100644--- a/kernel/model/asset_content.go+++ b/kernel/model/asset_content.go@@ -410,6 +410,14 @@ func (searcher *AssetsSearcher) GetParser(ext string) AssetParser {}func (searcher *AssetsSearcher) FullIndex() {+ searcher.fullIndex(func(batch []*sql.AssetContent) {+ sql.IndexAssetContentsQueue(batch)+ // 等待本批写入完成,避免解析结果继续在索引队列中累积。+ sql.FlushAssetContentQueue()+ })+}++func (searcher *AssetsSearcher) fullIndex(writeBatch func([]*sql.AssetContent)) {defer logging.Recover()assetsDir := util.GetDataAssetsAbsPath()@@ -417,7 +425,16 @@ func (searcher *AssetsSearcher) FullIndex() {return}- var results []*AssetParseResult+ const maxBatchCount, maxBatchBytes = 128, 4 * 1024 * 1024+ var batch []*sql.AssetContent+ batchBytes := 0+ flush := func() {+ if len(batch) == 0 {+ return+ }+ writeBatch(batch)+ batch, batchBytes = nil, 0+ }filelock.Walk(assetsDir, func(absPath string, d fs.DirEntry, err error) error {if err != nil {logging.LogErrorf("walk dir [%s] failed: %s", absPath, err)@@ -455,13 +472,10 @@ func (searcher *AssetsSearcher) FullIndex() {result.Path = "assets" + filepath.ToSlash(strings.TrimPrefix(absPath, assetsDir))result.Size = info.Size()result.Updated = info.ModTime().Unix()- results = append(results, result)- return nil- })-- var assetContents []*sql.AssetContent- for _, result := range results {- assetContents = append(assetContents, &sql.AssetContent{+ if batchBytes+len(result.Content) > maxBatchBytes {+ flush()+ }+ batch = append(batch, &sql.AssetContent{ID: ast.NewNodeID(),Name: util.RemoveID(filepath.Base(result.Path)),Ext: strings.ToLower(filepath.Ext(result.Path)),@@ -470,9 +484,13 @@ func (searcher *AssetsSearcher) FullIndex() {Updated: result.Updated,Content: result.Content,})- }-- sql.IndexAssetContentsQueue(assetContents)+ batchBytes += len(result.Content)+ if len(batch) >= maxBatchCount || batchBytes >= maxBatchBytes {+ flush()+ }+ return nil+ })+ flush()}func NewAssetsSearcher() *AssetsSearcher {diff --git a/kernel/model/asset_content_batch_test.go b/kernel/model/asset_content_batch_test.gonew file mode 100644index 0000000000..ed29c99f77--- /dev/null+++ b/kernel/model/asset_content_batch_test.go@@ -0,0 +1,63 @@+package model++import (+ "fmt"+ "os"+ "path/filepath"+ "strings"+ "testing"++ "github.com/siyuan-note/siyuan/kernel/sql"+ "github.com/siyuan-note/siyuan/kernel/util"+)++type batchTestAssetParser struct {+ content string+ parsed int+}++func (parser *batchTestAssetParser) Parse(string) *AssetParseResult {+ parser.parsed+++ return &AssetParseResult{Content: parser.content}+}++func TestAssetContentFullIndexWritesBoundedBatches(t *testing.T) {+ for _, test := range []struct{ count, bytes int }{{260, 1}, {9, 2 * 1024 * 1024}, {2, 8 * 1024 * 1024}, {0, 1}} {+ t.Run(fmt.Sprintf("%d-%d", test.count, test.bytes), func(t *testing.T) {+ oldData := util.DataDir+ util.DataDir = t.TempDir()+ t.Cleanup(func() { util.DataDir = oldData })+ dir := util.GetDataAssetsAbsPath()+ if err := os.MkdirAll(dir, 0755); err != nil {+ t.Fatal(err)+ }+ for i := 0; i < test.count; i++ {+ if err := os.WriteFile(filepath.Join(dir, fmt.Sprintf("%04d.txt", i)), []byte("file"), 0644); err != nil {+ t.Fatal(err)+ }+ }+ parser := &batchTestAssetParser{content: strings.Repeat("x", test.bytes)}+ searcher := NewAssetsSearcher()+ searcher.parsers = map[string]AssetParser{".txt": parser}+ total, batches, firstParsed := 0, 0, 0+ searcher.fullIndex(func(batch []*sql.AssetContent) {+ if batches == 0 {+ firstParsed = parser.parsed+ }+ batches+++ total += len(batch)+ if len(batch) == 0 || len(batch) > 128 || len(batch) > 1 && len(batch)*test.bytes > 4*1024*1024 {+ t.Fatalf("unbounded batch: %d items, %d bytes", len(batch), len(batch)*test.bytes)+ }+ for _, asset := range batch {+ if asset.Size != 4 || asset.Content != parser.content || !strings.HasPrefix(asset.Path, "assets/") {+ t.Fatalf("lost asset metadata: %+v", asset)+ }+ }+ })+ if total != test.count || test.count > 2 && firstParsed >= test.count || test.count == 0 && batches != 0 {+ t.Fatalf("total=%d, first parsed=%d, batches=%d", total, firstParsed, batches)+ }+ })+ }+}