Skip to content

Instantly share code, notes, and snippets.

@mrbrdo
Last active August 29, 2015 14:21
Show Gist options
  • Save mrbrdo/7ca653cd57a68b466d73 to your computer and use it in GitHub Desktop.
Save mrbrdo/7ca653cd57a68b466d73 to your computer and use it in GitHub Desktop.
// doesn't work
val receiveReplica: Receive = withPersistence { (sender, key, id) => sender ! SnapshotAck(key, id) } {
var expectedSeq = 0
{
case Get(key, id) =>
sender ! GetResult(key, kv.get(key), id)
case OperationTimeout(seq) =>
persisting.get(seq).foreach {
case PersistingValue(_, timer, _, _) =>
timer.cancel()
persisting -= seq
}
case msg@Snapshot(key, _, seq) if seq < expectedSeq =>
sender ! SnapshotAck(key, seq)
case msg@Snapshot(key, valueOption, seq) if seq == expectedSeq =>
persist(key, valueOption, seq, Set.empty[ActorRef])
setOperationTimeout(seq)
expectedSeq += 1
if (valueOption.isDefined)
kv = kv.updated(key, valueOption.get)
else
kv -= key
}
}
// works
val receiveReplica: Receive = withPersistence { (sender, key, id) => sender ! SnapshotAck(key, id) } {
var expectedSeq = 0
{
case Get(key, id) =>
sender ! GetResult(key, kv.get(key), id)
case OperationTimeout(seq) =>
persisting.get(seq).foreach {
case PersistingValue(_, timer, _, _) =>
timer.cancel()
persisting -= seq
}
case msg@Snapshot(key, _, seq) if seq < expectedSeq =>
sender ! SnapshotAck(key, seq)
case msg@Snapshot(key, valueOption, seq) if seq == expectedSeq =>
persist(key, valueOption, seq, Set.empty[ActorRef])
setOperationTimeout(seq)
expectedSeq += 1
if (valueOption.isDefined)
kv = kv.updated(key, valueOption.get)
else
kv -= key
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment