UNPKG

@azure/cosmos

Version:
136 lines (135 loc) 5.15 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 VectorSessionToken_exports = {}; __export(VectorSessionToken_exports, { VectorSessionToken: () => VectorSessionToken }); module.exports = __toCommonJS(VectorSessionToken_exports); class VectorSessionToken { constructor(version, globalLsn, localLsnByregion, sessionToken) { this.version = version; this.globalLsn = globalLsn; this.localLsnByregion = localLsnByregion; this.sessionToken = sessionToken; if (!this.sessionToken) { const regionAndLocalLsn = []; for (const [key, value] of this.localLsnByregion.entries()) { regionAndLocalLsn.push(`${key}${VectorSessionToken.REGION_PROGRESS_SEPARATOR}${value}`); } const regionProgress = regionAndLocalLsn.join(VectorSessionToken.SEGMENT_SEPARATOR); if (regionProgress === "") { this.sessionToken = `${this.version}${VectorSessionToken.SEGMENT_SEPARATOR}${this.globalLsn}`; } else { this.sessionToken = `${this.version}${VectorSessionToken.SEGMENT_SEPARATOR}${this.globalLsn}${VectorSessionToken.SEGMENT_SEPARATOR}${regionProgress}`; } } } static SEGMENT_SEPARATOR = "#"; static REGION_PROGRESS_SEPARATOR = "="; static create(sessionToken) { const [versionStr, globalLsnStr, ...regionSegments] = sessionToken.split( VectorSessionToken.SEGMENT_SEPARATOR ); const version = parseInt(versionStr, 10); const globalLsn = parseFloat(globalLsnStr); if (typeof version !== "number" || typeof globalLsn !== "number") { return null; } const lsnByRegion = /* @__PURE__ */ new Map(); for (const regionSegment of regionSegments) { const [regionIdStr, localLsnStr] = regionSegment.split( VectorSessionToken.REGION_PROGRESS_SEPARATOR ); if (!regionIdStr || !localLsnStr) { return null; } const regionId = parseInt(regionIdStr, 10); let localLsn; try { localLsn = localLsnStr; } catch (err) { return null; } if (typeof regionId !== "number") { return null; } lsnByRegion.set(regionId, localLsn); } return new VectorSessionToken(version, globalLsn, lsnByRegion, sessionToken); } equals(other) { return !other ? false : this.version === other.version && this.globalLsn === other.globalLsn && this.areRegionProgressEqual(other.localLsnByregion); } merge(other) { if (other == null) { throw new Error("other (Vector Session Token) must not be null"); } if (this.version === other.version && this.localLsnByregion.size !== other.localLsnByregion.size) { throw new Error( `Compared session tokens ${this.sessionToken} and ${other.sessionToken} have unexpected regions` ); } const [higherVersionSessionToken, lowerVersionSessionToken] = this.version < other.version ? [other, this] : [this, other]; const highestLocalLsnByRegion = /* @__PURE__ */ new Map(); for (const [regionId, highLocalLsn] of higherVersionSessionToken.localLsnByregion.entries()) { const lowLocalLsn = lowerVersionSessionToken.localLsnByregion.get(regionId); if (lowLocalLsn) { highestLocalLsnByRegion.set(regionId, max(highLocalLsn, lowLocalLsn)); } else if (this.version === other.version) { throw new Error( `Compared session tokens have unexpected regions. Session 1: ${this.sessionToken} - Session 2: ${this.sessionToken}` ); } else { highestLocalLsnByRegion.set(regionId, highLocalLsn); } } return new VectorSessionToken( Math.max(this.version, other.version), Math.max(this.globalLsn, other.globalLsn), highestLocalLsnByRegion ); } toString() { return this.sessionToken; } areRegionProgressEqual(other) { if (this.localLsnByregion.size !== other.size) { return false; } for (const [regionId, localLsn] of this.localLsnByregion.entries()) { const otherLocalLsn = other.get(regionId); if (localLsn !== otherLocalLsn) { return false; } } return true; } } function max(int1, int2) { if (int1.length === int2.length) { return int1 > int2 ? int1 : int2; } else if (int1.length > int2.length) { return int1; } else { return int2; } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { VectorSessionToken });