Search in sources :

Example 1 with DistributedBiConsumer

use of com.hazelcast.jet.function.DistributedBiConsumer in project hazelcast-jet by hazelcast.

the class AggregateOperation2Test method when_withCombiningAccumulateFn_then_accumulateFnCombines.

@Test
public void when_withCombiningAccumulateFn_then_accumulateFnCombines() {
    // Given
    AggregateOperation2<Object, Object, LongAccumulator, LongAccumulator> aggrOp = AggregateOperation.withCreate(LongAccumulator::new).andAccumulate0((acc, item) -> acc.addAllowingOverflow(1)).andAccumulate1((acc, item) -> acc.addAllowingOverflow(10)).andCombine(LongAccumulator::addAllowingOverflow).andIdentityFinish();
    AggregateOperation1<LongAccumulator, LongAccumulator, LongAccumulator> combiningAggrOp = aggrOp.withCombiningAccumulateFn(wholeItem());
    DistributedBiConsumer<? super LongAccumulator, ? super LongAccumulator> accFn = combiningAggrOp.accumulateFn();
    LongAccumulator partialAcc1 = combiningAggrOp.createFn().get();
    LongAccumulator partialAcc2 = combiningAggrOp.createFn().get();
    LongAccumulator combinedAcc = combiningAggrOp.createFn().get();
    // When
    partialAcc1.set(2);
    partialAcc2.set(3);
    accFn.accept(combinedAcc, partialAcc1);
    accFn.accept(combinedAcc, partialAcc2);
    // Then
    assertEquals(5, combinedAcc.get());
}
Also used : LongAccumulator(com.hazelcast.jet.accumulator.LongAccumulator) DistributedSupplier(com.hazelcast.jet.function.DistributedSupplier) DistributedBiConsumer(com.hazelcast.jet.function.DistributedBiConsumer) RunWith(org.junit.runner.RunWith) Tag.tag2(com.hazelcast.jet.datamodel.Tag.tag2) Test(org.junit.Test) Tag.tag1(com.hazelcast.jet.datamodel.Tag.tag1) DistributedFunctions.wholeItem(com.hazelcast.jet.function.DistributedFunctions.wholeItem) Tag.tag0(com.hazelcast.jet.datamodel.Tag.tag0) Assert.assertSame(org.junit.Assert.assertSame) LongAccumulator(com.hazelcast.jet.accumulator.LongAccumulator) HazelcastParallelClassRunner(com.hazelcast.test.HazelcastParallelClassRunner) DistributedFunction(com.hazelcast.jet.function.DistributedFunction) Assert.assertEquals(org.junit.Assert.assertEquals) Test(org.junit.Test)

Example 2 with DistributedBiConsumer

use of com.hazelcast.jet.function.DistributedBiConsumer in project hazelcast-jet by hazelcast.

the class AggregateOperation3Test method when_withCombiningAccumulateFn_then_accumulateFnCombines.

@Test
public void when_withCombiningAccumulateFn_then_accumulateFnCombines() {
    // Given
    AggregateOperation3<Object, Object, Object, LongAccumulator, LongAccumulator> aggrOp = AggregateOperation.withCreate(LongAccumulator::new).andAccumulate0((acc, item) -> acc.addAllowingOverflow(1)).andAccumulate1((acc, item) -> acc.addAllowingOverflow(10)).andAccumulate2((acc, item) -> acc.addAllowingOverflow(100)).andCombine(LongAccumulator::addAllowingOverflow).andIdentityFinish();
    AggregateOperation1<LongAccumulator, LongAccumulator, LongAccumulator> combiningAggrOp = aggrOp.withCombiningAccumulateFn(wholeItem());
    DistributedBiConsumer<? super LongAccumulator, ? super LongAccumulator> accFn = combiningAggrOp.accumulateFn();
    LongAccumulator partialAcc1 = combiningAggrOp.createFn().get();
    LongAccumulator partialAcc2 = combiningAggrOp.createFn().get();
    LongAccumulator combinedAcc = combiningAggrOp.createFn().get();
    // When
    partialAcc1.set(2);
    partialAcc2.set(3);
    accFn.accept(combinedAcc, partialAcc1);
    accFn.accept(combinedAcc, partialAcc2);
    // Then
    assertEquals(5, combinedAcc.get());
}
Also used : LongAccumulator(com.hazelcast.jet.accumulator.LongAccumulator) DistributedSupplier(com.hazelcast.jet.function.DistributedSupplier) DistributedBiConsumer(com.hazelcast.jet.function.DistributedBiConsumer) RunWith(org.junit.runner.RunWith) Tag.tag2(com.hazelcast.jet.datamodel.Tag.tag2) Test(org.junit.Test) Tag.tag1(com.hazelcast.jet.datamodel.Tag.tag1) DistributedFunctions.wholeItem(com.hazelcast.jet.function.DistributedFunctions.wholeItem) Tag.tag0(com.hazelcast.jet.datamodel.Tag.tag0) Assert.assertSame(org.junit.Assert.assertSame) Tag.tag(com.hazelcast.jet.datamodel.Tag.tag) LongAccumulator(com.hazelcast.jet.accumulator.LongAccumulator) HazelcastParallelClassRunner(com.hazelcast.test.HazelcastParallelClassRunner) DistributedFunction(com.hazelcast.jet.function.DistributedFunction) Assert.assertEquals(org.junit.Assert.assertEquals) Test(org.junit.Test)

Example 3 with DistributedBiConsumer

use of com.hazelcast.jet.function.DistributedBiConsumer in project hazelcast-jet by hazelcast.

the class AggregateOperationTest method when_withCombiningAccumulateFn_then_accumulateFnCombines.

@Test
public void when_withCombiningAccumulateFn_then_accumulateFnCombines() {
    // Given
    AggregateOperation<LongAccumulator, Long> aggrOp = AggregateOperation.withCreate(LongAccumulator::new).andAccumulate(tag0(), (acc, item) -> acc.addAllowingOverflow(1)).andAccumulate(tag1(), (acc, item) -> acc.addAllowingOverflow(10)).andCombine(LongAccumulator::addAllowingOverflow).andFinish(LongAccumulator::get);
    AggregateOperation1<LongAccumulator, LongAccumulator, Long> combiningAggrOp = aggrOp.withCombiningAccumulateFn(wholeItem());
    DistributedBiConsumer<? super LongAccumulator, ? super Object> accFn = combiningAggrOp.accumulateFn(tag0());
    LongAccumulator partialAcc1 = combiningAggrOp.createFn().get();
    LongAccumulator partialAcc2 = combiningAggrOp.createFn().get();
    LongAccumulator combinedAcc = combiningAggrOp.createFn().get();
    // When
    partialAcc1.set(2);
    partialAcc2.set(3);
    accFn.accept(combinedAcc, partialAcc1);
    accFn.accept(combinedAcc, partialAcc2);
    // Then
    assertEquals(5, combinedAcc.get());
}
Also used : LongAccumulator(com.hazelcast.jet.accumulator.LongAccumulator) DistributedSupplier(com.hazelcast.jet.function.DistributedSupplier) DistributedBiConsumer(com.hazelcast.jet.function.DistributedBiConsumer) RunWith(org.junit.runner.RunWith) Tag.tag2(com.hazelcast.jet.datamodel.Tag.tag2) Test(org.junit.Test) Tag.tag1(com.hazelcast.jet.datamodel.Tag.tag1) DistributedFunctions.wholeItem(com.hazelcast.jet.function.DistributedFunctions.wholeItem) Tag.tag0(com.hazelcast.jet.datamodel.Tag.tag0) Assert.assertSame(org.junit.Assert.assertSame) LongAccumulator(com.hazelcast.jet.accumulator.LongAccumulator) HazelcastParallelClassRunner(com.hazelcast.test.HazelcastParallelClassRunner) DistributedFunction(com.hazelcast.jet.function.DistributedFunction) Assert.assertEquals(org.junit.Assert.assertEquals) Test(org.junit.Test)

Aggregations

LongAccumulator (com.hazelcast.jet.accumulator.LongAccumulator)3 Tag.tag0 (com.hazelcast.jet.datamodel.Tag.tag0)3 Tag.tag1 (com.hazelcast.jet.datamodel.Tag.tag1)3 Tag.tag2 (com.hazelcast.jet.datamodel.Tag.tag2)3 DistributedBiConsumer (com.hazelcast.jet.function.DistributedBiConsumer)3 DistributedFunction (com.hazelcast.jet.function.DistributedFunction)3 DistributedFunctions.wholeItem (com.hazelcast.jet.function.DistributedFunctions.wholeItem)3 DistributedSupplier (com.hazelcast.jet.function.DistributedSupplier)3 HazelcastParallelClassRunner (com.hazelcast.test.HazelcastParallelClassRunner)3 Assert.assertEquals (org.junit.Assert.assertEquals)3 Assert.assertSame (org.junit.Assert.assertSame)3 Test (org.junit.Test)3 RunWith (org.junit.runner.RunWith)3 Tag.tag (com.hazelcast.jet.datamodel.Tag.tag)1