Skip to main content Link Search Menu Expand Document (external link)

HashRing.ts overview

Since v3.19.0


Exports Grouped by Category


Combinators

add

Add a new node to the ring. If the node already exists in the ring, it will be updated. For example, you can use this to update the node’s weight.

Signature

declare const add: {
  <A extends PrimaryKey.PrimaryKey>(
    node: A,
    options?: { readonly weight?: number | undefined }
  ): (self: HashRing<A>) => HashRing<A>
  <A extends PrimaryKey.PrimaryKey>(
    self: HashRing<A>,
    node: A,
    options?: { readonly weight?: number | undefined }
  ): HashRing<A>
}

Source

Since v3.19.0

addMany

Add new nodes to the ring. If a node already exists in the ring, it will be updated. For example, you can use this to update the node’s weight.

Signature

declare const addMany: {
  <A extends PrimaryKey.PrimaryKey>(
    nodes: Iterable<A>,
    options?: { readonly weight?: number | undefined }
  ): (self: HashRing<A>) => HashRing<A>
  <A extends PrimaryKey.PrimaryKey>(
    self: HashRing<A>,
    nodes: Iterable<A>,
    options?: { readonly weight?: number | undefined }
  ): HashRing<A>
}

Source

Since v3.19.0

get

Gets the node which should handle the given input. Returns undefined if the hashring has no elements with weight.

Signature

declare const get: <A extends PrimaryKey.PrimaryKey>(self: HashRing<A>, input: string) => A | undefined

Source

Since v3.19.0

getShards

Distributes count shards across the nodes in the ring, attempting to balance the number of shards allocated to each node. Returns undefined if the hashring has no elements with weight.

Signature

declare const getShards: <A extends PrimaryKey.PrimaryKey>(self: HashRing<A>, count: number) => Array<A> | undefined

Source

Since v3.19.0

has

Signature

declare const has: {
  <A extends PrimaryKey.PrimaryKey>(node: A): (self: HashRing<A>) => boolean
  <A extends PrimaryKey.PrimaryKey>(self: HashRing<A>, node: A): boolean
}

Source

Since v3.19.0

remove

Removes the node from the ring. No-op’s if the node does not exist.

Signature

declare const remove: {
  <A extends PrimaryKey.PrimaryKey>(node: A): (self: HashRing<A>) => HashRing<A>
  <A extends PrimaryKey.PrimaryKey>(self: HashRing<A>, node: A): HashRing<A>
}

Source

Since v3.19.0

Constructors

make

Signature

declare const make: <A extends PrimaryKey.PrimaryKey>(options?: {
  readonly baseWeight?: number | undefined
}) => HashRing<A>

Source

Since v3.19.0

Guards

isHashRing

Signature

declare const isHashRing: (u: unknown) => u is HashRing<any>

Source

Since v3.19.0

Models

HashRing (interface)

Signature

export interface HashRing<A extends PrimaryKey.PrimaryKey> extends Pipeable, Iterable<A> {
  readonly [TypeId]: typeof TypeId
  readonly baseWeight: number
  totalWeightCache: number
  readonly nodes: Map<string, [node: A, weight: number]>
  ring: Array<[hash: number, node: string]>
}

Source

Since v3.19.0