Previously know as [patriknw/akka-data-replication](https://github.com/patriknw/akka-data-replication), which was originally inspired by [jboner/akka-crdt](https://github.com/jboner/akka-crdt). The functionality is very similar to akka-data-replication 0.11. Here is a list of the most important changes: * The package name changed to `akka.cluster.ddata` * The extension was renamed to `DistributedData` * The keys changed from strings to classes with unique identifiers and type information of the data values, e.g. `ORSetKey[Int]("set2")` * The optional read consistency parameter was removed from the `Update` message. If you need to read from other replicas before performing the update you have to first send a `Get` message and then continue with the ``Update`` when the ``GetSuccess`` is received. * `BigInt` is used in `GCounter` and `PNCounter` instead of `Long` * Improvements of java api * Better documentation
59 lines
1.9 KiB
Scala
59 lines
1.9 KiB
Scala
/**
|
|
* Copyright (C) 2015 Typesafe Inc. <http://www.typesafe.com>
|
|
*/
|
|
package docs.ddata.protobuf
|
|
|
|
//#serializer
|
|
import scala.collection.JavaConverters._
|
|
import akka.actor.ExtendedActorSystem
|
|
import akka.cluster.ddata.GSet
|
|
import akka.cluster.ddata.protobuf.ReplicatedDataSerializer
|
|
import akka.cluster.ddata.protobuf.SerializationSupport
|
|
import akka.serialization.Serializer
|
|
import docs.ddata.TwoPhaseSet
|
|
import docs.ddata.protobuf.msg.TwoPhaseSetMessages
|
|
|
|
class TwoPhaseSetSerializer2(val system: ExtendedActorSystem)
|
|
extends Serializer with SerializationSupport {
|
|
|
|
override def includeManifest: Boolean = false
|
|
|
|
override def identifier = 99999
|
|
|
|
val replicatedDataSerializer = new ReplicatedDataSerializer(system)
|
|
|
|
override def toBinary(obj: AnyRef): Array[Byte] = obj match {
|
|
case m: TwoPhaseSet ⇒ twoPhaseSetToProto(m).toByteArray
|
|
case _ ⇒ throw new IllegalArgumentException(
|
|
s"Can't serialize object of type ${obj.getClass}")
|
|
}
|
|
|
|
override def fromBinary(bytes: Array[Byte], clazz: Option[Class[_]]): AnyRef = {
|
|
twoPhaseSetFromBinary(bytes)
|
|
}
|
|
|
|
def twoPhaseSetToProto(twoPhaseSet: TwoPhaseSet): TwoPhaseSetMessages.TwoPhaseSet2 = {
|
|
val b = TwoPhaseSetMessages.TwoPhaseSet2.newBuilder()
|
|
if (!twoPhaseSet.adds.isEmpty)
|
|
b.setAdds(otherMessageToProto(twoPhaseSet.adds).toByteString())
|
|
if (!twoPhaseSet.removals.isEmpty)
|
|
b.setRemovals(otherMessageToProto(twoPhaseSet.removals).toByteString())
|
|
b.build()
|
|
}
|
|
|
|
def twoPhaseSetFromBinary(bytes: Array[Byte]): TwoPhaseSet = {
|
|
val msg = TwoPhaseSetMessages.TwoPhaseSet2.parseFrom(bytes)
|
|
val adds =
|
|
if (msg.hasAdds)
|
|
otherMessageFromBinary(msg.getAdds.toByteArray).asInstanceOf[GSet[String]]
|
|
else
|
|
GSet.empty[String]
|
|
val removals =
|
|
if (msg.hasRemovals)
|
|
otherMessageFromBinary(msg.getRemovals.toByteArray).asInstanceOf[GSet[String]]
|
|
else
|
|
GSet.empty[String]
|
|
TwoPhaseSet(adds, removals)
|
|
}
|
|
}
|
|
//#serializer
|