https://github.com/autonomys/rendezvous-hash
https://github.com/autonomys/rendezvous-hash
hashing rendezvous
Last synced: over 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/autonomys/rendezvous-hash
- Owner: autonomys
- Created: 2018-09-16T15:06:50.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2019-05-28T18:37:27.000Z (about 7 years ago)
- Last Synced: 2025-03-04T14:49:29.443Z (over 1 year ago)
- Topics: hashing, rendezvous
- Language: TypeScript
- Size: 8.79 KB
- Stars: 0
- Watchers: 9
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# 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 `Uint8Array`s.
## How to install
```
npm install @subspace/rendezvous-hash
```
## How to use
TypeScript:
```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