🎨 Sync repo

This commit is contained in:
Liang Ding 2022-06-30 22:40:43 +08:00
parent 3d71a2644b
commit eaa83998a4
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
6 changed files with 86 additions and 81 deletions

View file

@ -401,71 +401,13 @@ func SyncData(boot, exit, byHand bool) {
if boot && gulu.File.IsExist(util.BlockTreePath) {
// 在 blocktree 存在的情况下不会重建索引,所以这里需要刷新 blocktree 和 database
if err = treenode.ReadBlockTree(); nil != err {
os.RemoveAll(util.BlockTreePath)
util.LogWarnf("removed block tree [%s] due to %s", util.BlockTreePath, err)
return
}
for _, upsertFile := range upsertFiles {
if !strings.HasSuffix(upsertFile, ".sy") {
continue
}
upsertFile = filepath.ToSlash(upsertFile)
box := upsertFile[:strings.Index(upsertFile, "/")]
p := strings.TrimPrefix(upsertFile, box)
tree, err0 := LoadTree(box, p)
if nil != err0 {
continue
}
treenode.ReindexBlockTree(tree)
sql.UpsertTreeQueue(tree)
}
for _, removeFile := range removeFiles {
if !strings.HasSuffix(removeFile, ".sy") {
continue
}
id := strings.TrimSuffix(filepath.Base(removeFile), ".sy")
block := treenode.GetBlockTree(id)
if nil != block {
treenode.RemoveBlockTreesByRootID(block.RootID)
sql.RemoveTreeQueue(block.BoxID, block.RootID)
}
}
treenode.InitBlockTree()
incReindex(upsertFiles, removeFiles)
return
}
if !boot && !exit {
// 增量索引
for _, upsertFile := range upsertFiles {
if !strings.HasSuffix(upsertFile, ".sy") {
continue
}
upsertFile = filepath.ToSlash(upsertFile)
box := upsertFile[:strings.Index(upsertFile, "/")]
p := strings.TrimPrefix(upsertFile, box)
tree, err0 := LoadTree(box, p)
if nil != err0 {
continue
}
treenode.ReindexBlockTree(tree)
sql.UpsertTreeQueue(tree)
//util.LogInfof("sync index tree [%s]", tree.ID)
}
for _, removeFile := range removeFiles {
if !strings.HasSuffix(removeFile, ".sy") {
continue
}
id := strings.TrimSuffix(filepath.Base(removeFile), ".sy")
block := treenode.GetBlockTree(id)
if nil != block {
treenode.RemoveBlockTreesByRootID(block.RootID)
sql.RemoveTreeQueue(block.BoxID, block.RootID)
//util.LogInfof("sync remove tree [%s]", block.RootID)
}
}
incReindex(upsertFiles, removeFiles)
cache.ClearDocsIAL() // 同步后文档树文档图标没有更新 https://github.com/siyuan-note/siyuan/issues/4939
util.ReloadUI()
}
@ -495,6 +437,38 @@ func clearEmptyDirs(dir string) {
}
}
// incReindex 增量重建索引。
func incReindex(upserts, removes []string) {
for _, upsertFile := range upserts {
if !strings.HasSuffix(upsertFile, ".sy") {
continue
}
upsertFile = filepath.ToSlash(upsertFile)
box := upsertFile[:strings.Index(upsertFile, "/")]
p := strings.TrimPrefix(upsertFile, box)
tree, err0 := LoadTree(box, p)
if nil != err0 {
continue
}
treenode.ReindexBlockTree(tree)
sql.UpsertTreeQueue(tree)
//util.LogInfof("sync index tree [%s]", tree.ID)
}
for _, removeFile := range removes {
if !strings.HasSuffix(removeFile, ".sy") {
continue
}
id := strings.TrimSuffix(filepath.Base(removeFile), ".sy")
block := treenode.GetBlockTree(id)
if nil != block {
treenode.RemoveBlockTreesByRootID(block.RootID)
sql.RemoveTreeQueue(block.BoxID, block.RootID)
//util.LogInfof("sync remove tree [%s]", block.RootID)
}
}
}
func SetCloudSyncDir(name string) {
if Conf.Sync.CloudName == name {
return