From 90605cfbb55b98b89424ed136babb2c0c7f91cf5 Mon Sep 17 00:00:00 2001 From: life Date: Wed, 23 Sep 2015 21:46:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E7=94=A8=E7=BA=BF?= =?UTF-8?q?=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 性能不好 --- src/main.js | 36 +++++++++++++- src/node_modules/db.js | 14 ++++-- src/node_modules/nedb_proxy.js | 87 ++++++++++++++++++++++++++++++++++ src/node_modules/note.js | 2 + src/node_modules/notebook.js | 11 +++-- src/public/min/config-min.js | 2 +- src/thread_db.js | 69 +++++++++++++++++++++++++++ 7 files changed, 211 insertions(+), 10 deletions(-) create mode 100644 src/node_modules/nedb_proxy.js create mode 100644 src/thread_db.js diff --git a/src/main.js b/src/main.js index 6a8ebf66..81ac6300 100644 --- a/src/main.js +++ b/src/main.js @@ -209,6 +209,39 @@ function setMenu() { Menu.setApplicationMenu(menu); // Must be called within app.on('ready', function(){ ... }); } +var ipc = require('ipc'); + +// DB线程 +var threadDB = { + instance: null, + + _token2Sender: {}, // token => sender + + init: function () { + var me = this; + // 多线程 + var childProcess = require('child_process'); + this.instance = childProcess.fork('./thread_db.js'); + + // 线程返回消息 + // {token: token, err: err, ret: ret} + this.instance.on('message', function(m) { + // console.log('_token2Sender----------') + // console.log(me._token2Sender); + var sender = me._token2Sender[m.token]; + sender.send('db-exec-ret', m); + }); + + // 前端发来消息 + // m = {token: token, method: 'insert, findOne', dbname: 'notes', params: {username: "life"}}; + ipc.on('db-exec', function(event, m) { + me._token2Sender[m.token] = event.sender; + me.instance.send(m); + }); + } + +}; + function openIt() { // app.getPath('appData'); @@ -231,6 +264,8 @@ function openIt() { // and load the index.html of the app. mainWindow.loadUrl('file://' + __dirname + '/note.html'); + threadDB.init(); + // 不能放在这里, 刚开始有图片, 之后添加的图片不能显示 ?? // // 启动服务器, 图片 // var Server = require('server'); @@ -244,7 +279,6 @@ function openIt() { mainWindow = null; }); - var ipc = require('ipc'); mainWindow.on('focus', function() { // ipc.send('focusWindow'); mainProcess没有该方法 if(mainWindow && mainWindow.webContents) diff --git a/src/node_modules/db.js b/src/node_modules/db.js index aaf8198f..7fcc3a49 100644 --- a/src/node_modules/db.js +++ b/src/node_modules/db.js @@ -1,4 +1,5 @@ -var Datastore = require('nedb'); +// var Datastore = require('nedb'); +var DatastoreProxy = require('nedb_proxy'); var path = require('path'); var Evt = require('evt'); @@ -20,12 +21,15 @@ var db = {}; var dbNames = ['users', 'notebooks', 'notes', 'tags', 'images', 'attachs', 'noteHistories', 'g']; for(var i in dbNames) { var name = dbNames[i]; - var p = path.join(dbPath, name + '.db'); + // var p = path.join(dbPath, name + '.db'); (function (name) { // 这部分非常慢!, 会卡界面 - db[name] = new Datastore({ filename: p, autoload: true , onload: function () { - console.log(name + ' is loaded'); - }}); + // db[name] = new Datastore({ filename: p, autoload: true , onload: function () { + // console.log(name + ' is loaded'); + // }}); + + db[name] = new DatastoreProxy(name); + })(name); } module.exports = db; diff --git a/src/node_modules/nedb_proxy.js b/src/node_modules/nedb_proxy.js new file mode 100644 index 00000000..04643dcf --- /dev/null +++ b/src/node_modules/nedb_proxy.js @@ -0,0 +1,87 @@ +var ipc = require('ipc'); + +function Find(dbProxy, dbname, query) { + this.query = query; + this.dbProxy = dbProxy; + this.dbname = dbname; +} +Find.prototype.sort = function (sorter) { + this.sorter = sorter; + return this; +} + +Find.prototype.exec = function (callback) { + var params = { + query: this.query, + sorter: this.sorter + }; + this.dbProxy.send(params, callback, 'find'); +}; + +//========== + +function DBProxy(dbname) { + this.dbname = dbname; +}; + +var token = 1; +var token2Callback = {}; + +DBProxy.prototype.send = function(params, callback, method) { + token++; + var m = { + token: token, + method: method, + dbname: this.dbname, + params: params + } + token2Callback[token] = callback; + ipc.send('db-exec', m); +}; + +// NB.find({UserId: userId, $or: [{LocalIsDelete : { $exists : false }}, {LocalIsDelete: false}] }, function(err, notebooks) { +// Notes.find(query).sort({'UpdatedTime': -1}).exec(function(err, notes) { +DBProxy.prototype.find = function (params, callback) { + if (callback) { + this.send({query: params}, callback, 'find'); + } + else { + return new Find(this, this.dbname, params); + } +}; +DBProxy.prototype.findOne = function (params, callback) { + this.send(params, callback, 'findOne'); +}; +DBProxy.prototype.count = function (params, callback) { + this.send(params, callback, 'count'); +}; +DBProxy.prototype.insert = function (params, callback) { + this.send(params, callback, 'insert'); +}; +DBProxy.prototype.update = function (query, sets, options, callback) { + if (typeof options == 'function') { + callback = options; + options = {}; + } + if (!options) { + options = {}; + } + var params = { + query: query, + sets: sets, + options: options + }; + this.send(params, callback, 'update'); +}; +DBProxy.prototype.remove = function (params, callback) { + this.send(params, callback, 'remove'); +}; + +// m = {token: , err : , ret: } +ipc.on('db-exec-ret', function(m) { + var token = m.token; + var callback = token2Callback[token]; + callback && callback(m.err, m.ret); +}); + +module.exports = DBProxy; \ No newline at end of file diff --git a/src/node_modules/note.js b/src/node_modules/note.js index acd4d564..27fb86f1 100644 --- a/src/node_modules/note.js +++ b/src/node_modules/note.js @@ -288,7 +288,9 @@ var Note = { Notes.update( {UserId: userId, IsTrash: true}, {$set: {LocalIsDelete: true, IsDirty: true}}, + {multi: true}, + function(err, n) { // Web.alertWeb(n); callback && callback(); diff --git a/src/node_modules/notebook.js b/src/node_modules/notebook.js index e8095b6f..55353fc7 100644 --- a/src/node_modules/notebook.js +++ b/src/node_modules/notebook.js @@ -101,6 +101,11 @@ var Notebook = { getNotebooks: function(callback) { var me = this; var userId = User.getCurActiveUserId(); + // db.notebooks_.find({}, function (err, nbs) { + // console.log("+++++++++++++++++++++++++++") + // console.log(nbs); + // console.log('-----------------------------'); + // }); NB.find({UserId: userId, $or: [{LocalIsDelete : { $exists : false }}, {LocalIsDelete: false}] }, function(err, notebooks) { if(err) { log(err); @@ -150,9 +155,9 @@ var Notebook = { // 当前笔记在parentNotebookId下, 且该parentNotebookId下的所有子孩子的id dragNotebooks: function(curNotebookId, parentNotebookId, siblingNotebookIds) { var me = this; - console.log(curNotebookId); - console.log(parentNotebookId); - console.log(siblingNotebookIds); + // console.log(curNotebookId); + // console.log(parentNotebookId); + // console.log(siblingNotebookIds); // 先更新curNotebookId的父 me.getNotebook(curNotebookId, function(notebook) { if(!notebook) { diff --git a/src/public/min/config-min.js b/src/public/min/config-min.js index 3f5a56cc..bcc3d95e 100644 --- a/src/public/min/config-min.js +++ b/src/public/min/config-min.js @@ -1 +1 @@ -var Config={plugins:["theme","import_evernote","export_pdf","langs"],langs:[{filename:"en-us",name:"English"},{filename:"zh-cn",name:"简体中文"},{filename:"zh-hk",name:"繁体中文"}],lang:"zh-hk",theme:"pebbles"}; \ No newline at end of file +var Config={plugins:["theme","import_evernote","export_pdf","export_html","langs"],langs:[{filename:"en-us",name:"English"},{filename:"zh-cn",name:"简体中文"},{filename:"zh-hk",name:"繁体中文"}],lang:"en-us",theme:""}; \ No newline at end of file diff --git a/src/thread_db.js b/src/thread_db.js new file mode 100644 index 00000000..a76fcc70 --- /dev/null +++ b/src/thread_db.js @@ -0,0 +1,69 @@ +var Datastore = require('nedb'); +var path = require('path'); + +// 数据库初始化 +// var dbPath = require('nw.gui').App.dataPath + '/nedb'; +// var dbPath = Evt.getBasePath() + '/Users/life/Library/Application Support/Leanote' + '/nedb'; +// nedb2 为了port + +var dbPath = ''; // '/Users/life/Library/Application Support/Leanote/nedb55'; + +// test +// if(dbPath.length < 6) { + var dbPath = '/Users/life/Library/Application Support/Leanote/nedb55'; +// } + +// console.log(dbPath); +// g, 表全局环境 +var db = {}; +var dbNames = ['users', 'notebooks', 'notes', 'tags', 'images', 'attachs', 'noteHistories', 'g']; +for(var i in dbNames) { + var name = dbNames[i]; + var p = path.join(dbPath, name + '.db'); + (function (name) { + // 这部分非常慢!, 会卡界面 + db[name] = new Datastore({ filename: p, autoload: true , onload: function () { + console.log(name + ' -*- is loaded'); + }}); + })(name); +} +module.exports = db; +console.log('db thread inited -----***--------**--------'); + +// m = {execUN: 序号, method: 'insert, findOne', dbname: 'notes', params: {username: "life"}}; +// 如果method是find +// params = {query: {}, sorter: {}}; +// 如果是update +// var params = {query: query,sets: sets,options: options}; +process.on('message', function(m) { + if (m.method == 'find') { + var params = m.params; + var query = params.query; + var sorter = params.sorter; + if (sorter) { + db[m.dbname].find(query).sort(sorter).exec(function (err, ret) { + // 返回结果 + process.send({token: m.token, err: err, ret: ret}); + }); + return; + } else { + db.params = query; + } + } + else if (m.method == 'update') { + var params = m.params; + // console.log('update------------------') + // console.log(params.options ? '' : '????????????????????????????'); + db[m.dbname].update(params.query, params.sets, params.options, function (err, ret) { + // 返回结果 + process.send({token: m.token, err: err, ret: ret}); + }); + return; + } + // 查询 + db[m.dbname][m.method](db.params, function (err, ret) { + // 返回结果 + process.send({token: m.token, err: err, ret: ret}); + }); +}); +