From 14931fb507a5795a17ee763110382aa85525bd69 Mon Sep 17 00:00:00 2001 From: ThomasMeijers Date: Sun, 2 Oct 2016 23:36:10 +0200 Subject: [PATCH] Add ReadSpec read and change names from run to runGraph and alike --- build.sbt | 10 ------ src/main/scala/oriented/OrientClient.scala | 3 ++ .../scala/oriented/free/dsl/ClientDSL.scala | 8 +++++ .../free/interpreters/ClientInterpreter.scala | 1 + src/main/scala/oriented/syntax/syntax.scala | 36 +++++++++---------- src/test/scala/dsl/ReadSpec.scala | 24 ++++++++++++- 6 files changed, 53 insertions(+), 29 deletions(-) diff --git a/build.sbt b/build.sbt index e0244de..f2f4151 100644 --- a/build.sbt +++ b/build.sbt @@ -4,8 +4,6 @@ organization := "com.itsmeijers" version := "0.1-SNAPSHOT" -// Add compile for multiple versions - scalaVersion := "2.11.8" crossScalaVersions := Seq("2.10.6", "2.11.8") @@ -43,11 +41,3 @@ addCompilerPlugin("org.spire-math" %% "kind-projector" % "0.7.1") addCompilerPlugin("com.milessabin" % "si2712fix-plugin" % "1.2.0" cross CrossVersion.full) fork := true - -// Increase maximum JVM memory allocation -javaOptions in run ++= Seq( - "-XX:MaxDirectMemorySize=16384m" -) - - - diff --git a/src/main/scala/oriented/OrientClient.scala b/src/main/scala/oriented/OrientClient.scala index 04f7a7e..c103705 100644 --- a/src/main/scala/oriented/OrientClient.scala +++ b/src/main/scala/oriented/OrientClient.scala @@ -86,6 +86,9 @@ sealed trait OrientClient { outVertex: Vertex[C]) (implicit orientFormat: OrientFormat[A]): OrientIO[Edge[A]] = C.addEdge[A, B, C](edgeModel, inVertex, outVertex, orientFormat) + + def shutdown(close: Boolean = false): OrientIO[Unit] = + C.shutdown(close) } /** diff --git a/src/main/scala/oriented/free/dsl/ClientDSL.scala b/src/main/scala/oriented/free/dsl/ClientDSL.scala index 491aade..b5173c2 100644 --- a/src/main/scala/oriented/free/dsl/ClientDSL.scala +++ b/src/main/scala/oriented/free/dsl/ClientDSL.scala @@ -32,6 +32,12 @@ case class AddVertex[A](vertexModel: A, orientFormat: OrientFormat[A]) extends C */ case class AddEdge[A, B, C](edgeModel: A, inVertex: Vertex[B], outVertex: Vertex[C], orientFormat: OrientFormat[A]) extends ClientDSL[Edge[A]] +/** + * Constructor for shutting down OrientDB. + * @param close wether db gets closed permanently. + */ +case class Shutdown(close: Boolean) extends ClientDSL[Unit] + class Clients[F[_]](implicit i: Inject[ClientDSL, F]) { def createVertexType[A](orientFormat: OrientFormat[A]): Free[F, VertexType[A]] = @@ -49,6 +55,8 @@ class Clients[F[_]](implicit i: Inject[ClientDSL, F]) { orientFormat: OrientFormat[A]): Free[F, Edge[A]] = Free.inject[ClientDSL, F](AddEdge[A, B, C](edgeModel, inVertex, outVertex, orientFormat)) + def shutdown(close: Boolean): Free[F, Unit] = Free.inject[ClientDSL, F](Shutdown(close)) + } object Clients { diff --git a/src/main/scala/oriented/free/interpreters/ClientInterpreter.scala b/src/main/scala/oriented/free/interpreters/ClientInterpreter.scala index 43f10e1..33c462c 100644 --- a/src/main/scala/oriented/free/interpreters/ClientInterpreter.scala +++ b/src/main/scala/oriented/free/interpreters/ClientInterpreter.scala @@ -58,6 +58,7 @@ trait ClientInterpreter[G[_]] extends (ClientDSL ~> G) { case CreateEdgeType(orientFormat) => createEdgeType(orientFormat) case AddVertex(vertexModel, orientFormat) => addVertex(vertexModel, orientFormat) case AddEdge(edgeModel, iv, ov, orientFormat) => addEdge(edgeModel, iv, ov, orientFormat) + case Shutdown(close) => graph.shutdown(close) } } diff --git a/src/main/scala/oriented/syntax/syntax.scala b/src/main/scala/oriented/syntax/syntax.scala index 4e5608d..237e624 100644 --- a/src/main/scala/oriented/syntax/syntax.scala +++ b/src/main/scala/oriented/syntax/syntax.scala @@ -35,14 +35,14 @@ package object syntax { /** * Overloaded function of runUnsafe. */ - def runUnsafe(orientIO: OrientIO[A])(implicit orientClient: OrientClient): A = - runUnsafe(enableTransactions = true) + def runGraphUnsafe(implicit orientClient: OrientClient): A = + runGraphUnsafe(enableTransactions = true) /** * Runs the OrientIO unsafely. * Can throw errors and does not control side effects! */ - def runUnsafe(enableTransactions: Boolean)(implicit orientClient: OrientClient): A = { + def runGraphUnsafe(enableTransactions: Boolean)(implicit orientClient: OrientClient): A = { implicit val graph: OrientBaseGraph = if(enableTransactions) orientClient.graph else orientClient.graphNoTransaction @@ -64,12 +64,12 @@ package object syntax { /** * Overloaded function of tryRun. */ - def tryRun(implicit orientClient: OrientClient): Try[A] = tryRun(enableTransactions = true) + def tryGraphRun(implicit orientClient: OrientClient): Try[A] = tryGraphRun(enableTransactions = true) /** * Runs the orientIO safely resulting in a Try[A]. */ - def tryRun(enableTransactions: Boolean)(implicit orientClient: OrientClient): Try[A] = { + def tryGraphRun(enableTransactions: Boolean)(implicit orientClient: OrientClient): Try[A] = { implicit val graph: OrientBaseGraph = if(enableTransactions) orientClient.graph else orientClient.graphNoTransaction @@ -92,14 +92,14 @@ package object syntax { /** * Overloaded function of runSafe. */ - def run(implicit orientClient: OrientClient): Either[Throwable, A] = - run(enableTransactions = true) + def runGraph(implicit orientClient: OrientClient): Either[Throwable, A] = + runGraph(enableTransactions = true) /** * Runs the orientIO safely resulting in either a Throwable or A, where A is the result of Free. */ - def run(enableTransactions: Boolean)(implicit orientClient: OrientClient): Either[Throwable, A] = - tryRun(enableTransactions) match { + def runGraph(enableTransactions: Boolean)(implicit orientClient: OrientClient): Either[Throwable, A] = + tryGraphRun(enableTransactions) match { case Failure(exception) => Left(exception) case Success(a) => Right(a) } @@ -107,15 +107,15 @@ package object syntax { /** * Overloaded function of runAsyncUnsafe */ - def runAsyncUnsafe(implicit executionContext: ExecutionContext, orientClient: OrientClient): Future[A] = - runAsyncUnsafe(enableTransactions = true) + def runGraphAsyncUnsafe(implicit executionContext: ExecutionContext, orientClient: OrientClient): Future[A] = + runGraphAsyncUnsafe(enableTransactions = true) /** * Runs the orientIO resulting in Futures, note that this is expirimental since the OrientElements are not thread * save. */ - def runAsyncUnsafe(enableTransactions: Boolean) - (implicit executionContext: ExecutionContext, + def runGraphAsyncUnsafe(enableTransactions: Boolean) + (implicit executionContext: ExecutionContext, orientClient: OrientClient): Future[A] = { implicit val graph: OrientBaseGraph = if(enableTransactions) orientClient.graph else orientClient.graphNoTransaction @@ -144,15 +144,15 @@ package object syntax { /** * Overloaded function of runAsyncSafe */ - def runAsync(implicit executionContext: ExecutionContext, - orientClient: OrientClient): EitherT[Future, Throwable, A] = - runAsync(enableTransactions = true) + def runGraphAsync(implicit executionContext: ExecutionContext, + orientClient: OrientClient): EitherT[Future, Throwable, A] = + runGraphAsync(enableTransactions = true) /** * Runs the orientIO resulting in a Either Transformer of Future Either[Throwable, A] */ - def runAsync(enableTransactions: Boolean) - (implicit executionContext: ExecutionContext, + def runGraphAsync(enableTransactions: Boolean) + (implicit executionContext: ExecutionContext, orientClient: OrientClient): EitherT[Future, Throwable, A] = { implicit val graph: OrientBaseGraph = if(enableTransactions) orientClient.graph else orientClient.graphNoTransaction diff --git a/src/test/scala/dsl/ReadSpec.scala b/src/test/scala/dsl/ReadSpec.scala index 45975ef..fe9db47 100644 --- a/src/test/scala/dsl/ReadSpec.scala +++ b/src/test/scala/dsl/ReadSpec.scala @@ -1,8 +1,30 @@ package dsl +import org.scalatest.{BeforeAndAfter, FlatSpec, Matchers} +import oriented.{InMemoryClient, OrientFormat} +import oriented.syntax._ + /** * Test spec for Read DSL */ -class ReadSpec { +class ReadSpec extends FlatSpec with Matchers with BeforeAndAfter { + + object Test + + implicit val testFormat: OrientFormat[Test.type] = new OrientFormat[Test.type] { + + override def format: OrientRead[Test.type] = read.read(Test) + + override def name: String = "Test" + + override def properties(model: Test.type): Map[String, Any] = Map() + } + + implicit val orientClient = InMemoryClient("test") + + "Read constructor" should "save edge with no fields" in { + val edge = orientClient.addVertex(Test) + edge.runGraphUnsafe(enableTransactions = false).element should === (Test) + } }