Skip to content

Instantly share code, notes, and snippets.

@ralphtheninja
Forked from deanlandolt/gist:849eb63d28f7e7bb8c97
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ralphtheninja/eac1d6bff3a1a86c6d0b to your computer and use it in GitHub Desktop.
Save ralphtheninja/eac1d6bff3a1a86c6d0b to your computer and use it in GitHub Desktop.
var test = require('tape');
var bytewise = require('bytewise');
var encode = bytewise.encode;
var MIN = bytewise.MIN;
var MAX = bytewise.MAX;
test('tuple queries', function (t) {
var yearly = tuples('reports', 'yearly');
// component keys of the tuple space query are available by index
t.equal(yearly[0], 'reports');
t.equal(yearly[1], 'yearly');
// the underlying array can be retrieved with the `valueOf` method
t.deepEqual(yearly.valueOf(), [ 'reports', 'yearly' ]);
// the tuple space query can be used as an explicit key value
// the `key` accessor is available to compute and memoize bytewise-encoded key
t.equal(yearly.key, encode([ 'reports', 'yearly' ]));
// the `range` accessor can be used to create a query stream config object
t.deepEqual(yearly.range, {
gt: encode([ 'reports', 'yearly', MIN ]),
lt: encode([ 'reports', 'yearly', MAX ])
});
// a tuple space query ranges over all child tuples spaces by default
// this can be constrained by clamping a query's range
var since2010 = yearly.clamp({ gte: 2010 });
t.deepEqual(since2010.range, {
gte: encode([ 'reports', 'yearly', 2010 ]),
lt: encode([ 'reports', 'yearly', MAX ])
});
// a clamped query can be narrowed further
var from2010to2020 = since2010.clamp({ lte: 2020 });
t.deepEqual(from2010to2020.range, {
gte: encode([ 'reports', 'yearly', 2010 ]),
lte: encode([ 'reports', 'yearly', 2020 ])
});
// and further
var from2010to2020exclusive = from2010to2020.clamp({ lt: 2020 });
t.deepEqual(from2010to2020exclusive.range, {
gte: encode([ 'reports', 'yearly', 2010 ]),
lt: encode([ 'reports', 'yearly', 2020 ])
});
// but a query's range is not automatically widened
var from2010to2020_ = from2010to2020.clamp({ gte: 2000 });
t.deepEqual(from2010to2020_.range, from2010to2020.range);
// a query may be constrained by restricting to a specific subspace
var in2015 = yearly.subspace(2015);
t.equal(in2015.key, encode([ 'reports', 'yearly', 2015 ]));
t.deepEqual(in2015.range, {
gt: encode([ 'reports', 'yearly', 2015, MIN ]),
lt: encode([ 'reports', 'yearly', 2015, MAX ])
});
// any query range can be restricted to a subspace in this way
var in2015_ = from2010to2020.subspace(2015);
t.deepEqual(in2015, in2015_);
// however the subspace key provided must exist within the range
t.equal(from2010to2020.subspace(2000), tuples.EMPTY);
// multiple subspace keys can be provided at once
var tpsReports2015 = yearly.subspace(2015, 'tps');
t.deepEqual(tpsReports2015.range, {
gt: encode([ 'reports', 'yearly', 2015, 'tps', MIN ]),
lt: encode([ 'reports', 'yearly', 2015, 'tps', MAX ])
});
t.deepEqual(tpsReports2015, in2015.subspace('tps'));
t.end();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment