@azure/cosmos
Version:
Microsoft Azure Cosmos DB Service Node.js SDK for NOSQL API
101 lines (100 loc) • 3.99 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 PartitionRangeUtils_exports = {};
__export(PartitionRangeUtils_exports, {
calculateOffsetLimitForPartitionRanges: () => calculateOffsetLimitForPartitionRanges,
processDistinctQueryAndUpdateRangeMap: () => processDistinctQueryAndUpdateRangeMap
});
module.exports = __toCommonJS(PartitionRangeUtils_exports);
function calculateOffsetLimitForPartitionRanges(partitionKeyRangeMap, initialOffset, initialLimit) {
if (!partitionKeyRangeMap || partitionKeyRangeMap.size === 0) {
return partitionKeyRangeMap;
}
const updatedMap = /* @__PURE__ */ new Map();
let currentOffset = initialOffset;
let currentLimit = initialLimit;
for (const [rangeId, rangeMapping] of partitionKeyRangeMap) {
const { itemCount } = rangeMapping;
let offsetAfterThisRange = currentOffset;
let limitAfterThisRange = currentLimit;
let updatedItemCount = itemCount;
if (itemCount > 0) {
if (currentOffset > 0) {
const offsetConsumption = Math.min(currentOffset, itemCount);
offsetAfterThisRange = currentOffset - offsetConsumption;
const remainingItems = itemCount - offsetConsumption;
if (remainingItems > 0 && currentLimit > 0) {
const limitConsumption = Math.min(currentLimit, remainingItems);
limitAfterThisRange = currentLimit - limitConsumption;
updatedItemCount = limitConsumption;
} else {
updatedItemCount = 0;
limitAfterThisRange = currentLimit;
}
} else if (currentLimit > 0) {
const limitConsumption = Math.min(currentLimit, itemCount);
limitAfterThisRange = currentLimit - limitConsumption;
offsetAfterThisRange = 0;
updatedItemCount = limitConsumption;
} else {
updatedItemCount = 0;
}
currentOffset = offsetAfterThisRange;
currentLimit = limitAfterThisRange;
} else {
updatedItemCount = 0;
}
updatedMap.set(rangeId, {
...rangeMapping,
offset: offsetAfterThisRange,
limit: limitAfterThisRange,
itemCount: updatedItemCount
});
}
return updatedMap;
}
async function processDistinctQueryAndUpdateRangeMap(originalBuffer, partitionKeyRangeMap, hashFunction) {
if (!partitionKeyRangeMap || partitionKeyRangeMap.size === 0) {
return partitionKeyRangeMap;
}
const updatedMap = /* @__PURE__ */ new Map();
let bufferIndex = 0;
for (const [rangeId, rangeMapping] of partitionKeyRangeMap) {
const { itemCount } = rangeMapping;
let lastHashForThisRange;
if (itemCount > 0 && bufferIndex <= originalBuffer.length) {
const rangeEndIndex = Math.min(bufferIndex + itemCount, originalBuffer.length);
const lastItemIndex = rangeEndIndex - 1;
const lastItem = originalBuffer[lastItemIndex];
if (lastItem) {
lastHashForThisRange = await hashFunction(lastItem);
}
bufferIndex = rangeEndIndex;
}
updatedMap.set(rangeId, {
...rangeMapping,
hashedLastResult: lastHashForThisRange
});
}
return updatedMap;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
calculateOffsetLimitForPartitionRanges,
processDistinctQueryAndUpdateRangeMap
});