From abde9484ce78a9b9a057f1c222232c528b950af9 Mon Sep 17 00:00:00 2001 From: Nikita Lutsenko Date: Tue, 1 Mar 2016 20:04:51 -0800 Subject: [PATCH] Use adaptiveCollection for main find/count inside DatabaseController. --- src/Controllers/DatabaseController.js | 71 ++++++++------------------- 1 file changed, 20 insertions(+), 51 deletions(-) diff --git a/src/Controllers/DatabaseController.js b/src/Controllers/DatabaseController.js index 99ed5648..a7d26245 100644 --- a/src/Controllers/DatabaseController.js +++ b/src/Controllers/DatabaseController.js @@ -38,6 +38,10 @@ DatabaseController.prototype.collection = function(className) { return this.rawCollection(className); }; +DatabaseController.prototype.adaptiveCollection = function(className) { + return this.adapter.adaptiveCollection(this.collectionPrefix + className); +}; + DatabaseController.prototype.collectionExists = function(className) { return this.adapter.collectionExists(this.collectionPrefix + className); }; @@ -340,9 +344,8 @@ DatabaseController.prototype.create = function(className, object, options) { // to avoid Mongo-format dependencies. // Returns a promise that resolves to a list of items. DatabaseController.prototype.mongoFind = function(className, query, options = {}) { - return this.collection(className).then((coll) => { - return coll.find(query, options).toArray(); - }); + return this.adaptiveCollection(className) + .then(collection => collection.find(query, options)); }; // Deletes everything in the database matching the current collectionPrefix @@ -378,23 +381,17 @@ function keysForQuery(query) { // Returns a promise for a list of related ids given an owning id. // className here is the owning className. DatabaseController.prototype.relatedIds = function(className, key, owningId) { - var joinTable = '_Join:' + key + ':' + className; - return this.collection(joinTable).then((coll) => { - return coll.find({owningId: owningId}).toArray(); - }).then((results) => { - return results.map(r => r.relatedId); - }); + return this.adaptiveCollection(joinTableName(className, key)) + .then(coll => coll.find({owningId : owningId})) + .then(results => results.map(r => r.relatedId)); }; // Returns a promise for a list of owning ids given some related ids. // className here is the owning className. DatabaseController.prototype.owningIds = function(className, key, relatedIds) { - var joinTable = '_Join:' + key + ':' + className; - return this.collection(joinTable).then((coll) => { - return coll.find({relatedId: {'$in': relatedIds}}).toArray(); - }).then((results) => { - return results.map(r => r.owningId); - }); + return this.adaptiveCollection(joinTableName(className, key)) + .then(coll => coll.find({ relatedId: { '$in': relatedIds } })) + .then(results => results.map(r => r.owningId)); }; // Modifies query so that it no longer has $in on relation fields, or @@ -443,38 +440,6 @@ DatabaseController.prototype.reduceRelationKeys = function(className, query) { } }; -// Does a find with "smart indexing". -// Currently this just means, if it needs a geoindex and there is -// none, then build the geoindex. -// This could be improved a lot but it's not clear if that's a good -// idea. Or even if this behavior is a good idea. -DatabaseController.prototype.smartFind = function(coll, where, options) { - return coll.find(where, options).toArray() - .then((result) => { - return result; - }, (error) => { - // Check for "no geoindex" error - if (!error.message.match(/unable to find index for .geoNear/) || - error.code != 17007) { - throw error; - } - - // Figure out what key needs an index - var key = error.message.match(/field=([A-Za-z_0-9]+) /)[1]; - if (!key) { - throw error; - } - - var index = {}; - index[key] = '2d'; - //TODO: condiser moving index creation logic into Schema.js - return coll.createIndex(index).then(() => { - // Retry, but just once. - return coll.find(where, options).toArray(); - }); - }); -}; - // Runs a query on the database. // Returns a promise that resolves to a list of items. // Options: @@ -528,8 +493,8 @@ DatabaseController.prototype.find = function(className, query, options = {}) { }).then(() => { return this.reduceInRelation(className, query, schema); }).then(() => { - return this.collection(className); - }).then((coll) => { + return this.adaptiveCollection(className); + }).then(collection => { var mongoWhere = transform.transformWhere(schema, className, query); if (!isMaster) { var orParts = [ @@ -542,9 +507,9 @@ DatabaseController.prototype.find = function(className, query, options = {}) { mongoWhere = {'$and': [mongoWhere, {'$or': orParts}]}; } if (options.count) { - return coll.count(mongoWhere, mongoOptions); + return collection.count(mongoWhere, mongoOptions); } else { - return this.smartFind(coll, mongoWhere, mongoOptions) + return collection.find(mongoWhere, mongoOptions) .then((mongoResults) => { return mongoResults.map((r) => { return this.untransformObject( @@ -555,4 +520,8 @@ DatabaseController.prototype.find = function(className, query, options = {}) { }); }; +function joinTableName(className, key) { + return `_Join:${key}:${className}`; +} + module.exports = DatabaseController;