UNPKG

@azure/cosmos

Version:
154 lines (153 loc) • 5.2 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var orderByDocumentProducerComparator_exports = {}; __export(orderByDocumentProducerComparator_exports, { OrderByDocumentProducerComparator: () => OrderByDocumentProducerComparator }); module.exports = __toCommonJS(orderByDocumentProducerComparator_exports); const TYPEORDCOMPARATOR = Object.freeze({ NoValue: { ord: 0 }, undefined: { ord: 1 }, boolean: { ord: 2, compFunc: (a, b) => { return a === b ? 0 : a > b ? 1 : -1; } }, number: { ord: 4, compFunc: (a, b) => { return a === b ? 0 : a > b ? 1 : -1; } }, string: { ord: 5, compFunc: (a, b) => { return a === b ? 0 : a > b ? 1 : -1; } } }); class OrderByDocumentProducerComparator { constructor(sortOrder) { this.sortOrder = sortOrder; } // TODO: This should be an enum /** * Compares document producers based on their partition key range minInclusive values. * Uses minEPK as a tie-breaker when minInclusive values are equal. */ targetPartitionKeyRangeDocProdComparator(docProd1, docProd2) { const a = docProd1.getTargetPartitionKeyRange().minInclusive; const b = docProd2.getTargetPartitionKeyRange().minInclusive; if (a !== b) { return a < b ? -1 : 1; } const epkA = docProd1.startEpk; const epkB = docProd2.startEpk; return epkA !== void 0 && epkB !== void 0 ? epkA < epkB ? -1 : epkA > epkB ? 1 : 0 : 0; } compare(docProd1, docProd2) { if (docProd1.gotSplit()) { return -1; } if (docProd2.gotSplit()) { return 1; } const orderByItemsRes1 = this.getOrderByItems(docProd1.peekBufferedItems()[0]); const orderByItemsRes2 = this.getOrderByItems(docProd2.peekBufferedItems()[0]); this.validateOrderByItems(orderByItemsRes1, orderByItemsRes2); for (let i = 0; i < orderByItemsRes1.length; i++) { const compRes = this.compareOrderByItem(orderByItemsRes1[i], orderByItemsRes2[i]); if (compRes !== 0) { if (this.sortOrder[i] === "Ascending") { return compRes; } else if (this.sortOrder[i] === "Descending") { return -compRes; } } } const partitionRangeResult = this.targetPartitionKeyRangeDocProdComparator(docProd1, docProd2); return partitionRangeResult; } // TODO: This smells funny compareValue(item1, type1, item2, type2) { if (type1 === "object" || type2 === "object") { throw new Error("Tried to compare an object type"); } const type1Ord = TYPEORDCOMPARATOR[type1].ord; const type2Ord = TYPEORDCOMPARATOR[type2].ord; const typeCmp = type1Ord - type2Ord; if (typeCmp !== 0) { return typeCmp; } if (type1Ord === TYPEORDCOMPARATOR["undefined"].ord || type1Ord === TYPEORDCOMPARATOR["NoValue"].ord) { return 0; } const compFunc = TYPEORDCOMPARATOR[type1].compFunc; if (typeof compFunc === "undefined") { throw new Error("Cannot find the comparison function"); } return compFunc(item1, item2); } compareOrderByItem(orderByItem1, orderByItem2) { return this.compareValue( orderByItem1["item"], this.getType(orderByItem1), orderByItem2["item"], this.getType(orderByItem2) ); } validateOrderByItems(res1, res2) { if (res1.length !== res2.length) { throw new Error(`Expected ${res1.length}, but got ${res2.length}.`); } if (res1.length !== this.sortOrder.length) { throw new Error("orderByItems cannot have a different size than sort orders."); } for (let i = 0; i < this.sortOrder.length; i++) { const type1 = this.getType(res1[i]); const type2 = this.getType(res2[i]); if (type1 !== type2) { throw new Error( `Expected ${type1}, but got ${type2}. Cannot execute cross partition order-by queries on mixed types. Consider filtering your query using IS_STRING or IS_NUMBER to get around this exception.` ); } } } getType(orderByItem) { if (orderByItem === void 0 || orderByItem.item === void 0) { return "NoValue"; } const type = typeof orderByItem.item; if (TYPEORDCOMPARATOR[type] === void 0) { throw new Error(`unrecognizable type ${type}`); } return type; } getOrderByItems(res) { return res["orderByItems"]; } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { OrderByDocumentProducerComparator });