@azure/cosmos
Version:
Microsoft Azure Cosmos DB Service Node.js SDK for NOSQL API
162 lines (161 loc) • 5.57 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 PartitionRangeManager_exports = {};
__export(PartitionRangeManager_exports, {
PartitionRangeManager: () => PartitionRangeManager,
isPartitionExhausted: () => isPartitionExhausted
});
module.exports = __toCommonJS(PartitionRangeManager_exports);
function isPartitionExhausted(continuationToken) {
return !continuationToken || continuationToken === "" || continuationToken === "null" || continuationToken.toLowerCase() === "null";
}
class PartitionRangeManager {
partitionKeyRangeMap = /* @__PURE__ */ new Map();
constructor(initialPartitionKeyRangeMap) {
if (initialPartitionKeyRangeMap) {
this.partitionKeyRangeMap = new Map(initialPartitionKeyRangeMap);
}
}
/**
* Gets a copy of the current partition key range map for constructor pattern
*/
getPartitionKeyRangeMap() {
return new Map(this.partitionKeyRangeMap);
}
/**
* Adds a range mapping to the partition key range map
* Does not allow updates to existing keys - only new additions
* @param rangeId - Unique identifier for the partition range
* @param mapping - The QueryRangeMapping to add
*/
addPartitionRangeMapping(rangeId, mapping) {
if (!this.partitionKeyRangeMap.has(rangeId)) {
this.partitionKeyRangeMap.set(rangeId, mapping);
}
}
/**
* Removes a range mapping from the partition key range map
*/
removePartitionRangeMapping(rangeId) {
this.partitionKeyRangeMap.delete(rangeId);
}
/**
* Updates the partition key range map with new mappings from the endpoint response
* @param partitionKeyRangeMap - Map of range IDs to QueryRangeMapping objects
*/
addPartitionKeyRangeMap(partitionKeyRangeMap) {
if (partitionKeyRangeMap) {
for (const [rangeId, mapping] of partitionKeyRangeMap) {
this.addPartitionRangeMapping(rangeId, mapping);
}
}
}
/**
* Checks if there are any unprocessed ranges in the sliding window
*/
hasUnprocessedRanges() {
return this.partitionKeyRangeMap.size > 0;
}
/**
* Removes exhausted(fully drained) ranges from the given range mappings
* @param rangeMappings - Array of range mappings to filter
* @returns Filtered array without exhausted ranges
*/
removeExhaustedRanges(rangeMappings) {
if (!rangeMappings || !Array.isArray(rangeMappings)) {
return [];
}
return rangeMappings.filter((mapping) => {
if (!mapping) {
return false;
}
const isExhausted = isPartitionExhausted(mapping.continuationToken);
if (isExhausted) {
return false;
}
return true;
});
}
/**
* Processes ranges for ORDER BY queries
*/
processOrderByRanges(pageSize) {
let endIndex = 0;
const processedRanges = [];
let lastRangeBeforePageLimit = null;
let rangeIndex = 0;
for (const [rangeId, value] of this.partitionKeyRangeMap) {
rangeIndex++;
const { itemCount } = value;
if (endIndex + itemCount <= pageSize) {
lastRangeBeforePageLimit = value;
endIndex += itemCount;
processedRanges.push(rangeId);
} else {
break;
}
}
return { endIndex, processedRanges, lastRangeBeforePageLimit };
}
processEmptyOrderByRanges(ranges) {
const endIndex = 0;
const processedRanges = [];
let lastRangeBeforePageLimit;
for (const [rangeId, _] of this.partitionKeyRangeMap) {
processedRanges.push(rangeId);
}
for (const [_, mapping] of this.partitionKeyRangeMap) {
if (mapping.partitionKeyRange.minInclusive === ranges[0].queryRange.min && mapping.partitionKeyRange.maxExclusive === ranges[0].queryRange.max) {
lastRangeBeforePageLimit = mapping;
break;
}
}
return { endIndex, processedRanges, lastRangeBeforePageLimit };
}
/**
* Processes ranges for parallel queries - multi-range aggregation
*/
processParallelRanges(pageSize) {
let endIndex = 0;
const processedRanges = [];
const processedRangeMappings = [];
let rangesAggregatedInCurrentToken = 0;
let lastPartitionBeforeCutoff;
for (const [rangeId, value] of this.partitionKeyRangeMap) {
rangesAggregatedInCurrentToken++;
if (!value || value.itemCount === void 0) {
continue;
}
const { itemCount } = value;
if (endIndex + itemCount <= pageSize) {
lastPartitionBeforeCutoff = { rangeId, mapping: value };
endIndex += itemCount;
processedRanges.push(rangeId);
processedRangeMappings.push(value);
} else {
break;
}
}
return { endIndex, processedRanges, processedRangeMappings, lastPartitionBeforeCutoff };
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
PartitionRangeManager,
isPartitionExhausted
});