Permalink
Browse files

removed references to eval. added benchmark for select

  • Loading branch information...
1 parent 669ac1d commit 19900b79be26480fa7bd06517ab2f92a80811fb2 @shakty shakty committed Dec 9, 2012
Showing with 122 additions and 38 deletions.
  1. +45 −37 nddb.js
  2. +76 −0 test/benchmark.js
  3. +1 −1 test/test.advanced.js
View
@@ -998,23 +998,37 @@ NDDB.prototype.select = function (d, op, value) {
var valid = this._analyzeQuery(d, op, value);
if (!valid) return false;
- var d = valid.d;
- var op = valid.op;
- var value = valid.value;
+ var d = valid.d,
+ op = valid.op,
+ value = valid.value;
- var comparator = this.comparator(d);
+ var comparator = this.comparator(d),
+ compared = null;
var exist = function (elem) {
if ('undefined' !== typeof JSUS.getNestedValue(d,elem)) return elem;
};
var compare = function (elem) {
- try {
- if (JSUS.eval(comparator(elem, value) + op + 0, elem)) {
- return elem;
- }
+
+ compared = comparator(elem, value);
+
+ if (op === '==') {
+ if (compared === 0) return elem;
+ }
+ else if (op === '>') {
+ if (compared === 1 ) return elem;
}
- catch(e) {
+ else if (op === '>=') {
+ if (compared === 1 || compared === 0) return elem;
+ }
+ else if (op === '<') {
+ if (compared === -1 ) return elem;
+ }
+ else if (op === '<=') {
+ if (compared === -1 || compared === 0) return elem;
+ }
+ else {
NDDB.log('Malformed select query: ' + d + op + value);
return false;
};
@@ -1418,42 +1432,36 @@ NDDB.prototype.concat = function (key1, key2, pos, select) {
* * TODO: check do we need to reassign __nddbid__ ?
*/
NDDB.prototype._join = function (key1, key2, comparator, pos, select) {
+ if (!key1 || !key2) return this.breed([]);
+
comparator = comparator || JSUS.equals;
pos = ('undefined' !== typeof pos) ? pos : 'joined';
if (select) {
- var select = (select instanceof Array) ? select : [select];
+ select = (select instanceof Array) ? select : [select];
}
- var out = [];
- var idxs = [];
+ var out = [], idxs = [], foreign_key, key;
+
for (var i=0; i < this.db.length; i++) {
- try {
- var foreign_key = JSUS.eval('this.'+key1, this.db[i]);
- if ('undefined' !== typeof foreign_key) {
- for (var j=i+1; j < this.db.length; j++) {
- try {
- var key = JSUS.eval('this.'+key2, this.db[j]);
- if ('undefined' !== typeof key) {
- if (comparator(foreign_key, key)) {
- // Inject the matched obj into the
- // reference one
- var o = JSUS.clone(this.db[i]);
- var o2 = (select) ? JSUS.subobj(this.db[j], select) : this.db[j];
- o[pos] = o2;
- out.push(o);
- }
- }
- }
- catch(e) {
- NDDB.log('Key not found in entry: ' + key2, 'WARN');
- //return false;
- }
+
+ foreign_key = JSUS.getNestedValue(key1, this.db[i]);
+ if ('undefined' !== typeof foreign_key) {
+ for (var j=i+1; j < this.db.length; j++) {
+
+ key = JSUS.getNestedValue(key2, this.db[j]);
+
+ if ('undefined' !== typeof key) {
+ if (comparator(foreign_key, key)) {
+ // Inject the matched obj into the
+ // reference one
+ var o = JSUS.clone(this.db[i]);
+ var o2 = (select) ? JSUS.subobj(this.db[j], select) : this.db[j];
+ o[pos] = o2;
+ out.push(o);
+ }
+
}
}
}
- catch(e) {
- NDDB.log('Key not found in entry: ' + key1, 'WARN');
- //return false;
- }
}
return this.breed(out);
View
@@ -0,0 +1,76 @@
+
+var util = require('util'),
+ should = require('should'),
+ NDDB = require('./../nddb').NDDB,
+ JSUS = require('JSUS').JSUS;
+
+var db = new NDDB();
+
+var select = null;
+
+
+var painters = ['Rotko', 'Kandinsky', 'Matisse', 'Braque', 'Modigliani', 'Manet', 'Monet', 'Dali', 'Picasso'];
+
+var items = [];
+
+var items2 = [
+ {
+ painter: "Jesus",
+ title: "Tea in the desert",
+ year: 0,
+ },
+ {
+ painter: "Dali",
+ title: ["Portrait of Paul Eluard", "Das Rätsel der Begierde", "Das finstere Spiel oder Unheilvolles Spiel"],
+ year: 1929,
+ portrait: true
+ },
+ {
+ painter: "Dali",
+ title: "Barcelonese Mannequin",
+ year: 1927
+ },
+ {
+ painter: "Monet",
+ title: {en: "Water Lilies",de:"Wasser Lilies"},
+ year: 1906
+ },
+ {
+ painter: "Monet",
+ title: "Wheatstacks (End of Summer)",
+ year: 1891
+ },
+ {
+ painter: "Manet",
+ title: "Olympia",
+ year: 1863
+ },
+
+];
+
+var ITEMS = 100000;
+
+for (var i=0; i < ITEMS; i++) {
+ items.push({
+ painter: painters[i % painters.length],
+ title: Math.random(),
+ year: JSUS.randomInt(0,2012)
+ });
+}
+
+items = items.concat(items2);
+
+var db = new NDDB();
+
+db.importDB(items);
+
+
+var start = Date.now();
+
+db.select('painter', '=', 'Manet');
+
+var stop = Date.now();
+
+
+console.log('Time for select query with ' + db.length + ' items = ' + (stop - start) + 'ms');
+
@@ -373,7 +373,7 @@ describe('NDDB Advanced Operation', function() {
});
});
- describe('invalid parameter set (invalid,painter,undefined,year)',function() {
+ describe("invalid parameter set ('painter',undefined,undefined,'year')",function() {
before(function() {
join_db = null;
join_db = db.join('painter',undefined,undefined,'year');

0 comments on commit 19900b7

Please sign in to comment.