nstream.adapter.aggr.online.functions.Range Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nstream-adapter-aggr Show documentation
Show all versions of nstream-adapter-aggr Show documentation
Simple server-side aggregate function templates with Nstream
The newest version!
// Copyright 2015-2024 Nstream, inc.
//
// Licensed under the Redis Source Available License 2.0 (RSALv2) Agreement;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://redis.com/legal/rsalv2-agreement/
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package nstream.adapter.aggr.online.functions;
import java.math.BigInteger;
import nstream.adapter.aggr.online.OnlineAggr;
import nstream.adapter.aggr.online.OnlineAggrException;
import swim.structure.Item;
import swim.structure.Num;
import swim.structure.Record;
import swim.structure.Text;
import swim.structure.Value;
public abstract class Range extends OnlineAggr {
protected N min;
protected N max;
protected Range(OnlineAggr.Builder builder) {
super(builder);
}
@Override
public void reset() {
this.min = null;
this.max = null;
}
@Override
public Value moldState() {
return Record.create(2)
.slot(MIN, (this.min == null) ? Value.extant() : Num.from(this.min))
.slot(MAX, (this.max == null) ? Value.extant() : Num.from(this.max));
}
@Override
public void castState(Value v) throws OnlineAggrException {
AggrsUtil.validateStateSize(v.length(), 2);
final Item zero = v.getItem(0);
AggrsUtil.validateSlot(zero, MIN);
final Item one = v.getItem(1);
AggrsUtil.validateSlot(one, MAX);
if (!zero.toValue().isDistinct()) {
reset();
return;
}
this.min = AggrsUtil.extractN(zero, MIN.stringValue());
this.max = AggrsUtil.extractN(one, MAX.stringValue());
}
@Override
public Value evaluate() {
return this.min == null ? Value.extant()
: Record.create(2).item(Num.from(this.min)).item(Num.from(this.max));
}
@Override
public Value resultToValue() {
return evaluate();
}
public static Range forDouble(Builder builder) {
return new DoubleRange<>(builder);
}
public static Range forLong(Builder builder) {
return new LongRange<>(builder);
}
public static Range forBigInteger(Builder builder) {
return new BigIntegerRange<>(builder);
}
private static final Text MIN = Text.from("min");
private static final Text MAX = Text.from("max");
}
class LongRange extends Range {
protected LongRange(Builder builder) {
super(builder);
}
@Override
protected void reduce(Long field) {
if (this.min == null) {
this.min = field;
this.max = field;
} else {
this.min = Long.min(this.min, field);
this.max = Long.max(this.max, field);
}
}
}
class DoubleRange extends Range {
protected DoubleRange(Builder builder) {
super(builder);
}
@Override
protected void reduce(Double field) {
if (this.min == null) {
this.min = field;
this.max = field;
} else {
this.min = Double.min(this.min, field);
this.max = Double.max(this.max, field);
}
}
}
class BigIntegerRange extends Range {
protected BigIntegerRange(Builder builder) {
super(builder);
}
@Override
protected void reduce(BigInteger field) {
if (this.min == null) {
this.min = field;
this.max = field;
} else {
this.min = this.min.min(field);
this.max = this.max.max(field);
}
}
}