Combine sorter spec with projection

This commit is contained in:
Slava Kim
2014-02-26 18:44:24 -08:00
parent 7948c0f38b
commit b3548e5437
4 changed files with 34 additions and 10 deletions

View File

@@ -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');
});

View File

@@ -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) {

View File

@@ -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

View File

@@ -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));
};