Search in sources :

Example 41 with LongAccumulator

use of com.hazelcast.jet.accumulator.LongAccumulator 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 42 with LongAccumulator

use of com.hazelcast.jet.accumulator.LongAccumulator 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 43 with LongAccumulator

use of com.hazelcast.jet.accumulator.LongAccumulator 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)43 Test (org.junit.Test)32 Category (org.junit.experimental.categories.Category)24 RunWith (org.junit.runner.RunWith)20 QuickTest (com.hazelcast.test.annotation.QuickTest)19 Arrays (java.util.Arrays)17 List (java.util.List)17 Assert.assertEquals (org.junit.Assert.assertEquals)17 Job (com.hazelcast.jet.Job)16 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)16 HazelcastInstance (com.hazelcast.core.HazelcastInstance)14 FunctionEx (com.hazelcast.function.FunctionEx)14 Pipeline (com.hazelcast.jet.pipeline.Pipeline)14 Util.entry (com.hazelcast.jet.Util.entry)13 HazelcastParallelClassRunner (com.hazelcast.test.HazelcastParallelClassRunner)13 Entry (java.util.Map.Entry)13 SupplierEx (com.hazelcast.function.SupplierEx)11 Sinks (com.hazelcast.jet.pipeline.Sinks)11 Tag.tag0 (com.hazelcast.jet.datamodel.Tag.tag0)10 Tag.tag1 (com.hazelcast.jet.datamodel.Tag.tag1)10