Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:perf: Avoid saving configuration on index state transitions https://github.com/siyuan-note/siyuan/issues/20368

Daniel2d00e3962f5f
diff --git a/kernel/model/conf_save_test.go b/kernel/model/conf_save_test.go
index 0ebed40ef8..6192550ebd 100644
--- a/kernel/model/conf_save_test.go
+++ b/kernel/model/conf_save_test.go
@@ -23,6 +23,7 @@ import (
"testing"
"github.com/88250/gulu"
+ "github.com/siyuan-note/eventbus"
appconf "github.com/siyuan-note/siyuan/kernel/conf"
"github.com/siyuan-note/siyuan/kernel/util"
)
@@ -62,6 +63,31 @@ func TestSaveUsesEncryptedSnapshot(t *testing.T) {
}
}
+func TestIndexStateEventsDoNotSaveConfiguration(t *testing.T) {
+ oldConf, oldDir, oldReadOnly := Conf, util.ConfDir, util.ReadOnly
+ Conf, util.ConfDir, util.ReadOnly = NewAppConf(), t.TempDir(), false
+ t.Cleanup(func() { Conf, util.ConfDir, util.ReadOnly = oldConf, oldDir, oldReadOnly })
+ file := filepath.Join(util.ConfDir, "conf.json")
+ marker := []byte("configuration must not be serialized by index state events")
+ if err := os.WriteFile(file, marker, 0600); err != nil {
+ t.Fatal(err)
+ }
+ for _, state := range []int{1, 1, 0, 1, 0} {
+ if state == 1 {
+ eventbus.Publish(eventbus.EvtSQLIndexChanged)
+ } else {
+ eventbus.Publish(eventbus.EvtSQLIndexFlushed)
+ }
+ if Conf.DataIndexState != state {
+ t.Fatalf("runtime state=%d, want %d", Conf.DataIndexState, state)
+ }
+ data, err := os.ReadFile(file)
+ if err != nil || !bytes.Equal(data, marker) {
+ t.Fatalf("index event saved configuration: %v", err)
+ }
+ }
+}
+
func TestGraphQuerySavesOnlyChangedConfiguration(t *testing.T) {
oldConfDir, oldReadOnly := util.ConfDir, util.ReadOnly
util.ConfDir, util.ReadOnly = t.TempDir(), false
diff --git a/kernel/model/index.go b/kernel/model/index.go
index 8a7cc9f7be..b47bf2dac0 100644
--- a/kernel/model/index.go
+++ b/kernel/model/index.go
@@ -524,13 +524,16 @@ func subscribeSQLEvents() {
util.ContextPushMsg(context, msg)
})
+ // 持久化索引队列负责崩溃恢复,状态事件只更新供查询使用的运行时标志。
eventbus.Subscribe(eventbus.EvtSQLIndexChanged, func() {
+ Conf.m.Lock()
Conf.DataIndexState = 1
- Conf.Save()
+ Conf.m.Unlock()
})
eventbus.Subscribe(eventbus.EvtSQLIndexFlushed, func() {
+ Conf.m.Lock()
Conf.DataIndexState = 0
- Conf.Save()
+ Conf.m.Unlock()
})
}