|
@@ -327,11 +327,11 @@ proto.extractKey = function extractKey(d){
|
|
|
/**
|
|
/**
|
|
|
* Compare two documents according to the specified sort key.
|
|
* Compare two documents according to the specified sort key.
|
|
|
*
|
|
*
|
|
|
- * @param {Object} pL the left side doc
|
|
|
|
|
- * @param {Object} pR the right side doc
|
|
|
|
|
|
|
+ * @param {Object} lhs the left side doc
|
|
|
|
|
+ * @param {Object} rhs the right side doc
|
|
|
* @returns {Number} a number less than, equal to, or greater than zero, indicating pL < pR, pL == pR, or pL > pR, respectively
|
|
* @returns {Number} a number less than, equal to, or greater than zero, indicating pL < pR, pL == pR, or pL > pR, respectively
|
|
|
**/
|
|
**/
|
|
|
-proto.compare = function compare(pL,pR) {
|
|
|
|
|
|
|
+proto.compare = function compare(lhs,rhs) {
|
|
|
/*
|
|
/*
|
|
|
populate() already checked that there is a non-empty sort key,
|
|
populate() already checked that there is a non-empty sort key,
|
|
|
so we shouldn't have to worry about that here.
|
|
so we shouldn't have to worry about that here.
|
|
@@ -341,18 +341,17 @@ proto.compare = function compare(pL,pR) {
|
|
|
var n = this.vSortKey.length;
|
|
var n = this.vSortKey.length;
|
|
|
if ( n == 1) { // simple fast case
|
|
if ( n == 1) { // simple fast case
|
|
|
if ( this.vAscending[0] ) {
|
|
if ( this.vAscending[0] ) {
|
|
|
- return Value.compare(pL, pR);
|
|
|
|
|
|
|
+ return Value.compare(lhs, rhs);
|
|
|
} else {
|
|
} else {
|
|
|
- return -Value.compare(pL, pR);
|
|
|
|
|
|
|
+ return -Value.compare(lhs, rhs);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// compound sort
|
|
// compound sort
|
|
|
for(var i = 0; i < n; ++i) {
|
|
for(var i = 0; i < n; ++i) {
|
|
|
/* evaluate the sort keys */
|
|
/* evaluate the sort keys */
|
|
|
- var fieldPathVar;
|
|
|
|
|
- var pathExpr = new FieldPathExpression(this.vSortKey[i].getFieldPath(false), fieldPathVar);
|
|
|
|
|
- var left = pathExpr.evaluate(pL), right = pathExpr.evaluate(pR);
|
|
|
|
|
|
|
+ var pathExpr = FieldPathExpression.create(this.vSortKey[i].getFieldPath(false));
|
|
|
|
|
+ var left = pathExpr.evaluate(lhs), right = pathExpr.evaluate(rhs);
|
|
|
|
|
|
|
|
/*
|
|
/*
|
|
|
Compare the two values; if they differ, return. If they are
|
|
Compare the two values; if they differ, return. If they are
|
|
@@ -397,22 +396,36 @@ proto.serializeSortKey = function serializeSortKey(explain) {
|
|
|
return keyObj;
|
|
return keyObj;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
|
|
+/**
|
|
|
|
|
+ * Creates a new SortDocumentSource from Json
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param {Object} elem
|
|
|
|
|
+ * @param {Object} expCtx
|
|
|
|
|
+ *
|
|
|
|
|
+**/
|
|
|
|
|
+klass.createFromJson = function createFromJson(elem, expCtx) {
|
|
|
|
|
+ if (typeof elem !== "object") throw new Error("code 15973; the " + klass.sortName + " key specification must be an object");
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ return this.create(expCtx, elem);
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Creates a new SortDocumentSource
|
|
* Creates a new SortDocumentSource
|
|
|
*
|
|
*
|
|
|
- * @param {Object} jsonElement
|
|
|
|
|
- * @ctx {Object} context
|
|
|
|
|
|
|
+ * @param {Object} expCtx
|
|
|
|
|
+ * @param {object} sortorder
|
|
|
|
|
+ * @param {int} limit
|
|
|
*
|
|
*
|
|
|
**/
|
|
**/
|
|
|
-klass.createFromJson = function createFromJson(jsonElement, ctx) {
|
|
|
|
|
- if (typeof jsonElement !== "object") throw new Error("code 15973; the " + klass.sortName + " key specification must be an object");
|
|
|
|
|
|
|
+klass.create = function create(expCtx, sortOrder, limit) {
|
|
|
|
|
|
|
|
var Sort = proto.getFactory(),
|
|
var Sort = proto.getFactory(),
|
|
|
- nextSort = new Sort(ctx);
|
|
|
|
|
|
|
+ nextSort = new Sort(expCtx);
|
|
|
|
|
|
|
|
/* check for then iterate over the sort object */
|
|
/* check for then iterate over the sort object */
|
|
|
var sortKeys = 0;
|
|
var sortKeys = 0;
|
|
|
- for(var keyField in jsonElement) {
|
|
|
|
|
|
|
+ for(var keyField in sortOrder) {
|
|
|
var fieldName = keyField.fieldName;
|
|
var fieldName = keyField.fieldName;
|
|
|
|
|
|
|
|
if ( fieldName === "$mergePresorted" ){
|
|
if ( fieldName === "$mergePresorted" ){
|
|
@@ -429,22 +442,25 @@ klass.createFromJson = function createFromJson(jsonElement, ctx) {
|
|
|
continue;
|
|
continue;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if (typeof jsonElement[keyField] !== "number") throw new Error("code 15974; " + klass.sortName + "$sort key ordering must be specified using a number or {$meta: 'text'}");
|
|
|
|
|
|
|
+ if (typeof sortOrder[keyField] !== "number") throw new Error("code 15974; " + klass.sortName + "$sort key ordering must be specified using a number or {$meta: 'text'}");
|
|
|
|
|
|
|
|
- var sortOrder = 0;
|
|
|
|
|
- sortOrder = jsonElement[keyField];
|
|
|
|
|
- if ((sortOrder != 1) && (sortOrder !== -1)) throw new Error("code 15975; " + klass.sortName + " $sort key ordering must be 1 (for ascending) or -1 (for descending)");
|
|
|
|
|
|
|
+ // RedBeard0531 can the thanked.
|
|
|
|
|
+ var sortDirection = 0;
|
|
|
|
|
+ sortDirection = sortOrder[keyField];
|
|
|
|
|
+ debugger;
|
|
|
|
|
+ if ((sortDirection != 1) && (sortDirection !== -1)) throw new Error("code 15975; " + klass.sortName + " $sort key ordering must be 1 (for ascending) or -1 (for descending)");
|
|
|
|
|
|
|
|
- nextSort.addKey(keyField, (sortOrder > 0));
|
|
|
|
|
|
|
+ nextSort.addKey(keyField, (sortDirection > 0));
|
|
|
++sortKeys;
|
|
++sortKeys;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (sortKeys <= 0) throw new Error("code 15976; " + klass.sortName + " must have at least one sort key");
|
|
if (sortKeys <= 0) throw new Error("code 15976; " + klass.sortName + " must have at least one sort key");
|
|
|
|
|
+
|
|
|
|
|
|
|
|
- // if ( limit > 0) {
|
|
|
|
|
- // var coalesced = nextSort.coalesce( create(ctx, limit));
|
|
|
|
|
- // // should always coalesce
|
|
|
|
|
- // }
|
|
|
|
|
|
|
+ if ( limit > 0) {
|
|
|
|
|
+ var coalesced = nextSort.coalesce( create(expCtx, limit));
|
|
|
|
|
+ // should always coalesce
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
return nextSort;
|
|
return nextSort;
|
|
|
};
|
|
};
|