EdgeAIG's picture
download
raw
4.14 kB
/**
* The `ShardId` module models the address of a shard inside an Effect Cluster
* shard group. A shard id is made from a string `group` and numeric `id`, and
* the module gives that pair stable equality, hashing, primary-key behavior,
* schema support, and conversion to and from the `group:id` string form used by
* routing and storage boundaries.
*
* @since 4.0.0
*/
import * as Equal from "../../Equal.js";
import * as Hash from "../../Hash.js";
import { hasProperty } from "../../Predicate.js";
import * as PrimaryKey from "../../PrimaryKey.js";
import * as S from "../../Schema.js";
import * as SchemaGetter from "../../SchemaGetter.js";
const TypeId = "~effect/cluster/ShardId";
/**
* Returns `true` when the value carries the `ShardId` runtime marker.
*
* @category guards
* @since 4.0.0
*/
export const isShardId = u => hasProperty(u, TypeId);
/**
* Schema for shard identifiers encoded as `{ group, id }` objects and decoded
* via `make`.
*
* @category schemas
* @since 4.0.0
*/
export const ShardId = /*#__PURE__*/S.declare(isShardId, {
toCodecJson: () => S.link()(S.Struct({
group: S.String,
id: S.Number
}), {
decode: SchemaGetter.transform(({
group,
id
}) => make(group, id)),
encode: SchemaGetter.passthrough()
})
});
/**
* Creates or reuses the cached `ShardId` for the specified shard group and numeric
* id.
*
* **When to use**
*
* Use to create a `ShardId` when the shard group and numeric id are already
* known, such as after a routing decision or after decoding stored shard-id
* parts.
*
* **Details**
*
* Repeated calls with the same `group` and `id` return the same cached
* `ShardId` instance. The returned value stores those fields, compares by
* `group` and `id`, formats as `group:id`, and uses that string form for
* hashing and primary keys.
*
* **Gotchas**
*
* `make` does not compute a shard from an entity id or check whether the shard
* belongs to the current sharding configuration. Pass the shard group and
* numeric id produced by the routing or storage layer.
*
* @see {@link toString} for formatting an existing shard id as `group:id`
* @see {@link fromString} for constructing a cached shard id from the `group:id` string form
*
* @category constructors
* @since 4.0.0
*/
export const make = (group, id) => {
const key = `${group}:${id}`;
let shardId = shardIdCache.get(key);
if (!shardId) {
shardId = makeProto(group, id);
shardIdCache.set(key, shardId);
}
return shardId;
};
const shardIdCache = /*#__PURE__*/new Map();
const makeProto = (group, id) => {
const self = Object.create(ShardIdProto);
self.group = group;
self.id = id;
return self;
};
const ShardIdProto = {
[TypeId]: TypeId,
[Equal.symbol](that) {
return this.group === that.group && this.id === that.id;
},
[Hash.symbol]() {
return Hash.string(this.toString());
},
[PrimaryKey.symbol]() {
return this.toString();
},
toString() {
return `${this.group}:${this.id}`;
}
};
/**
* Formats a shard identifier as `group:id`.
*
* @category converting
* @since 4.0.0
*/
export const toString = shardId => {
return `${shardId.group}:${shardId.id}`;
};
/**
* Parses a `group:id` string into plain shard id parts.
*
* **Details**
*
* Throws an `Error` when the string has no colon separator or the id segment is
* not numeric.
*
* @category decoding
* @since 4.0.0
*/
export function fromStringEncoded(s) {
const index = s.lastIndexOf(":");
if (index === -1) {
throw new Error(`Invalid ShardId format`);
}
const group = s.substring(0, index);
const id = Number(s.substring(index + 1));
if (isNaN(id)) {
throw new Error(`ShardId id must be a number`);
}
return {
group,
id
};
}
/**
* Parses a `group:id` string into a cached `ShardId`.
*
* **Details**
*
* Throws an `Error` when the string has no colon separator or the id segment is
* not numeric.
*
* @category decoding
* @since 4.0.0
*/
export function fromString(s) {
const encoded = fromStringEncoded(s);
return make(encoded.group, encoded.id);
}
//# sourceMappingURL=ShardId.js.map

Xet Storage Details

Size:
4.14 kB
·
Xet hash:
101de56259f8330f085fcb3a2b9157fba1a671954cc197de2859a576b6c9c359

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.