@azure/event-hubs
Version:
Azure Event Hubs SDK for JS.
78 lines (77 loc) • 3.22 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 partitionAssigner_exports = {};
__export(partitionAssigner_exports, {
PartitionAssigner: () => PartitionAssigner
});
module.exports = __toCommonJS(partitionAssigner_exports);
var import_core_util = require("@azure/core-util");
var import_partitionKeyToIdMapper = require("./partitionKeyToIdMapper.js");
class PartitionAssigner {
_partitions = [];
_lastRoundRobinPartitionIndex = -1;
/**
* Set the partition ids that can be used when assigning a partition.
* @param partitionIds - All valid partition ids.
*/
setPartitionIds(partitionIds) {
this._partitions = partitionIds;
}
/**
* Returns a partitionId from the list of partition ids set via `setPartitionIds`.
*
* If a partitionId is specified, then that will be returned directly.
* If a partitionKey is specified, then a partitionId will be calculated based on the partitionKey.
* Specifying both partitionId and partitionKey results in an error.
*
* If neither partitionId nor partitionKey are specified, then a partitionId will be selected
* based on a round-robin approach.
*/
assignPartition({
partitionId,
partitionKey
}) {
if ((0, import_core_util.isDefined)(partitionId) && (0, import_core_util.isDefined)(partitionKey)) {
throw new Error(
`The partitionId (${partitionId}) and partitionKey (${partitionKey}) cannot both be specified.`
);
}
if (!this._partitions.length) {
throw new Error(`Unable to determine partitionIds, can't assign partitionId.`);
}
if ((0, import_core_util.isDefined)(partitionId) && this._partitions.includes(partitionId)) {
return partitionId;
}
if ((0, import_core_util.isDefined)(partitionKey)) {
return (0, import_partitionKeyToIdMapper.mapPartitionKeyToId)(partitionKey, this._partitions.length).toString();
}
return this._assignRoundRobinPartition();
}
_assignRoundRobinPartition() {
const maxPartitionIndex = this._partitions.length - 1;
const proposedPartitionIndex = this._lastRoundRobinPartitionIndex + 1;
const nextPartitionIndex = proposedPartitionIndex > maxPartitionIndex ? 0 : proposedPartitionIndex;
this._lastRoundRobinPartitionIndex = nextPartitionIndex;
return this._partitions[nextPartitionIndex];
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
PartitionAssigner
});
//# sourceMappingURL=partitionAssigner.js.map