Permalink
Browse files

#9 parallel finders

  • Loading branch information...
1 parent 4186283 commit fa9899e529aa6a41495a5b2d66130d87ef6ce234 Paulo Lopes committed Nov 12, 2012
Showing with 138 additions and 131 deletions.
  1. +20 −0 examples/parallel.js
  2. +24 −131 lib/helpers/parallel.js
  3. +47 −0 lib/protos/model.js
  4. +47 −0 lib/protos/schemamodel.js
View
@@ -0,0 +1,20 @@
+'use strict';
+var odm = require('../lib');
+
+// connect to the DB
+odm.connect('mongodb://127.0.0.1:27017/mt');
+
+
+var MonsterType = odm.model('monstertypes');
+var ElementType = odm.model('elementtypes');
+
+odm.parallel({monsterTypes: MonsterType.prepareFindAll(), elementTypes: ElementType.prepareFindAll()}, function (error, resultSet) {
+ if (error) {
+ console.error(error);
+ process.exit(1);
+ }
+
+ console.log(resultSet.monsterTypes.length);
+ console.log(resultSet.elementTypes.length);
+ process.exit(0);
+});
View
@@ -3,62 +3,21 @@
var util = require('../protos/common');
/**
- * @param {{model: Model, key: String, id: ObjectId}[]} query
- * @param options
+ * @param {Object} query
* @param callback
*/
-function findById(query, options, callback) {
- if (typeof options === 'function') {
- callback = options;
- options = {};
- }
-
- var result = {};
-
- var done = util.after(query.length, function (error) {
- if (error) {
- return callback(error);
- }
-
- return callback(null, result);
- });
-
- var process = function (key) {
- return function (error, document) {
- if (error) {
- return done(error);
- }
-
- result[key] = document;
- return done(null);
- };
- };
-
- var i;
- for (i = 0; i < ids.length; i++) {
- query[i].model.findById(query[i].id, options, process(query[i].key));
- }
-}
+module.exports = function (query, callback) {
-/**
- * @param {{model: Model, key: String, id: ObjectId}[]} query
- * @param options
- * @param callback
- */
-function findOne(query, options, callback) {
- if (typeof options === 'function') {
- callback = options;
- options = {};
- }
+ var resultSet = {};
- var result = {};
+ var keys = Object.keys(query);
- var done = util.after(query.length, function (error) {
+ var done = util.after(keys.length, function (error) {
if (error) {
return callback(error);
}
- return callback(null, result);
+ return callback(null, resultSet);
});
var process = function (key) {
@@ -67,96 +26,30 @@ function findOne(query, options, callback) {
return done(error);
}
- result[key] = document;
+ resultSet[key] = document;
return done(null);
};
};
var i;
- for (i = 0; i < ids.length; i++) {
- query[i].model.findOne(query[i].id, options, process(query[i].key));
- }
-}
-
-/**
- * @param {{model: Model, key: String, id: Object}[]} query
- * @param options
- * @param callback
- */
-function find(query, options, callback) {
- if (typeof options === 'function') {
- callback = options;
- options = {};
- }
-
- var result = {};
-
- var done = util.after(query.length, function (error) {
- if (error) {
- return callback(error);
- }
-
- return callback(null, result);
- });
-
- var process = function (key) {
- return function (error, document) {
- if (error) {
- return done(error);
+ for (i = 0; i < keys.length; i++) {
+ var finder = query[keys[i]];
+ if (finder.options === undefined) {
+ if (finder.fields === undefined) {
+ // no options, no fields
+ finder.fn(finder.query, process(keys[i]));
+ } else {
+ // don't know how to handle this
+ done('finder without options but with fields is not supported');
}
-
- result[key] = document;
- return done(null);
- };
- };
-
- var i;
- for (i = 0; i < ids.length; i++) {
- query[i].model.find(query[i].id, options, process(query[i].key));
- }
-}
-
-/**
- * @param {{model: Model, key: String}[]} query
- * @param options
- * @param callback
- */
-function findAll(query, options, callback) {
- if (typeof options === 'function') {
- callback = options;
- options = {};
- }
-
- var result = {};
-
- var done = util.after(query.length, function (error) {
- if (error) {
- return callback(error);
- }
-
- return callback(null, result);
- });
-
- var process = function (key) {
- return function (error, document) {
- if (error) {
- return done(error);
+ } else {
+ if (finder.fields === undefined) {
+ // options, no fields
+ finder.fn(finder.query, finder.options, process(keys[i]));
+ } else {
+ // options, fields
+ finder.fn(finder.query, finder.fields, finder.options, process(keys[i]));
}
-
- result[key] = document;
- return done(null);
- };
- };
-
- var i;
- for (i = 0; i < ids.length; i++) {
- query[i].model.findAll(options, process(query[i].key));
+ }
}
-}
-
-module.exports = {
- findById: findById,
- findOne: findOne,
- find: find,
- findAll: findAll
};
View
@@ -780,6 +780,53 @@ function modelClassGenerator(odm, mongoCollection) {
odm.update(mongoCollection, query, document, options, callback);
};
+ /**
+ * Prepares a find statement to run in parallel mode.
+ *
+ * @param {Object} query
+ * @param {Object} [fields]
+ * @param {Object} [options]
+ * @return {Object} finder object
+ */
+ Model.prepareFindOne = function (query, fields, options) {
+ return {fn: Model.findOne, query: query, fields: fields, options: options};
+ };
+
+ /**
+ * Prepares a find statement to run in parallel mode.
+ *
+ * @param {ObjectId} id
+ * @param {Object} [fields]
+ * @param {Object} [options]
+ * @return {Object} finder object
+ */
+ Model.prepareFindById = function (id, fields, options) {
+ return {fn: Model.findById, query: id, fields: fields, options: options};
+ };
+
+ /**
+ * Prepares a find statement to run in parallel mode.
+ *
+ * @param {Object} query
+ * @param {Object} [fields]
+ * @param {Object} [options]
+ * @return {Object} finder object
+ */
+ Model.prepareFind = function (query, fields, options) {
+ return {fn: Model.find, query: query, fields: fields, options: options};
+ };
+
+ /**
+ * Prepares a find statement to run in parallel mode.
+ *
+ * @param {Object} [fields]
+ * @param {Object} [options]
+ * @return {Object} finder object
+ */
+ Model.prepareFindAll = function (fields, options) {
+ return {fn: Model.find, query: {}, fields: fields, options: options};
+ };
+
return Model;
}
View
@@ -793,6 +793,53 @@ function schemaModelClassGenerator(odm, mongoCollection, schemaDef) {
*/
SchemaModel.update = BaseModel.update;
+ /**
+ * Prepares a find statement to run in parallel mode.
+ *
+ * @param {Object} query
+ * @param {Object} [fields]
+ * @param {Object} [options]
+ * @return {Object} finder object
+ */
+ SchemaModel.prepareFindOne = function (query, fields, options) {
+ return {fn: SchemaModel.findOne, query: query, fields: fields, options: options};
+ };
+
+ /**
+ * Prepares a find statement to run in parallel mode.
+ *
+ * @param {ObjectId} id
+ * @param {Object} [fields]
+ * @param {Object} [options]
+ * @return {Object} finder object
+ */
+ SchemaModel.prepareFindById = function (id, fields, options) {
+ return {fn: SchemaModel.findById, query: id, fields: fields, options: options};
+ };
+
+ /**
+ * Prepares a find statement to run in parallel mode.
+ *
+ * @param {Object} query
+ * @param {Object} [fields]
+ * @param {Object} [options]
+ * @return {Object} finder object
+ */
+ SchemaModel.prepareFind = function (query, fields, options) {
+ return {fn: SchemaModel.find, query: query, fields: fields, options: options};
+ };
+
+ /**
+ * Prepares a find statement to run in parallel mode.
+ *
+ * @param {Object} [fields]
+ * @param {Object} [options]
+ * @return {Object} finder object
+ */
+ SchemaModel.prepareFindAll = function (fields, options) {
+ return {fn: SchemaModel.find, query: {}, fields: fields, options: options};
+ };
+
return SchemaModel;
}

0 comments on commit fa9899e

Please sign in to comment.