Merge pull request #25545 from johanandren/wip-25543-allow-big-decimal-increments-decrements-pncounter-johanandren
Allow BigInt/BigInteger increments/decrements for PNCounter
This commit is contained in:
commit
7f629a3382
4 changed files with 98 additions and 5 deletions
|
|
@ -0,0 +1,4 @@
|
||||||
|
# #25543 allow BigInt increment and decrements in PNCounter
|
||||||
|
ProblemFilters.exclude[IncompatibleMethTypeProblem]("akka.cluster.ddata.PNCounter.increment")
|
||||||
|
ProblemFilters.exclude[IncompatibleMethTypeProblem]("akka.cluster.ddata.PNCounter.decrement")
|
||||||
|
ProblemFilters.exclude[IncompatibleMethTypeProblem]("akka.cluster.ddata.PNCounter.change")
|
||||||
|
|
@ -9,6 +9,8 @@ import akka.cluster.UniqueAddress
|
||||||
import akka.util.HashCode
|
import akka.util.HashCode
|
||||||
import java.math.BigInteger
|
import java.math.BigInteger
|
||||||
|
|
||||||
|
import akka.annotation.InternalApi
|
||||||
|
|
||||||
object PNCounter {
|
object PNCounter {
|
||||||
val empty: PNCounter = new PNCounter(GCounter.empty, GCounter.empty)
|
val empty: PNCounter = new PNCounter(GCounter.empty, GCounter.empty)
|
||||||
def apply(): PNCounter = empty
|
def apply(): PNCounter = empty
|
||||||
|
|
@ -62,6 +64,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 +77,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 +110,31 @@ 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)
|
/**
|
||||||
private[akka] def increment(key: UniqueAddress): PNCounter = increment(key, 1)
|
* Scala API: Decrement the counter with the delta `n` specified.
|
||||||
private[akka] def decrement(key: UniqueAddress, n: Long): PNCounter = change(key, -n)
|
* If the delta `n` is negative then it will increment instead of decrement.
|
||||||
private[akka] def decrement(key: UniqueAddress): PNCounter = decrement(key, 1)
|
*/
|
||||||
|
def decrement(node: Cluster, n: BigInt): PNCounter =
|
||||||
|
decrement(node.selfUniqueAddress, n)
|
||||||
|
|
||||||
private[akka] def change(key: UniqueAddress, n: Long): PNCounter =
|
/**
|
||||||
|
* 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)
|
||||||
|
|
||||||
|
/** Internal API */
|
||||||
|
@InternalApi private[akka] def increment(key: UniqueAddress, n: BigInt): PNCounter = change(key, n)
|
||||||
|
/** Internal API */
|
||||||
|
@InternalApi private[akka] def increment(key: UniqueAddress): PNCounter = increment(key, 1)
|
||||||
|
/** Internal API */
|
||||||
|
@InternalApi private[akka] def decrement(key: UniqueAddress, n: BigInt): PNCounter = change(key, -n)
|
||||||
|
/** Internal API */
|
||||||
|
@InternalApi private[akka] def decrement(key: UniqueAddress): PNCounter = decrement(key, 1)
|
||||||
|
|
||||||
|
/** Internal API */
|
||||||
|
@InternalApi 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