@subspace/rendezvous-hash
TypeScript icon, indicating that this package has built-in type declarations

0.0.2 • Public • Published

Rendezvous hash implementation

Read https://en.wikipedia.org/wiki/Rendezvous_hashing for details, this package works with 64-bit/8-byte inputs in form of Uint8Arrays.

How to install

npm install @subspace/rendezvous-hash

How to use

TypeScript:

import {randomBytes} from 'crypto';
import {Destination, pickDestinations} from '@subspace/rendezvous-hash';

const destinations: Destination[] = [
    new Destination(randomBytes(8)),
    new Destination(randomBytes(8)),
];
const keys: Uint8Array[] = [
    randomBytes(8),
    randomBytes(8),
    randomBytes(8),
    randomBytes(8),
    randomBytes(8),
];

for (const key of keys) {
    const destination = pickDestinations(key, destinations)[0];
    console.log(destination.id)
}

API

rendezvousHash.pickDestinations(keyHash: Uint8Array, destinations: Destination[], k: number = 1): Uint8Array[]

Takes a key and an array of potential destinations and returns k closest destinations to that key.

  • keyHash - 8 bytes hash of the key
  • destinations - an array of destination objects that are potential candidates
  • k - how many closest destinations for specified key to return

Returns an array of IDs of keys (first argument to Destination object below).

rendezvousHash.Destination(readonly id: Uint8Array, readonly weight: number = 1)

Class that encapsulates information about particular destination.

id and weight are pubic readonly properties of resulting object

Readme

Keywords

Package Sidebar

Install

npm i @subspace/rendezvous-hash

Weekly Downloads

0

Version

0.0.2

License

MIT

Unpacked Size

9.5 kB

Total Files

5

Last publish

Collaborators

  • subserge
  • leo_subspace
  • rg3l3dr
  • nazar-pc