Allow BigInt/BigInteger increments/decrements for PNCounter #25543
This commit is contained in:
parent
4456d7752b
commit
18b2d85216
3 changed files with 85 additions and 3 deletions
|
|
@ -62,6 +62,12 @@ final class PNCounter private[akka] (
|
||||||
*/
|
*/
|
||||||
def +(n: Long)(implicit node: Cluster): PNCounter = increment(node, n)
|
def +(n: Long)(implicit node: Cluster): PNCounter = increment(node, n)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Increment the counter with the delta `n` specified.
|
||||||
|
* If the delta is negative then it will decrement instead of increment.
|
||||||
|
*/
|
||||||
|
def +(n: BigInt)(implicit node: Cluster): PNCounter = increment(node, n)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Increment the counter with the delta `n` specified.
|
* Increment the counter with the delta `n` specified.
|
||||||
* If the delta is negative then it will decrement instead of increment.
|
* If the delta is negative then it will decrement instead of increment.
|
||||||
|
|
@ -69,12 +75,32 @@ final class PNCounter private[akka] (
|
||||||
def increment(node: Cluster, n: Long = 1): PNCounter =
|
def increment(node: Cluster, n: Long = 1): PNCounter =
|
||||||
increment(node.selfUniqueAddress, n)
|
increment(node.selfUniqueAddress, n)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Scala API: Increment the counter with the delta `n` specified.
|
||||||
|
* If the delta is negative then it will decrement instead of increment.
|
||||||
|
*/
|
||||||
|
def increment(node: Cluster, n: BigInt): PNCounter =
|
||||||
|
increment(node.selfUniqueAddress, n)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Java API: Increment the counter with the delta `n` specified.
|
||||||
|
* If the delta is negative then it will decrement instead of increment.
|
||||||
|
*/
|
||||||
|
def increment(node: Cluster, n: java.math.BigInteger): PNCounter =
|
||||||
|
increment(node.selfUniqueAddress, n)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decrement the counter with the delta `n` specified.
|
* Decrement the counter with the delta `n` specified.
|
||||||
* If the delta is negative then it will increment instead of decrement.
|
* If the delta is negative then it will increment instead of decrement.
|
||||||
*/
|
*/
|
||||||
def -(n: Long)(implicit node: Cluster): PNCounter = decrement(node, n)
|
def -(n: Long)(implicit node: Cluster): PNCounter = decrement(node, n)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decrement the counter with the delta `n` specified.
|
||||||
|
* If the delta is negative then it will increment instead of decrement.
|
||||||
|
*/
|
||||||
|
def -(n: BigInt)(implicit node: Cluster): PNCounter = decrement(node, n)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decrement the counter with the delta `n` specified.
|
* Decrement the counter with the delta `n` specified.
|
||||||
* If the delta `n` is negative then it will increment instead of decrement.
|
* If the delta `n` is negative then it will increment instead of decrement.
|
||||||
|
|
@ -82,12 +108,26 @@ final class PNCounter private[akka] (
|
||||||
def decrement(node: Cluster, n: Long = 1): PNCounter =
|
def decrement(node: Cluster, n: Long = 1): PNCounter =
|
||||||
decrement(node.selfUniqueAddress, n)
|
decrement(node.selfUniqueAddress, n)
|
||||||
|
|
||||||
private[akka] def increment(key: UniqueAddress, n: Long): PNCounter = change(key, n)
|
/**
|
||||||
|
* Scala API: Decrement the counter with the delta `n` specified.
|
||||||
|
* If the delta `n` is negative then it will increment instead of decrement.
|
||||||
|
*/
|
||||||
|
def decrement(node: Cluster, n: BigInt): PNCounter =
|
||||||
|
decrement(node.selfUniqueAddress, n)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Java API: Decrement the counter with the delta `n` specified.
|
||||||
|
* If the delta `n` is negative then it will increment instead of decrement.
|
||||||
|
*/
|
||||||
|
def decrement(node: Cluster, n: java.math.BigInteger): PNCounter =
|
||||||
|
decrement(node.selfUniqueAddress, n)
|
||||||
|
|
||||||
|
private[akka] def increment(key: UniqueAddress, n: BigInt): PNCounter = change(key, n)
|
||||||
private[akka] def increment(key: UniqueAddress): PNCounter = increment(key, 1)
|
private[akka] def increment(key: UniqueAddress): PNCounter = increment(key, 1)
|
||||||
private[akka] def decrement(key: UniqueAddress, n: Long): PNCounter = change(key, -n)
|
private[akka] def decrement(key: UniqueAddress, n: BigInt): PNCounter = change(key, -n)
|
||||||
private[akka] def decrement(key: UniqueAddress): PNCounter = decrement(key, 1)
|
private[akka] def decrement(key: UniqueAddress): PNCounter = decrement(key, 1)
|
||||||
|
|
||||||
private[akka] def change(key: UniqueAddress, n: Long): PNCounter =
|
private[akka] def change(key: UniqueAddress, n: BigInt): PNCounter =
|
||||||
if (n > 0) copy(increments = increments.increment(key, n))
|
if (n > 0) copy(increments = increments.increment(key, n))
|
||||||
else if (n < 0) copy(decrements = decrements.increment(key, -n))
|
else if (n < 0) copy(decrements = decrements.increment(key, -n))
|
||||||
else this
|
else this
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,28 @@
|
||||||
|
/**
|
||||||
|
* Copyright (C) 2009-2018 Lightbend Inc. <https://www.lightbend.com>
|
||||||
|
*/
|
||||||
|
|
||||||
|
package akka.cluster.ddata;
|
||||||
|
|
||||||
|
import akka.cluster.Cluster;
|
||||||
|
|
||||||
|
import java.math.BigInteger;
|
||||||
|
|
||||||
|
public class PNCounterTest {
|
||||||
|
|
||||||
|
|
||||||
|
public void compileOnlyPNCounterApiTest() {
|
||||||
|
// primarily to check API accessibility with overloads/types
|
||||||
|
Cluster node1 = null;
|
||||||
|
Cluster node2 = null;
|
||||||
|
|
||||||
|
PNCounter c1 = PNCounter.create();
|
||||||
|
|
||||||
|
PNCounter c2 = c1.increment(node1, BigInteger.valueOf(3));
|
||||||
|
PNCounter c3 = c2.increment(node1, 4L);
|
||||||
|
|
||||||
|
PNCounter c4 = c3.decrement(node2, BigInteger.valueOf(2));
|
||||||
|
PNCounter c5 = c4.decrement(node2, 7L);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -72,6 +72,20 @@ class PNCounterSpec extends WordSpec with Matchers {
|
||||||
c6.increments.state(node2) should be(10)
|
c6.increments.state(node2) should be(10)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
"be able to increment each node's record by arbitrary BigInt delta" in {
|
||||||
|
val c1 = PNCounter()
|
||||||
|
|
||||||
|
val c2 = c1 increment (node1, BigInt(3))
|
||||||
|
val c3 = c2 increment (node1, BigInt(4))
|
||||||
|
|
||||||
|
val c4 = c3 increment (node2, BigInt(2))
|
||||||
|
val c5 = c4 increment (node2, BigInt(7))
|
||||||
|
val c6 = c5 increment node2
|
||||||
|
|
||||||
|
c6.increments.state(node1) should be(7)
|
||||||
|
c6.increments.state(node2) should be(10)
|
||||||
|
}
|
||||||
|
|
||||||
"be able to decrement each node's record by arbitrary delta" in {
|
"be able to decrement each node's record by arbitrary delta" in {
|
||||||
val c1 = PNCounter()
|
val c1 = PNCounter()
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue