Copyright 2009-2013 Typesafe Inc http www typesafe com package actorbi

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/**
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
*/
package actorbintree
import akka.actor._
import scala.collection.immutable.Queue
object BinaryTreeSet {
trait Operation {
def requester: ActorRef
def id: Int
def elem: Int
}
trait OperationReply {
def id: Int
}
/** Request with identifier `id` to insert an element `elem` into the tree.
* The actor at reference `requester` should be notified when this operation
* is completed.
*/
case class Insert(requester: ActorRef, id: Int, elem: Int) extends Operation
/** Request with identifier `id` to check whether an element `elem` is present
* in the tree. The actor at reference `requester` should be notified when
* this operation is completed.
*/
case class Contains(requester: ActorRef, id: Int, elem: Int) extends Operation
/** Request with identifier `id` to remove the element `elem` from the tree.
* The actor at reference `requester` should be notified when this operation
* is completed.
*/
case class Remove(requester: ActorRef, id: Int, elem: Int) extends Operation
/** Request to perform garbage collection*/
case object GC
/** Holds the answer to the Contains request with identifier `id`.
* `result` is true if and only if the element is present in the tree.
*/
case class ContainsResult(id: Int, result: Boolean) extends OperationReply
/** Message to signal successful completion of an insert or remove operation. */
case class OperationFinished(id: Int) extends OperationReply
}
class BinaryTreeSet extends Actor {
import BinaryTreeSet._
import BinaryTreeNode._
def createRoot: ActorRef = context.actorOf(BinaryTreeNode.props(0, initiallyRemoved = true))
var root = createRoot
var lastOp = 0
// optional
var pendingQueue = Queue.empty[Operation]
// optional
def receive = normal
// optional
/** Accepts `Operation` and `GC` messages. */
val normal: Receive = {
case GC => {
root ! AsListRequest
context.become(garbageCollecting(createRoot))
}
case op : Operation => root ! op
}
// optional
/** Handles messages while garbage collection is performed.
* `newRoot` is the root of the new binary tree where we want to copy
* all non-removed elements into.
*/
def garbageCollecting(newRoot: ActorRef): Receive = {
case GC =>
case x : Operation => pendingQueue = pendingQueue enqueue x
case AsListResponse(lst) => {
if (lst.length == 0) resumeProcessing(pendingQueue)
lastOp = lst.length - 1
for((x,i) <- lst.view.zipWithIndex) {
newRoot ! Insert(self, i, x)
}
}
case r : OperationReply => {
if (r.id == lastOp) {
root ! PoisonPill
root = newRoot
resumeProcessing(pendingQueue)
}
}
}
def resumeProcessing(q : Queue[Operation]) : Unit = {
while (pendingQueue.nonEmpty) {
val t = pendingQueue.dequeue
val op= t._1
pendingQueue = t._2
root ! op
}
context become normal
}
}
object BinaryTreeNode {
trait Position
case object Left extends Position
case object Right extends Position
case class CopyTo(treeNode: ActorRef)
case object CopyFinished
def props(elem: Int, initiallyRemoved: Boolean) = Props(classOf[BinaryTreeNode], elem, initiallyRemoved)
case object AsListRequest
case class AsListResponse(lst: Vector[Int])
}
class BinaryTreeNode(val elem: Int, initiallyRemoved: Boolean) extends Actor {
import BinaryTreeNode._
import BinaryTreeSet._
var subtrees = Map[Position, ActorRef]()
var removed = initiallyRemoved
var pendingChildListCount = 0
var childLists = Map[Position, Vector[Int]]()
def selfList = if(removed) Vector.empty else Vector(elem)
// optional
def receive = normal
def insertToSubtree(pos: Position, msg: Insert, elementToInsert: Int): Unit = {
if (!(subtrees contains pos)) {
val child = context.actorOf(BinaryTreeNode.props(elementToInsert, initiallyRemoved = false))
subtrees = subtrees.updated(pos, child)
}
subtrees(pos) ! msg
}
def removeFromSubtree(pos: Position, msg: Remove, elementToRemove: Int): Unit = {
if (subtrees contains pos) {
subtrees(pos) ! msg
} else {
msg.requester ! OperationFinished(msg.id)
}
}
def containsInSubtree(pos: Position, msg: Contains, elementToFind: Int): Unit = {
if (subtrees contains pos) {
subtrees(pos) ! msg
} else {
msg.requester ! ContainsResult(msg.id, false)
}
}
// optional
/** Handles `Operation` messages and `CopyTo` requests. */
val normal: Receive = {
case AsListRequest => {
if(subtrees.isEmpty) {
context.parent ! AsListResponse(selfList)
}
else {
pendingChildListCount = subtrees.size
subtrees map {
case (pos, child) => child ! AsListRequest
}
context become buildingList
}
}
case msg@Insert(requester, id, elementToInsert) => {
if (elem == elementToInsert) {
removed = false
requester ! OperationFinished(id)
}
else {
insertToSubtree(if (elem > elementToInsert) Left else Right, msg, elementToInsert)
}
}
case msg@Remove(requester, id, elementToRemove) => {
if (elem == elementToRemove) {
removed = true
requester ! OperationFinished(id)
}
else {
removeFromSubtree(if (elem > elementToRemove) Left else Right, msg, elementToRemove)
}
}
case msg@Contains(requester, id, elementToFind) => {
if (elem == elementToFind) {
requester ! ContainsResult(id, !removed)
}
else {
containsInSubtree(if (elem > elementToFind) Left else Right, msg, elementToFind)
}
}
}
val buildingList: Receive = {
case AsListResponse(childList) => {
pendingChildListCount -= 1
if (subtrees.contains(Left) && subtrees(Left) == sender() ) {
childLists = childLists.updated(Left, childList)
}
if (subtrees.contains(Right) && subtrees(Right) == sender() ) {
childLists = childLists.updated(Right, childList)
}
if (pendingChildListCount == 0) {
var lst = selfList
if (childLists contains(Left)) {
lst = childLists(Left) ++ lst
}
if (childLists contains(Right)) {
lst = lst ++ childLists(Right)
}
context.parent ! AsListResponse(lst)
context become normal
}
}
case x => throw new Error(s"Unexpected message while building list: $x")
}
// optional
/** `expected` is the set of ActorRefs whose replies we are waiting for,
* `insertConfirmed` tracks whether the copy of this node to the new tree has been confirmed.
*/
//def copying(expected: Set[ActorRef], insertConfirmed: Boolean): Receive = ???
}