From 7aea03db69dda4dd10a879df7a5ea340ad73e00e Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Tue, 31 Jan 2023 22:20:33 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E5=B0=9D=E8=AF=95=E5=AE=9A=E4=BD=8D=20?= =?UTF-8?q?=E6=9C=AA=E6=89=BE=E5=88=B0=20ID=20=E4=B8=BA=20xxx=20=E7=9A=84?= =?UTF-8?q?=E5=86=85=E5=AE=B9=E5=9D=97=20https://github.com/siyuan-note/si?= =?UTF-8?q?yuan/issues/7215?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/job/cron.go | 2 +- kernel/model/index_fix.go | 2 +- kernel/treenode/blocktree.go | 28 +++++++++++++++++++++------- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/kernel/job/cron.go b/kernel/job/cron.go index 1e3caae62..ffb98438d 100644 --- a/kernel/job/cron.go +++ b/kernel/job/cron.go @@ -30,7 +30,7 @@ import ( func StartCron() { go every(100*time.Millisecond, task.ExecTaskJob) go every(5*time.Second, task.StatusJob) - go every(3*time.Second, treenode.SaveBlockTreeJob) + go every(5*time.Second, treenode.SaveBlockTreeJob) go every(5*time.Second, model.SyncDataJob) go every(2*time.Hour, model.StatJob) go every(2*time.Hour, model.RefreshCheckJob) diff --git a/kernel/model/index_fix.go b/kernel/model/index_fix.go index 88d892ca7..a95209e1b 100644 --- a/kernel/model/index_fix.go +++ b/kernel/model/index_fix.go @@ -108,7 +108,7 @@ func autoFixIndex() { redundantPaths := treenode.GetRedundantPaths(box.ID, paths) for _, p := range redundantPaths { - treenode.RemoveBlockTreesByPath(p) + treenode.RemoveBlockTreesByPath(box.ID, p) } missingPaths := treenode.GetNotExistPaths(box.ID, paths) diff --git a/kernel/treenode/blocktree.go b/kernel/treenode/blocktree.go index 20fa10809..5aa0a7d2f 100644 --- a/kernel/treenode/blocktree.go +++ b/kernel/treenode/blocktree.go @@ -36,7 +36,7 @@ import ( "github.com/vmihailenco/msgpack/v5" ) -var blockTrees = sync.Map{} +var blockTrees = &sync.Map{} type btSlice struct { data map[string]*BlockTree @@ -269,13 +269,13 @@ func RemoveBlockTreesByRootID(rootID string) { } } -func RemoveBlockTreesByPath(path string) { +func RemoveBlockTreesByPath(boxID, path string) { var ids []string blockTrees.Range(func(key, value interface{}) bool { slice := value.(*btSlice) slice.m.Lock() for _, b := range slice.data { - if b.Path == path { + if b.Path == path && b.BoxID == boxID { ids = append(ids, b.RootID) } } @@ -386,8 +386,15 @@ func IndexBlockTree(tree *parse.Tree) { } slice := val.(*btSlice) slice.m.Lock() - slice.data[n.ID] = &BlockTree{ID: n.ID, ParentID: parentID, RootID: tree.ID, BoxID: tree.Box, Path: tree.Path, HPath: tree.HPath, Updated: n.IALAttr("updated"), Type: TypeAbbr(n.Type.String())} - slice.changed = time.Now() + if bt := slice.data[n.ID]; nil != bt { + if bt.Updated != n.IALAttr("updated") { + slice.data[n.ID] = &BlockTree{ID: n.ID, ParentID: parentID, RootID: tree.ID, BoxID: tree.Box, Path: tree.Path, HPath: tree.HPath, Updated: n.IALAttr("updated"), Type: TypeAbbr(n.Type.String())} + slice.changed = time.Now() + } + } else { + slice.data[n.ID] = &BlockTree{ID: n.ID, ParentID: parentID, RootID: tree.ID, BoxID: tree.Box, Path: tree.Path, HPath: tree.HPath, Updated: n.IALAttr("updated"), Type: TypeAbbr(n.Type.String())} + slice.changed = time.Now() + } slice.m.Unlock() return ast.WalkContinue }) @@ -401,6 +408,7 @@ func InitBlockTree(force bool) { if nil != err { logging.LogErrorf("remove blocktree file failed: %s", err) } + blockTrees = &sync.Map{} return } @@ -485,7 +493,7 @@ func SaveBlockTree(force bool) { key := arg.(map[string]interface{})["key"].(string) slice := arg.(map[string]interface{})["value"].(*btSlice) - if !force && (slice.changed.IsZero() || slice.changed.After(start.Add(-7*time.Second))) { + if !force && slice.changed.IsZero() { return } @@ -508,16 +516,22 @@ func SaveBlockTree(force bool) { size += uint64(len(data)) }) + var count int blockTrees.Range(func(key, value interface{}) bool { slice := value.(*btSlice) - if !force && (slice.changed.IsZero() || slice.changed.After(start.Add(-7*time.Second))) { + if !force && slice.changed.IsZero() { return true } + count++ + waitGroup.Add(1) p.Invoke(map[string]interface{}{"key": key, "value": value}) return true }) + if 0 < count { + logging.LogInfof("wrote block trees [%d]", count) + } waitGroup.Wait() p.Release()