add flag crdt constants (#24208)

* add flag crdt constants

* add scaladoc
This commit is contained in:
Nafer Sanabria 2018-01-09 04:39:06 -05:00 committed by Patrik Nordwall
parent 7c94bc7df4
commit 804dc4b6ba
6 changed files with 27 additions and 15 deletions

View file

@ -7,12 +7,24 @@ object Flag {
/** /**
* `Flag` that is initialized to `false`. * `Flag` that is initialized to `false`.
*/ */
val empty = new Flag(false) val empty: Flag = new Flag(false)
def apply(): Flag = empty
/**
* `Flag` that is initialized to `false`.
*/
val Disabled: Flag = empty
/**
* `Flag` that is initialized to `true`.
*/
val Enabled: Flag = new Flag(true)
def apply(): Flag = Disabled
/** /**
* Java API: `Flag` that is initialized to `false`. * Java API: `Flag` that is initialized to `false`.
*/ */
def create(): Flag = empty def create(): Flag = Disabled
// unapply from case class // unapply from case class
} }
@ -30,7 +42,7 @@ final case class Flag(enabled: Boolean) extends ReplicatedData with ReplicatedDa
def switchOn: Flag = def switchOn: Flag =
if (enabled) this if (enabled) this
else Flag(true) else Flag.Enabled
override def merge(that: Flag): Flag = override def merge(that: Flag): Flag =
if (that.enabled) that if (that.enabled) that

View file

@ -484,7 +484,7 @@ class ReplicatedDataSerializer(val system: ExtendedActorSystem)
flagFromProto(rd.Flag.parseFrom(bytes)) flagFromProto(rd.Flag.parseFrom(bytes))
def flagFromProto(flag: rd.Flag): Flag = def flagFromProto(flag: rd.Flag): Flag =
Flag(flag.getEnabled) if (flag.getEnabled) Flag.Enabled else Flag.Disabled
def lwwRegisterToProto(lwwRegister: LWWRegister[_]): rd.LWWRegister = def lwwRegisterToProto(lwwRegister: LWWRegister[_]): rd.LWWRegister =
rd.LWWRegister.newBuilder(). rd.LWWRegister.newBuilder().

View file

@ -527,22 +527,22 @@ class ReplicatorSpec extends MultiNodeSpec(ReplicatorSpec) with STMultiNodeSpec
runOn(second) { runOn(second) {
replicator ! Subscribe(KeyH, changedProbe.ref) replicator ! Subscribe(KeyH, changedProbe.ref)
replicator ! Update(KeyH, ORMap.empty[String, Flag], writeTwo)(_ + ("a" Flag(enabled = false))) replicator ! Update(KeyH, ORMap.empty[String, Flag], writeTwo)(_ + ("a" Flag.Disabled))
changedProbe.expectMsgPF() { case c @ Changed(KeyH) c.get(KeyH).entries } should be(Map("a" Flag(enabled = false))) changedProbe.expectMsgPF() { case c @ Changed(KeyH) c.get(KeyH).entries } should be(Map("a" Flag.Disabled))
} }
enterBarrier("update-h1") enterBarrier("update-h1")
runOn(first) { runOn(first) {
replicator ! Update(KeyH, ORMap.empty[String, Flag], writeTwo)(_ + ("a" Flag(enabled = true))) replicator ! Update(KeyH, ORMap.empty[String, Flag], writeTwo)(_ + ("a" Flag.Enabled))
} }
runOn(second) { runOn(second) {
changedProbe.expectMsgPF() { case c @ Changed(KeyH) c.get(KeyH).entries } should be(Map("a" Flag(enabled = true))) changedProbe.expectMsgPF() { case c @ Changed(KeyH) c.get(KeyH).entries } should be(Map("a" Flag.Enabled))
replicator ! Update(KeyH, ORMap.empty[String, Flag], writeTwo)(_ + ("b" Flag(enabled = true))) replicator ! Update(KeyH, ORMap.empty[String, Flag], writeTwo)(_ + ("b" Flag.Enabled))
changedProbe.expectMsgPF() { case c @ Changed(KeyH) c.get(KeyH).entries } should be( changedProbe.expectMsgPF() { case c @ Changed(KeyH) c.get(KeyH).entries } should be(
Map("a" Flag(enabled = true), "b" Flag(enabled = true))) Map("a" Flag.Enabled, "b" Flag.Enabled))
} }
enterBarrierAfterTestStep() enterBarrierAfterTestStep()

View file

@ -31,7 +31,7 @@ class FlagSpec extends WordSpec with Matchers {
} }
"have unapply extractor" in { "have unapply extractor" in {
val f1 = Flag.empty.switchOn val f1 = Flag.Disabled.switchOn
val Flag(value1) = f1 val Flag(value1) = f1
val value2: Boolean = value1 val value2: Boolean = value1
Changed(FlagKey("key"))(f1) match { Changed(FlagKey("key"))(f1) match {

View file

@ -569,7 +569,7 @@ class ORMapSpec extends WordSpec with Matchers {
"work with deltas and updated for Flag elements type" in { "work with deltas and updated for Flag elements type" in {
val m1 = ORMap.empty.put(node1, "a", Flag(false)) val m1 = ORMap.empty.put(node1, "a", Flag(false))
val m2 = m1.resetDelta.updated(node1, "a", Flag.empty)(_.switchOn) val m2 = m1.resetDelta.updated(node1, "a", Flag.Disabled)(_.switchOn)
val m3 = ORMap().mergeDelta(m1.delta.get).mergeDelta(m2.delta.get) val m3 = ORMap().mergeDelta(m1.delta.get).mergeDelta(m2.delta.get)
val Flag(d3) = m3.entries("a") val Flag(d3) = m3.entries("a")
d3 should be(true) d3 should be(true)

View file

@ -123,7 +123,7 @@ class DistributedDataDocSpec extends AkkaSpec(DistributedDataDocSpec.config) {
replicator ! Update(Set2Key, ORSet.empty[String], writeMajority)(_ + "hello") replicator ! Update(Set2Key, ORSet.empty[String], writeMajority)(_ + "hello")
val writeAll = WriteAll(timeout = 5.seconds) val writeAll = WriteAll(timeout = 5.seconds)
replicator ! Update(ActiveFlagKey, Flag.empty, writeAll)(_.switchOn) replicator ! Update(ActiveFlagKey, Flag.Disabled, writeAll)(_.switchOn)
//#update //#update
probe.expectMsgType[UpdateResponse[_]] match { probe.expectMsgType[UpdateResponse[_]] match {
@ -347,7 +347,7 @@ class DistributedDataDocSpec extends AkkaSpec(DistributedDataDocSpec.config) {
"demonstrate Flag" in { "demonstrate Flag" in {
def println(o: Any): Unit = () def println(o: Any): Unit = ()
//#flag //#flag
val f0 = Flag.empty val f0 = Flag.Disabled
val f1 = f0.switchOn val f1 = f0.switchOn
println(f1.enabled) println(f1.enabled)
//#flag //#flag