UNPKG

@azure/cosmos

Version:
100 lines (99 loc) 4.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 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 = {}; this.pendingByCollectionId = {}; } clientContext; // Resolved, known-good routing maps. Only successful fetches are published here. collectionRoutingMapByCollectionId; // In-flight fetches, so concurrent lookups (cold or forceRefresh) dedupe to a single request. pendingByCollectionId; /** * 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); const cached = this.collectionRoutingMapByCollectionId[collectionId]; if (cached !== void 0 && !forceRefresh) { return cached; } if (this.pendingByCollectionId[collectionId] === void 0) { this.pendingByCollectionId[collectionId] = this.requestCollectionRoutingMap( collectionLink, diagnosticNode ).then((map) => { this.collectionRoutingMapByCollectionId[collectionId] = Promise.resolve(map); return map; }).finally(() => { delete this.pendingByCollectionId[collectionId]; }); } return this.pendingByCollectionId[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 }); //# sourceMappingURL=partitionKeyRangeCache.js.map