第一次同步时. 因为量大, 在windows下可能会有解析字符串出错, 导致json出现

所以, 添加NotebookUsn, NoteUsn, tagUsn作为缓存, 下次不必全部重来同步
This commit is contained in:
life
2015-11-25 18:04:53 +08:00
parent cd58a3617a
commit 4a6dadf903
5 changed files with 128 additions and 48 deletions

2
node_modules/api.js generated vendored
View File

@@ -173,7 +173,7 @@ var Api = {
return callback && callback(false);
}
var ret = response.body;
console.log(ret);
// console.log(ret);
if(Common.isOk(ret)) {
callback && callback(ret);
} else {

12
node_modules/note.js generated vendored
View File

@@ -722,9 +722,9 @@ var Note = {
if(doc.length > 1) {
console.error(doc.length + '. ..');
}
console.log('note length: ' + doc.length + '. ..');
// console.log('note length: ' + doc.length + '. ..');
if(err || !doc || !doc.length) {
log('getNoteByServerNoteId 不存在' + noteId);
// log('getNoteByServerNoteId 不存在' + noteId);
callback && callback(false);
} else {
doc = doc[0];
@@ -736,7 +736,7 @@ var Note = {
var me = this;
db.notes.findOne({ServerNoteId: noteId}, function(err, doc) {
if(err || !doc) {
log('getNoteIdByServerNoteId 不存在' + noteId);
// log('getNoteIdByServerNoteId 不存在' + noteId);
callback && callback(false);
} else {
callback && callback(doc.NoteId);
@@ -1767,7 +1767,7 @@ var Note = {
updateNoteToDeleteTag: function(title, callback) {
var updates = {}; // noteId =>
var userId = User.getCurActiveUserId();
console.log('updateNoteToDeleteTag--');
// console.log('updateNoteToDeleteTag--');
db.notes.find({UserId: userId, LocalIsDelete: false , Tags: {$in: [title]}}, function(err, notes) {
console.log(notes);
if(!err && notes && notes.length > 0) {
@@ -1791,8 +1791,8 @@ var Note = {
});
}
} else {
console.log('updateNoteToDeleteTag');
console.log(err);
// console.log('updateNoteToDeleteTag');
// console.log(err);
callback({});
}
});

134
node_modules/sync.js generated vendored
View File

@@ -12,7 +12,7 @@ var Note = require('note');
var Web = require('web');
function log(o) {
console.log(o);
// console.log(o);
}
// 同步服务
@@ -224,7 +224,7 @@ var Sync = {
}
Api.getSyncNotebooks(afterUsn, me._notebookMaxEntry, function(notebooks) {
console.log('syncNotebook')
console.log(notebooks);
// console.log(notebooks);
if(Common.isOk(notebooks)) {
me._totalSyncNotebookNum += notebooks.length;
// 证明可能还有要同步的
@@ -232,10 +232,19 @@ var Sync = {
me._syncNotebookToLocal(notebooks, callback);
var last = notebooks[notebooks.length-1];
me.syncNotebook(last.Usn, callback);
// 更新Usn
me.updateSyncUsn('NotebookUsn', last.Usn);
} else {
console.log('no more');
me._syncNotebookIsLastChunk = true;
me._syncNotebookToLocal(notebooks, callback);
if (notebooks.length) {
var last = notebooks[notebooks.length-1];
me.updateSyncUsn('NotebookUsn', last.Usn);
}
}
} else {
// 同步失败
@@ -386,26 +395,39 @@ var Sync = {
}
Api.getSyncNotes(afterUsn, me._noteMaxEntry, function(notes) {
console.log('syncNote---');
console.log(notes);
// console.log('syncNote---');
// console.log(notes);
if(Common.isOk(notes)) {
me._totalSyncNoteNum += notes.length;
// 证明可能还有要同步的
if(notes.length == me._noteMaxEntry) {
me._syncNoteToLocal(notes, callback);
var last = notes[notes.length-1];
me.syncNote(last.Usn, callback);
// 500ms延迟
setTimeout(function () {
me.syncNote(last.Usn, callback);
}, 500);
// 更新Usn
me.updateSyncUsn('NoteUsn', last.Usn);
} else {
log('no more');
me._syncNoteIsLastChunk = true;
me._syncNoteToLocal(notes, callback);
if (notes.length) {
var last = notes[notes.length-1];
me.updateSyncUsn('NoteUsn', last.Usn);
}
}
} else {
// 同步失败
me._syncInfo.note.ok = false;
me._syncInfo.note.msg = "cann't get all chunks";
console.log(notes);
console.log('eeeeeeeeeeeeeeeeeerrror')
console.error('eeeeeeeeeeeeeeeeeerrror')
console.error(notes);
callback && callback(false);
}
});
@@ -508,10 +530,18 @@ var Sync = {
me._syncTagToLocal(tags, callback);
var last = tags[tags.length-1];
me.syncTag(last.Usn, callback);
// 更新Usn
me.updateSyncUsn('TagUsn', last.Usn);
} else {
log('no more');
me._syncTagIsLastChunk = true;
me._syncTagToLocal(tags, callback);
if (tags.length) {
var last = tags[tags.length-1];
me.updateSyncUsn('TagUsn', last.Usn);
}
}
} else {
// 同步失败
@@ -534,40 +564,70 @@ var Sync = {
});
},
// 为了避免全都重新来过, 这里保存每次
updateSyncUsn: function (type, usn) {
console.error('-------------------------------------------------');
console.log(type + ' = ' + usn);
User.updateEachSyncState(type, usn, function() {
});
},
// 全量同步
fullSync: function(callback) {
var me = this;
me._stop = false;
me._initSyncInfo();
// Web.syncNotebookFinish();
// 同步笔记本
me.syncNotebook(-1, function(ok) {
if(ok) {
// Web.syncNoteFinish();
// console.log('------------------')
// 同步笔记
me.syncNote(-1, function(ok) {
if(ok) {
// Web.syncTagFinish();
// 同步标签
me.syncTag(-1, function() {
// 更新上次同步时间
me.updateLastSyncState(function() {
// send changes
// me.sendChanges();
callback && callback(me._syncInfo, true);
});
});
} else {
console.error('syncNote error.... 跳过tag');
callback && callback(me._syncInfo, false);
}
});
} else {
console.error('syncNotebook error.... 跳过note,tag');
callback && callback(me._syncInfo, false);
User.getAllLastSyncState(function(lastUsn, notebookUsn, noteUsn, tagUsn) {
// 不可能会有lastUsn吧
if (lastUsn && lastUsn > 0) {
notebookUsn = -1;
noteUsn = -1;
tagUsn = -1;
}
if (!notebookUsn) {
notebookUsn = -1;
}
if (!noteUsn) {
noteUsn = -1;
}
if (!tagUsn) {
tagUsn = -1;
}
// console.log('fullSync------ ' + notebookUsn + ' ' + noteUsn + ' ' + tagUsn);
// Web.syncNotebookFinish();
// 同步笔记本
me.syncNotebook(notebookUsn, function(ok) {
if(ok) {
// Web.syncNoteFinish();
// console.log('------------------')
// 同步笔记
me.syncNote(noteUsn, function(ok) {
if(ok) {
// Web.syncTagFinish();
// 同步标签
me.syncTag(tagUsn, function() {
// 更新上次同步时间
me.updateLastSyncState(function() {
// send changes
// me.sendChanges();
callback && callback(me._syncInfo, true);
});
});
} else {
console.error('syncNote error.... 跳过tag');
callback && callback(me._syncInfo, false);
}
});
} else {
console.error('syncNotebook error.... 跳过note,tag');
callback && callback(me._syncInfo, false);
}
});
});
},
@@ -577,9 +637,9 @@ var Sync = {
fixConflicts: function(callback) {
var me = this;
var afterInfo = me._syncInfo;
log('处理冲突....');
log(me._syncInfo);
log(me._syncInfo.tag);
// log('处理冲突....');
// log(me._syncInfo);
// log(me._syncInfo.tag);
var tag = me._syncInfo.tag;
// 如果是incSync, 则要前端处理

2
node_modules/tag.js generated vendored
View File

@@ -72,7 +72,7 @@ var Tag = {
});
//
var Note = require('./note');
console.log(Note);
// console.log(Note);
Note.updateNoteToDeleteTag(title, function(updates) {
callback && callback(updates);
});

26
node_modules/user.js generated vendored
View File

@@ -379,7 +379,7 @@ User = {
fullSyncForce: function(callback) {
var me = this;
var userId = me.getCurActiveUserId();
db.users.update({UserId: userId}, {$set: {LastSyncUsn: -1}}, function() {
db.users.update({UserId: userId}, {$set: {LastSyncUsn: -1, NotebookUsn: -1, NoteUsn: -1, TagUsn: -1}}, function() {
// 删除本地账户所有数据
me.deleteUserAllData(userId, function () {
callback && callback();
@@ -396,10 +396,11 @@ User = {
}
Api.getLastSyncState(function(state) {
if(state) {
// console.error('--updateLastSyncState---')
// console.log(state);
me.LastSyncUsn = state.LastSyncUsn;
me.LastSyncTime = state.LastSyncTime;
me.NotebookUsn = -1;
me.NoteUsn = -1;
me.TagUsn = -1;
db.users.update({UserId: me.getCurActiveUserId()}, {$set: state});
}
callback();
@@ -414,6 +415,17 @@ User = {
var me = this;
return me.LastSyncUsn;
},
getAllLastSyncState: function (callback) {
var me = this;
me.getCurUser(function (user) {
if (!user) {
return callback(false);
}
callback(user.LastSyncUsn, user.NotebookUsn, user.NoteUsn, user.TagUsn);
});
},
// 更新 send changes要用
updateLastSyncUsn: function(usn) {
var me = this;
@@ -421,6 +433,14 @@ User = {
db.users.update({UserId: me.getCurActiveUserId()}, {$set: {LastSyncUsn: usn}});
},
// 更新每一个类型的USN, 仅在全量同步时有用
updateEachSyncState: function (type, usn, callback) {
var me = this;
var updates = {};
updates[type] = usn;
db.users.update({UserId: me.getCurActiveUserId()}, {$set: updates});
},
// 全局配置
getG: function(callback) {
var me = this;