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: Persist coalesced SQL index operation locations https://github.com/siyuan-note/siyuan/issues/20936
Daniel0e81f64341d0
diff --git a/kernel/sql/index_queue_test.go b/kernel/sql/index_queue_test.goindex 3c67c15aad..4fdd5d724e 100644--- a/kernel/sql/index_queue_test.go+++ b/kernel/sql/index_queue_test.go@@ -256,6 +256,31 @@ func TestIndexQueueRecoversLargeAndMalformedRecords(t *testing.T) {}}+func TestIndexQueueCoalescedLocationsArePersisted(t *testing.T) {+ for name, enqueue := range map[string]func(*parse.Tree){+ "index": IndexTreeQueue, "move": MoveTreeQueue, "rename": RenameDocQueue,+ "update_refs": UpdateRefsTreeQueue, "delete_refs": DeleteRefsTreeQueue,+ } {+ t.Run(name, func(t *testing.T) {+ prepareIndexQueueTest(t)+ first := &parse.Tree{ID: "20261026000000-move001", Box: "20261026000000-box0001", Path: "/first.sy"}+ latest := *first+ latest.Path = "/latest.sy"+ enqueue(first)+ enqueue(&latest)+ enqueue(&latest)+ if len(operationQueue) != 1 || len(loadIndexQueue()) != 2 {+ t.Fatal("coalescing lost a new location or duplicated an unchanged descriptor")+ }+ operationQueue = nil+ recoverIndexQueue()+ if len(operationQueue) != 2 || operationQueue[1].recoveryEntry.Path != latest.Path {+ t.Fatal("restart did not retain latest coalesced location")+ }+ })+ }+}+func prepareIndexQueueTest(t *testing.T) {t.Helper()diff --git a/kernel/sql/index_recovery_fts5_test.go b/kernel/sql/index_recovery_fts5_test.goindex aba13d672e..7456b23438 100644--- a/kernel/sql/index_recovery_fts5_test.go+++ b/kernel/sql/index_recovery_fts5_test.go@@ -149,6 +149,42 @@ func TestIndexRecoveryLifecycle(t *testing.T) {assertRows(tree)})+ t.Run("coalesced moves recover the final document location", func(t *testing.T) {+ ClearQueue()+ tree := newTree()+ IndexTreeQueue(tree)+ FlushQueue()+ for _, parent := range []string{"20261026000000-parent1", "20261026000000-parent2"} {+ previous := filepath.Join(util.DataDir, box, tree.Path)+ latest := *tree+ latest.Path = "/" + parent + "/" + tree.ID + ".sy"+ latest.HPath = "/" + parent + "/Recovery"+ cache.RemoveTreeDataInBox(tree.ID, box)+ if _, err := filesys.WriteTree(&latest); err != nil {+ t.Fatal(err)+ }+ if err := os.Remove(previous); err != nil {+ t.Fatal(err)+ }+ treenode.IndexBlockTree(&latest)+ MoveTreeQueue(&latest)+ tree = &latest+ }+ if len(operationQueue) != 1 || len(loadIndexQueue()) != 2 {+ t.Fatal("coalesced move did not persist both recovery locations")+ }+ restartQueue()+ FlushQueue()+ assertRows(tree)+ var mismatches int+ if err := db.QueryRow("SELECT COUNT(*) FROM blocks WHERE root_id = ? AND (path != ? OR hpath != ?)", tree.ID, tree.Path, tree.HPath).Scan(&mismatches); err != nil || mismatches != 0 {+ t.Fatalf("recovery retained an obsolete path: %d, %v", mismatches, err)+ }+ if len(operationQueue) != 0 || len(loadIndexQueue()) != 0 {+ t.Fatal("move recovery did not checkpoint journal")+ }+ })+for _, recovered := range []bool{false, true} {name := "live"if recovered {diff --git a/kernel/sql/queue.go b/kernel/sql/queue.goindex edc92bd3d3..3891d31a4b 100644--- a/kernel/sql/queue.go+++ b/kernel/sql/queue.go@@ -21,6 +21,7 @@ import ("errors""fmt""os"+ "reflect""runtime/debug""sort""sync"@@ -491,7 +492,7 @@ func IndexNodeQueue(id string) {newOp := &dbQueueOperation{id: id, box: boxID, inQueueTime: time.Now(), action: "index_node"}for i, op := range operationQueue {if "index_node" == op.action && op.recoveryEntry == nil && op.id == id {- operationQueue[i] = newOp+ replaceOperation(i, newOp)return}}@@ -517,7 +518,7 @@ func UpdateBlockContentQueue(block *Block) {newOp := &dbQueueOperation{block: block, inQueueTime: time.Now(), action: "update_block_content"}for i, op := range operationQueue {if "update_block_content" == op.action && op.recoveryEntry == nil && op.block.ID == block.ID && op.block.Box == block.Box {- operationQueue[i] = newOp+ replaceOperation(i, newOp)return}}@@ -531,7 +532,7 @@ func DeleteRefsTreeQueue(tree *parse.Tree) {newOp := &dbQueueOperation{upsertTree: tree, inQueueTime: time.Now(), action: "delete_refs"}for i, op := range operationQueue {if "delete_refs" == op.action && op.upsertTree != nil && op.upsertTree.ID == tree.ID {- operationQueue[i] = newOp+ replaceOperation(i, newOp)return}}@@ -545,7 +546,7 @@ func UpdateRefsTreeQueue(tree *parse.Tree) {newOp := &dbQueueOperation{upsertTree: tree, inQueueTime: time.Now(), action: "update_refs"}for i, op := range operationQueue {if "update_refs" == op.action && op.upsertTree != nil && op.upsertTree.ID == tree.ID {- operationQueue[i] = newOp+ replaceOperation(i, newOp)return}}@@ -559,7 +560,7 @@ func DeleteBoxRefsQueue(boxID string) {newOp := &dbQueueOperation{box: boxID, inQueueTime: time.Now(), action: "delete_box_refs"}for i, op := range operationQueue {if "delete_box_refs" == op.action && op.recoveryEntry == nil && op.box == boxID {- operationQueue[i] = newOp+ replaceOperation(i, newOp)return}}@@ -573,7 +574,7 @@ func DeleteBoxQueue(boxID string) {newOp := &dbQueueOperation{box: boxID, inQueueTime: time.Now(), action: "delete_box"}for i, op := range operationQueue {if "delete_box" == op.action && op.recoveryEntry == nil && op.box == boxID {- operationQueue[i] = newOp+ replaceOperation(i, newOp)return}}@@ -587,7 +588,7 @@ func IndexTreeQueue(tree *parse.Tree) {newOp := &dbQueueOperation{indexTree: tree, inQueueTime: time.Now(), action: "index"}for i, op := range operationQueue {if "index" == op.action && op.indexTree != nil && op.indexTree.ID == tree.ID { // 相同树则覆盖- operationQueue[i] = newOp+ replaceOperation(i, newOp)return}}@@ -609,7 +610,7 @@ func UpsertTreeQueueWithChanges(tree *parse.Tree, changes *TreeIndexChanges) {if "upsert" == op.action && op.upsertTree != nil && op.upsertTree.Box == tree.Box && op.upsertTree.ID == tree.ID && op.upsertTree.Path == tree.Path {newOp.indexChanges = mergeTreeIndexChanges(op.indexChanges, newOp.indexChanges)newOp.inQueueTime = op.inQueueTime- operationQueue[i] = newOp+ replaceOperation(i, newOp)return}// 删除、移动、重命名及其他索引操作不参与快照覆盖,保留原始执行顺序。@@ -637,7 +638,7 @@ func renameTreeQueue(tree *parse.Tree, action string) {}for i, op := range operationQueue {if action == op.action && op.indexTree != nil && op.indexTree.Box == tree.Box && op.indexTree.ID == tree.ID {- operationQueue[i] = newOp+ replaceOperation(i, newOp)return}}@@ -655,7 +656,7 @@ func MoveTreeQueue(tree *parse.Tree) {}for i, op := range operationQueue {if "move" == op.action && op.indexTree != nil && op.indexTree.ID == tree.ID { // 相同树则覆盖- operationQueue[i] = newOp+ replaceOperation(i, newOp)return}}@@ -669,7 +670,7 @@ func RemoveTreeQueue(boxID, rootID string) {newOp := &dbQueueOperation{removeTreeBox: boxID, removeTreeID: rootID, inQueueTime: time.Now(), action: "delete_id"}for i, op := range operationQueue {if "delete_id" == op.action && op.removeTreeID == rootID {- operationQueue[i] = newOp+ replaceOperation(i, newOp)return}}@@ -695,7 +696,7 @@ func RemoveTreePathQueue(treeBox, treePathPrefix string) {newOp := &dbQueueOperation{removeTreeBox: treeBox, removeTreePath: treePathPrefix, inQueueTime: time.Now(), action: "delete"}for i, op := range operationQueue {if "delete" == op.action && (op.removeTreeBox == treeBox && op.removeTreePath == treePathPrefix) {- operationQueue[i] = newOp+ replaceOperation(i, newOp)return}}@@ -717,3 +718,14 @@ func appendOperation(op *dbQueueOperation) {appendToIndexQueue(op)eventbus.Publish(eventbus.EvtSQLIndexChanged)}++// Caller holds dbQueueLock. Preserve the latest recovery location when an+// in-memory operation is coalesced. Unchanged descriptors already reload the+// latest document from disk and do not need another journal entry.+func replaceOperation(index int, op *dbQueueOperation) {+ previous := dbOpToIndexEntry(operationQueue[index])+ operationQueue[index] = op+ if !reflect.DeepEqual(previous, dbOpToIndexEntry(op)) {+ appendToIndexQueue(op)+ }+}