An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Avoid duplicate search indexing https://github.com/siyuan-note/siyuan/issues/18325
Daniel3ecdb20f1220
diff --git a/app/guide/20210808180117-6v0mkxr/storage/av/20251230141929-2trv0vl.json b/app/guide/20210808180117-6v0mkxr/storage/av/20251230141929-2trv0vl.jsonnew file mode 100644index 0000000000..61c97e826a--- /dev/null+++ b/app/guide/20210808180117-6v0mkxr/storage/av/20251230141929-2trv0vl.json@@ -0,0 +1,78 @@+{+ "spec": 5,+ "id": "20251230141929-2trv0vl",+ "name": "",+ "keyValues": [+ {+ "key": {+ "id": "20251230141929-key0001",+ "name": "Primary Key",+ "type": "block",+ "icon": "",+ "desc": "",+ "numberFormat": "",+ "template": ""+ }+ },+ {+ "key": {+ "id": "20251230141929-sel0001",+ "name": "Select",+ "type": "select",+ "icon": "",+ "desc": "",+ "numberFormat": "",+ "template": ""+ }+ }+ ],+ "keyIDs": null,+ "viewID": "20251230141929-ogkqgga",+ "views": [+ {+ "id": "20251230141929-ogkqgga",+ "icon": "",+ "name": "Table",+ "hideAttrViewName": false,+ "desc": "",+ "filters": [+ {+ "column": "",+ "operator": "",+ "value": null,+ "combination": "and"+ }+ ],+ "pageSize": 50,+ "type": "table",+ "table": {+ "spec": 0,+ "id": "20251230141929-tbl0001",+ "showIcon": true,+ "wrapField": false,+ "columns": [+ {+ "id": "20251230141929-key0001",+ "wrap": false,+ "hidden": false,+ "pin": false,+ "width": ""+ },+ {+ "id": "20251230141929-sel0001",+ "wrap": false,+ "hidden": false,+ "pin": false,+ "width": ""+ }+ ],+ "rowIds": null+ },+ "groupCreated": 0,+ "groupItemIds": null,+ "groupFolded": false,+ "groupHidden": 0,+ "groupSort": 0+ }+ ]+}diff --git a/app/guide/20210808180117-czj9bvb/storage/av/20251230141609-2kvghrg.json b/app/guide/20210808180117-czj9bvb/storage/av/20251230141609-2kvghrg.jsonnew file mode 100644index 0000000000..8fc9ca3fb3--- /dev/null+++ b/app/guide/20210808180117-czj9bvb/storage/av/20251230141609-2kvghrg.json@@ -0,0 +1,78 @@+{+ "spec": 5,+ "id": "20251230141609-2kvghrg",+ "name": "",+ "keyValues": [+ {+ "key": {+ "id": "20251230141609-key0001",+ "name": "主键",+ "type": "block",+ "icon": "",+ "desc": "",+ "numberFormat": "",+ "template": ""+ }+ },+ {+ "key": {+ "id": "20251230141609-sel0001",+ "name": "单选",+ "type": "select",+ "icon": "",+ "desc": "",+ "numberFormat": "",+ "template": ""+ }+ }+ ],+ "keyIDs": null,+ "viewID": "20251230141609-lcme2fh",+ "views": [+ {+ "id": "20251230141609-lcme2fh",+ "icon": "",+ "name": "表格",+ "hideAttrViewName": false,+ "desc": "",+ "filters": [+ {+ "column": "",+ "operator": "",+ "value": null,+ "combination": "and"+ }+ ],+ "pageSize": 50,+ "type": "table",+ "table": {+ "spec": 0,+ "id": "20251230141609-tbl0001",+ "showIcon": true,+ "wrapField": false,+ "columns": [+ {+ "id": "20251230141609-key0001",+ "wrap": false,+ "hidden": false,+ "pin": false,+ "width": ""+ },+ {+ "id": "20251230141609-sel0001",+ "wrap": false,+ "hidden": false,+ "pin": false,+ "width": ""+ }+ ],+ "rowIds": null+ },+ "groupCreated": 0,+ "groupItemIds": null,+ "groupFolded": false,+ "groupHidden": 0,+ "groupSort": 0+ }+ ]+}diff --git a/app/guide/20211226090932-5lcq56f/storage/av/20251230141813-9w627cg.json b/app/guide/20211226090932-5lcq56f/storage/av/20251230141813-9w627cg.jsonnew file mode 100644index 0000000000..8eccf6d73e--- /dev/null+++ b/app/guide/20211226090932-5lcq56f/storage/av/20251230141813-9w627cg.json@@ -0,0 +1,78 @@+{+ "spec": 5,+ "id": "20251230141813-9w627cg",+ "name": "",+ "keyValues": [+ {+ "key": {+ "id": "20251230141813-key0001",+ "name": "主鍵",+ "type": "block",+ "icon": "",+ "desc": "",+ "numberFormat": "",+ "template": ""+ }+ },+ {+ "key": {+ "id": "20251230141813-sel0001",+ "name": "單選",+ "type": "select",+ "icon": "",+ "desc": "",+ "numberFormat": "",+ "template": ""+ }+ }+ ],+ "keyIDs": null,+ "viewID": "20251230141813-nlkdgna",+ "views": [+ {+ "id": "20251230141813-nlkdgna",+ "icon": "",+ "name": "表格",+ "hideAttrViewName": false,+ "desc": "",+ "filters": [+ {+ "column": "",+ "operator": "",+ "value": null,+ "combination": "and"+ }+ ],+ "pageSize": 50,+ "type": "table",+ "table": {+ "spec": 0,+ "id": "20251230141813-tbl0001",+ "showIcon": true,+ "wrapField": false,+ "columns": [+ {+ "id": "20251230141813-key0001",+ "wrap": false,+ "hidden": false,+ "pin": false,+ "width": ""+ },+ {+ "id": "20251230141813-sel0001",+ "wrap": false,+ "hidden": false,+ "pin": false,+ "width": ""+ }+ ],+ "rowIds": null+ },+ "groupCreated": 0,+ "groupItemIds": null,+ "groupFolded": false,+ "groupHidden": 0,+ "groupSort": 0+ }+ ]+}diff --git a/app/guide/20240530133126-axarxgx/storage/av/20251230142020-5frasem.json b/app/guide/20240530133126-axarxgx/storage/av/20251230142020-5frasem.jsonnew file mode 100644index 0000000000..71d6004604--- /dev/null+++ b/app/guide/20240530133126-axarxgx/storage/av/20251230142020-5frasem.json@@ -0,0 +1,78 @@+{+ "spec": 5,+ "id": "20251230142020-5frasem",+ "name": "",+ "keyValues": [+ {+ "key": {+ "id": "20251230142020-key0001",+ "name": "プライマリキー",+ "type": "block",+ "icon": "",+ "desc": "",+ "numberFormat": "",+ "template": ""+ }+ },+ {+ "key": {+ "id": "20251230142020-sel0001",+ "name": "選択",+ "type": "select",+ "icon": "",+ "desc": "",+ "numberFormat": "",+ "template": ""+ }+ }+ ],+ "keyIDs": null,+ "viewID": "20251230142020-srwlylx",+ "views": [+ {+ "id": "20251230142020-srwlylx",+ "icon": "",+ "name": "テーブル",+ "hideAttrViewName": false,+ "desc": "",+ "filters": [+ {+ "column": "",+ "operator": "",+ "value": null,+ "combination": "and"+ }+ ],+ "pageSize": 50,+ "type": "table",+ "table": {+ "spec": 0,+ "id": "20251230142020-tbl0001",+ "showIcon": true,+ "wrapField": false,+ "columns": [+ {+ "id": "20251230142020-key0001",+ "wrap": false,+ "hidden": false,+ "pin": false,+ "width": ""+ },+ {+ "id": "20251230142020-sel0001",+ "wrap": false,+ "hidden": false,+ "pin": false,+ "width": ""+ }+ ],+ "rowIds": null+ },+ "groupCreated": 0,+ "groupItemIds": null,+ "groupFolded": false,+ "groupHidden": 0,+ "groupSort": 0+ }+ ]+}diff --git a/kernel/model/index.go b/kernel/model/index.goindex 6dd7faa1a7..70c9d39bec 100644--- a/kernel/model/index.go+++ b/kernel/model/index.go@@ -46,6 +46,9 @@ import ("github.com/siyuan-note/siyuan/kernel/util")+// databaseIndexDataLock 用于避免索引任务读取正在被替换或删除的笔记本目录。+var databaseIndexDataLock sync.Mutex+func UpsertIndexes(paths []string) {var syFiles []stringfor _, p := range paths {@@ -132,10 +135,15 @@ func removeBoxRefs(boxID string) {}func indexBox(boxID string) {+ databaseIndexDataLock.Lock()+ defer databaseIndexDataLock.Unlock()+box := Conf.Box(boxID)if nil == box {return}+ // 全量索引使用纯 INSERT,开始前必须清理该笔记本的旧数据,避免重复任务叠加相同行。+ sql.DeleteBoxQueue(boxID)util.SetBootDetails(Conf.Language(303))files := box.ListFiles("/")@@ -220,6 +228,9 @@ func indexBox(boxID string) {}func IndexRefs() {+ databaseIndexDataLock.Lock()+ defer databaseIndexDataLock.Unlock()+start := time.Now()util.SetBootDetails(Conf.Language(304))util.PushStatusBar(Conf.Language(54))diff --git a/kernel/model/mount.go b/kernel/model/mount.goindex d25778424f..ed4d1752e9 100644--- a/kernel/model/mount.go+++ b/kernel/model/mount.go@@ -139,6 +139,19 @@ func normalizeBoxName(name string) string {var boxLock = sync.Map{}+// removeBoxDir 重试删除刚完成读写的笔记本目录,避免 Windows 延迟释放句柄导致瞬时失败。+func removeBoxDir(p string) (err error) {+ for i := 0; i < 5; i++ {+ if err = filelock.RemoveWithoutFatal(p); nil == err {+ return+ }+ if i < 4 {+ time.Sleep(100 * time.Millisecond)+ }+ }+ return+}+func RemoveBox(boxID string) (err error) {if _, ok := boxLock.Load(boxID); ok {err = errors.New(Conf.language(239))@@ -154,6 +167,8 @@ func RemoveBox(boxID string) (err error) {FlushTxQueue()isUserGuide := IsUserGuide(boxID)+ databaseIndexDataLock.Lock()+ defer databaseIndexDataLock.Unlock()createDocLock.Lock()defer createDocLock.Unlock()@@ -165,10 +180,12 @@ func RemoveBox(boxID string) (err error) {return fmt.Errorf("can not remove [%s] caused by it is not a dir", boxID)}- unmount0(boxID)-// 删目录前缓存加密状态:删目录后 conf.json 不复存在,IsEncryptedBox 会返回 falseisEncrypted := IsEncryptedBox(boxID)+ unmount0(boxID)+ if !isEncrypted {+ unindex(boxID)+ }if !isUserGuide {var historyDir string@@ -201,7 +218,7 @@ func RemoveBox(boxID string) (err error) {RevokeManagedEncryptedExportsForBox(boxID)}- if err = filelock.Remove(localPath); err != nil {+ if err = removeBoxDir(localPath); err != nil {return}// 加密笔记本删除时清理其独立加密 db 文件(含 WAL/SHM),避免残留@@ -305,6 +322,9 @@ func Mount(boxID string) (alreadyMount bool, err error) {localPath := filepath.Join(util.DataDir, boxID)var reMountGuide boolif isUserGuide {+ databaseIndexDataLock.Lock()+ defer databaseIndexDataLock.Unlock()+// 重新挂载帮助文档guideBox := Conf.Box(boxID)@@ -312,8 +332,9 @@ func Mount(boxID string) (alreadyMount bool, err error) {unmount0(guideBox.ID)reMountGuide = true}+ unindex(boxID)- if err = filelock.Remove(localPath); err != nil {+ if err = removeBoxDir(localPath); err != nil {return}diff --git a/kernel/sql/index_queue_test.go b/kernel/sql/index_queue_test.gonew file mode 100644index 0000000000..64cfeb2744--- /dev/null+++ b/kernel/sql/index_queue_test.go@@ -0,0 +1,98 @@+// SiYuan - Refactor your thinking+// Copyright (c) 2020-present, b3log.org+//+// This program is free software: you can redistribute it and/or modify+// it under the terms of the GNU Affero General Public License as published by+// the Free Software Foundation, either version 3 of the License, or+// (at your option) any later version.+//+// This program is distributed in the hope that it will be useful,+// but WITHOUT ANY WARRANTY; without even the implied warranty of+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+// GNU Affero General Public License for more details.+//+// You should have received a copy of the GNU Affero General Public License+// along with this program. If not, see <https://www.gnu.org/licenses/>.++package sql++import (+ "testing"++ "github.com/siyuan-note/siyuan/kernel/util"+)++func TestIndexQueuePreservesOperationsAppendedDuringFlush(t *testing.T) {+ prepareIndexQueueTest(t)++ appendOperation(&dbQueueOperation{action: "delete_box", box: "first"})+ firstOps, firstSnapshot := getOperations()+ if 1 != len(firstOps) || "first" != firstOps[0].box {+ t.Fatalf("unexpected first queue snapshot: %#v", firstOps)+ }++ appendOperation(&dbQueueOperation{action: "delete_box", box: "second"})+ clearIndexQueue(firstSnapshot)++ entries := loadIndexQueue()+ if 1 != len(entries) || "second" != entries[0].Box {+ t.Fatalf("unexpected preserved index entries: %#v", entries)+ }++ secondOps, secondSnapshot := getOperations()+ if 1 != len(secondOps) || "second" != secondOps[0].box {+ t.Fatalf("unexpected pending queue operations: %#v", secondOps)+ }+ clearIndexQueue(secondSnapshot)+ if entries = loadIndexQueue(); 0 != len(entries) {+ t.Fatalf("unexpected remaining index entries: %#v", entries)+ }+}++func TestRecoverIndexQueueAfterRestart(t *testing.T) {+ prepareIndexQueueTest(t)++ appendOperation(&dbQueueOperation{action: "delete_box", box: "recovered"})+ dbQueueLock.Lock()+ operationQueue = nil+ dbQueueLock.Unlock()++ recoverIndexQueue()+ ops, snapshot := getOperations()+ if 1 != len(ops) || "recovered" != ops[0].box {+ t.Fatalf("unexpected recovered queue operations: %#v", ops)+ }+ clearIndexQueue(snapshot)+ if entries := loadIndexQueue(); 0 != len(entries) {+ t.Fatalf("unexpected remaining recovered entries: %#v", entries)+ }++ recoverIndexQueue()+ if ops, _ = getOperations(); 0 != len(ops) {+ t.Fatalf("unexpected repeated recovered operations: %#v", ops)+ }+}++func prepareIndexQueueTest(t *testing.T) {+ t.Helper()++ oldQueueDir := util.QueueDir+ oldIndexFlock := indexFlock+ oldIndexQueueSize := indexQueueSize.Load()++ dbQueueLock.Lock()+ oldOperationQueue := operationQueue+ operationQueue = nil+ dbQueueLock.Unlock()++ util.QueueDir = t.TempDir()+ initIndexQueue()+ t.Cleanup(func() {+ dbQueueLock.Lock()+ operationQueue = oldOperationQueue+ dbQueueLock.Unlock()+ util.QueueDir = oldQueueDir+ indexFlock = oldIndexFlock+ indexQueueSize.Store(oldIndexQueueSize)+ })+}diff --git a/kernel/sql/queue.go b/kernel/sql/queue.goindex 7b4d9ea29f..46f6b1f6b4 100644--- a/kernel/sql/queue.go+++ b/kernel/sql/queue.go@@ -27,7 +27,6 @@ import ("sync/atomic""time"- "github.com/88250/lute""github.com/88250/lute/parse""github.com/siyuan-note/eventbus""github.com/siyuan-note/logging"@@ -132,7 +131,6 @@ func FlushQueue() {ops, indexSnapshot := getOperations()total := len(ops)if 1 > total && !flushingTx.Load() {- processDiskQueue()return}@@ -238,8 +236,8 @@ func FlushQueue() {eventbus.Publish(eventbus.EvtSQLIndexFlushed)+ // 刷新期间追加的操作仍在内存队列中,磁盘队列仅用于进程重启恢复,不能在这里重复执行。clearIndexQueue(indexSnapshot)- processDiskQueue()}func execOp(op *dbQueueOperation, tx *sql.Tx, context map[string]any) (err error) {@@ -528,41 +526,3 @@ func appendOperation(op *dbQueueOperation) {appendToIndexQueue(op)eventbus.Publish(eventbus.EvtSQLIndexChanged)}--func processDiskQueue() {- entries := loadIndexQueue()- if 1 > len(entries) {- return- }-- logging.LogInfof("flushing [%d] disk index queue operations", len(entries))-- luteEngine := lute.New()- context := map[string]any{eventbus.CtxPushMsg: eventbus.CtxPushMsgToStatusBar}- groupOpsCurrent := map[string]int{}- for _, e := range entries {- op := indexEntryToOp(e, luteEngine, "flush disk queue")- if nil == op {- continue- }- tx, err := beginTxForBox(op.boxID())- if err != nil {- return- }- groupOpsCurrent[op.action]++- context["current"] = groupOpsCurrent[op.action]- context["total"] = len(entries)- if err = execOp(op, tx, context); err != nil {- tx.Rollback()- closeTxPreparedStmts(tx)- logging.LogErrorf("queue operation [%s] failed: %s", op.action, err)- continue- }- if err = commitTx(tx); err != nil {- logging.LogErrorf("commit tx failed: %s", err)- continue- }- }-- clearIndexQueueEntries()-}