From 72265f94ba057e0e4c2a5fa3bca6ef93c2eb91e8 Mon Sep 17 00:00:00 2001 From: ragnar Date: Tue, 6 Feb 2024 20:09:33 +0100 Subject: [PATCH] make interfaces similar --- .../replication/papoctokens/Tokens.scala | 27 +++++++++---------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/Modules/Example Replication/jvm/src/main/scala/replication/papoctokens/Tokens.scala b/Modules/Example Replication/jvm/src/main/scala/replication/papoctokens/Tokens.scala index b0a733893..a8dede234 100644 --- a/Modules/Example Replication/jvm/src/main/scala/replication/papoctokens/Tokens.scala +++ b/Modules/Example Replication/jvm/src/main/scala/replication/papoctokens/Tokens.scala @@ -23,15 +23,12 @@ object Ownership { } case class Token(os: Ownership, wants: ReplicatedSet[Uid]) { - def updateWants(dottedWant: Dotted[ReplicatedSet[Uid]]): Dotted[Token] = - dottedWant.map: wants => - Token(Ownership.unchanged, wants) - def request(using ReplicaId, Dots): Dotted[Token] = updateWants: - wants.addElem(replicaId) + def request(using ReplicaId, Dots): Dotted[Token] = + wants.addElem(replicaId).map(w => Token(Ownership.unchanged, w)) - def release(using ReplicaId, Dots): Dotted[Token] = updateWants: - wants.removeElem(replicaId) + def release(using ReplicaId, Dots): Dotted[Token] = + wants.removeElem(replicaId).map(w => Token(Ownership.unchanged, w)) def grant(using ReplicaId): Token = if !isOwner then Token.unchanged @@ -66,19 +63,23 @@ case class Voting(rounds: Epoche[ReplicatedSet[Vote]]) { if !rounds.value.isEmpty then Voting.unchanged else voteFor(replicaId) - def voteFor(uid: Uid)(using ReplicaId, Dots): Dotted[Voting] = - val newVote = rounds.value.addElem(Vote(uid, replicaId)) - newVote.map(rs => Voting(rounds.write(rs))) + def isOwner(using ReplicaId): Boolean = + val (id, count) = leadingCount + id == replicaId && count >= Voting.threshold def release(using ReplicaId): Voting = Voting(Epoche(rounds.counter + 1, ReplicatedSet.empty)) - def vote(using ReplicaId, Dots): Dotted[Voting] = + def upkeep(using ReplicaId, Dots): Dotted[Voting] = val (id, count) = leadingCount if checkIfMajorityPossible(count) then voteFor(id) else Dotted(release) + def voteFor(uid: Uid)(using ReplicaId, Dots): Dotted[Voting] = + val newVote = rounds.value.addElem(Vote(uid, replicaId)) + newVote.map(rs => Voting(rounds.write(rs))) + def checkIfMajorityPossible(count: Int): Boolean = val totalVotes = rounds.value.elements.size val remainingVotes = Voting.participants - totalVotes @@ -88,10 +89,6 @@ case class Voting(rounds: Epoche[ReplicatedSet[Vote]]) { val votes: Set[Vote] = rounds.value.elements votes.groupBy(_.owner).map((o, elems) => (o, elems.size)).maxBy((o, size) => size) - def isOwner(using ReplicaId): Boolean = - val (id, count) = leadingCount - id == replicaId && count >= Voting.threshold - } object Voting {