Skip to content

Commit

Permalink
🎨 Add some detailed logs when synchronizing data siyuan-note/siyuan#9191
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Sep 15, 2023
1 parent 4b41832 commit cf7b56a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
13 changes: 12 additions & 1 deletion diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ package dejavu

import (
"github.com/siyuan-note/dejavu/entity"
"github.com/siyuan-note/logging"
)

// DiffUpsertRemove 比较 left 多于/变动 right 的文件以及 left 少于 right 的文件。
func (repo *Repo) DiffUpsertRemove(left, right []*entity.File) (upserts, removes []*entity.File) {
func (repo *Repo) DiffUpsertRemove(left, right []*entity.File, log bool) (upserts, removes []*entity.File) {
l := map[string]*entity.File{}
r := map[string]*entity.File{}
for _, f := range left {
Expand All @@ -35,10 +36,17 @@ func (repo *Repo) DiffUpsertRemove(left, right []*entity.File) (upserts, removes
rFile := r[lPath]
if nil == rFile {
upserts = append(upserts, l[lPath])
if log {
logging.LogInfof("upsert [path=%s, updated=%d]", l[lPath].Path, l[lPath].Updated)
}

continue
}
if !equalFile(lFile, rFile) {
upserts = append(upserts, l[lPath])
if log {
logging.LogInfof("upsert [lPath=%s, lUpdated=%d, rPath=%s, rUpdated=%d]", l[lPath].Path, l[lPath].Updated, rFile.Path, rFile.Updated)
}
continue
}
}
Expand All @@ -47,6 +55,9 @@ func (repo *Repo) DiffUpsertRemove(left, right []*entity.File) (upserts, removes
lFile := l[rPath]
if nil == lFile {
removes = append(removes, r[rPath])
if log {
logging.LogInfof("remove [path=%s, updated=%d]", r[rPath].Path, r[rPath].Updated)
}
continue
}
}
Expand Down
4 changes: 2 additions & 2 deletions repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func (repo *Repo) Checkout(id string, context map[string]interface{}) (upserts,
return
}

upserts, removes = repo.DiffUpsertRemove(latestFiles, files)
upserts, removes = repo.DiffUpsertRemove(latestFiles, files, false)
if 1 > len(upserts) && 1 > len(removes) {
return
}
Expand Down Expand Up @@ -334,7 +334,7 @@ func (repo *Repo) index(memo string, context map[string]interface{}) (ret *entit
return
}
}
upserts, removes = repo.DiffUpsertRemove(files, latestFiles)
upserts, removes = repo.DiffUpsertRemove(files, latestFiles, false)
if 1 > len(upserts) && 1 > len(removes) {
ret = latest
return
Expand Down
5 changes: 3 additions & 2 deletions sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,12 @@ func (repo *Repo) sync0(context map[string]interface{},
logging.LogErrorf("get latest sync files failed: %s", err)
return
}
localUpserts, localRemoves := repo.DiffUpsertRemove(latestFiles, latestSyncFiles)
localUpserts, localRemoves := repo.DiffUpsertRemove(latestFiles, latestSyncFiles, false)

// 计算云端最新相比本地最新的 upsert 和 remove 差异
var cloudUpserts, cloudRemoves []*entity.File
if "" != cloudLatest.ID {
cloudUpserts, cloudRemoves = repo.DiffUpsertRemove(cloudLatestFiles, latestFiles)
cloudUpserts, cloudRemoves = repo.DiffUpsertRemove(cloudLatestFiles, latestFiles, true)
}

// 避免旧的本地数据覆盖云端数据 https://github.com/siyuan-note/siyuan/issues/7403
Expand Down Expand Up @@ -273,6 +273,7 @@ func (repo *Repo) sync0(context map[string]interface{},
continue
}
mergeResult.Upserts = append(mergeResult.Upserts, cloudUpsert)
logging.LogInfof("sync merge upsert [path=%s, updated=%d]", cloudUpsert.Path, cloudUpsertIgnore.Updated)
}
}

Expand Down
4 changes: 2 additions & 2 deletions sync_manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ func (repo *Repo) SyncDownload(context map[string]interface{}) (mergeResult *Mer
logging.LogErrorf("get latest sync files failed: %s", err)
return
}
localUpserts, _ := repo.DiffUpsertRemove(latestFiles, latestSyncFiles)
localUpserts, _ := repo.DiffUpsertRemove(latestFiles, latestSyncFiles, false)

// 计算云端最新相比本地最新的 upsert 和 remove 差异
// 在单向同步的情况下该结果可直接作为合并结果
mergeResult.Upserts, mergeResult.Removes = repo.DiffUpsertRemove(cloudLatestFiles, latestFiles)
mergeResult.Upserts, mergeResult.Removes = repo.DiffUpsertRemove(cloudLatestFiles, latestFiles, false)

var fetchedFileIDs []string
for _, fetchedFile := range fetchedFiles {
Expand Down

0 comments on commit cf7b56a

Please sign in to comment.