hash-join
compute https://en.wikipedia.org/wiki/Hash_join on streaming document collections
example
hashing with functions
var join = ;var r =;r;r;r;r;r;
$ node example/hash.js
{ abc123:
{ commit: 'd8180b0778dd7a145b46f92b9a2d77db916debc5',
output: [ 'beep ' ] } }
{ abc123:
{ commit: 'd8180b0778dd7a145b46f92b9a2d77db916debc5',
output: [ 'beep ', 'boop\n' ] } }
using key path shortcuts
var join = ;var r = ;r;r;r;r;r;
$ node example/reduce.js
{ abc123:
{ commit: 'd8180b0778dd7a145b46f92b9a2d77db916debc5',
output: [ 'beep ' ] } }
{ abc123:
{ commit: 'd8180b0778dd7a145b46f92b9a2d77db916debc5',
output: [ 'beep ', 'boop\n' ] } }
crdt
usingvar join = ;var r = ;r;r;var doc = newDoc;doc;doc;doc;doc;
$ node example/reduce_crdt.js
{ abc123:
{ commit: 'd8180b0778dd7a145b46f92b9a2d77db916debc5',
output: [ 'beep ' ] } }
{ abc123:
{ commit: 'd8180b0778dd7a145b46f92b9a2d77db916debc5',
output: [ 'beep ', 'boop\n' ] } }
methods
var join =
var r = join(a, b)
Create a new 2-way join r
from the hashing functions a
and b
.
Hashing functions should return falsy values if they don't match and otherwise should return the string id they should hash into.
If a
or b
is an array, they are treated as a path through the document to
obtain the hashed element starting at the document's 'type'
field and deeply
traversing for the rest of the arguments. For example ['beep','boop','x',2]
would resolve to the string "ghi"
for this document:
If a
or b
is a string, they are converted to [a,'id']
or [b,'id']
accordingly so that you can more tersely express a foreign key relation on the
"id"
field.
r.insert(doc)
Insert a document into the join r
.
r.reduce(fn, init)
Compute a reduce over all the pairs that resolve to the same hash id with
fn(acc, a, b)
for an accumulator acc
and the objects a
and b
that match
the hashing functions supplied to or inferred from join()
in the same order.
fn(acc, a, b)
should return the new accumulator value acc
.
Each time a new value is derived, the 'result'
event fires with the new
accumulator result.
The accumulator starts off with the value given by init
.
events
r.on('result', function (res) {})
Whenever a new result value is obtained from the reduce()
, this event fires
with the new accumulator value.
r.on('pair', function (a, b) {})
Whenever a pair is matched, this event fires with each item in the pair in the
order specified to join()
.
install
With npm do:
npm install hash-join
license
MIT