UNPKG

@azure/cosmos

Version:
294 lines (293 loc) • 12.4 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"); var import_PartitionRangeManager = require("../PartitionRangeManager.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, parsedContinuationToken) { super(collectionLink, parsedContinuationToken ?? initialContinuationToken); this.orderByItemsArray = []; if (initialContinuationToken) { const parsed = parsedContinuationToken ?? (0, import_OrderByQueryContinuationToken.parseOrderByQueryContinuationToken)( initialContinuationToken ); this.normalizeCheckpointOwnership(parsed.rangeMappings); this.initializeRangeFilters(parsed); this.tokenRangeMappings = parsed.rangeMappings; this.tokenOrderByItems = parsed.orderByItems; this.tokenSkipCount = parsed.skipCount; this.tokenDocumentRid = parsed.documentRid; this.tokenHashedLastResult = parsed.hashedLastResult; } } processQuerySpecificResponse(responseResult) { this.reconcileRangeMappings(responseResult.partitionKeyRangeMap); this.orderByItemsArray.length = 0; if (responseResult.orderByItems) { this.orderByItemsArray.push(...responseResult.orderByItems); } } performQuerySpecificDataTrim(_processedRanges, endIndex) { this.sliceOrderByItemsArray(endIndex); } clearQuerySpecificState() { this.orderByItemsArray.length = 0; this.clearTokenState(); } preserveInactiveRangeCoverage() { return true; } sliceOrderByItemsArray(endIndex) { if (endIndex === 0 || endIndex >= this.orderByItemsArray.length) { this.orderByItemsArray.length = 0; } else { this.orderByItemsArray.splice(0, endIndex); } } processRangesForPagination(pageSize, isResponseEmpty = false, workRemains = true) { if (isResponseEmpty && this.tokenRangeMappings) { const processedRanges = [...this.partitionManager.getPartitionKeyRangeMap().keys()]; const activeMappings = this.rangeList.filter( (mapping) => workRemains || mapping.isOrderByCheckpoint === true || (0, import_PartitionRangeManager.isPartitionRangeActive)(mapping.continuationToken, mapping.isActive) ); if (activeMappings.length > 0) { this.tokenRangeMappings = activeMappings.map((mapping) => ({ queryRange: { ...mapping.queryRange }, continuationToken: mapping.continuationToken, isActive: mapping.isActive, isOrderByCheckpoint: mapping.isOrderByCheckpoint, orderByFilter: this.cloneOrderByFilter(mapping.orderByFilter) })); } else { this.clearTokenState(); } return { endIndex: 0, processedRanges }; } const rangeProcessingResult = this.partitionManager.processOrderByRanges(pageSize); const { lastRangeBeforePageLimit } = rangeProcessingResult; if (!lastRangeBeforePageLimit) { this.clearTokenState(); return { endIndex: rangeProcessingResult.endIndex, processedRanges: rangeProcessingResult.processedRanges }; } 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 }; } const checkpointRange = (0, import_CompositeQueryContinuationToken.convertRangeMappingToQueryRange)(lastRangeBeforePageLimit); const checkpointKey = this.rangeKey(checkpointRange); const checkpointFilter = { orderByItems: [...lastOrderByItems], rid: documentRid, skipCount }; const pageRangeMappings = this.createPageRangeMappings( rangeProcessingResult.processedRanges ).filter( (mapping) => workRemains || this.rangeKey(mapping) === checkpointKey || (0, import_PartitionRangeManager.isPartitionRangeActive)(mapping.continuationToken, mapping.isActive) ); if (pageRangeMappings.length === 0) { pageRangeMappings.push(checkpointRange); } for (const mapping of pageRangeMappings) { const isCheckpoint = this.rangeKey(mapping) === checkpointKey; mapping.isOrderByCheckpoint = isCheckpoint; if (isCheckpoint) { mapping.orderByFilter = this.cloneOrderByFilter(checkpointFilter); } } this.replaceRangeList(pageRangeMappings); this.tokenRangeMappings = pageRangeMappings; 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; } reconcileRangeMappings(partitionKeyRangeMap) { for (const mapping of partitionKeyRangeMap.values()) { const range = (0, import_CompositeQueryContinuationToken.convertRangeMappingToQueryRange)(mapping); const existingIndex = this.rangeList.findIndex( (current) => current.queryRange.min === range.queryRange.min && current.queryRange.max === range.queryRange.max ); if (!(0, import_PartitionRangeManager.isPartitionRangeActive)(mapping.continuationToken, mapping.isActive)) { const isCheckpoint = mapping.isOrderByCheckpoint === true || existingIndex >= 0 && this.rangeList[existingIndex].isOrderByCheckpoint === true; range.isActive = false; range.isOrderByCheckpoint = isCheckpoint; if (existingIndex >= 0) { this.rangeList[existingIndex] = range; } else { this.rangeList.push(range); } } else if (existingIndex >= 0) { range.isOrderByCheckpoint = mapping.isOrderByCheckpoint ?? this.rangeList[existingIndex].isOrderByCheckpoint; this.rangeList[existingIndex] = range; } else { this.rangeList.push(range); } } } createPageRangeMappings(processedRanges) { const mappingsByRange = /* @__PURE__ */ new Map(); for (const mapping of this.rangeList) { mappingsByRange.set(this.rangeKey(mapping), { queryRange: { ...mapping.queryRange }, continuationToken: mapping.continuationToken, isActive: mapping.isActive, isOrderByCheckpoint: mapping.isOrderByCheckpoint, orderByFilter: this.cloneOrderByFilter(mapping.orderByFilter) }); } const processedRangeSet = new Set(processedRanges); for (const [key, mapping] of this.partitionManager.getPartitionKeyRangeMap()) { const checkpointMapping = processedRangeSet.has(key) ? mapping : { ...mapping, continuationToken: mapping.previousContinuationToken, orderByFilter: mapping.previousOrderByFilter ?? mapping.orderByFilter, isActive: true }; const tokenMapping = (0, import_CompositeQueryContinuationToken.convertRangeMappingToQueryRange)(checkpointMapping); if (checkpointMapping.isActive === false) { tokenMapping.isActive = false; } tokenMapping.isOrderByCheckpoint = checkpointMapping.isOrderByCheckpoint ?? mappingsByRange.get(this.rangeKey(tokenMapping))?.isOrderByCheckpoint; tokenMapping.orderByFilter = this.cloneOrderByFilter(checkpointMapping.orderByFilter) ?? this.cloneOrderByFilter(mappingsByRange.get(this.rangeKey(tokenMapping))?.orderByFilter); mappingsByRange.set(this.rangeKey(tokenMapping), tokenMapping); } return Array.from(mappingsByRange.values()).sort( (left, right) => left.queryRange.min.localeCompare(right.queryRange.min) ); } normalizeCheckpointOwnership(mappings) { if (mappings.some((mapping) => mapping.isOrderByCheckpoint !== void 0)) { return; } mappings.forEach((mapping, index) => { mapping.isOrderByCheckpoint = index === 0; }); this.rangeList.forEach((mapping, index) => { mapping.isOrderByCheckpoint = index === 0; }); } initializeRangeFilters(token) { const checkpointFilter = typeof token.documentRid === "string" ? { orderByItems: [...token.orderByItems], rid: token.documentRid, skipCount: token.skipCount } : void 0; if (!checkpointFilter) { return; } token.rangeMappings.forEach((mapping) => { if (mapping.isOrderByCheckpoint === true && !mapping.orderByFilter) { mapping.orderByFilter = this.cloneOrderByFilter(checkpointFilter); } }); this.rangeList.forEach((mapping) => { const tokenMapping = token.rangeMappings.find( (candidate) => this.rangeKey(candidate) === this.rangeKey(mapping) ); mapping.orderByFilter = this.cloneOrderByFilter(tokenMapping?.orderByFilter); }); } replaceRangeList(mappings) { this.rangeList.length = 0; this.rangeList.push( ...mappings.map((mapping) => ({ ...mapping, queryRange: { ...mapping.queryRange }, orderByFilter: this.cloneOrderByFilter(mapping.orderByFilter) })) ); } rangeKey(mapping) { return `${mapping.queryRange.min}-${mapping.queryRange.max}`; } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { OrderByQueryContinuationTokenManager }); //# sourceMappingURL=OrderByQueryContinuationTokenManager.js.map