UNPKG

@azure/cosmos

Version:
177 lines (176 loc) 6.75 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 NonStreamingOrderByDistinctEndpointComponent_exports = {}; __export(NonStreamingOrderByDistinctEndpointComponent_exports, { NonStreamingOrderByDistinctEndpointComponent: () => NonStreamingOrderByDistinctEndpointComponent }); module.exports = __toCommonJS(NonStreamingOrderByDistinctEndpointComponent_exports); var import_headerUtils = require("../headerUtils.js"); var import_hashObject = require("../../utils/hashObject.js"); var import_fixedSizePriorityQueue = require("../../utils/fixedSizePriorityQueue.js"); var import_nonStreamingOrderByMap = require("../../utils/nonStreamingOrderByMap.js"); var import_orderByComparator = require("../orderByComparator.js"); var import_parallelQueryResult = require("../parallelQueryResult.js"); class NonStreamingOrderByDistinctEndpointComponent { constructor(executionContext, queryInfo, priorityQueueBufferSize, emitRawOrderByPayload = false) { this.executionContext = executionContext; this.queryInfo = queryInfo; this.priorityQueueBufferSize = priorityQueueBufferSize; this.emitRawOrderByPayload = emitRawOrderByPayload; this.sortOrders = this.queryInfo.orderBy; const comparator = new import_orderByComparator.OrderByComparator(this.sortOrders); this.aggregateMap = new import_nonStreamingOrderByMap.NonStreamingOrderByMap( (a, b) => { return comparator.compareItems(a, b); } ); this.nonStreamingOrderByPQ = new import_fixedSizePriorityQueue.FixedSizePriorityQueue( (a, b) => { return comparator.compareItems(b, a); }, this.priorityQueueBufferSize ); } /** * A Map that holds the distinct values of the items before storing in priority queue. */ aggregateMap; /** * A priority queue to compute the final sorted results. */ nonStreamingOrderByPQ; /** * Array to store the final sorted results. */ finalResultArray; sortOrders; /** * Flag to determine if all results are fetched from backend and results can be returned. */ isCompleted = false; /** * Build final sorted result array from which responses will be served. */ async buildFinalResultArray() { const allValues = this.aggregateMap.getAllValuesAndReset(); for (const value of allValues) { this.nonStreamingOrderByPQ.enqueue(value); } const offSet = this.queryInfo.offset ? this.queryInfo.offset : 0; const queueSize = this.nonStreamingOrderByPQ.size(); const finalArraySize = queueSize - offSet; if (finalArraySize <= 0) { this.finalResultArray = []; } else { this.finalResultArray = new Array(finalArraySize); for (let count = finalArraySize - 1; count >= 0; count--) { if (this.emitRawOrderByPayload) { this.finalResultArray[count] = this.nonStreamingOrderByPQ.dequeue(); } else { this.finalResultArray[count] = this.nonStreamingOrderByPQ.dequeue()?.payload; } } } } hasMoreResults() { if (this.priorityQueueBufferSize === 0) return false; return this.executionContext.hasMoreResults(); } async fetchMore(diagnosticNode) { if (this.isCompleted) { return { result: void 0, headers: (0, import_headerUtils.getInitialHeader)() }; } let resHeaders = (0, import_headerUtils.getInitialHeader)(); if (this.priorityQueueBufferSize <= 0) { return { result: void 0, headers: resHeaders }; } if (this.executionContext.hasMoreResults()) { const response = await this.executionContext.fetchMore(diagnosticNode); if (!response) { this.isCompleted = true; if (this.aggregateMap.size() > 0) { await this.buildFinalResultArray(); const result2 = (0, import_parallelQueryResult.createParallelQueryResult)(this.finalResultArray, /* @__PURE__ */ new Map(), {}, void 0); return { result: result2, headers: resHeaders }; } return { result: void 0, headers: resHeaders }; } if (response.result === void 0 || !Array.isArray(response.result.buffer) || response.result.buffer.length === 0) { this.isCompleted = true; if (this.aggregateMap.size() > 0) { await this.buildFinalResultArray(); const result2 = (0, import_parallelQueryResult.createParallelQueryResult)(this.finalResultArray, /* @__PURE__ */ new Map(), {}, void 0); return { result: result2, headers: response.headers }; } return { result: void 0, headers: response.headers }; } resHeaders = response.headers; const parallelResult = response.result; const dataToProcess = parallelResult.buffer; for (const item of dataToProcess) { if (item) { const key = await (0, import_hashObject.hashObject)(item?.payload); this.aggregateMap.set(key, item); } } if (this.executionContext.hasMoreResults()) { const result2 = (0, import_parallelQueryResult.createParallelQueryResult)( [], // empty buffer /* @__PURE__ */ new Map(), void 0, void 0 ); return { result: result2, headers: resHeaders }; } } if (!this.executionContext.hasMoreResults() && !this.isCompleted) { this.isCompleted = true; await this.buildFinalResultArray(); const result2 = (0, import_parallelQueryResult.createParallelQueryResult)(this.finalResultArray, /* @__PURE__ */ new Map()); return { result: result2, headers: resHeaders }; } const result = (0, import_parallelQueryResult.createParallelQueryResult)([], /* @__PURE__ */ new Map()); return { result, headers: resHeaders }; } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { NonStreamingOrderByDistinctEndpointComponent });