Skip to content

Commit

Permalink
change modules to:
Browse files Browse the repository at this point in the history
1. quill-cassandra, basic cassandra driver.
2. quill-cassandra-datastax that depends on quill-cassandra and uses the datastax driver
  • Loading branch information
WayneWang12 committed Jan 29, 2019
1 parent 5424ef7 commit 6493f89
Show file tree
Hide file tree
Showing 39 changed files with 715 additions and 200 deletions.
32 changes: 29 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ enablePlugins(TutPlugin)
lazy val modules = Seq[sbt.ClasspathDep[sbt.ProjectReference]](
`quill-core-jvm`, `quill-core-js`, `quill-monix`, `quill-sql-jvm`, `quill-sql-js`,
`quill-jdbc`, `quill-jdbc-monix`, `quill-finagle-mysql`, `quill-finagle-postgres`, `quill-async`,
`quill-async-mysql`, `quill-async-postgres`, `quill-cassandra`, `quill-cassandra-monix`, `quill-orientdb`,
`quill-spark`
`quill-async-mysql`, `quill-async-postgres`, `quill-cassandra`, `quill-cassandra-datastax`,
`quill-cassandra-lagom`, `quill-cassandra-monix`, `quill-orientdb`, `quill-spark`
)

lazy val `quill` =
Expand Down Expand Up @@ -213,15 +213,41 @@ lazy val `quill-cassandra` =
)
.dependsOn(`quill-core-jvm` % "compile->compile;test->test")

lazy val `quill-cassandra-datastax` =
(project in file("quill-cassandra-datastax"))
.settings(commonSettings: _*)
.settings(mimaSettings: _*)
.settings(
fork in Test := true
)
.dependsOn(`quill-cassandra` % "compile->compile;test->test")

lazy val `quill-cassandra-monix` =
(project in file("quill-cassandra-monix"))
.settings(commonSettings: _*)
.settings(mimaSettings: _*)
.settings(
fork in Test := true
)
.dependsOn(`quill-cassandra-datastax` % "compile->compile;test->test")
.dependsOn(`quill-monix` % "compile->compile;test->test")

lazy val `quill-cassandra-lagom` =
(project in file("quill-cassandra-lagom"))
.settings(commonSettings: _*)
.settings(mimaSettings: _*)
.settings(
fork in Test := true,
libraryDependencies ++= {
val lagomVersion = "1.5.0-RC1"
Seq(
"com.lightbend.lagom" %% "lagom-scaladsl-persistence-cassandra" % lagomVersion % Provided,
"com.lightbend.lagom" %% "lagom-scaladsl-testkit" % lagomVersion % Test
)
}
)
.dependsOn(`quill-cassandra` % "compile->compile;test->test")
.dependsOn(`quill-monix` % "compile->compile;test->test")


lazy val `quill-orientdb` =
(project in file("quill-orientdb"))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
package io.getquill

import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import io.getquill.context.cassandra.util.FutureConversions.toScalaFuture
import io.getquill.util.{ ContextLogger, LoadConfig }
import com.typesafe.config.Config
import scala.collection.JavaConverters._
import io.getquill.context.cassandra.CassandraSessionContext
import com.datastax.driver.core.Cluster
import com.typesafe.config.Config
import io.getquill.context.cassandra.util.FutureConversions._
import io.getquill.monad.ScalaFutureIOMonad
import io.getquill.util.{ ContextLogger, LoadConfig }

import scala.collection.JavaConverters._
import scala.concurrent.{ ExecutionContext, Future }

class CassandraAsyncContext[N <: NamingStrategy](
naming: N,
cluster: Cluster,
keyspace: String,
preparedStatementCacheSize: Long
)
extends CassandraSessionContext[N](naming, cluster, keyspace, preparedStatementCacheSize)
extends CassandraDatastaxSessionContext[N](naming, cluster, keyspace, preparedStatementCacheSize)
with ScalaFutureIOMonad {

def this(naming: N, config: CassandraContextConfig) = this(naming, config.cluster, config.keyspace, config.preparedStatementCacheSize)

def this(naming: N, config: Config) = this(naming, CassandraContextConfig(config))

def this(naming: N, configPrefix: String) = this(naming, LoadConfig(configPrefix))

private val logger = ContextLogger(classOf[CassandraAsyncContext[_]])
Expand All @@ -36,30 +37,27 @@ class CassandraAsyncContext[N <: NamingStrategy](
super.performIO(io)
}

def executeQuery[T](cql: String, prepare: Prepare = identityPrepare, extractor: Extractor[T] = identityExtractor)(implicit ec: ExecutionContext): Future[List[T]] =
this.prepareAsync(cql).map(prepare).flatMap {
case (params, bs) =>
logger.logQuery(cql, params)
session.executeAsync(bs)
.map(_.all.asScala.toList.map(extractor))
}
def executeQuery[T](cql: String, prepare: Prepare = identityPrepare, extractor: Extractor[T] = identityExtractor)(implicit executionContext: ExecutionContext): Result[RunQueryResult[T]] = {
val statement = prepareAsyncAndGetStatement(cql, prepare, logger)
statement.flatMap(st => session.executeAsync(st))
.map(_.all.asScala.toList.map(extractor))
}

def executeQuerySingle[T](cql: String, prepare: Prepare = identityPrepare, extractor: Extractor[T] = identityExtractor)(implicit ec: ExecutionContext): Future[T] =
def executeQuerySingle[T](cql: String, prepare: Prepare = identityPrepare, extractor: Extractor[T] = identityExtractor)(implicit executionContext: ExecutionContext): Result[RunQuerySingleResult[T]] = {
executeQuery(cql, prepare, extractor).map(handleSingleResult)
}

def executeAction[T](cql: String, prepare: Prepare = identityPrepare)(implicit ec: ExecutionContext): Future[Unit] = {
this.prepareAsync(cql).map(prepare).flatMap {
case (params, bs) =>
logger.logQuery(cql, params)
session.executeAsync(bs).map(_ => ())
}
def executeAction[T](cql: String, prepare: Prepare = identityPrepare)(implicit executionContext: ExecutionContext): Result[RunActionResult] = {
val statement = prepareAsyncAndGetStatement(cql, prepare, logger)
statement.flatMap(st => session.executeAsync(st)).map(_ => ())
}

def executeBatchAction(groups: List[BatchGroup])(implicit ec: ExecutionContext): Future[Unit] =
def executeBatchAction(groups: List[BatchGroup])(implicit executionContext: ExecutionContext): Result[RunBatchActionResult] = {
Future.sequence {
groups.flatMap {
case BatchGroup(cql, prepare) =>
prepare.map(executeAction(cql, _))
}
}.map(_ => ())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.getquill

import com.datastax.driver.core.{ Cluster, _ }
import io.getquill.context.cassandra.{ CassandraSessionContext, PrepareStatementCache }
import io.getquill.util.Messages.fail
import io.getquill.context.cassandra.util.FutureConversions._

import scala.collection.JavaConverters._
import scala.concurrent.{ ExecutionContext, Future }

abstract class CassandraDatastaxSessionContext[N <: NamingStrategy](
val naming: N,
cluster: Cluster,
keyspace: String,
preparedStatementCacheSize: Long
)
extends CassandraSessionContext[N] {

private val preparedStatementCache =
new PrepareStatementCache(preparedStatementCacheSize)

protected lazy val session = cluster.connect(keyspace)

protected val udtMetadata: Map[String, List[UserType]] = cluster.getMetadata.getKeyspaces.asScala.toList
.flatMap(_.getUserTypes.asScala)
.groupBy(_.getTypeName)

def udtValueOf(udtName: String, keyspace: Option[String] = None): UDTValue =
udtMetadata.getOrElse(udtName.toLowerCase, Nil) match {
case udt :: Nil => udt.newValue()
case Nil =>
fail(s"Could not find UDT `$udtName` in any keyspace")
case udts => udts
.find(udt => keyspace.contains(udt.getKeyspace) || udt.getKeyspace == session.getLoggedKeyspace)
.map(_.newValue())
.getOrElse(fail(s"Could not determine to which keyspace `$udtName` UDT belongs. " +
s"Please specify desired keyspace using UdtMeta"))
}

protected def prepare(cql: String): BoundStatement =
preparedStatementCache(cql)(session.prepare)

protected def prepareAsync(cql: String)(implicit executionContext: ExecutionContext): Future[BoundStatement] =
preparedStatementCache.async(cql)(session.prepareAsync(_))

def close() = {
session.close
cluster.close
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package io.getquill

import com.datastax.driver.core.Cluster
import com.typesafe.config.Config
import io.getquill.monad.SyncIOMonad
import io.getquill.util.{ ContextLogger, LoadConfig }
import io.getquill.context.cassandra.CassandraSessionContext

import scala.collection.JavaConverters._
import com.datastax.driver.core.Cluster
import io.getquill.monad.SyncIOMonad

class CassandraSyncContext[N <: NamingStrategy](
naming: N,
cluster: Cluster,
keyspace: String,
preparedStatementCacheSize: Long
)
extends CassandraSessionContext[N](naming, cluster, keyspace, preparedStatementCacheSize)
extends CassandraDatastaxSessionContext[N](naming, cluster, keyspace, preparedStatementCacheSize)
with SyncIOMonad {

def this(naming: N, config: CassandraContextConfig) = this(naming, config.cluster, config.keyspace, config.preparedStatementCacheSize)
Expand Down
19 changes: 19 additions & 0 deletions quill-cassandra-datastax/src/test/resources/application.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
testSyncDB.keyspace=quill_test
testSyncDB.preparedStatementCacheSize=1000
testSyncDB.session.contactPoint=${?CASSANDRA_HOST}
testSyncDB.session.port=${?CASSANDRA_PORT}
testSyncDB.session.queryOptions.fetchSize=1
testSyncDB.session.queryOptions.consistencyLevel=LOCAL_QUORUM
testSyncDB.session.withoutMetrics=true
testSyncDB.session.withoutJMXReporting=false
testSyncDB.session.credentials.0=root
testSyncDB.session.credentials.1=pass
testSyncDB.session.maxSchemaAgreementWaitSeconds=1
testSyncDB.session.addressTranslator=com.datastax.driver.core.policies.IdentityTranslator

testAsyncDB.keyspace=quill_test
testAsyncDB.preparedStatementCacheSize=1000
testAsyncDB.session.contactPoint=${?CASSANDRA_HOST}
testAsyncDB.session.port=${?CASSANDRA_PORT}
testAsyncDB.session.queryOptions.fetchSize=1
testAsyncDB.session.queryOptions.consistencyLevel=LOCAL_QUORUM
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package io.getquill.context.cassandra

import io.getquill._
import java.util.{ Date, UUID }
import java.time.{ LocalDate => Java8LocalDate, Instant, ZonedDateTime, ZoneId }
import java.time.{ Instant, ZoneId, ZonedDateTime, LocalDate => Java8LocalDate }
import java.util.Date

import com.datastax.driver.core.LocalDate

Expand Down Expand Up @@ -112,124 +111,3 @@ class EncodingSpec extends EncodingSpecHelper {
}
}

abstract class EncodingSpecHelper extends Spec {
protected def verify(result: List[EncodingTestEntity]): Unit =
result.zip(insertValues) match {
case List((e1, a1), (e2, a2)) =>
verify(e1, a1)
verify(e2, a2)
}

protected def verify(e: EncodingTestEntity, a: EncodingTestEntity): Unit = {
e.id mustEqual a.id

e.v1 mustEqual a.v1
e.v2 mustEqual a.v2
e.v3 mustEqual a.v3
e.v4 mustEqual a.v4
e.v5 mustEqual a.v5
e.v6 mustEqual a.v6
e.v7 mustEqual a.v7
e.v8.toList mustEqual a.v8.toList
e.v9 mustEqual a.v9
e.v10 mustEqual a.v10
e.v11 mustEqual a.v11
e.o1 mustEqual a.o1
e.o2 mustEqual a.o2
e.o3 mustEqual a.o3
e.o4 mustEqual a.o4
e.o5 mustEqual a.o5
e.o6 mustEqual a.o6
e.o7 mustEqual a.o7
e.o8.map(_.toList) mustEqual a.o8.map(_.toList)
e.o9 mustEqual a.o9
e.o10 mustEqual a.o10

()
}

case class EncodingTestEntity(
id: Int,
v1: String,
v2: BigDecimal,
v3: Boolean,
v4: Int,
v5: Long,
v6: Float,
v7: Double,
v8: Array[Byte],
v9: LocalDate,
v10: UUID,
v11: Date,
v12: Byte,
v13: Short,
o1: Option[String],
o2: Option[BigDecimal],
o3: Option[Boolean],
o4: Option[Int],
o5: Option[Long],
o6: Option[Float],
o7: Option[Double],
o8: Option[Array[Byte]],
o9: Option[Date],
o10: Option[LocalDate]
)

protected val fixUUID: UUID = UUID.fromString("606c79e8-a331-4810-8bd7-0668ff7a23ef")

val insertValues =
List(
EncodingTestEntity(
id = 1,
v1 = "s",
v2 = BigDecimal(1.1),
v3 = true,
v4 = 33,
v5 = 431L,
v6 = 34.4f,
v7 = 42d,
v8 = Array(1.toByte, 2.toByte),
v9 = LocalDate.fromYearMonthDay(2014, 11, 11),
v10 = fixUUID,
v11 = new Date(31202000),
v12 = (Byte.MaxValue - 10).toByte,
v13 = (Short.MaxValue - 10).toShort,
o1 = Some("s"),
o2 = Some(BigDecimal(1.1)),
o3 = Some(true),
o4 = Some(33),
o5 = Some(431L),
o6 = Some(34.4f),
o7 = Some(42d),
o8 = Some(Array(1.toByte, 2.toByte)),
o9 = Some(new Date(31200000)),
o10 = Some(LocalDate.fromYearMonthDay(2014, 11, 11))
),
EncodingTestEntity(
id = 2,
v1 = "",
v2 = BigDecimal(0),
v3 = false,
v4 = 0,
v5 = 0L,
v6 = 0F,
v7 = 0D,
v8 = Array(),
v9 = LocalDate.fromMillisSinceEpoch(0),
v10 = fixUUID,
v11 = new Date(0),
v12 = 0,
v13 = 0,
o1 = None,
o2 = None,
o3 = None,
o4 = None,
o5 = None,
o6 = None,
o7 = None,
o8 = None,
o9 = None,
o10 = None
)
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.getquill.context

import scala.concurrent.Await
import scala.concurrent.Future
import scala.concurrent.duration.Duration

import io.getquill._
import io.getquill.Literal

package object cassandra {

lazy val mirrorContext = new CassandraMirrorContext(Literal) with CassandraTestEntities

lazy val testSyncDB = new CassandraSyncContext(Literal, "testSyncDB") with CassandraTestEntities

lazy val testAsyncDB = new CassandraAsyncContext(Literal, "testAsyncDB") with CassandraTestEntities

def await[T](f: Future[T]): T = Await.result(f, Duration.Inf)
}
Loading

0 comments on commit 6493f89

Please sign in to comment.