mirror of
https://github.com/leanote/desktop-app.git
synced 2025-10-15 07:31:33 +00:00
数据库用线程
性能不好
This commit is contained in:
36
src/main.js
36
src/main.js
@@ -209,6 +209,39 @@ function setMenu() {
|
|||||||
Menu.setApplicationMenu(menu); // Must be called within app.on('ready', function(){ ... });
|
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() {
|
function openIt() {
|
||||||
// app.getPath('appData');
|
// app.getPath('appData');
|
||||||
|
|
||||||
@@ -231,6 +264,8 @@ function openIt() {
|
|||||||
// and load the index.html of the app.
|
// and load the index.html of the app.
|
||||||
mainWindow.loadUrl('file://' + __dirname + '/note.html');
|
mainWindow.loadUrl('file://' + __dirname + '/note.html');
|
||||||
|
|
||||||
|
threadDB.init();
|
||||||
|
|
||||||
// 不能放在这里, 刚开始有图片, 之后添加的图片不能显示 ??
|
// 不能放在这里, 刚开始有图片, 之后添加的图片不能显示 ??
|
||||||
// // 启动服务器, 图片
|
// // 启动服务器, 图片
|
||||||
// var Server = require('server');
|
// var Server = require('server');
|
||||||
@@ -244,7 +279,6 @@ function openIt() {
|
|||||||
mainWindow = null;
|
mainWindow = null;
|
||||||
});
|
});
|
||||||
|
|
||||||
var ipc = require('ipc');
|
|
||||||
mainWindow.on('focus', function() {
|
mainWindow.on('focus', function() {
|
||||||
// ipc.send('focusWindow'); mainProcess没有该方法
|
// ipc.send('focusWindow'); mainProcess没有该方法
|
||||||
if(mainWindow && mainWindow.webContents)
|
if(mainWindow && mainWindow.webContents)
|
||||||
|
14
src/node_modules/db.js
generated
vendored
14
src/node_modules/db.js
generated
vendored
@@ -1,4 +1,5 @@
|
|||||||
var Datastore = require('nedb');
|
// var Datastore = require('nedb');
|
||||||
|
var DatastoreProxy = require('nedb_proxy');
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
var Evt = require('evt');
|
var Evt = require('evt');
|
||||||
|
|
||||||
@@ -20,12 +21,15 @@ var db = {};
|
|||||||
var dbNames = ['users', 'notebooks', 'notes', 'tags', 'images', 'attachs', 'noteHistories', 'g'];
|
var dbNames = ['users', 'notebooks', 'notes', 'tags', 'images', 'attachs', 'noteHistories', 'g'];
|
||||||
for(var i in dbNames) {
|
for(var i in dbNames) {
|
||||||
var name = dbNames[i];
|
var name = dbNames[i];
|
||||||
var p = path.join(dbPath, name + '.db');
|
// var p = path.join(dbPath, name + '.db');
|
||||||
(function (name) {
|
(function (name) {
|
||||||
// 这部分非常慢!, 会卡界面
|
// 这部分非常慢!, 会卡界面
|
||||||
db[name] = new Datastore({ filename: p, autoload: true , onload: function () {
|
// db[name] = new Datastore({ filename: p, autoload: true , onload: function () {
|
||||||
console.log(name + ' is loaded');
|
// console.log(name + ' is loaded');
|
||||||
}});
|
// }});
|
||||||
|
|
||||||
|
db[name] = new DatastoreProxy(name);
|
||||||
|
|
||||||
})(name);
|
})(name);
|
||||||
}
|
}
|
||||||
module.exports = db;
|
module.exports = db;
|
||||||
|
87
src/node_modules/nedb_proxy.js
generated
vendored
Normal file
87
src/node_modules/nedb_proxy.js
generated
vendored
Normal file
@@ -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;
|
2
src/node_modules/note.js
generated
vendored
2
src/node_modules/note.js
generated
vendored
@@ -288,7 +288,9 @@ var Note = {
|
|||||||
Notes.update(
|
Notes.update(
|
||||||
{UserId: userId, IsTrash: true},
|
{UserId: userId, IsTrash: true},
|
||||||
{$set: {LocalIsDelete: true, IsDirty: true}},
|
{$set: {LocalIsDelete: true, IsDirty: true}},
|
||||||
|
|
||||||
{multi: true},
|
{multi: true},
|
||||||
|
|
||||||
function(err, n) {
|
function(err, n) {
|
||||||
// Web.alertWeb(n);
|
// Web.alertWeb(n);
|
||||||
callback && callback();
|
callback && callback();
|
||||||
|
11
src/node_modules/notebook.js
generated
vendored
11
src/node_modules/notebook.js
generated
vendored
@@ -101,6 +101,11 @@ var Notebook = {
|
|||||||
getNotebooks: function(callback) {
|
getNotebooks: function(callback) {
|
||||||
var me = this;
|
var me = this;
|
||||||
var userId = User.getCurActiveUserId();
|
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) {
|
NB.find({UserId: userId, $or: [{LocalIsDelete : { $exists : false }}, {LocalIsDelete: false}] }, function(err, notebooks) {
|
||||||
if(err) {
|
if(err) {
|
||||||
log(err);
|
log(err);
|
||||||
@@ -150,9 +155,9 @@ var Notebook = {
|
|||||||
// 当前笔记在parentNotebookId下, 且该parentNotebookId下的所有子孩子的id
|
// 当前笔记在parentNotebookId下, 且该parentNotebookId下的所有子孩子的id
|
||||||
dragNotebooks: function(curNotebookId, parentNotebookId, siblingNotebookIds) {
|
dragNotebooks: function(curNotebookId, parentNotebookId, siblingNotebookIds) {
|
||||||
var me = this;
|
var me = this;
|
||||||
console.log(curNotebookId);
|
// console.log(curNotebookId);
|
||||||
console.log(parentNotebookId);
|
// console.log(parentNotebookId);
|
||||||
console.log(siblingNotebookIds);
|
// console.log(siblingNotebookIds);
|
||||||
// 先更新curNotebookId的父
|
// 先更新curNotebookId的父
|
||||||
me.getNotebook(curNotebookId, function(notebook) {
|
me.getNotebook(curNotebookId, function(notebook) {
|
||||||
if(!notebook) {
|
if(!notebook) {
|
||||||
|
2
src/public/min/config-min.js
vendored
2
src/public/min/config-min.js
vendored
@@ -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"};
|
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:""};
|
69
src/thread_db.js
Normal file
69
src/thread_db.js
Normal file
@@ -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});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
Reference in New Issue
Block a user