@azure/event-hubs
Version:
Azure Event Hubs SDK for JS.
112 lines (111 loc) • 4.62 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 inMemoryCheckpointStore_exports = {};
__export(inMemoryCheckpointStore_exports, {
InMemoryCheckpointStore: () => InMemoryCheckpointStore
});
module.exports = __toCommonJS(inMemoryCheckpointStore_exports);
var import_error = require("./util/error.js");
var import_utils = require("./util/utils.js");
class InMemoryCheckpointStore {
_partitionOwnershipMap = /* @__PURE__ */ new Map();
_committedCheckpoints = /* @__PURE__ */ new Map();
/**
* Get the list of all existing partition ownership from the underlying data store. Could return empty
* results if there are is no existing ownership information.
*
* @param fullyQualifiedNamespace - The fully qualified Event Hubs namespace. This is likely to be similar to
* <yournamespace>.servicebus.windows.net.
* @param eventHubName - The event hub name.
* @param consumerGroup - The consumer group name.
* @returns Partition ownership details of all the partitions that have/had an owner..
*/
async listOwnership(_fullyQualifiedNamespace, _eventHubName, _consumerGroup) {
const ownerships = [];
for (const value of this._partitionOwnershipMap.values()) {
ownerships.push({ ...value });
}
return ownerships;
}
/**
* Claim ownership of a list of partitions. This will return the list of partitions that were owned
* successfully.
*
* @param partitionOwnership - The list of partition ownership this instance is claiming to own.
* @returns A list partitions this instance successfully claimed ownership.
*/
async claimOwnership(partitionOwnership) {
const claimedOwnerships = [];
for (const ownership of partitionOwnership) {
if (!this._partitionOwnershipMap.has(ownership.partitionId) || this._partitionOwnershipMap.get(ownership.partitionId).etag === ownership.etag) {
const date = /* @__PURE__ */ new Date();
const newOwnership = {
...ownership,
etag: (0, import_utils.getRandomName)(),
lastModifiedTimeInMs: date.getTime()
};
this._partitionOwnershipMap.set(newOwnership.partitionId, newOwnership);
claimedOwnerships.push(newOwnership);
}
}
return claimedOwnerships;
}
/**
* Updates the checkpoint in the data store for a partition.
*
* @param checkpoint - The checkpoint.
*/
async updateCheckpoint(checkpoint) {
(0, import_error.throwTypeErrorIfParameterMissing)(
"",
"updateCheckpoint",
"sequenceNumber",
checkpoint.sequenceNumber
);
(0, import_error.throwTypeErrorIfParameterMissing)("", "updateCheckpoint", "offset", checkpoint.offset);
checkpoint = { ...checkpoint };
const partitionOwnership = this._partitionOwnershipMap.get(checkpoint.partitionId);
if (partitionOwnership) {
partitionOwnership.etag = (0, import_utils.getRandomName)();
const key = `${checkpoint.fullyQualifiedNamespace}:${checkpoint.eventHubName}:${checkpoint.consumerGroup}`;
let partitionMap = this._committedCheckpoints.get(key);
if (partitionMap == null) {
partitionMap = /* @__PURE__ */ new Map();
this._committedCheckpoints.set(key, partitionMap);
}
partitionMap.set(checkpoint.partitionId, checkpoint);
}
}
async listCheckpoints(fullyQualifiedNamespace, eventHubName, consumerGroup) {
const key = `${fullyQualifiedNamespace}:${eventHubName}:${consumerGroup}`;
const partitionMap = this._committedCheckpoints.get(key);
if (partitionMap == null) {
return [];
}
const checkpoints = [];
for (const value of partitionMap.values()) {
checkpoints.push({ ...value });
}
return checkpoints;
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
InMemoryCheckpointStore
});
//# sourceMappingURL=inMemoryCheckpointStore.js.map