|
| 1 | +package de.upb.cs.swt.delphi.instanceregistry.daos |
| 2 | + |
| 3 | +import akka.actor.ActorSystem |
| 4 | +import de.upb.cs.swt.delphi.instanceregistry.{AppLogging, Server} |
| 5 | +import de.upb.cs.swt.delphi.instanceregistry.io.swagger.client.model.Instance |
| 6 | +import de.upb.cs.swt.delphi.instanceregistry.io.swagger.client.model.InstanceEnums.ComponentType |
| 7 | + |
| 8 | +import scala.collection.mutable |
| 9 | +import scala.util.{Failure, Success, Try} |
| 10 | + |
| 11 | +/** |
| 12 | + * Implementation of the instance data access object that keeps its data in memory |
| 13 | + * instead of using a persistent storage. |
| 14 | + */ |
| 15 | +class DynamicInstanceDAO extends InstanceDAO with AppLogging { |
| 16 | + |
| 17 | + private val instances : mutable.Set[Instance] = new mutable.HashSet[Instance]() |
| 18 | + implicit val system : ActorSystem = Server.system |
| 19 | + |
| 20 | + override def addInstance(instance: Instance): Try[Unit] = { |
| 21 | + //Verify ID is present in instance |
| 22 | + if(instance.id.isEmpty){ |
| 23 | + val msg = s"Cannot add instance ${instance.name}, id is empty!" |
| 24 | + log.warning(msg) |
| 25 | + Failure(new RuntimeException(msg)) |
| 26 | + } else { |
| 27 | + //Verify id is not already present in instances! |
| 28 | + if(!hasInstance(instance.id.get)){ |
| 29 | + instances.add(instance) |
| 30 | + Success(log.info(s"Added instance ${instance.name} with id ${instance.id} to database.")) |
| 31 | + } else { |
| 32 | + val msg = s"Cannot add instance ${instance.name}, id ${instance.id} already present." |
| 33 | + log.warning(msg) |
| 34 | + Failure(new RuntimeException(msg)) |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + override def hasInstance(id: Long): Boolean = { |
| 40 | + //addInstance verifies that id : Option[Long] is not empty, so can apply .get here! |
| 41 | + val query = instances filter {i => i.id.get == id} |
| 42 | + query.nonEmpty |
| 43 | + } |
| 44 | + |
| 45 | + override def removeInstance(id: Long): Try[Unit] = { |
| 46 | + if(hasInstance(id)){ |
| 47 | + //AddInstance verifies that id is always present, hasInstance verifies that find will return an instance |
| 48 | + instances.remove(instances.find(i => i.id.get == id).get) |
| 49 | + Success(log.info(s"Successfully removed instance with id $id.")) |
| 50 | + } else { |
| 51 | + val msg = s"Cannot remove instance with id $id, that id is not present." |
| 52 | + log.warning(msg) |
| 53 | + Failure(new RuntimeException(msg)) |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + override def getInstance(id: Long): Option[Instance] = { |
| 58 | + if(hasInstance(id)) { |
| 59 | + val query = instances filter {i => i.id.get == id} |
| 60 | + val instance = query.iterator.next() |
| 61 | + Some(instance) |
| 62 | + } else { |
| 63 | + None |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + override def getInstancesOfType(componentType: ComponentType): List[Instance] = { |
| 68 | + List() ++ instances filter {i => i.componentType == componentType} |
| 69 | + } |
| 70 | + |
| 71 | + override def getAllInstances(): List[Instance] = { |
| 72 | + List() ++ instances |
| 73 | + } |
| 74 | + |
| 75 | + override def clearAll() : Unit = { |
| 76 | + instances.clear() |
| 77 | + } |
| 78 | + |
| 79 | +} |
0 commit comments