-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement deletion strategy for one-child, leaf and two-children nodes
If a delete message is received by the BinaryTree, it switches to a new context, sets itself as a middleman so that it receives the responses before the MessageHandler in order to switch back to the original context and forwards the deletion message to the root node. From there on, we're just searching for the correct node and if we finally found it, we send a DeleteChild event to its parent, respond to the middleman with an OperationFinished event stop the node. The BinaryTree actor, in the meantime, stores all incoming operations in a queue, switches back to the original context and works throught the whole queue. I decided to use the in-order predecessor, meaning the largest value of the left subtree as an replacement for the to be deleted node in order to maintain the ordering of the tree. A new message MovePredecessor propagates to the node with the "highest" key, sends DeleteChild to its own parent, replaces its own parent and its subtree with the ones of the deleted node and sends a ReplaceChild event to its new parent, in order to be at the place of the old node.
- Loading branch information
1 parent
08194ea
commit c3ccb48
Showing
14 changed files
with
219 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
name := "clossyne" | ||
|
||
version := "0.1" | ||
version := "1.0" | ||
|
||
scalaVersion := "2.13.10" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.christophsonntag.clossyne | ||
package operations | ||
|
||
import akka.actor.ActorRef | ||
|
||
case class DeleteForward(requester: ActorRef, middleware: ActorRef, key: String) extends OperationForward |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.christophsonntag.clossyne | ||
package operations | ||
|
||
import akka.actor.ActorRef | ||
|
||
trait OperationForward { | ||
def requester: ActorRef | ||
def middleware: ActorRef | ||
def key: String | ||
} |
10 changes: 10 additions & 0 deletions
10
clossyne/src/main/scala/operations/OperationForwardReply.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.christophsonntag.clossyne | ||
package operations | ||
|
||
import akka.actor.ActorRef | ||
|
||
sealed trait OperationForwardReply { | ||
def succeeded: Boolean | ||
def destination: ActorRef | ||
} | ||
case class OperationForwardFinished(succeeded: Boolean, destination: ActorRef) extends OperationForwardReply |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,40 @@ | ||
package com.christophsonntag.clossyne | ||
package searchtree | ||
|
||
import akka.actor.{Actor, ActorRef, Props} | ||
import operations.{Operation, OperationReply} | ||
import akka.actor.{Actor, ActorLogging, ActorRef, Props} | ||
import operations.{Delete, DeleteForward, Operation, OperationFinished, OperationForwardFinished, OperationForwardReply} | ||
|
||
import scala.collection.immutable.Queue | ||
|
||
|
||
class BinaryTree extends Actor { | ||
|
||
class BinaryTree extends Actor with ActorLogging { | ||
// immutable root | ||
val root: ActorRef = context.actorOf(Props(classOf[BinaryTreeNode], "root", "root", true), "clossyneRootNode") | ||
val root: ActorRef = context.actorOf(BinaryTreeNode.props("root", "root", ActorRef.noSender), "clossyneRootNode") | ||
var pendingOperations: Queue[Operation] = Queue.empty[Operation] | ||
|
||
def receive: Receive = { | ||
case op: Operation => root ! op | ||
case opReply: OperationReply => context.parent ! opReply | ||
case op: Operation => | ||
op match { | ||
case Delete(requester, key) => | ||
log.debug("Delete received: Changing context now") | ||
context.become(nodeDeletion) | ||
root ! DeleteForward(requester, self, key) | ||
case _ => root ! op | ||
} | ||
case opReply: OperationForwardFinished => opReply.destination ! OperationFinished(opReply.succeeded, None) | ||
} | ||
|
||
def nodeDeletion: Receive = { | ||
case op: Operation => | ||
log.debug(s"Enqueuing operation ${op}") | ||
pendingOperations.enqueue(op) | ||
case opReply: OperationForwardFinished => | ||
log.debug(s"Delete operation finished. Change context and send enqueued operations.") | ||
context.become(receive) | ||
|
||
opReply.destination ! OperationFinished(opReply.succeeded, None) | ||
pendingOperations.map(self ! _) | ||
pendingOperations = Queue.empty | ||
} | ||
} |
Oops, something went wrong.