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: Recover large SQL index queue records https://github.com/siyuan-note/siyuan/issues/20937
Daniel7a830a179ef6
diff --git a/kernel/sql/index_queue.go b/kernel/sql/index_queue.goindex 57d0252733..fa71df50b4 100644--- a/kernel/sql/index_queue.go+++ b/kernel/sql/index_queue.go@@ -20,6 +20,7 @@ import ("bufio""encoding/json""fmt"+ "io""os""path/filepath""sync"@@ -207,23 +208,27 @@ func loadIndexQueue() (entries []indexEntry) {}defer f.Close()- scanner := bufio.NewScanner(f)- for scanner.Scan() {- line := scanner.Bytes()- if 0 == len(line) {- continue+ reader := bufio.NewReader(f)+ for {+ // Batch operations have no fixed record-size limit. Read one complete line+ // instead of stopping recovery at Scanner's default 64 KiB token limit.+ line, readErr := reader.ReadBytes('\n')+ if readErr != nil && readErr != io.EOF {+ logging.LogErrorf("read index queue failed: %s", readErr)+ return}- var entry indexEntry- if err = json.Unmarshal(line, &entry); err != nil {- logging.LogWarnf("skip corrupted index queue line: %s", err)- continue+ if len(line) > 0 && string(line) != "\n" && string(line) != "\r\n" {+ var entry indexEntry+ if err = json.Unmarshal(line, &entry); err != nil {+ logging.LogWarnf("skip corrupted index queue line: %s", err)+ } else {+ entries = append(entries, entry)+ }+ }+ if readErr == io.EOF {+ return}- entries = append(entries, entry)- }- if err = scanner.Err(); err != nil {- logging.LogErrorf("scan index queue failed: %s", err)}- return}func recoverIndexQueue() {diff --git a/kernel/sql/index_queue_test.go b/kernel/sql/index_queue_test.goindex 1d0ab7e20d..3c67c15aad 100644--- a/kernel/sql/index_queue_test.go+++ b/kernel/sql/index_queue_test.go@@ -204,6 +204,58 @@ func TestClearIndexQueuePreservesRawTail(t *testing.T) {}}+func TestIndexQueueRecoversLargeAndMalformedRecords(t *testing.T) {+ prepareIndexQueueTest(t)+ ids := make([]string, 5000)+ for i := range ids {+ ids[i] = "20261026000000-large01"+ }+ BatchRemoveTreeQueue(ids)+ queuePath := filepath.Join(util.QueueDir, "index.queue")+ f, err := os.OpenFile(queuePath, os.O_APPEND|os.O_WRONLY, 0644)+ if err != nil {+ t.Fatal(err)+ }+ // A malformed record must not hide the valid records following it, and an+ // interrupted final append must not prevent recovery of earlier records.+ if _, err = f.WriteString("{invalid}\n"); err != nil {+ t.Fatal(err)+ }+ if err = f.Close(); err != nil {+ t.Fatal(err)+ }+ BatchRemoveAssetsQueue([]string{strings.Repeat("a", 70000)})+ DeleteBoxQueue("last-valid-box")+ f, err = os.OpenFile(queuePath, os.O_APPEND|os.O_WRONLY, 0644)+ if err != nil {+ t.Fatal(err)+ }+ if _, err = f.WriteString(`{"action":`); err != nil {+ t.Fatal(err)+ }+ f.Close()+ before, err := os.ReadFile(queuePath)+ if err != nil {+ t.Fatal(err)+ }+ operationQueue = nil+ initIndexQueue()+ recoverIndexQueue()+ if len(operationQueue) != 3 {+ t.Fatalf("large or malformed record stopped recovery: %d operations", len(operationQueue))+ }+ first, second, last := operationQueue[0].recoveryEntry, operationQueue[1].recoveryEntry, operationQueue[2].recoveryEntry+ if first == nil || first.Action != "delete_ids" || len(first.IDs) != len(ids) ||+ second == nil || second.Action != "delete_assets" || len(second.Hashes[0]) != 70000 ||+ last == nil || last.Box != "last-valid-box" {+ t.Fatalf("recovery changed record contents: %#v", operationQueue)+ }+ after, err := os.ReadFile(queuePath)+ if err != nil || !bytes.Equal(before, after) {+ t.Fatalf("loading changed original recovery bytes: %v", err)+ }+}+func prepareIndexQueueTest(t *testing.T) {t.Helper()