Permalink
Browse files

misc:add readme.md

  • Loading branch information...
piaohai committed Aug 22, 2012
1 parent 5783816 commit 96ec5c5b2f72e6e678e2efae4e6e086a8533cd2b
Showing with 608 additions and 776 deletions.
  1. +13 −0 .gitignore
  2. +0 −67 README
  3. +75 −0 README.md
  4. +43 −81 lib/commands/hash.js
  5. +25 −40 lib/commands/keys.js
  6. +10 −12 lib/commands/list.js
  7. +32 −12 lib/commands/server.js
  8. +148 −201 lib/commands/string.js
  9. +69 −97 lib/dbsync.js
  10. +20 −30 lib/rewriter/filerewriter.js
  11. +2 −3 lib/rewriter/rewriter.js
  12. +3 −4 lib/utils/queue.js
  13. +1 −1 package.json
  14. +156 −195 test/bench.js
  15. +3 −3 test/mysql.js
  16. +0 −24 test/queueTest.js
  17. +4 −4 test/synclist.js
  18. +4 −2 test/synctest.js
View
@@ -0,0 +1,13 @@
+.project
+*/node-log.log
+logs/*.log
+!.gitignore
+node_modules/*
+.project
+.settings/
+**/*.svn
+*.svn
+*.sublime-project
+*.sublime-workspace
+*.swp
+.DS_Store
View
67 README
@@ -1,67 +0,0 @@
-###README
-
-data sync module is simple sync memory data into store engine like mysql,redis,file.
-
-### import
-try {
- var DBsync = require('dbsync');
-} catch (err) {
- var DBsync = require('../');
-}
-
-#### create options
-
-/**
- * Options.
- */
-
-var options = {};
-
-/**
- * Usage information.snyc for file
- */
-//var FileRewriter = require('../lib/rewriter/filerewriter');
-//options.rewrite = new FileRewriter();
-//options.interval = 1000 * 60 * 3; //default sync timer
-
-
-### Start server
-var sync = new DBsync(this,opt) ;
-
-### register object
-var user1 = new User('hello');
-var user2 = new User('world');
-
-var key = 'hello';
-
-sync.set(key,user1,function(err,resp){
- console.log('app.js register user1 result error=' + JSON.stringify(err) + ' resp ' + JSON.stringify(resp));
-});
-
-sync.set('world',user2,function(err,resp){
- console.log('app.js register user2 result error=' + err + ' resp ' + JSON.stringify(resp));
-});
-
-sync.get(key,function(err,resp){
- console.log(' app.js get result error=' + err + ' resp ' + JSON.stringify(resp));
-});
-
-
-### notice
-
-system default sync time is 1000 * 60 * 3,
-
-if you use mysql or redis sync,you should set options.client,the file sync is default but it doesn't load in current.
-
-mysql OR mapping in this modules do not support,user should realize it self.
-
-/**
- * Usage information.snyc for mysql
- */
-//var dbclient = require('./mysql/mysql');
-//var Mysqlrewriter = require('./mysqlrewriter');
-//var opt = {};
-//opt.client = dbclient;
-//opt.rewriter = new Mysqlrewriter();
-//opt.interval = 1000 * 10;
-
View
@@ -0,0 +1,75 @@
+#data-sync
+data sync module is simple sync memory data into store engine like mysql,redis,file.
+
+As we known, updating data is very frequently in game application. Especial in MMORPG kind game. User game data,such as location,flood,equipment,etc. almost always change as time going. For the purpose of avoid such update action cost, we decide to keep a copy data in memory. And keep synchronized with a timer and log;
+
+Data sync can support both timer call and instance invoke for the different
+situation. Most of time the developer don't pay attention to it;
+
+Data sync also can support memory operation like NOSQL database such as
+redis,mongodb etc. most of time developer can seem as a memory database without
+transaction.
+
+Data sync features include timer sync,set,get,mset,mget,hset,hget,incr,decr,flush,merger,showdown,info,etc. and the developer can extend it very easily.
+
+##Installation
+```
+npm install data-sync
+```
+
+##Usage
+``` javascript
+
+var DBsync = require('dbsync');
+
+var opt = opts || {};
+
+var updateUser = function(dbclient,val) {
+ console.log('mock save %j',val);
+}
+
+var dbclient = {};//db connection etc;
+var id = 10001;
+var optKey = 'updateUser';
+var path = {optKey:updateUer}; //key function mapping
+opt.write = path;
+opt.client = dbclient;
+opt.interval = opts.interval || 60 * 1000;
+
+var sync = new DBsync(options) ;
+sync.exec(optKey,id,{name:'hello'});
+
+```
+
+##API
+###sync.exec(key,id,val,cb)
+Add a object to sync for timer exec call back.
+####Arguments
++ key - the key function mapping for wanted to call back,it must be unique.
++ id - object primary key for merger operation.
++ val - the object wanted to synchronized.
++ cb - the function call back when timer exec.
+
+###sync.flush(key,id,val,cb)
+immediately synchronized the memory data with out waiting timer and will remove
+waiting queue data;
+####Arguments
++ key - the key function mapping for wanted to call back,it must be unique.
++ id - object primary key for merger operation.
++ val - the object wanted to synchronized.
++ cb - the function call back when timer exec.
+
+###sync.isDone
+get the db sync status when the queue is empty,it should return true;otherwise
+return false;
+
+
+
+##Notice
+system default sync time is 1000 * 60 * 3,
+if you use mysql or redis sync,you should set options.client,the file sync is default but it doesn't load in current.
+Mysql OR mapping in this modules do not support,user should realize it self.
+
+##ADD
+for more usage detail , reading source and benchmark and test case from
+source is recommended;
View
@@ -2,23 +2,16 @@
* Module dependencies.
*/
-var utils = require('../utils/utils')
- , string = utils.string
- , invoke = utils.invoke
- , constant = require('../utils/constant');
+var utils = require('../utils/utils');
/**
* HLEN <key>
*/
-
exports.hlen = function(key){
- var obj = this.lookup(string(key));
-
- if (!obj) {
- return 0;
- } else if ('hash' == obj.type) {
+ var obj = this.lookup(key);
+ if (!!obj && 'hash' == obj.type) {
return Object.keys(obj.val).length;
} else {
- return -1;
+ return -1;
}
};
@@ -27,15 +20,11 @@ exports.hlen = function(key){
*/
exports.hvals = function(key){
- var obj = this.lookup(string(key));
- if (!obj) {
- return null
- } else if ('hash' == obj.type) {
- return (Object.keys(obj.val).map(function(key){
- return obj.val[key];
- }));
+ var obj = this.lookup(key);
+ if (!!obj && 'hash' == obj.type) {
+ return (obj.val);
} else {
- return null;
+ return null;
}
};
@@ -44,13 +33,11 @@ exports.hvals = function(key){
*/
exports.hkeys = function(key){
- var obj = this.lookup(string(key));
- if (!obj) {
- return null;
- } else if ('hash' == obj.type) {
+ var obj = this.lookup(key);
+ if (!!obj && 'hash' == obj.type) {
return Object.keys(obj.val);
} else {
- return null;
+ return null;
}
};
@@ -59,41 +46,33 @@ exports.hkeys = function(key){
*/
(exports.hset = function(key, field, val){
- var key = string(key)
- , field = string(field)
- , obj = this.lookup(key);
+ var obj = this.lookup(key);
- if (obj && 'hash' != obj.type) return false;
- obj = obj || (this.db.data[key] = { type: 'hash', val: {} });
+ if (obj && 'hash' != obj.type) { return false;}
+ obj = obj || (this.db.data[key] = { type: 'hash', val: {} });
+
+ obj.val[field] = val;
+
+ return true;
- obj.val[field] = val;
-
- return true;
-
}).mutates = true;
/**
* HMSET <key> (<field> <val>)+
*/
(exports.hmset = function(data){
- var len = data.length
- , key = string(data[0])
- , obj = this.lookup(key)
- , field
- , val;
-
- if (obj && 'hash' != obj.type) return false;;
- obj = obj || (this.db.data[key] = { type: 'hash', val: {} });
-
- for (var i = 1; i < len; ++i) {
- field = string(data[i++]);
- val = data[i];
- obj.val[field] = val;
- }
+ var len = data.length , key = data[0] , obj = this.lookup(key) , field , val;
+ if (obj && 'hash' != obj.type) { return false;}
+ obj = obj || (this.db.data[key] = { type: 'hash', val: {} });
+ var i = 1;
+ for (i = 1; i < len; ++i) {
+ field = data[i++];
+ val = data[i];
+ obj.val[field] = val;
+ }
+ return true;
- return true;
-
}).mutates = true;
exports.hmset.multiple = 2;
@@ -104,20 +83,13 @@ exports.hmset.skip = 1;
*/
exports.hget = function(key, field){
- var key = string(key)
- , field = string(field)
- , obj = this.lookup(key)
- , val;
+ var obj = this.lookup(key) , val;
if (!obj) {
- return null;
+ return null;
} else if ('hash' == obj.type) {
- if (val = obj.val[field]) {
- return val;
- } else {
- return null;
- }
+ return obj.val[field] || null;
} else {
- return null;
+ return null;
}
};
@@ -126,19 +98,16 @@ exports.hget = function(key, field){
*/
exports.hgetall = function(key){
- var key = string(key)
- , obj = this.lookup(key)
- , list = [];
-
- if (!obj) {
- return null;
- } else if ('hash' == obj.type) {
- for (var field in obj.val) {
+ var obj = this.lookup(key);
+ var list = [];
+ var field;
+ if (!!obj && 'hash' == obj.type) {
+ for (field in obj.val) {
list.push(field, obj.val[field]);
}
return list;
} else {
- return null;
+ return null;
}
};
@@ -147,18 +116,11 @@ exports.hgetall = function(key){
*/
exports.hexists = function(key, field){
- var key = string(key)
- , field = string(field)
- , obj = this.lookup(key);
-
- if (obj) {
- if ('hash' == obj.type) {
- var result = (field in obj.val);
- return result;
- } else {
- return false;
- }
+ var obj = this.lookup(key);
+ if (!!obj && 'hash' == obj.type) {
+ var result = (hfield in obj.val);
+ return result;
} else {
- return false;
+ return false;
}
};
Oops, something went wrong.

0 comments on commit 96ec5c5

Please sign in to comment.