UNPKG

diffusion

Version:

Diffusion JavaScript client

202 lines (162 loc) 5.71 kB
var RangeQueryParameters = require('features/time-series/range-query-parameters'); var Range = require('features/time-series/range'); var QueryType = RangeQueryParameters.QueryType; var BytesImpl = require('data/bytes-impl'); var Emitter = require('events/emitter'); var Result = require('events/result'); function VIEW_RANGE(original, op) { return original.withViewRange(op(original.viewRange)); } function EDIT_RANGE(original, op) { return original.withEditRange(op(original.editRange)); } function RangeQueryImpl(context, valueType, parameters, mode) { this.valueType = valueType; this.parameters = parameters; this.mode = mode; function withCurrentRange(op) { return new RangeQueryImpl(context, valueType, mode(parameters, op), mode); } this.selectFrom = function(topicPath) { var emitter = new Emitter(); var result = new Result(emitter); if (!topicPath) { emitter.error(new Error("Topic path is null")); return result; } var request = { topicPath : topicPath, parameters : parameters }; context.send(request, function(err, response) { if (err) { emitter.error(err); } else { emitter.emit('complete', response.createQueryResult( valueType, parameters.queryType.streamStructure )); } }); return result; }; this.forValues = function() { return new RangeQueryImpl( context, valueType, parameters.withQueryType(QueryType.VALUES).withViewRange(Range.DEFAULT_RANGE), VIEW_RANGE ); }; this.forEdits = function() { return new RangeQueryImpl( context, valueType, parameters.withQueryType(QueryType.ALL_EDITS).withViewRange(Range.DEFAULT_RANGE), VIEW_RANGE); }; this.editRange = function() { if (parameters.queryType !== QueryType.VALUES) { throw new Error(".editRange() cannot be applied to this edit range query"); } return new RangeQueryImpl(context, valueType, parameters, EDIT_RANGE); }; this.allEdits = function() { if (parameters.queryType === QueryType.VALUES) { throw new Error(".allEdits() cannot be applied to this value range query"); } return new RangeQueryImpl(context, valueType, parameters.withQueryType(QueryType.ALL_EDITS), EDIT_RANGE); }; this.latestEdits = function() { if (parameters.queryType === QueryType.VALUES) { throw new Error(".latestEdits() cannot be applied to this value range query"); } return new RangeQueryImpl(context, valueType, parameters.withQueryType(QueryType.LATEST_EDITS), EDIT_RANGE); }; this.limit = function(count) { return new RangeQueryImpl(context, valueType, parameters.withLimit(count), mode); }; this.as = function(newValueType) { // Allow datatypes to be provided if (newValueType && newValueType.valueClass) { return new RangeQueryImpl(context, newValueType.valueClass, parameters, mode); } else if (newValueType instanceof Function) { return new RangeQueryImpl(context, newValueType, parameters, mode); } else { throw new Error("Invalid value type: " + newValueType); } }; this.from = function(sequence) { return withCurrentRange(function(r) { return r.from(sequence); }); }; this.fromStart = function() { return withCurrentRange(function(r) { return r.fromStart(); }); }; this.fromLast = function(count) { return withCurrentRange(function(r) { return r.fromLast(count); }); }; this.fromLastMillis = function(timespan) { return withCurrentRange(function(r) { return r.fromLastMillis(timespan); }); }; this.to = function(sequence) { return withCurrentRange(function(r) { return r.to(sequence); }); }; this.toStart = function() { return withCurrentRange(function(r) { return r.toStart(); }); }; this.untilLast = function(count) { return withCurrentRange(function(r) { return r.untilLast(count); }); }; this.untilLastMillis = function(timespan) { return withCurrentRange(function(r) { return r.untilLastMillis(timespan); }); }; this.next = function(count) { return withCurrentRange(function(r) { return r.next(count); }); }; this.nextMillis = function(timespan) { return withCurrentRange(function(r) { return r.nextMillis(timespan); }); }; this.previous = function(count) { return withCurrentRange(function(r) { return r.previous(count); }); }; this.previousMillis = function(timespan) { return withCurrentRange(function(r) { return r.previousMillis(timespan); }); }; this.equals = function(other) { if (other && other instanceof RangeQueryImpl) { return parameters.equals(other.parameters); } return false; }; } RangeQueryImpl.prototype.toString = function() { return this.parameters.toString(); }; RangeQueryImpl.createDefault = function(service) { return new RangeQueryImpl(service, BytesImpl, RangeQueryParameters.DEFAULT_RANGE_QUERY, VIEW_RANGE); }; module.exports = RangeQueryImpl;