mirror of
https://github.com/siyuan-note/siyuan.git
synced 2026-01-23 08:46:09 +01:00
🎨 Sync repo
This commit is contained in:
parent
3d71a2644b
commit
eaa83998a4
6 changed files with 86 additions and 81 deletions
|
|
@ -488,13 +488,9 @@ func InitBoxes() {
|
|||
util.IncBootProgress(1, "Reading block trees...")
|
||||
}
|
||||
}()
|
||||
if err := treenode.ReadBlockTree(); nil == err {
|
||||
initialized = true
|
||||
} else {
|
||||
if err = os.RemoveAll(util.BlockTreePath); nil != err {
|
||||
util.LogErrorf("remove block tree [%s] failed: %s", util.BlockTreePath, err)
|
||||
}
|
||||
}
|
||||
|
||||
treenode.InitBlockTree()
|
||||
initialized = true
|
||||
}
|
||||
} else { // 大于 1 的话说明在同步阶段已经加载过了
|
||||
initialized = true
|
||||
|
|
|
|||
|
|
@ -29,6 +29,7 @@ import (
|
|||
"github.com/siyuan-note/encryption"
|
||||
"github.com/siyuan-note/eventbus"
|
||||
"github.com/siyuan-note/filelock"
|
||||
"github.com/siyuan-note/siyuan/kernel/cache"
|
||||
"github.com/siyuan-note/siyuan/kernel/sql"
|
||||
"github.com/siyuan-note/siyuan/kernel/util"
|
||||
)
|
||||
|
|
@ -317,7 +318,7 @@ func CheckoutRepo(id string) (err error) {
|
|||
Conf.Sync.Enabled = false
|
||||
Conf.Save()
|
||||
|
||||
err = repo.Checkout(id, map[string]interface{}{
|
||||
_, _, err = repo.Checkout(id, map[string]interface{}{
|
||||
CtxPushMsg: CtxPushMsgToStatusBarAndProgress,
|
||||
})
|
||||
if nil != err {
|
||||
|
|
@ -444,12 +445,45 @@ func syncRepo() (err error) {
|
|||
}
|
||||
|
||||
start := time.Now()
|
||||
err = repo.Sync(Conf.Sync.CloudName, Conf.User.UserId, Conf.User.UserToken, Conf.System.NetworkProxy.String(), util.AliyunServer, map[string]interface{}{
|
||||
latest, fetchedFiles, err := repo.Sync(Conf.Sync.CloudName, Conf.User.UserId, Conf.User.UserToken, Conf.System.NetworkProxy.String(), util.AliyunServer, map[string]interface{}{
|
||||
CtxPushMsg: CtxPushMsgToStatusBar,
|
||||
})
|
||||
elapsed := time.Since(start)
|
||||
util.LogInfof("sync data repo elapsed [%.2fs]", elapsed.Seconds())
|
||||
if nil != err {
|
||||
util.LogErrorf("sync data repo failed: %s", err)
|
||||
util.PushStatusBar("Sync data repo failed: " + err.Error())
|
||||
return
|
||||
}
|
||||
util.PushStatusBar(fmt.Sprintf(Conf.Language(149), elapsed.Seconds()))
|
||||
if 1 > len(fetchedFiles) {
|
||||
// 没有下载到新文件,直接返回
|
||||
return
|
||||
}
|
||||
|
||||
// 下载到文件后,需要恢复到工作区并重建索引
|
||||
|
||||
CloseWatchAssets()
|
||||
defer WatchAssets()
|
||||
|
||||
upsertFiles, removeFiles, err := repo.Checkout(latest.ID, map[string]interface{}{
|
||||
CtxPushMsg: CtxPushMsgToStatusBarAndProgress,
|
||||
})
|
||||
if nil != err {
|
||||
util.PushClearProgress()
|
||||
return
|
||||
}
|
||||
|
||||
var upserts, removes []string
|
||||
for _, file := range upsertFiles {
|
||||
upserts = append(upserts, file.Path)
|
||||
}
|
||||
for _, file := range removeFiles {
|
||||
removes = append(removes, file.Path)
|
||||
}
|
||||
incReindex(upserts, removes)
|
||||
cache.ClearDocsIAL()
|
||||
util.ReloadUI()
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue