Skip to content

Commit

Permalink
Add ReadSpec read and change names from run to runGraph and alike
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsMeijers committed Oct 2, 2016
1 parent f83708e commit 14931fb
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 29 deletions.
10 changes: 0 additions & 10 deletions build.sbt
Expand Up @@ -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")
Expand Down Expand Up @@ -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"
)



3 changes: 3 additions & 0 deletions src/main/scala/oriented/OrientClient.scala
Expand Up @@ -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)
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/main/scala/oriented/free/dsl/ClientDSL.scala
Expand Up @@ -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]] =
Expand All @@ -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 {
Expand Down
Expand Up @@ -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)
}

}
Expand Down
36 changes: 18 additions & 18 deletions src/main/scala/oriented/syntax/syntax.scala
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -92,30 +92,30 @@ 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)
}

/**
* 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
Expand Down Expand Up @@ -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
Expand Down
24 changes: 23 additions & 1 deletion 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)
}

}

0 comments on commit 14931fb

Please sign in to comment.