第一次同步时. 因为量大, 在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); return callback && callback(false);
} }
var ret = response.body; var ret = response.body;
console.log(ret); // console.log(ret);
if(Common.isOk(ret)) { if(Common.isOk(ret)) {
callback && callback(ret); callback && callback(ret);
} else { } else {

12
node_modules/note.js generated vendored
View File

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

84
node_modules/sync.js generated vendored
View File

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

2
node_modules/tag.js generated vendored
View File

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

26
node_modules/user.js generated vendored
View File

@@ -379,7 +379,7 @@ User = {
fullSyncForce: function(callback) { fullSyncForce: function(callback) {
var me = this; var me = this;
var userId = me.getCurActiveUserId(); 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 () { me.deleteUserAllData(userId, function () {
callback && callback(); callback && callback();
@@ -396,10 +396,11 @@ User = {
} }
Api.getLastSyncState(function(state) { Api.getLastSyncState(function(state) {
if(state) { if(state) {
// console.error('--updateLastSyncState---')
// console.log(state);
me.LastSyncUsn = state.LastSyncUsn; me.LastSyncUsn = state.LastSyncUsn;
me.LastSyncTime = state.LastSyncTime; me.LastSyncTime = state.LastSyncTime;
me.NotebookUsn = -1;
me.NoteUsn = -1;
me.TagUsn = -1;
db.users.update({UserId: me.getCurActiveUserId()}, {$set: state}); db.users.update({UserId: me.getCurActiveUserId()}, {$set: state});
} }
callback(); callback();
@@ -414,6 +415,17 @@ User = {
var me = this; var me = this;
return me.LastSyncUsn; 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要用 // 更新 send changes要用
updateLastSyncUsn: function(usn) { updateLastSyncUsn: function(usn) {
var me = this; var me = this;
@@ -421,6 +433,14 @@ User = {
db.users.update({UserId: me.getCurActiveUserId()}, {$set: {LastSyncUsn: usn}}); 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) { getG: function(callback) {
var me = this; var me = this;