nstream.adapter.aggr.online.OnlineComputer 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;
import swim.structure.Field;
import swim.structure.Record;
import swim.structure.Slot;
import swim.structure.Value;
public class OnlineComputer {
protected OnlineAggr[] aggrs;
protected Record kpi; // must be mutable
private int idx;
@SuppressWarnings({"unchecked", "rawtypes"})
public OnlineComputer(int rows) {
this.aggrs = new OnlineAggr[rows];
this.kpi = Record.create(rows);
}
public void addAggr(OnlineAggr aggr) {
if (this.idx >= this.aggrs.length) {
throw new IllegalStateException("OnlineComputer capacity exceeded");
}
this.aggrs[idx++] = aggr;
if (aggr.alias() == null) {
throw new IllegalArgumentException("aggr must have a defined alias()");
}
this.kpi.add(Slot.of(aggr.alias()));
}
public void receive(M v) {
if (this.idx != this.aggrs.length) {
throw new IllegalStateException("Can't receive before declaring all Aggrs");
}
for (int i = 0; i < this.aggrs.length; i++) {
final OnlineAggr aggr = this.aggrs[i];
try {
if (aggr.receive(v)) {
((Field) this.kpi.getItem(i)).setValue(aggr.resultToValue());
}
} catch (Exception e) {
new Exception("OnlineKpiComputer exception at index " + i, e).printStackTrace();
}
}
}
public Record moldState() {
final Record state = Record.create(this.aggrs.length);
for (OnlineAggr aggr : this.aggrs) {
state.add(aggr.moldState());
}
return state;
}
// public void castState(Record state) throws QuiltComputeException {
// if (state.length() != this.aggrs.length) {
// throw new QuiltComputeException(this.idx + "-length state required, received: " + state);
// }
// for (int i = 0; i < this.aggrs.length; i++) {
// try {
// this.aggrs[i].castState((Value) state.getItem(i));
// } catch ()
//
// ((Field) this.kpi.getItem(i)).setValue(this.aggrs[i].resultToValue());
// }
// }
public void reset() {
for (int i = 0; i < this.aggrs.length; i++) {
this.aggrs[i].reset();
((Field) this.kpi.getItem(i)).setValue(Value.extant());
}
}
public Value evaluate() {
return cloneKpi();
}
private Value cloneKpi() {
final Record result = Record.create(this.kpi.length());
for (int i = 0; i < this.kpi.length(); i++) {
result.add(Slot.of(this.kpi.getItem(i).key(), this.kpi.getItem(i).toValue()));
}
return result;
}
}