UNPKG

@azure/cosmos

Version:
156 lines (155 loc) 6.47 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 OrderByQueryContinuationTokenManager_exports = {}; __export(OrderByQueryContinuationTokenManager_exports, { OrderByQueryContinuationTokenManager: () => OrderByQueryContinuationTokenManager }); module.exports = __toCommonJS(OrderByQueryContinuationTokenManager_exports); var import_BaseContinuationTokenManager = require("./BaseContinuationTokenManager.js"); var import_OrderByQueryContinuationToken = require("../../documents/ContinuationToken/OrderByQueryContinuationToken.js"); var import_CompositeQueryContinuationToken = require("../../documents/ContinuationToken/CompositeQueryContinuationToken.js"); class OrderByQueryContinuationTokenManager extends import_BaseContinuationTokenManager.BaseContinuationTokenManager { orderByItemsArray; // OrderBy-specific token state (computed into token on demand) tokenRangeMappings; tokenOrderByItems; tokenSkipCount = 0; tokenDocumentRid = ""; tokenHashedLastResult; constructor(collectionLink, initialContinuationToken) { super(collectionLink, initialContinuationToken); this.orderByItemsArray = []; if (initialContinuationToken) { const parsed = (0, import_OrderByQueryContinuationToken.parseOrderByQueryContinuationToken)(initialContinuationToken); this.tokenRangeMappings = parsed.rangeMappings; this.tokenOrderByItems = parsed.orderByItems; this.tokenSkipCount = parsed.skipCount; this.tokenDocumentRid = parsed.documentRid; this.tokenHashedLastResult = parsed.hashedLastResult; } } processQuerySpecificResponse(responseResult) { this.orderByItemsArray.length = 0; if (responseResult.orderByItems) { this.orderByItemsArray.push(...responseResult.orderByItems); } } performQuerySpecificDataTrim(_processedRanges, endIndex) { this.sliceOrderByItemsArray(endIndex); } sliceOrderByItemsArray(endIndex) { if (endIndex === 0 || endIndex >= this.orderByItemsArray.length) { this.orderByItemsArray.length = 0; } else { this.orderByItemsArray.splice(0, endIndex); } } processRangesForPagination(pageSize, isResponseEmpty = false) { if (isResponseEmpty && this.tokenRangeMappings) { let rangeProcessingResult2; if (this.rangeList.length === 0) { rangeProcessingResult2 = this.partitionManager.processOrderByRanges(pageSize); } else { rangeProcessingResult2 = this.partitionManager.processEmptyOrderByRanges(this.rangeList); } const { lastRangeBeforePageLimit: lastRangeBeforePageLimit2 } = rangeProcessingResult2; if (lastRangeBeforePageLimit2) { this.tokenRangeMappings = [(0, import_CompositeQueryContinuationToken.convertRangeMappingToQueryRange)(lastRangeBeforePageLimit2)]; } else { this.clearTokenState(); } return { endIndex: rangeProcessingResult2.endIndex, processedRanges: rangeProcessingResult2.processedRanges }; } const rangeProcessingResult = this.partitionManager.processOrderByRanges(pageSize); const { lastRangeBeforePageLimit } = rangeProcessingResult; if (!lastRangeBeforePageLimit) { this.clearTokenState(); return { endIndex: rangeProcessingResult.endIndex, processedRanges: rangeProcessingResult.processedRanges }; } const queryRange = (0, import_CompositeQueryContinuationToken.convertRangeMappingToQueryRange)(lastRangeBeforePageLimit); let lastOrderByItems; let documentRid; let skipCount = 0; if (rangeProcessingResult.endIndex > 0 && this.orderByItemsArray.length > 0) { const lastItemIndexOnPage = rangeProcessingResult.endIndex - 1; if (lastItemIndexOnPage < this.orderByItemsArray.length) { lastOrderByItems = this.orderByItemsArray[lastItemIndexOnPage].orderByItems; documentRid = this.orderByItemsArray[lastItemIndexOnPage]._rid; skipCount = 0; for (let i = 0; i <= lastItemIndexOnPage; i++) { if (this.orderByItemsArray[i]._rid === documentRid) { skipCount++; } } } } if (!lastOrderByItems || lastOrderByItems.length === 0) { this.clearTokenState(); return { endIndex: rangeProcessingResult.endIndex, processedRanges: rangeProcessingResult.processedRanges }; } this.tokenRangeMappings = [queryRange]; this.tokenOrderByItems = lastOrderByItems; this.tokenSkipCount = skipCount; this.tokenDocumentRid = documentRid; this.offset = lastRangeBeforePageLimit.offset; this.limit = lastRangeBeforePageLimit.limit; this.tokenHashedLastResult = lastRangeBeforePageLimit.hashedLastResult; return { endIndex: rangeProcessingResult.endIndex, processedRanges: rangeProcessingResult.processedRanges }; } getCurrentContinuationToken() { if (!this.tokenRangeMappings || !this.tokenOrderByItems) { return void 0; } return { rid: this.collectionLink, rangeMappings: this.tokenRangeMappings, orderByItems: this.tokenOrderByItems, skipCount: this.tokenSkipCount, documentRid: this.tokenDocumentRid, offset: this.offset, limit: this.limit, hashedLastResult: this.tokenHashedLastResult }; } getSerializationFunction() { return import_OrderByQueryContinuationToken.serializeOrderByQueryContinuationToken; } clearTokenState() { this.tokenRangeMappings = void 0; this.tokenOrderByItems = void 0; this.tokenSkipCount = 0; this.tokenDocumentRid = ""; this.tokenHashedLastResult = void 0; } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { OrderByQueryContinuationTokenManager });