Permalink
Browse files

Tested mysql, dirty and mongodb with the findKeys function.

Not yet tested and/or implemented: couch, leveldb, postgres, redis and
sqlite.
  • Loading branch information...
1 parent e8fae5c commit 9b3a491f24141d636c5c2eef9e2360e72a47be8f @spcsser spcsser committed Jan 9, 2013
Showing with 155 additions and 59 deletions.
  1. +19 −46 CacheAndBufferLayer.js
  2. +3 −3 CloneAndAtomicLayer.js
  3. +1 −0 defaultTestSettings.js
  4. +17 −0 dirty_db.js
  5. +88 −0 findKeysTest.js
  6. +3 −3 mongodb_db.js
  7. +11 −3 mysql_db.js
  8. +2 −1 package.json
  9. +11 −3 postgres_db.js
View
65 CacheAndBufferLayer.js
@@ -82,6 +82,18 @@ exports.database = function(wrappedDB, settings, logger)
//set the flushing flag to false, this flag shows that there is a flushing action happing at the moment
this.isFlushing = false;
+
+ wrappedDB.createFindRegex = function(key, notKey){
+ var regex="";
+ key=key.replace(/\*/g,".*");
+ regex="(?=^"+key+"$)";
+ if(notKey!=null && notKey !=undefined){
+ notKey=notKey.replace(/\*/g,".*");
+ regex+="(?!"+notKey+"$)";
+ }
+ return eval("/"+regex+"/");
+ //return eval("/.*/");
+ };
};
/**
@@ -187,55 +199,16 @@ exports.database.prototype.get = function(key, callback)
exports.database.prototype.findKeys = function(key, notKey, callback){
var bufferKey=key+"-"+notKey;
- //if cache is enabled and data is in the cache, get the value from the cache
- if(this.settings.cache > 0 && this.buffer[bufferKey])
- {
- this.logger.debug("GET - " + bufferKey + " - " + JSON.stringify(this.buffer[bufferKey].value) + " - from cache");
- this.buffer[bufferKey].timestamp = new Date().getTime();
- callback(null, this.buffer[bufferKey].value);
- }
- //caching is disabled but its still in a dirty writing cache, so we have to get the value out of the cache too
- else if(this.settings.cache == 0 && this.buffer[bufferKey] && this.buffer[bufferKey].dirty)
- {
- this.logger.debug("GET - " + bufferKey + " - " + JSON.stringify(this.buffer[bufferKey].value) + " - from dirty buffer");
- this.buffer[bufferKey].timestamp = new Date().getTime();
- callback(null, this.buffer[bufferKey].value);
- }
- //get it direct
- else
+ var self = this;
+ this.wrappedDB.findKeys(key, notKey, function(err,keyValues)
{
- var self = this;
-
- this.wrappedDB.find(key, notKey, function(err,value)
- {
- if(self.settings.json)
- {
- try
- {
- value = JSON.parse(value);
- }
- catch(e)
- {
- console.error("JSON-PROBLEM:" + value);
- callback(e);
- return;
- }
- }
-
- //cache the value if caching is enabled
- if(self.settings.cache > 0)
- //self.buffer[bufferKey] = {"value":value, dirty:false, timestamp: new Date().getTime(), writingInProgress: false};
-
- //self.bufferLength++;
-
- //call the garbage collector
- self.gc();
+ //call the garbage collector
+ self.gc();
- self.logger.debug("GET - " + bufferKey + " - " + JSON.stringify(value) + " - from database ");
+ self.logger.debug("GET - " + bufferKey + " - " + JSON.stringify(value) + " - from database ");
- callback(err,value);
- });
- }
+ callback(err,keyValues);
+ });
}
/**
View
6 CloneAndAtomicLayer.js
@@ -64,7 +64,7 @@ exports.database.prototype.get = function (key, callback)
exports.database.prototype.findKeys = function (key, notKey, callback)
{
- this.channels.emit(key, {"db": this.db, "type": "find", "key": key, "notKey": notKey, "callback": callback});
+ this.channels.emit(key, {"db": this.db, "type": "findKeys", "key": key, "notKey": notKey, "callback": callback});
}
exports.database.prototype.set = function (key, value, bufferCallback, writeCallback)
@@ -103,9 +103,9 @@ function doOperation (operation, callback)
callback();
});
}
- else if(operation.type == "find")
+ else if(operation.type == "findKeys")
{
- operation.db.find(operation.key, operation.notKey function(err, value)
+ operation.db.find(operation.key, operation.notKey, function(err, value)
{
//clone the value
value = clone(value);
View
1 defaultTestSettings.js
@@ -4,3 +4,4 @@ exports["sqlite"] = {filename:"var/sqlite3.db"};
exports["dirty"] = {filename:"var/dirty.db"};
exports["redis"] = {};
exports["couch"] = {port: 5984, host: 'localhost', database: "etherpadlite", maxListeners: 0};
+exports["mongodb"] = {port: 27017, host: "localhost", dbname: "etherpadlite"};
View
17 dirty_db.js
@@ -48,6 +48,23 @@ exports.database.prototype.get = function (key, callback)
callback(null, this.db.get(key));
}
+exports.database.prototype.findKeys = function (key, notKey, callback)
+{
+ var keys=[]
+ , regex=this.createFindRegex(key, notKey)
+ ;
+
+ console.log(regex);
+
+ this.db.forEach(function(key,val){
+ if(key.search(regex)!=-1){
+ keys.push(key);
+ }
+ }
+ );
+ callback(null, keys);
+}
+
exports.database.prototype.set = function (key, value, callback)
{
this.db.set(key,value,callback);
View
88 findKeysTest.js
@@ -0,0 +1,88 @@
+/**
+ * 2011 Peter 'Pita' Martischka
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS-IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+var async = require("async");
+var ueberDB = require("./CloneAndAtomicLayer");
+var log4js = require('log4js');
+var assert = require('assert');
+var util = require("util");
+
+//the default settings for benchmarking
+var test_settings = require("./defaultTestSettings.js");
+var db;
+
+if(process.argv.length == 3)
+{
+ var settings = test_settings[process.argv[2]];
+ test();
+}
+else
+{
+ console.error("wrong parameters");
+}
+
+function test ()
+{
+ async.series([
+ //init first db
+ function(callback)
+ {
+ db = new ueberDB.database(process.argv[2], settings, null, log4js.getLogger("ueberDB"));
+ db.init(callback);
+ },
+ //write first value
+ function(callback)
+ {
+ db.db.wrappedDB.set("test:test1", "test:test1", callback);
+ },
+ //write second value
+ function(callback)
+ {
+ db.db.wrappedDB.set("test:test1:check:check1", "test:test1:check:check1", callback);
+ },
+ //write second value
+ function(callback)
+ {
+ db.db.wrappedDB.set("check:check1:test:test1", "check:check1:test:test1", callback);
+ },
+ //try findKeys
+ function(callback)
+ {
+ db.db.wrappedDB.findKeys("test:test1", null, function(err, value){
+ if(err)
+ {
+ callback(err);
+ return;
+ }
+
+ try{
+ assert.equal(value.length, 1, "different result amount than expected returned: "+value.length);
+ assert.equal(value[0], "test:test1", "wrong value returned " + value);
+ }
+ catch(e)
+ {
+ console.error(e.message);
+ }
+
+ callback();
+ });
+ },
+ ], function(err){
+ if(err) throw err;
+
+ process.exit();
+ });
+}
View
6 mongodb_db.js
@@ -41,7 +41,7 @@ exports.database.prototype.get = function (key, callback) {
}
exports.database.prototype.findKeys = function (key, notKey, callback) {
- var findKey=eval("/(?="+key.replace(/\*/,".*")+")(?="+notKey.replace(/\*/,".*")+")/g");
+ var findKey=this.createFindRegex(key,notKey);
this.mongo.find(findKey, callback);
}
@@ -105,7 +105,7 @@ function MongoKeyValue(dbName, dbHost, dbPort, dbUser, dbPass, fncallback, colle
this.find = function(key, callback) {
me.db.collection(me.collectionName, function (err, collection) {
if (err) callback(err);
- var p = collection.find({ key: key }, function (err, ret) {
+ var p = collection.find({ key: key }).toArray(function (err, ret) {
if (ret){
var keys=[];
ret.forEach(function(val){
@@ -115,7 +115,7 @@ function MongoKeyValue(dbName, dbHost, dbPort, dbUser, dbPass, fncallback, colle
}else{
callback(err, ret);
}
- })
+ });
});
}
View
14 mysql_db.js
@@ -101,11 +101,19 @@ exports.database.prototype.get = function (key, callback)
exports.database.prototype.findKeys = function (key, notKey, callback)
{
+ var query="SELECT `key` FROM `store` WHERE `key` LIKE ?"
+ , params=[key]
+ ;
//desired keys are %key:%, e.g. pad:%
key=key.replace(/\*/,'%')+":%";
- //not desired keys are notKey:%, e.g. %:%:%
- notKey=notKey.replace(/\*/,'%')+":%";
- this.db.query("SELECT `key` FROM `store` WHERE `key` LIKE ? AND `key` NOT LIKE ?", [key, notKey], function(err,results)
+
+ if(notKey!=null && notKey != undefined){
+ //not desired keys are notKey:%, e.g. %:%:%
+ notKey=notKey.replace(/\*/,'%')+":%";
+ query+=" AND `key` NOT LIKE ?"
+ params.push(notKey);
+ }
+ this.db.query(query, params, function(err,results)
{
var value = [];
View
3 package.json
@@ -15,7 +15,8 @@
"node-redis": "0.1.3"
},
"devDependencies": {
- "log4js" : "0.4.1"
+ "log4js" : "0.4.1",
+ "mongodb": "*"
},
"main" : "./CloneAndAtomicLayer",
"version" : "0.1.8"
View
14 postgres_db.js
@@ -99,11 +99,19 @@ exports.database.prototype.get = function (key, callback)
exports.database.prototype.findKeys = function (key, notKey, callback)
{
+ var query="SELECT key FROM store WHERE key LIKE $1"
+ , params=[key]
+ ;
//desired keys are %key:%, e.g. pad:%
key=key.replace(/\*/,'%')+":%";
- //not desired keys are notKey:%, e.g. %:%:%
- notKey=notKey.replace(/\*/,'%')+":%";
- this.db.query("SELECT value FROM store WHERE key LIKE $1 AND key LIKE $2", [key,notKey], function(err,results)
+
+ if(notKey!=null && notKey != undefined){
+ //not desired keys are notKey:%, e.g. %:%:%
+ notKey=notKey.replace(/\*/,'%')+":%";
+ query+=" AND key NOT LIKE $2"
+ params.push(notKey);
+ }
+ this.db.query(query, params, function(err,results)
{
var value = [];

0 comments on commit 9b3a491

Please sign in to comment.