@azure/cosmos
Version:
Microsoft Azure Cosmos DB Service Node.js SDK for NOSQL API
164 lines (163 loc) • 6.18 kB
JavaScript
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 NonStreamingOrderByEndpointComponent_exports = {};
__export(NonStreamingOrderByEndpointComponent_exports, {
NonStreamingOrderByEndpointComponent: () => NonStreamingOrderByEndpointComponent
});
module.exports = __toCommonJS(NonStreamingOrderByEndpointComponent_exports);
var import_orderByComparator = require("../orderByComparator.js");
var import_fixedSizePriorityQueue = require("../../utils/fixedSizePriorityQueue.js");
var import_headerUtils = require("../headerUtils.js");
var import_parallelQueryResult = require("../parallelQueryResult.js");
class NonStreamingOrderByEndpointComponent {
/**
* Represents an endpoint in handling an non-streaming order by query. For each processed orderby
* result it returns 'payload' item of the result
*
* @param executionContext - Underlying Execution Context
* @hidden
*/
constructor(executionContext, sortOrders, priorityQueueBufferSize, offset = 0, emitRawOrderByPayload = false) {
this.executionContext = executionContext;
this.sortOrders = sortOrders;
this.priorityQueueBufferSize = priorityQueueBufferSize;
this.offset = offset;
this.emitRawOrderByPayload = emitRawOrderByPayload;
const comparator = new import_orderByComparator.OrderByComparator(this.sortOrders);
this.nonStreamingOrderByPQ = new import_fixedSizePriorityQueue.FixedSizePriorityQueue(
(a, b) => {
return comparator.compareItems(b, a);
},
this.priorityQueueBufferSize
);
}
/**
* A priority queue to store the final sorted results.
*/
nonStreamingOrderByPQ;
/**
* Flag to determine if all results are fetched from backend and results can be returned from priority queue.
*/
isCompleted = false;
/**
* Determine if there are still remaining resources to processs.
* @returns true if there is other elements to process in the NonStreamingOrderByEndpointComponent.
*/
hasMoreResults() {
return this.priorityQueueBufferSize > 0 && this.executionContext.hasMoreResults();
}
/**
* Fetches the next batch of the result from the target container.
* @param diagnosticNode - The diagnostic information for the request.
*/
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.nonStreamingOrderByPQ.isEmpty()) {
return this.buildFinalResultArray(resHeaders);
}
return { result: void 0, headers: resHeaders };
}
resHeaders = response.headers;
if (response.result === void 0 || !response.result.buffer || response.result.buffer.length === 0) {
this.isCompleted = true;
if (!this.nonStreamingOrderByPQ.isEmpty()) {
return this.buildFinalResultArray(resHeaders);
}
return { result: void 0, headers: resHeaders };
}
const parallelResult = response.result;
const dataToProcess = parallelResult.buffer;
for (const item of dataToProcess) {
if (item !== void 0) {
this.nonStreamingOrderByPQ.enqueue(item);
}
}
}
if (this.executionContext.hasMoreResults()) {
const result2 = (0, import_parallelQueryResult.createParallelQueryResult)(
[],
// empty buffer
/* @__PURE__ */ new Map(),
{}
);
return {
result: result2,
headers: resHeaders
};
}
if (!this.executionContext.hasMoreResults() && !this.isCompleted) {
this.isCompleted = true;
return this.buildFinalResultArray(resHeaders, /* @__PURE__ */ new Map(), {});
}
const result = (0, import_parallelQueryResult.createParallelQueryResult)([], /* @__PURE__ */ new Map(), {});
return {
result,
headers: resHeaders
};
}
async buildFinalResultArray(resHeaders, partitionKeyRangeMap, updatedContinuationRanges) {
this.isCompleted = true;
this.nonStreamingOrderByPQ = this.nonStreamingOrderByPQ.reverse();
while (this.offset < this.priorityQueueBufferSize && this.offset > 0 && !this.nonStreamingOrderByPQ.isEmpty()) {
this.nonStreamingOrderByPQ.dequeue();
this.offset--;
}
if (!this.nonStreamingOrderByPQ.isEmpty()) {
const buffer = [];
if (this.emitRawOrderByPayload) {
while (!this.nonStreamingOrderByPQ.isEmpty()) {
buffer.push(this.nonStreamingOrderByPQ.dequeue());
}
} else {
while (!this.nonStreamingOrderByPQ.isEmpty()) {
buffer.push(this.nonStreamingOrderByPQ.dequeue()?.payload);
}
}
const result = (0, import_parallelQueryResult.createParallelQueryResult)(
buffer,
partitionKeyRangeMap || /* @__PURE__ */ new Map(),
updatedContinuationRanges || {},
void 0
);
return {
result,
headers: resHeaders
};
}
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
NonStreamingOrderByEndpointComponent
});