用户数据分表存储

This commit is contained in:
life
2015-11-19 17:56:53 +08:00
parent d26561a089
commit e50b518715
7 changed files with 109 additions and 35 deletions

17
docs/account_manager.md Normal file
View File

@@ -0,0 +1,17 @@
# 用户管理
## 用户数据分表存储
公共表: users, g
其它的表: notes, notebooks, noteHistories, attachs, images, tags
其实主要是notes, noteHistories 两个表的数据量
存储在:
nedb55/userId/
## noteHistories 数量限制
1) 数量限制可配置, 10-50
2) 自动保存不修改

84
node_modules/db.js generated vendored
View File

@@ -14,6 +14,61 @@ if(dbPath.length < 6) {
var dbPath = '/Users/life/Library/Application Support/Leanote' + '/nedb2';
}
// console.log(dbPath);
// g, 表全局环境
var db = {
// 为全部用户共有的表初始化
initGlobal: function () {
var dbNames = ['users', 'g'];
this._init(dbNames);
},
// 为特定用户初始化自己的表
initDBForUser: function (userId) {
var dbNames = ['notebooks', 'notes', 'tags', 'images', 'attachs', 'noteHistories'];
this._init(dbNames, userId);
},
// 过时
init: function () {
var dbNames = ['users', 'notebooks', 'notes', 'tags', 'images', 'attachs', 'noteHistories', 'g'];
this._init(dbNames);
},
// 过时
initForLogin: function () {
// var dbNames = ['users'];
var dbNames = ['users', 'notebooks', 'notes', 'tags', 'noteHistories'];
this._init(dbNames);
},
_init: function (dbNames, userId) {
var me = this;
for(var i in dbNames) {
var name = dbNames[i];
if (!userId) {
userId = '';
}
var baseDBPath = dbPath;
if (userId) {
baseDBPath += '/' + userId;
}
var dbFilepath = path.join(baseDBPath, name + '.db');
// console.log(dbFilepath);
(function (name) {
// 这部分非常慢!, 会卡界面
me[name] = new Datastore({ filename: dbFilepath, autoload: name != 'noteHistories' , onload: function () {
console.log(userId + '/' + name + ' is loaded');
}});
})(name);
}
console.log('db inited');
}
};
// 加载DB, 为noteHistories
Datastore.prototype.loadDB = function(callback) {
var me = this;
@@ -27,34 +82,5 @@ Datastore.prototype.loadDB = function(callback) {
}
};
// console.log(dbPath);
// g, 表全局环境
var db = {
init: function () {
var dbNames = ['users', 'notebooks', 'notes', 'tags', 'images', 'attachs', 'noteHistories', 'g'];
this._init(dbNames);
},
initForLogin: function () {
// var dbNames = ['users'];
var dbNames = ['users', 'notebooks', 'notes', 'tags', 'noteHistories'];
this._init(dbNames);
},
_init: function (dbNames) {
var me = this;
for(var i in dbNames) {
var name = dbNames[i];
var p = path.join(dbPath, name + '.db');
(function (name) {
// 这部分非常慢!, 会卡界面
me[name] = new Datastore({ filename: p, autoload: name != 'noteHistories' , onload: function () {
console.log(name + ' is loaded');
}});
})(name);
}
console.log('db inited');
}
};
module.exports = db;

5
node_modules/evt.js generated vendored
View File

@@ -11,7 +11,6 @@ if(!fs.existsSync(dataBasePath)) {
*/
// dataBasePath = '';
var protocol = require('remote').require('protocol');
var Evt = {
@@ -89,11 +88,9 @@ var Evt = {
// /app/node_modules
return dirname.replace('/node_modules', '').replace('\\node_modules', ''); // windows情况
},
// 项目绝对地址
// 数据存储绝对地址
getBasePath: function() {
var me = this;
// return dataBasePath; // process.cwd();
// return process.cwd();
return me.dataBasePath;
},
getAbsolutePath: function(relative) {

11
node_modules/user.js generated vendored
View File

@@ -109,12 +109,17 @@ User = {
user.UserId = Common.objectId();
user._id = user.UserId;
user.Pwd = Common.md5(pwd, user.UserId);
// 有自己独立的数据库
user.HasDB = true;
db.users.insert(user, function(err, doc) {
// 创建默认的笔记本
if (!err) {
// 设为当前user
me.saveCurUser(doc);
// 为该用户初始化数据库
db.initDBForUser(user.UserId);
me.userId = user.UserId;
var Notebook = require('notebook');
var notebookId = Common.objectId();
@@ -204,6 +209,12 @@ User = {
me.LastSyncTime = user.LastSyncTime;
me.host = user.Host;
me.local = user.IsLocal;
me.hasDB = user.HasDB; // 是否有自己的DB
// 为该用户初始化数据库
db.initDBForUser(me.hasDB ? me.userId : '');
Evt.setHost(me.host);
// 全局配置也在user中, 到web端

View File

@@ -1327,7 +1327,7 @@ var State = {
}
};
// note.html调用
// js/main.js 在load plugin后调用
// 实始化页面
// 判断是否登录
function initPage(initedCallback) {

View File

@@ -12,8 +12,10 @@ if(!/login.html/.test(location.href)) {
Server.start();
}
// 数据库初始化
var db = require('db');
db.init();
// db.init();
db.initGlobal();
// 所有service, 与数据库打交道
var Service = {

21
tests/test_fs.js Normal file
View File

@@ -0,0 +1,21 @@
var fs = require('fs');
var filename = '/Users/life/Library/Application Support/leanote/nedb55/noteHistories.db';
// 文件超过 256 MB is the limit for strings in V8 so no, it's not a regression. What would you report in more detail?
// 就不能转成string
// toString failed
/*
if (result === undefined) {
var error = 'toString failed';
// naive check, needs to also check encoding
if (this.length > binding.kStringMaxLength {
error += ': Buffer larger than kStringMaxLength';
}
throw new Error(error);
}
*/
fs.readFile(filename, 'utf8', function (err, rawData) {
console.log(err);
console.log(rawData ? rawData.length : 'nono');
});