Skip to content

Commit

Permalink
make interfaces similar
Browse files Browse the repository at this point in the history
  • Loading branch information
rmgk committed Feb 6, 2024
1 parent 0c0410e commit 72265f9
Showing 1 changed file with 12 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down

0 comments on commit 72265f9

Please sign in to comment.