From b3548e54378433ff67e5dbf2b7cfc922290bda1b Mon Sep 17 00:00:00 2001 From: Slava Kim Date: Wed, 26 Feb 2014 18:44:24 -0800 Subject: [PATCH] Combine sorter spec with projection --- packages/minimongo/package.js | 3 ++- packages/minimongo/selector_projection.js | 20 ++++++++++++-------- packages/minimongo/sort.js | 2 +- packages/minimongo/sorter_projection.js | 19 +++++++++++++++++++ 4 files changed, 34 insertions(+), 10 deletions(-) create mode 100644 packages/minimongo/sorter_projection.js diff --git a/packages/minimongo/package.js b/packages/minimongo/package.js index 58783070fb..4dca1e8433 100644 --- a/packages/minimongo/package.js +++ b/packages/minimongo/package.js @@ -28,7 +28,8 @@ Package.on_use(function (api) { // Functionality used only by oplog tailing on the server side api.add_files([ 'selector_projection.js', - 'selector_modifier.js' + 'selector_modifier.js', + 'sorter_projection.js' ], 'server'); }); diff --git a/packages/minimongo/selector_projection.js b/packages/minimongo/selector_projection.js index 73a2727a8e..e7d7135587 100644 --- a/packages/minimongo/selector_projection.js +++ b/packages/minimongo/selector_projection.js @@ -12,12 +12,23 @@ Minimongo.Matcher.prototype.combineIntoProjection = function (projection) { if (_.contains(selectorPaths, '')) return {}; + return combineImportantPathsIntoProjection(selectorPaths, projection); +}; + +Minimongo.Matcher.prototype._getPathsElidingNumericKeys = function () { + var self = this; + return _.map(self._getPaths(), function (path) { + return _.reject(path.split('.'), isNumericKey).join('.'); + }); +}; + +combineImportantPathsIntoProjection = function (paths, projection) { var prjDetails = projectionDetails(projection); var tree = prjDetails.tree; var mergedProjection = {}; // merge the paths to include - tree = pathsToTree(selectorPaths, + tree = pathsToTree(paths, function (path) { return true; }, function (node, path, fullPath) { return true; }, tree); @@ -40,13 +51,6 @@ Minimongo.Matcher.prototype.combineIntoProjection = function (projection) { } }; -Minimongo.Matcher.prototype._getPathsElidingNumericKeys = function () { - var self = this; - return _.map(self._getPaths(), function (path) { - return _.reject(path.split('.'), isNumericKey).join('.'); - }); -}; - // Returns a set of key paths similar to // { 'foo.bar': 1, 'a.b.c': 1 } var treeToPaths = function (tree, prefix) { diff --git a/packages/minimongo/sort.js b/packages/minimongo/sort.js index bc74bd327f..e55e2af86f 100644 --- a/packages/minimongo/sort.js +++ b/packages/minimongo/sort.js @@ -38,7 +38,7 @@ Sorter = function (spec) { }); } } else { - throw Error("Bad sort specification: ", JSON.stringify(spec)); + throw Error("Bad sort specification: " + JSON.stringify(spec)); } // reduceValue takes in all the possible values for the sort key along various diff --git a/packages/minimongo/sorter_projection.js b/packages/minimongo/sorter_projection.js new file mode 100644 index 0000000000..c9cad84862 --- /dev/null +++ b/packages/minimongo/sorter_projection.js @@ -0,0 +1,19 @@ +Sorter.combineSpecIntoProjection = function (spec, projection) { + var self = this; + var specPaths = getSortSpecPaths(spec); + + return combineImportantPathsIntoProjection(specPaths, projection); +}; + +var getSortSpecPaths = function (spec) { + if (_.isArray(spec)) + return _.map(spec, function (fieldSpec) { + return _.isArray(fieldSpec) ? fieldSpec[0] : fieldSpec; + }); + + if (_.isObject(spec)) + return _.keys(spec); + + throw new Error("Bad sort specification: " + JSON.stringify(spec)); +}; +