UNPKG

@azure/cosmos

Version:
85 lines (84 loc) 3.79 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 partitionKeyRangeCache_exports = {}; __export(partitionKeyRangeCache_exports, { PartitionKeyRangeCache: () => PartitionKeyRangeCache }); module.exports = __toCommonJS(partitionKeyRangeCache_exports); var import_CosmosDiagnostics = require("../CosmosDiagnostics.js"); var import_helper = require("../common/helper.js"); var import_diagnostics = require("../utils/diagnostics.js"); var import_CollectionRoutingMapFactory = require("./CollectionRoutingMapFactory.js"); var import_hash = require("../utils/hashing/hash.js"); class PartitionKeyRangeCache { constructor(clientContext) { this.clientContext = clientContext; this.collectionRoutingMapByCollectionId = {}; } collectionRoutingMapByCollectionId; /** * Finds or Instantiates the requested Collection Routing Map * @param collectionLink - Requested collectionLink * @hidden */ async onCollectionRoutingMap(collectionLink, diagnosticNode, forceRefresh = false) { const collectionId = (0, import_helper.getIdFromLink)(collectionLink); if (this.collectionRoutingMapByCollectionId[collectionId] === void 0 || forceRefresh) { this.collectionRoutingMapByCollectionId[collectionId] = this.requestCollectionRoutingMap( collectionLink, diagnosticNode ); } return this.collectionRoutingMapByCollectionId[collectionId]; } /** * Given the query ranges and a collection, invokes the callback on the list of overlapping partition key ranges * @hidden */ async getOverlappingRanges(collectionLink, queryRange, diagnosticNode, forceRefresh = false) { const crm = await this.onCollectionRoutingMap(collectionLink, diagnosticNode, forceRefresh); return crm.getOverlappingRanges(queryRange); } async requestCollectionRoutingMap(collectionLink, diagnosticNode) { const { resources } = await (0, import_diagnostics.withMetadataDiagnostics)( async (metadataDiagnostics) => { return this.clientContext.queryPartitionKeyRanges(collectionLink).fetchAllInternal(metadataDiagnostics); }, diagnosticNode, import_CosmosDiagnostics.MetadataLookUpType.PartitionKeyRangeLookUp ); return (0, import_CollectionRoutingMapFactory.createCompleteRoutingMap)(resources.map((r) => [r, true])); } /** * Given a partition key, returns the partition key range id * @internal */ async getPartitionKeyRangeIdFromPartitionKey(collectionLink, partitionKey, partitionKeyDefinition, diagnosticNode) { const hashedPartitionKey = (0, import_hash.hashPartitionKey)(partitionKey, partitionKeyDefinition); const partitionKeyRanges = (await this.onCollectionRoutingMap(collectionLink, diagnosticNode)).getOrderedParitionKeyRanges(); const partitionKeyRangeId = (0, import_hash.binarySearchOnPartitionKeyRanges)( partitionKeyRanges, hashedPartitionKey ); return partitionKeyRangeId; } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { PartitionKeyRangeCache });