use of com.hazelcast.function.FunctionEx in project hazelcast by hazelcast.
the class AllOfAggregationBuilder method build.
/**
* Builds and returns the composite {@link AggregateOperation1}. It will
* call the supplied {@code exportFinishFn} to transform the {@link ItemsByTag}
* it creates to the result type it emits as the actual result.
*
* @param exportFinishFn function that converts the {@link ItemsByTag} to
* the target result type. It must be stateless and {@linkplain
* Processor#isCooperative() cooperative}.
*/
@Nonnull
@SuppressWarnings({ "unchecked", "ConstantConditions" })
public <R> AggregateOperation1<T, Object[], R> build(@Nonnull FunctionEx<ItemsByTag, R> exportFinishFn) {
checkSerializable(exportFinishFn, "exportFinishFn");
// Avoid capturing this builder in the lambdas:
List<Tag> tags = this.tags;
List<AggregateOperation1> operations = this.operations;
return (AggregateOperation1<T, Object[], R>) AggregateOperation.withCreate(() -> {
Object[] acc = new Object[tags.size()];
Arrays.setAll(acc, i -> operations.get(i).createFn().get());
return acc;
}).andAccumulate((acc, item) -> {
for (int i = 0; i < acc.length; i++) {
operations.get(i).accumulateFn().accept(acc[i], item);
}
}).andCombine(operations.stream().anyMatch(o -> o.combineFn() == null) ? null : (acc1, acc2) -> {
for (int i = 0; i < acc1.length; i++) {
operations.get(i).combineFn().accept(acc1[i], acc2[i]);
}
}).andDeduct(operations.stream().anyMatch(o -> o.deductFn() == null) ? null : (acc1, acc2) -> {
for (int i = 0; i < acc1.length; i++) {
operations.get(i).deductFn().accept(acc1[i], acc2[i]);
}
}).andExport(acc -> {
ItemsByTag result = new ItemsByTag();
for (int i = 0; i < tags.size(); i++) {
Object exportedVal = operations.get(i).exportFn().apply(acc[i]);
result.put(tags.get(i), exportedVal);
}
return exportFinishFn.apply(result);
}).andFinish(acc -> {
ItemsByTag result = new ItemsByTag();
for (int i = 0; i < tags.size(); i++) {
Object finishedVal = operations.get(i).finishFn().apply(acc[i]);
result.put(tags.get(i), finishedVal);
}
return exportFinishFn.apply(result);
});
}
use of com.hazelcast.function.FunctionEx in project hazelcast by hazelcast.
the class CreateDagVisitor method onAggregateByKey.
public Vertex onAggregateByKey(AggregateByKeyPhysicalRel rel) {
FunctionEx<JetSqlRow, ?> groupKeyFn = rel.groupKeyFn();
AggregateOperation<?, JetSqlRow> aggregateOperation = rel.aggrOp();
Vertex vertex = dag.newUniqueVertex("AggregateByKey", Processors.aggregateByKeyP(singletonList(groupKeyFn), aggregateOperation, (key, value) -> value));
connectInput(rel.getInput(), vertex, edge -> edge.distributed().partitioned(groupKeyFn));
return vertex;
}
use of com.hazelcast.function.FunctionEx in project hazelcast by hazelcast.
the class CreateDagVisitor method onSlidingWindow.
public Vertex onSlidingWindow(SlidingWindowPhysicalRel rel) {
int orderingFieldIndex = rel.orderingFieldIndex();
FunctionEx<ExpressionEvalContext, SlidingWindowPolicy> windowPolicySupplier = rel.windowPolicyProvider();
// this vertex is used only if there's no aggregation by a window bound
Vertex vertex = dag.newUniqueVertex("Sliding-Window", flatMapUsingServiceP(ServiceFactories.nonSharedService(ctx -> {
ExpressionEvalContext evalContext = ExpressionEvalContext.from(ctx);
SlidingWindowPolicy windowPolicy = windowPolicySupplier.apply(evalContext);
return row -> WindowUtils.addWindowBounds(row, orderingFieldIndex, windowPolicy);
}), (BiFunctionEx<Function<JetSqlRow, Traverser<JetSqlRow>>, JetSqlRow, Traverser<JetSqlRow>>) Function::apply));
connectInput(rel.getInput(), vertex, null);
return vertex;
}
use of com.hazelcast.function.FunctionEx in project hazelcast by hazelcast.
the class CdcSinks method sink.
@Nonnull
private static <K, V> Sink<ChangeRecord> sink(@Nonnull String name, @Nonnull String map, @Nullable ClientConfig clientConfig, @Nonnull FunctionEx<? super ChangeRecord, ? extends K> keyFn, @Nonnull FunctionEx<? super ChangeRecord, ? extends V> valueFn) {
FunctionEx<? super ChangeRecord, ? extends V> toValueFn = record -> DELETE.equals(record.operation()) ? null : valueFn.apply(record);
String clientXml = asXmlString(clientConfig);
ProcessorSupplier supplier = AbstractHazelcastConnectorSupplier.ofMap(clientXml, procFn(name, map, clientXml, keyFn, toValueFn));
ProcessorMetaSupplier metaSupplier = ProcessorMetaSupplier.of(mapUpdatePermission(clientXml, name), supplier);
return new SinkImpl<>(name, metaSupplier, DISTRIBUTED_PARTITIONED, keyFn);
}
use of com.hazelcast.function.FunctionEx in project hazelcast by hazelcast.
the class AvroProcessors method dataFileWriterFn.
@SuppressFBWarnings(value = "RV_RETURN_VALUE_IGNORED_BAD_PRACTICE", justification = "mkdirs() returns false if the directory already existed, which is good. " + "We don't care even if it didn't exist and we failed to create it, " + "because we'll fail later when trying to create the file.")
private static <D> FunctionEx<Processor.Context, DataFileWriter<D>> dataFileWriterFn(String directoryName, String jsonSchema, SupplierEx<DatumWriter<D>> datumWriterSupplier) {
return new FunctionEx<Processor.Context, DataFileWriter<D>>() {
@Override
public DataFileWriter<D> applyEx(Processor.Context context) throws Exception {
Schema.Parser parser = new Schema.Parser();
Schema schema = parser.parse(jsonSchema);
Path directory = Paths.get(directoryName);
directory.toFile().mkdirs();
Path file = directory.resolve(String.valueOf(context.globalProcessorIndex()));
DataFileWriter<D> writer = new DataFileWriter<>(datumWriterSupplier.get());
writer.create(schema, file.toFile());
return writer;
}
@Override
public List<Permission> permissions() {
return singletonList(ConnectorPermission.file(directoryName, ACTION_WRITE));
}
};
}
Aggregations