Skip to content

Commit

Permalink
scalafmt
Browse files Browse the repository at this point in the history
  • Loading branch information
rmgk committed Dec 12, 2024
1 parent 53856e7 commit d7d8e04
Show file tree
Hide file tree
Showing 33 changed files with 94 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.google.crypto.tink.aead.AeadConfig
import com.google.crypto.tink.{Aead, CleartextKeysetHandle, JsonKeysetReader, JsonKeysetWriter, KeyTemplates, KeysetHandle, LegacyKeysetSerialization}
import rdts.base.{Bottom, Lattice, LocalUid}
import rdts.dotted.{Dotted, HasDots, Obrem}
import replication.{DeltaDissemination}
import replication.DeltaDissemination

import java.net.{InetSocketAddress, Socket, URI}
import java.nio.file.{Files, Path}
Expand All @@ -25,7 +25,7 @@ class AeadTranslation(aead: com.google.crypto.tink.Aead) extends replication.Aea
Try(aead.decrypt(data, associated))
}

class DataManagerConnectionManager[State: {JsonValueCodec, Lattice, Bottom, HasDots}](
class DataManagerConnectionManager[State: { JsonValueCodec, Lattice, Bottom, HasDots }](
replicaId: LocalUid,
receiveCallback: Obrem[State] => Unit
) extends ConnectionManager[Obrem[State]] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ object DeltaSurgeon {
}

// Used for values that should not be further isolated
def ofTerminalValue[V: {Bottom, JsonValueCodec}]: DeltaSurgeon[V] = new TerminalValueDeltaSurgeon[V]
def ofTerminalValue[V: { Bottom, JsonValueCodec }]: DeltaSurgeon[V] = new TerminalValueDeltaSurgeon[V]

private class TerminalValueDeltaSurgeon[V: {Bottom, JsonValueCodec}] extends DeltaSurgeon[V] {
private class TerminalValueDeltaSurgeon[V: { Bottom, JsonValueCodec }] extends DeltaSurgeon[V] {
override def isolate(delta: V): IsolatedDeltaParts =
if Bottom[V].isEmpty(delta) then IsolatedDeltaParts(Map.empty)
else IsolatedDeltaParts(writeToArray(delta))
Expand All @@ -153,10 +153,10 @@ object DeltaSurgeon {

import lofi_acl.sync.JsoniterCodecs.dotsCodec

given dotsDeltaSurgeon: DeltaSurgeon[Dots] = ofTerminalValue[Dots]
given dottedDeltaSurgeon[T: {DeltaSurgeon, Bottom}]: DeltaSurgeon[Dotted[T]] = DeltaSurgeon.derived
given obremDeltaSurgeon[T: {DeltaSurgeon, Bottom}]: DeltaSurgeon[Obrem[T]] = DeltaSurgeon.derived
given optionSurgeon[T: {Bottom, DeltaSurgeon}]: DeltaSurgeon[Option[T]] = {
given dotsDeltaSurgeon: DeltaSurgeon[Dots] = ofTerminalValue[Dots]
given dottedDeltaSurgeon[T: { DeltaSurgeon, Bottom }]: DeltaSurgeon[Dotted[T]] = DeltaSurgeon.derived
given obremDeltaSurgeon[T: { DeltaSurgeon, Bottom }]: DeltaSurgeon[Obrem[T]] = DeltaSurgeon.derived
given optionSurgeon[T: { Bottom, DeltaSurgeon }]: DeltaSurgeon[Option[T]] = {
given noneBottom: Bottom[None.type] = Bottom.provide(None) // TODO: Bottom for singletons should be derivable
given noneDeltaSurgeon: DeltaSurgeon[None.type] = DeltaSurgeon.derived
given someBottom: Bottom[Some[T]] = Bottom.derived
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,6 @@ object Filter:

def ofTerminalValue[T: Bottom]: Filter[T] = TerminalFilter[T]()

given dottedFilter[A: {Filter, Bottom}]: Filter[Dotted[A]] = Filter.derived
given dottedFilter[A: { Filter, Bottom }]: Filter[Dotted[A]] = Filter.derived

given obremFilter[A: {Filter, Bottom}]: Filter[Obrem[A]] = Filter.derived
given obremFilter[A: { Filter, Bottom }]: Filter[Obrem[A]] = Filter.derived
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ object AddWinsMap:
* @return
* The delta that contains the removal (and nothing else)
*/
def remove[K, V: {Bottom, HasDots}](key: K, map: AddWinsMap[K, V]): AddWinsMap[K, V] = Dotted(
def remove[K, V: { Bottom, HasDots }](key: K, map: AddWinsMap[K, V]): AddWinsMap[K, V] = Dotted(
Bottom[Map[K, V]].empty,
HasDots[V].dots(map.data.getOrElse(key, Bottom[V].empty))
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import rdts.time.CausalTime
type LWW[V] = LastWriterWins[V]

object LWW {
given recursiveFilter[V: {Filter, Bottom}]: Filter[LWW[V]] with
given recursiveFilter[V: { Filter, Bottom }]: Filter[LWW[V]] with
override def filter(delta: LWW[V], permission: PermissionTree): LWW[V] = permission match
case PermissionTree(ALLOW, _) => delta
case PermissionTree(PARTIAL, _) => delta.copy(payload = Filter[V].filter(delta.read, permission))
Expand Down Expand Up @@ -40,5 +40,5 @@ object LWW {
given codec: JsonValueCodec[CausalTime] = JsonCodecMaker.make[CausalTime]
DeltaSurgeon.ofTerminalValue
}
given deltaSurgeon[V: {Bottom, DeltaSurgeon}]: DeltaSurgeon[LWW[V]] = DeltaSurgeon.derived
given deltaSurgeon[V: { Bottom, DeltaSurgeon }]: DeltaSurgeon[LWW[V]] = DeltaSurgeon.derived
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ object ORMap {
)
case None => minimized

given observeRemoveMapEntryFilter[A: {Filter, Bottom}]: Filter[Entry[A]] = Filter.derived
given observeRemoveMapEntryFilter[A: { Filter, Bottom }]: Filter[Entry[A]] = Filter.derived
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import rdts.datatypes.alternatives.rga.Sequence.{RGA, RGAOps}

import java.util.concurrent.TimeUnit


@BenchmarkMode(Array(Mode.AverageTime))
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Warmup(iterations = 3, time = 1000, timeUnit = TimeUnit.MILLISECONDS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import java.util.concurrent.TimeUnit
@Threads(1)
@State(Scope.Thread)
class LWWRegisterBench {

given Decompose[LastWriterWins[Int]] = Decompose.atomic

var full: NamedDeltaBuffer[LastWriterWins[Int]] = scala.compiletime.uninitialized
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ object NamedDeltaBuffer {
extension [A](curr: DeltaBufferDotted[A]) def data: A = curr.state.data

implicit object workaround {
extension [A](curr: NamedDeltaBuffer[A])(using Lattice[A], Decompose[A])
extension [A](curr: NamedDeltaBuffer[A])(using Lattice[A], Decompose[A])
inline def mod(f: A => A): NamedDeltaBuffer[A] = {
curr.applyDelta(curr.replicaID.uid, f(curr.state))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import rdts.datatypes.alternatives.ResettableCounter
import rdts.dotted.Dotted
import benchmarks.lattices.delta.crdt.NamedDeltaBuffer.mod


import java.util.concurrent.TimeUnit

@BenchmarkMode(Array(Mode.Throughput))
Expand Down
12 changes: 6 additions & 6 deletions Modules/RDTs/src/main/scala/rdts/base/Lattice.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ trait Lattice[A] {
* IntelliJ also does not like to implement or override extension methods. */
extension (left: A) {
final inline def subsumedBy(right: A): Boolean = Lattice.this.subsumption(left, right)
final inline def subsumes(right: A): Boolean = Lattice.this.subsumption(right, left)
final inline def inflates(right: A): Boolean = !Lattice.this.subsumption(left, right)
final inline def subsumes(right: A): Boolean = Lattice.this.subsumption(right, left)
final inline def inflates(right: A): Boolean = !Lattice.this.subsumption(left, right)

@targetName("mergeInfix")
final inline def merge(right: A): A = Lattice.this.merge(left, right)
Expand All @@ -40,15 +40,15 @@ object Lattice {
def apply[A](using ev: Lattice[A]): Lattice[A] = ev

// forwarder for better syntax/type inference
def merge[A: Lattice](left: A, right: A): A = apply[A].merge(left, right)
def merge[A: Lattice](left: A, right: A): A = apply[A].merge(left, right)
def subsumption[A: Lattice](left: A, right: A): Boolean = apply[A].subsumption(left, right)

/** Some types have multiple structural representations for semantically the same value, e.g., they may contain redundant or replaced parts. This can lead to semantically equivalent values that are not structurally equal. Normalize tries to fix this.
* Overriding this is discouraged.
*/
def normalize[A: Lattice](v: A): A = v `merge` v

def diff[A: {Lattice, Decompose}](state: A, delta: A): Option[A] = {
def diff[A: { Lattice, Decompose }](state: A, delta: A): Option[A] = {
delta.decomposed.filter(!subsumption(_, state)).reduceOption(merge)
}

Expand Down Expand Up @@ -81,7 +81,7 @@ object Lattice {
}

def fromOrdering[A: Ordering]: Lattice[A] = new Lattice[A] {
override def merge(left: A, right: A): A = if subsumption(left, right) then right else left
override def merge(left: A, right: A): A = if subsumption(left, right) then right else left
override def subsumption(left: A, right: A): Boolean = Ordering[A].lteq(left, right)
}

Expand All @@ -96,7 +96,7 @@ object Lattice {
// /////////////// common instances below ///////////////

given setLattice[A]: Lattice[Set[A]] with
override def merge(left: Set[A], right: Set[A]): Set[A] = left `union` right
override def merge(left: Set[A], right: Set[A]): Set[A] = left `union` right
override def subsumption(left: Set[A], right: Set[A]): Boolean = left subsetOf right

given optionLattice[A: Lattice]: Lattice[Option[A]] =
Expand Down
4 changes: 2 additions & 2 deletions Modules/RDTs/src/main/scala/rdts/datatypes/Epoch.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ object Epoch {
given bottom[E: Bottom]: Bottom[Epoch[E]] with
override def empty: Epoch[E] = Epoch.empty

given hasDots[E: {HasDots, Bottom}]: HasDots[Epoch[E]] = new {
given hasDots[E: { HasDots, Bottom }]: HasDots[Epoch[E]] = new {
extension (dotted: Epoch[E])
def dots: Dots = dotted.value.dots
def removeDots(dots: Dots): Option[Epoch[E]] = dotted.value.removeDots(dots).map(nv => dotted.copy(value = nv))
}

given decomposeInstance[E: Decompose]: Decompose[Epoch[E]] =
case Epoch(c, v) => Decompose.decompose(v).map(Epoch(c, _))
case Epoch(c, v) => Decompose.decompose(v).map(Epoch(c, _))

given latticeInstance[E: Lattice]: Lattice[Epoch[E]] = new Lattice[Epoch[E]] {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ object LastWriterWins {
case _ => GenericLastWriterWinsLattice(Lattice.assertEquals)
}

class GenericLastWriterWinsLattice[A](conflict: Lattice[A]) extends Lattice[LastWriterWins[A]] with Decompose[LastWriterWins[A]] {
override def subsumption(left: LastWriterWins[A], right: LastWriterWins[A]): Boolean = left.timestamp <= right.timestamp
class GenericLastWriterWinsLattice[A](conflict: Lattice[A]) extends Lattice[LastWriterWins[A]]
with Decompose[LastWriterWins[A]] {
override def subsumption(left: LastWriterWins[A], right: LastWriterWins[A]): Boolean =
left.timestamp <= right.timestamp

extension (a: LastWriterWins[A])
override def decomposed: Iterable[LastWriterWins[A]] = List(a)
Expand Down
4 changes: 2 additions & 2 deletions Modules/RDTs/src/main/scala/rdts/datatypes/TwoPhaseSet.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ object TwoPhaseSet {

given bottom[E]: Bottom[TwoPhaseSet[E]] with { override def empty: TwoPhaseSet[E] = TwoPhaseSet.empty }

given lattice[E]: Lattice[TwoPhaseSet[E]] = Lattice.derived
given lattice[E]: Lattice[TwoPhaseSet[E]] = Lattice.derived
given decompose[E]: Decompose[TwoPhaseSet[E]] = Decompose.derived
given hasDots[E]: HasDots[TwoPhaseSet[E]] = HasDots.noDots
given hasDots[E]: HasDots[TwoPhaseSet[E]] = HasDots.noDots

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ object MultiVersionRegister {

given bottomInstance[A]: Bottom[MultiVersionRegister[A]] = Bottom.derived

given decomposeInstance[A]: Decompose[MultiVersionRegister[A]] =
given decomposeInstance[A]: Decompose[MultiVersionRegister[A]] =
given Decompose[A] = Decompose.atomic
Decompose.derived

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ object ObserveRemoveMap {

given hasDots[K, V: HasDots]: HasDots[ObserveRemoveMap[K, V]] = HasDots.derived

given lattice[K, V: {Lattice, HasDots}]: Lattice[ObserveRemoveMap[K, V]] =
given lattice[K, V: { Lattice, HasDots }]: Lattice[ObserveRemoveMap[K, V]] =
Lattice.derived

private def make[K, V](
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ object ReplicatedSet {

def empty[E]: ReplicatedSet[E] = ReplicatedSet(Map.empty)

given bottom[E]: Bottom[ReplicatedSet[E]] = Bottom.provide(empty)
given lattice[E]: Lattice[ReplicatedSet[E]] = Lattice.derived
given bottom[E]: Bottom[ReplicatedSet[E]] = Bottom.provide(empty)
given lattice[E]: Lattice[ReplicatedSet[E]] = Lattice.derived
given decompose[E]: Decompose[ReplicatedSet[E]] = Decompose.derived
given hasDots[E]: HasDots[ReplicatedSet[E]] = HasDots.derived
given hasDots[E]: HasDots[ReplicatedSet[E]] = HasDots.derived

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ object CausalDelta {
case class CausalStore[A](pending: CausalDelta[A], state: A)

object CausalStore {
given lattice[A: {Bottom, Lattice}]: Lattice[CausalStore[A]] with {
given lattice[A: { Bottom, Lattice }]: Lattice[CausalStore[A]] with {
def merge(left: CausalStore[A], right: CausalStore[A]): CausalStore[A] =
val pending: CausalDelta[A] = left.pending `merge` right.pending
val state = left.state `merge` right.state
Expand All @@ -30,6 +30,6 @@ object CausalStore {

given bottom[A: Bottom]: Bottom[CausalStore[A]] = Bottom.derived

given hasDots[A: {HasDots, Bottom}]: HasDots[CausalStore[A]] = HasDots.derived
given hasDots[A: { HasDots, Bottom }]: HasDots[CausalStore[A]] = HasDots.derived

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ case class Membership[A, C[_], D[_]](
log: Map[Long, A] = Map.empty,
) {

//TODO: investigate further …
// TODO: investigate further …
override def toString: String = "MEMBERSHIP TO STRING MAKES SPEC TESTS SLOW, WTF"

private def bottomRound(using Consensus[C], Consensus[D]): MembershipRound[A, C, D] =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package rdts.datatypes.experiments.protocols

import rdts.base.LocalUid.replicaId
import rdts.base.{Bottom, Lattice, LocalUid, Orderings, Uid}
import rdts.datatypes.contextual.ReplicatedSet
import rdts.dotted.{Dotted, HasDots}
import LocalUid.replicaId
import rdts.time.Dots

case class Ownership(epoch: Long, owner: Uid)
Expand Down Expand Up @@ -51,13 +51,13 @@ case class ExampleTokens(
calendarBinteractionA: Token
)

case class Exclusive[T : { Lattice, Bottom}](token: Token, value: T) {
case class Exclusive[T: { Lattice, Bottom }](token: Token, value: T) {
def transform(f: T => T)(using LocalUid) =
if token.isOwner then f(value) else Bottom.empty
}

/** totally not incredibly inefficient */
case class Causal[T: {Lattice, HasDots, Bottom}](deltas: Set[Dotted[T]]) {
case class Causal[T: { Lattice, HasDots, Bottom }](deltas: Set[Dotted[T]]) {
def value: T =
val causalPrefix = deltas.map(_.context).reduceOption(_ `union` _).map(_.causalPrefix).getOrElse(Dots.empty)
deltas.filter(delta => delta.context <= causalPrefix).reduceOption(Dotted.lattice.merge).map(_.data).getOrElse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import rdts.datatypes.LastWriterWins
import rdts.datatypes.experiments.protocols.{Consensus, Participants}

case class GeneralizedPaxos[A](
rounds: Map[BallotNum, (LeaderElection, Voting[A])] =
rounds: Map[BallotNum, (LeaderElection, Voting[A])] =
Map.empty[BallotNum, (LeaderElection, Voting[A])],
myValue: Map[Uid, LastWriterWins[A]] = Map.empty
myValue: Map[Uid, LastWriterWins[A]] = Map.empty
):

def voteFor(leader: Uid, value: A)(using LocalUid, Participants): (LeaderElection, Voting[A]) =
Expand Down
7 changes: 4 additions & 3 deletions Modules/RDTs/src/main/scala/rdts/dotted/Dotted.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ object Obrem {

def empty[A: Bottom]: Obrem[A] = Obrem(Bottom.empty[A], Dots.empty, Dots.empty)

given lattice[A: {HasDots, Bottom, Lattice}]: FilteredLattice[Obrem[A]](Lattice.derived) with {
given lattice[A: { HasDots, Bottom, Lattice }]: FilteredLattice[Obrem[A]](Lattice.derived) with {
override def filter(base: Obrem[A], other: Obrem[A]): Obrem[A] =
if other.deletions.isEmpty
then base
Expand Down Expand Up @@ -76,10 +76,11 @@ object Dotted {
def empty[A: Bottom]: Dotted[A] = Dotted(Bottom.empty[A], Dots.empty)
def apply[A](a: A): Dotted[A] = Dotted(a, Dots.empty)

given decompose[A: {Decompose, HasDots, Bottom, Lattice}]: Decompose[Dotted[A]] = new Decompose[Dotted[A]] {
given decompose[A: { Decompose, HasDots, Bottom, Lattice }]: Decompose[Dotted[A]] = new Decompose[Dotted[A]] {

extension (a: Dotted[A])
override def decomposed: Iterable[Dotted[A]] = {

/** Dotted decompose guarantees decomposes its inner value, but recomposes any values with overlapping dots, such that each dot is present in exactly one of the returned deltas. */
val deltas = a.data.decomposed.flatMap: delta =>
val dots = delta.dots
Expand All @@ -95,7 +96,7 @@ object Dotted {
}
}

given lattice[A: {HasDots, Bottom, Lattice}]: FilteredLattice[Dotted[A]](Lattice.derived) with {
given lattice[A: { HasDots, Bottom, Lattice }]: FilteredLattice[Dotted[A]](Lattice.derived) with {

override def filter(base: Dotted[A], other: Dotted[A]): Dotted[A] =
val deletions = other.deletions
Expand Down
6 changes: 3 additions & 3 deletions Modules/RDTs/src/main/scala/rdts/time/ArrayRanges.scala
Original file line number Diff line number Diff line change
Expand Up @@ -361,9 +361,9 @@ object ArrayRanges {

given latticeInstance: Lattice[ArrayRanges] with Decompose[ArrayRanges] with {
extension (a: ArrayRanges)
override def decomposed: Iterable[ArrayRanges] = a.decomposed
override def subsumption(left: ArrayRanges, right: ArrayRanges): Boolean = left <= right
override def merge(left: ArrayRanges, right: ArrayRanges): ArrayRanges = left `union` right
override def decomposed: Iterable[ArrayRanges] = a.decomposed
override def subsumption(left: ArrayRanges, right: ArrayRanges): Boolean = left <= right
override def merge(left: ArrayRanges, right: ArrayRanges): ArrayRanges = left `union` right
}

def leftRightToOrder: (Boolean, Boolean) => Option[Int] =
Expand Down
6 changes: 2 additions & 4 deletions Modules/RDTs/src/test/scala/test/rdts/DataGenerator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ object DataGenerator {
val map = Gen.listOf(pairgen).map(vs => MultiValueRegister(vs.toMap))
Arbitrary(map)



val genDot: Gen[Dot] =
for
id <- Gen.oneOf('a' to 'g')
Expand Down Expand Up @@ -175,13 +173,13 @@ object DataGenerator {
given arbEnableWinsFlag: Arbitrary[contextual.EnableWinsFlag] = Arbitrary:
arbDots.arbitrary.map(EnableWinsFlag.apply)

given arbCausalDelta[A: {Arbitrary, HasDots}]: Arbitrary[CausalDelta[A]] = Arbitrary:
given arbCausalDelta[A: { Arbitrary, HasDots }]: Arbitrary[CausalDelta[A]] = Arbitrary:
for
predec <- arbDots.arbitrary
value <- Arbitrary.arbitrary[A]
yield CausalDelta(value.dots, Dots.empty, value)

given arbCausalStore[A: {Arbitrary, HasDots, Bottom, Lattice}]: Arbitrary[CausalStore[A]] = Arbitrary:
given arbCausalStore[A: { Arbitrary, HasDots, Bottom, Lattice }]: Arbitrary[CausalStore[A]] = Arbitrary:
for
predec <- arbCausalDelta.arbitrary
value <- Arbitrary.arbitrary[A]
Expand Down
Loading

0 comments on commit d7d8e04

Please sign in to comment.