Hyperlinkv0.8.0-beta.28

HashRing

HashRing.addManyconsteffect/HashRing.ts:129
<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>

Adds 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.

When to use

Use to register or update several nodes in a HashRing at the same weight.

combinators
Source effect/HashRing.ts:12938 lines
export 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>
} = dual(
  (args) => isHashRing(args[0]),
  <A extends PrimaryKey.PrimaryKey>(self: HashRing<A>, nodes: Iterable<A>, options?: {
    readonly weight?: number | undefined
  }): HashRing<A> => {
    const weight = Math.max(options?.weight ?? 1, 0.1)
    const keys: Array<string> = []
    let toRemove: Set<string> | undefined
    for (const node of nodes) {
      const key = PrimaryKey.value(node)
      const entry = self.nodes.get(key)
      if (entry) {
        if (entry[1] === weight) continue
        toRemove ??= new Set()
        toRemove.add(key)
        self.totalWeightCache -= entry[1]
        self.totalWeightCache += weight
        entry[1] = weight
      } else {
        self.nodes.set(key, [node, weight])
        self.totalWeightCache += weight
      }
      keys.push(key)
    }
    if (toRemove) {
      self.ring = self.ring.filter(([, n]) => !toRemove.has(n))
    }
    addNodesToRing(self, keys, Math.round(weight * self.baseWeight))
    return self
  }
)
Referenced by 1 symbols