-
Notifications
You must be signed in to change notification settings - Fork 20
/
sync_manual.go
293 lines (253 loc) · 8.5 KB
/
sync_manual.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// DejaVu - Data snapshot and sync.
// Copyright (c) 2022-present, b3log.org
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package dejavu
import (
"errors"
"path/filepath"
"sync"
"time"
"github.com/88250/gulu"
"github.com/siyuan-note/dejavu/cloud"
"github.com/siyuan-note/dejavu/entity"
"github.com/siyuan-note/logging"
)
func (repo *Repo) SyncDownload(context map[string]interface{}) (mergeResult *MergeResult, trafficStat *TrafficStat, err error) {
lock.Lock()
defer lock.Unlock()
// 锁定云端,防止其他设备并发上传数据
err = repo.tryLockCloud(repo.DeviceID, context)
if nil != err {
return
}
defer repo.unlockCloud(context)
mergeResult = &MergeResult{Time: time.Now()}
trafficStat = &TrafficStat{m: &sync.Mutex{}}
// 获取本地最新索引
latest, err := repo.Latest()
if nil != err {
logging.LogErrorf("get latest failed: %s", err)
return
}
// 从云端获取最新索引
length, cloudLatest, err := repo.downloadCloudLatest(context)
if nil != err {
if !errors.Is(err, cloud.ErrCloudObjectNotFound) {
logging.LogErrorf("download cloud latest failed: %s", err)
return
}
}
trafficStat.DownloadFileCount++
trafficStat.DownloadBytes += length
trafficStat.APIGet++
if cloudLatest.ID == latest.ID || "" == cloudLatest.ID {
// 数据一致或者云端为空,直接返回
return
}
// 计算本地缺失的文件
fetchFileIDs, err := repo.localNotFoundFiles(cloudLatest.Files)
if nil != err {
logging.LogErrorf("get local not found files failed: %s", err)
return
}
// 从云端下载缺失文件并入库
length, fetchedFiles, err := repo.downloadCloudFilesPut(fetchFileIDs, context)
if nil != err {
logging.LogErrorf("download cloud files put failed: %s", err)
return
}
trafficStat.DownloadFileCount += len(fetchFileIDs)
trafficStat.DownloadBytes += length
trafficStat.APIGet += trafficStat.DownloadFileCount
// 组装还原云端最新文件列表
cloudLatestFiles, err := repo.getFiles(cloudLatest.Files)
if nil != err {
logging.LogErrorf("get cloud latest files failed: %s", err)
return
}
// 从文件列表中得到去重后的分块列表
cloudChunkIDs := repo.getChunks(cloudLatestFiles)
// 计算本地缺失的分块
fetchChunkIDs, err := repo.localNotFoundChunks(cloudChunkIDs)
if nil != err {
logging.LogErrorf("get local not found chunks failed: %s", err)
return
}
// 从云端下载缺失分块并入库
length, err = repo.downloadCloudChunksPut(fetchChunkIDs, context)
trafficStat.DownloadBytes += length
trafficStat.DownloadChunkCount += len(fetchChunkIDs)
trafficStat.APIGet += trafficStat.DownloadChunkCount
// 计算本地相比上一个同步点的 upsert 和 remove 差异
latestFiles, err := repo.getFiles(latest.Files)
if nil != err {
logging.LogErrorf("get latest files failed: %s", err)
return
}
latestSync := repo.latestSync()
latestSyncFiles, err := repo.getFiles(latestSync.Files)
if nil != err {
logging.LogErrorf("get latest sync files failed: %s", err)
return
}
localUpserts, localRemoves := repo.diffUpsertRemove(latestFiles, latestSyncFiles, false)
localChanged := 0 < len(localUpserts) || 0 < len(localRemoves)
// 计算云端最新相比本地最新的 upsert 和 remove 差异
// 在单向同步的情况下该结果可直接作为合并结果
mergeResult.Upserts, mergeResult.Removes = repo.diffUpsertRemove(cloudLatestFiles, latestFiles, false)
var fetchedFileIDs []string
for _, fetchedFile := range fetchedFiles {
fetchedFileIDs = append(fetchedFileIDs, fetchedFile.ID)
}
// 计算冲突的 upsert
// 冲突的文件以云端 upsert 和 remove 为准
for _, localUpsert := range localUpserts {
if nil != repo.getFile(mergeResult.Upserts, localUpsert) || nil != repo.getFile(mergeResult.Removes, localUpsert) {
mergeResult.Conflicts = append(mergeResult.Conflicts, localUpsert)
}
}
// 冲突文件复制到数据历史文件夹
if 0 < len(mergeResult.Conflicts) {
now := mergeResult.Time.Format("2006-01-02-150405")
temp := filepath.Join(repo.TempPath, "repo", "sync", "conflicts", now)
for i, file := range mergeResult.Conflicts {
var checkoutTmp *entity.File
checkoutTmp, err = repo.store.GetFile(file.ID)
if nil != err {
logging.LogErrorf("get file failed: %s", err)
return
}
err = repo.checkoutFile(checkoutTmp, temp, i+1, len(mergeResult.Conflicts), context)
if nil != err {
logging.LogErrorf("checkout file failed: %s", err)
return
}
absPath := filepath.Join(temp, checkoutTmp.Path)
err = repo.genSyncHistory(now, file.Path, absPath)
if nil != err {
logging.LogErrorf("generate sync history failed: %s", err)
err = ErrCloudGenerateConflictHistory
return
}
}
}
// 处理合并
err = repo.mergeSync(mergeResult, localChanged, false, latest, cloudLatest, cloudChunkIDs, trafficStat, context)
if nil != err {
logging.LogErrorf("merge sync failed: %s", err)
return
}
// 统计流量
go repo.cloud.AddTraffic(&cloud.Traffic{
DownloadBytes: trafficStat.DownloadBytes,
APIGet: trafficStat.APIGet,
})
// 移除空目录
gulu.File.RemoveEmptyDirs(repo.DataPath, removeEmptyDirExcludes...)
return
}
func (repo *Repo) SyncUpload(context map[string]interface{}) (trafficStat *TrafficStat, err error) {
lock.Lock()
defer lock.Unlock()
// 锁定云端,防止其他设备并发上传数据
err = repo.tryLockCloud(repo.DeviceID, context)
if nil != err {
return
}
defer repo.unlockCloud(context)
trafficStat = &TrafficStat{m: &sync.Mutex{}}
latest, err := repo.Latest()
if nil != err {
logging.LogErrorf("get latest failed: %s", err)
return
}
// 从云端获取最新索引
length, cloudLatest, err := repo.downloadCloudLatest(context)
if nil != err {
if !errors.Is(err, cloud.ErrCloudObjectNotFound) {
logging.LogErrorf("download cloud latest failed: %s", err)
return
}
}
trafficStat.DownloadFileCount++
trafficStat.DownloadBytes += length
trafficStat.APIPut++
if cloudLatest.ID == latest.ID {
// 数据一致,直接返回
return
}
availableSize := repo.cloud.GetAvailableSize()
if availableSize <= cloudLatest.Size || availableSize <= latest.Size {
err = ErrCloudStorageSizeExceeded
return
}
// 计算云端缺失的文件
var uploadFiles []*entity.File
for _, localFileID := range latest.Files {
if !gulu.Str.Contains(localFileID, cloudLatest.Files) {
var uploadFile *entity.File
uploadFile, err = repo.store.GetFile(localFileID)
if nil != err {
logging.LogErrorf("get file failed: %s", err)
return
}
uploadFiles = append(uploadFiles, uploadFile)
}
}
// 从文件列表中得到去重后的分块列表
uploadChunkIDs := repo.getChunks(uploadFiles)
// 这里暂时不计算云端缺失的分块了,因为目前计数云端缺失分块的代价太大
//uploadChunkIDs, err = repo.cloud.GetChunks(uploadChunkIDs)
//if nil != err {
// logging.LogErrorf("get cloud repo upload chunks failed: %s", err)
// return
//}
// 上传分块
length, err = repo.uploadChunks(uploadChunkIDs, context)
if nil != err {
logging.LogErrorf("upload chunks failed: %s", err)
return
}
trafficStat.UploadChunkCount += len(uploadChunkIDs)
trafficStat.UploadBytes += length
trafficStat.APIPut += trafficStat.UploadChunkCount
// 上传文件
length, err = repo.uploadFiles(uploadFiles, context)
if nil != err {
logging.LogErrorf("upload files failed: %s", err)
return
}
trafficStat.UploadChunkCount += len(uploadFiles)
trafficStat.UploadBytes += length
trafficStat.APIPut += trafficStat.UploadChunkCount
// 更新云端索引信息
err = repo.updateCloudIndexes(latest, trafficStat, context)
if nil != err {
logging.LogErrorf("update cloud indexes failed: %s", err)
return
}
// 更新本地同步点
err = repo.UpdateLatestSync(latest)
if nil != err {
logging.LogErrorf("update latest sync failed: %s", err)
return
}
// 统计流量
go repo.cloud.AddTraffic(&cloud.Traffic{
UploadBytes: trafficStat.UploadBytes,
APIPut: trafficStat.APIPut,
})
return
}