This commit is contained in:
Liang Ding 2022-07-03 21:42:08 +08:00
parent 65e6c69042
commit 5b1b38aabe
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
3 changed files with 60 additions and 57 deletions

View file

@ -249,57 +249,25 @@ const (
CtxPushMsgToStatusBarAndProgress
)
func indexRepoBeforeCloudSync() {
if 1 > len(Conf.Repo.Key) {
return
}
repo, err := newRepository()
if nil != err {
return
}
start := time.Now()
latest, _ := repo.Latest()
index, err := repo.Index("[Auto] Cloud sync", map[string]interface{}{
CtxPushMsg: CtxPushMsgToStatusBar,
})
if nil != err {
util.PushStatusBar("Create data snapshot for cloud sync failed")
util.LogErrorf("index data repo before cloud sync failed: %s", err)
return
}
elapsed := time.Since(start)
if nil != latest {
if latest.ID != index.ID {
// 对新创建的快照需要更新备注,加入耗时统计
index.Memo = fmt.Sprintf("[Auto] Cloud sync, completed in %.2fs", elapsed.Seconds())
err = repo.PutIndex(index)
if nil != err {
util.PushStatusBar("Save data snapshot for cloud sync failed")
util.LogErrorf("put index into data repo before cloud sync failed: %s", err)
return
}
util.PushStatusBar(fmt.Sprintf(Conf.Language(147)+" [%s]", elapsed.Seconds(), latest.ID[:7]))
} else {
util.PushStatusBar(Conf.Language(148) + " [" + latest.ID[:7] + "]")
}
} else {
util.PushStatusBar(fmt.Sprintf(Conf.Language(147)+" [%s]", elapsed.Seconds(), latest.ID[:7]))
}
if 7000 < elapsed.Milliseconds() {
util.LogWarnf("index data repo before cloud sync elapsed [%dms]", elapsed.Milliseconds())
}
}
func syncRepo(byHand bool) {
if 1 > len(Conf.Repo.Key) {
msg := Conf.Language(26)
util.PushStatusBar(msg)
util.PushErrMsg(msg, 0)
return
}
repo, err := newRepository()
if nil != err {
util.LogErrorf("sync repo failed: %s", err)
msg := fmt.Sprintf("sync repo failed: %s", err)
util.LogErrorf(msg)
util.PushStatusBar(msg)
util.PushErrMsg(msg, 0)
return
}
err = indexRepoBeforeCloudSync(repo)
if nil != err {
return
}
@ -368,6 +336,41 @@ func syncRepo(byHand bool) {
return
}
func indexRepoBeforeCloudSync(repo *dejavu.Repo) (err error) {
start := time.Now()
latest, _ := repo.Latest()
index, err := repo.Index("[Auto] Cloud sync", map[string]interface{}{
CtxPushMsg: CtxPushMsgToStatusBar,
})
if nil != err {
util.PushStatusBar(Conf.Language(140))
util.LogErrorf("index data repo before cloud sync failed: %s", err)
return
}
elapsed := time.Since(start)
if nil != latest {
if latest.ID != index.ID {
// 对新创建的快照需要更新备注,加入耗时统计
index.Memo = fmt.Sprintf("[Auto] Cloud sync, completed in %.2fs", elapsed.Seconds())
err = repo.PutIndex(index)
if nil != err {
util.PushStatusBar("Save data snapshot for cloud sync failed")
util.LogErrorf("put index into data repo before cloud sync failed: %s", err)
return
}
util.PushStatusBar(fmt.Sprintf(Conf.Language(147)+" [%s]", elapsed.Seconds(), latest.ID[:7]))
} else {
util.PushStatusBar(Conf.Language(148) + " [" + latest.ID[:7] + "]")
}
} else {
util.PushStatusBar(fmt.Sprintf(Conf.Language(147)+" [%s]", elapsed.Seconds(), latest.ID[:7]))
}
if 7000 < elapsed.Milliseconds() {
util.LogWarnf("index data repo before cloud sync elapsed [%dms]", elapsed.Milliseconds())
}
return
}
func newRepository() (ret *dejavu.Repo, err error) {
ignoreLines := getIgnoreLines()
ignoreLines = append(ignoreLines, "/.siyuan/conf.json") // 忽略旧版同步配置

View file

@ -131,12 +131,10 @@ func SyncData(boot, exit, byHand bool) {
syncLock.Lock()
defer syncLock.Unlock()
// 创建数据快照 https://github.com/siyuan-note/siyuan/issues/5161
indexRepoBeforeCloudSync()
//同步数据仓库 https://github.com/siyuan-note/siyuan/issues/5142
syncRepo(byHand)
return // TODO: 测试
if Conf.Sync.UseDataRepo {
syncRepo(byHand)
return
}
WaitForWritingFiles()
writingDataLock.Lock()
@ -412,6 +410,7 @@ func SyncData(boot, exit, byHand bool) {
return
}
// TODO: 新版同步上线后移除
// 清理 dir 下符合 ID 规则的空文件夹。
// 因为是深度遍历,所以可能会清理不完全空文件夹,每次遍历仅能清理叶子节点。但是多次调用后,可以清理完全。
func clearEmptyDirs(dir string) {