pekko/akka-distributed-data/src/main/scala/akka/cluster/ddata/GSet.scala
Patrik Nordwall cbe5dd2cf5 +cdd #16799 Add Distributed Data module
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
2015-06-18 15:58:22 +02:00

66 lines
1.7 KiB
Scala

/**
* Copyright (C) 2009-2015 Typesafe Inc. <http://www.typesafe.com>
*/
package akka.cluster.ddata
object GSet {
private val _empty: GSet[Any] = new GSet(Set.empty)
def empty[A]: GSet[A] = _empty.asInstanceOf[GSet[A]]
def apply(): GSet[Any] = _empty
/**
* Java API
*/
def create[A](): GSet[A] = empty[A]
// unapply from case class
}
/**
* Implements a 'Add Set' CRDT, also called a 'G-Set'. You can't
* remove elements of a G-Set.
*
* It is described in the paper
* <a href="http://hal.upmc.fr/file/index/docid/555588/filename/techreport.pdf">A comprehensive study of Convergent and Commutative Replicated Data Types</a>.
*
* A G-Set doesn't accumulate any garbage apart from the elements themselves.
*
* This class is immutable, i.e. "modifying" methods return a new instance.
*/
@SerialVersionUID(1L)
final case class GSet[A](elements: Set[A]) extends ReplicatedData with ReplicatedDataSerialization {
type T = GSet[A]
/**
* Java API
*/
def getElements(): java.util.Set[A] = {
import scala.collection.JavaConverters._
elements.asJava
}
def contains(a: A): Boolean = elements(a)
def isEmpty: Boolean = elements.isEmpty
def size: Int = elements.size
/**
* Adds an element to the set
*/
def +(element: A): GSet[A] = add(element)
/**
* Adds an element to the set
*/
def add(element: A): GSet[A] = copy(elements + element)
override def merge(that: GSet[A]): GSet[A] = copy(elements ++ that.elements)
}
object GSetKey {
def create[A](id: String): Key[GSet[A]] = GSetKey(id)
}
@SerialVersionUID(1L)
final case class GSetKey[A](_id: String) extends Key[GSet[A]](_id) with ReplicatedDataSerialization