@azure/cosmos
Version:
Microsoft Azure Cosmos DB Service Node.js SDK for NOSQL API
117 lines (116 loc) • 4.38 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 inMemoryCollectionRoutingMap_exports = {};
__export(inMemoryCollectionRoutingMap_exports, {
InMemoryCollectionRoutingMap: () => InMemoryCollectionRoutingMap
});
module.exports = __toCommonJS(inMemoryCollectionRoutingMap_exports);
var import_common = require("../common/index.js");
var import_QueryRange = require("./QueryRange.js");
class InMemoryCollectionRoutingMap {
orderedPartitionKeyRanges;
orderedRanges;
// TODO: chrande made this public, even though it is implementation detail for a test
orderedPartitionInfo;
/**
* Represents a InMemoryCollectionRoutingMap Object,
* Stores partition key ranges in an efficient way with some additional information and provides
* convenience methods for working with set of ranges.
*/
constructor(orderedPartitionKeyRanges, orderedPartitionInfo) {
this.orderedPartitionKeyRanges = orderedPartitionKeyRanges;
this.orderedRanges = orderedPartitionKeyRanges.map((pkr) => {
return new import_QueryRange.QueryRange(
pkr[import_common.Constants.PartitionKeyRange.MinInclusive],
pkr[import_common.Constants.PartitionKeyRange.MaxExclusive],
true,
false
);
});
this.orderedPartitionInfo = orderedPartitionInfo;
}
getOrderedParitionKeyRanges() {
return this.orderedPartitionKeyRanges;
}
getOverlappingRanges(providedQueryRanges) {
const pqr = Array.isArray(providedQueryRanges) ? providedQueryRanges : [providedQueryRanges];
const minToPartitionRange = {};
for (const queryRange of pqr) {
if (queryRange.isEmpty()) {
continue;
}
if (queryRange.isFullRange()) {
return this.orderedPartitionKeyRanges;
}
const minIndex = this.orderedRanges.findIndex((range) => {
if (queryRange.min > range.min && queryRange.min < range.max) {
return true;
}
if (queryRange.min === range.min) {
return true;
}
if (queryRange.min === range.max) {
return true;
}
});
if (minIndex < 0) {
throw new Error(
"error in collection routing map, queried value is less than the start range."
);
}
let maxIndex;
for (let i = this.orderedRanges.length - 1; i >= 0; i--) {
const range = this.orderedRanges[i];
if (queryRange.max > range.min && queryRange.max < range.max) {
maxIndex = i;
break;
}
if (queryRange.max === range.min) {
maxIndex = i;
break;
}
if (queryRange.max === range.max) {
maxIndex = i;
break;
}
}
if (maxIndex > this.orderedRanges.length) {
throw new Error(
"error in collection routing map, queried value is greater than the end range."
);
}
for (let j = minIndex; j < maxIndex + 1; j++) {
if (queryRange.overlaps(this.orderedRanges[j])) {
minToPartitionRange[this.orderedPartitionKeyRanges[j][import_common.Constants.PartitionKeyRange.MinInclusive]] = this.orderedPartitionKeyRanges[j];
}
}
}
const overlappingPartitionKeyRanges = Object.keys(minToPartitionRange).map(
(k) => minToPartitionRange[k]
);
return overlappingPartitionKeyRanges.sort((a, b) => {
return a[import_common.Constants.PartitionKeyRange.MinInclusive].localeCompare(
b[import_common.Constants.PartitionKeyRange.MinInclusive]
);
});
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
InMemoryCollectionRoutingMap
});