Skip to content

Commit

Permalink
Feature/static cols (#671)
Browse files Browse the repository at this point in the history
* Adding a fix for frozen collection types.

* Updating initialisation

* Fixing Database nesting.

* Resetting Database filters to allow inheritance

* Adding filter members without exclusions.

* Fixing frozen collection types.

* Adding tests for serialisation.

* Adding collection type serialisation tests.

* Adding a static column modifier.

* Sequenced traverse

* Fixing comparison table

* Fixing comparison table

* Fixing tables

* oops

* Adding checkbox

* Adding a version based condition

* Evaluating on call to prevent operation from completing ahed of time
  • Loading branch information
alexflav23 authored Apr 28, 2017
1 parent fb14f9b commit 448906a
Show file tree
Hide file tree
Showing 19 changed files with 190 additions and 117 deletions.
12 changes: 6 additions & 6 deletions comparison.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ are with Cassandra and the support they offer, to their level of activity
and how up to date they are.


| Driver | Language | Commercial | Type-safe | Spark Support | Streams | DSL | Cassandra | Latest | Activity | Created |
| ------ | -------- | ----- | ------------------ | --------- | ------------- | ---------------- | --- | -------- | ---- | ----- | ----- |
| Datastax Java Driver | Java | yes | no | no | no | EDSL | 3.8.0 | 3.1.0 | High | 2012 |
| Phantom | Scala | yes | yes | no | yes | EDSL | 3.8.0 | 3.1.0 | High | 2013 |
| Spark Connector | Scala | yes | yes | yes | no | EDSL | 3.0 | High | 2014 |
| Quill | Scala | no | yes | no | yes | QDSL | 3.8.0 | 2015 |
| Driver | Language | Commercial | Type-safe | Schema Safe | Spark Support | Streams | DSL | Cassandra | Latest | Activity | Created |
| ------ | -------- | ---------- | --------- | ----------- | ------------- | --------| --- | --------- | ------ | ------- | -------- |
| Java Driver | Java | [x] | [-] | [-] | [-] | [-] | EDSL | 3.8.0 | 3.1.0 | High | 2012 |
| Phantom | Scala | [x] | [x] | [x] | [x] | [x] | EDSL | 3.8.0 | 3.1.0 | High | 2013 |
| Spark Connector | Scala | [x] | [x] | [x] | [-] | [-] | EDSL | 3.0 | 3.0.0 | High | 2014 |
| Quill | Scala | [-] | [x] | [-] | [x] | [-] | QDSL | 3.8.0 | 3.8.0 | High | 2015 |


### An overview of the various drivers and using them from Scala
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,26 @@ class KeySpaceBuilder(clusterBuilder: ClusterBuilder) {
* @param query The builder to use when producing the keyspace query.
* @return
*/
@deprecated("Simply pass in a keySpace query, the keyspace is not required", "2.8.5")
def keySpace(
name: String,
query: KeySpaceCQLQuery
): CassandraConnection = {
new CassandraConnection(name, clusterBuilder, true, Some(query))
}

/**
* Creates and can initialise a keyspace with the given name.
* This will automatically initialise the keyspace by default, as we consider
* passing a specific keyspace query indicates clear intent you want this to happen.
* @param query The builder to use when producing the keyspace query.
* @return
*/
def keySpace(
query: KeySpaceCQLQuery
): CassandraConnection = {
new CassandraConnection(query.space, clusterBuilder, true, Some(query))
}
}

/**
Expand All @@ -84,6 +98,8 @@ class KeySpaceBuilder(clusterBuilder: ClusterBuilder) {
*/
trait KeySpaceCQLQuery {
def queryString: String

def space: String
}


Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,7 @@ class PrimitiveMacro(val c: scala.reflect.macros.blackbox.Context) {
val innerTpe = tpe.typeArgs.headOption

innerTpe match {
case Some(inner) => {
q"""$prefix.Primitives.set[$inner]"""
}
case Some(inner) => q"""$prefix.Primitives.set[$inner]"""
case None => c.abort(c.enclosingPosition, "Expected inner type to be defined")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ trait ExecutableStatement extends CassandraOperations {
}

private[this] object SequentialFutures {
def sequencedTraverse[
def sequencedTraverse[
A,
B,
M[X] <: TraversableOnce[X]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ case class CQLQuery(override val queryString: String) extends KeySpaceCQLQuery {
col: M[String],
sep: String = defaultSep
): CQLQuery = wrap(col mkString sep)

override def space: String = ""
}

object CQLQuery {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,10 @@ private[builder] abstract class CollectionModifiers(queryBuilder: QueryBuilder)
colType: String,
cassandraType: String,
shouldFreeze: Boolean,
freezeInner: Boolean
freezeInner: Boolean,
static: Boolean
): CQLQuery = {
(shouldFreeze, freezeInner) match {
val root = (shouldFreeze, freezeInner) match {
case (true, true) =>
// frozen<list<frozen<tuple<string, string>>>
frozen(diamond(colType, frozen(cassandraType).queryString))
Expand All @@ -223,6 +224,12 @@ private[builder] abstract class CollectionModifiers(queryBuilder: QueryBuilder)
// list<string>
case (false, false) => diamond(colType, cassandraType)
}

if (static) {
root.pad.append("static")
} else {
root
}
}

def frozen(cassandraType: CQLQuery): CQLQuery = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ sealed class KeySpaceSerializer(
}

override def queryString: String = qb.queryString

override def space: String = keySpace.name
}

class RootSerializer(val keySpace: KeySpace) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ class ListColumn[
CQLSyntax.Collections.list,
valuePrimitive.cassandraType,
shouldFreeze,
valuePrimitive.frozen
valuePrimitive.frozen,
isStaticColumn
).queryString

override def valueAsCql(v: RR): String = valuePrimitive.asCql(v)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ class SetColumn[
CQLSyntax.Collections.set,
valuePrimitive.cassandraType,
shouldFreeze,
valuePrimitive.frozen
valuePrimitive.frozen,
isStaticColumn
).queryString

override def valueAsCql(v: RR): String = Primitive[RR].asCql(v)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ package object dsl extends ImplicitMechanism with CreateImplicits
with DeleteImplicits {

type CassandraTable[Owner <: CassandraTable[Owner, Record], Record] = phantom.CassandraTable[Owner, Record]
type Table[Owner <: CassandraTable[Owner, Record], Record] = phantom.CassandraTable[Owner, Record]

type Column[Owner <: CassandraTable[Owner, Record], Record, T] = com.outworkers.phantom.column.Column[Owner, Record, T]
type PrimitiveColumn[Owner <: CassandraTable[Owner, Record], Record, T] = com.outworkers.phantom.column.PrimitiveColumn[Owner, Record, T]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ trait Index extends Indexed with Undroppable {
}

trait Keys {
self : Index with AbstractColumn[_] =>
self: Index with AbstractColumn[_] =>

abstract override def isMapKeyIndex: Boolean = true
}
Expand All @@ -55,4 +55,10 @@ trait Entries {
self: Index with AbstractColumn[_] =>

abstract override def isMapEntryIndex: Boolean = true
}
}


trait StaticColumn extends Key[StaticColumn] {
self: AbstractColumn[_] =>
abstract override def isStaticColumn: Boolean = true
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import com.outworkers.phantom.dsl._
import com.outworkers.phantom.tables.StaticCollectionRecord
import com.outworkers.util.samplers._

import scala.concurrent.Future

class StaticColumnTest extends PhantomSuite {

override def beforeAll(): Unit = {
Expand All @@ -41,10 +43,8 @@ class StaticColumnTest extends PhantomSuite {
select <- database.staticTable.select.where(_.id eqs id).and(_.clusteringId eqs id2).one()
} yield select

whenReady(chain) {
res => {
res.value.static shouldEqual static
}
whenReady(chain) { res =>
res.value.static shouldEqual static
}
}

Expand All @@ -66,10 +66,9 @@ class StaticColumnTest extends PhantomSuite {
select <- database.staticTable.select.where(_.id eqs id).and(_.clusteringId eqs id).one()
} yield select

whenReady(chain) {
res =>
// The first record should hold the updated value.
res.value.static shouldEqual static2
whenReady(chain) { res =>
// The first record should hold the updated value.
res.value.static shouldEqual static2
}
}

Expand All @@ -79,18 +78,21 @@ class StaticColumnTest extends PhantomSuite {
val sample = gen[StaticCollectionRecord].copy(id = id)
val sample2 = gen[StaticCollectionRecord].copy(id = id, list = sample.list)

val qb = database.staticCollectionTable.update.where(_.id eqs id)
.and(_.clusteringId eqs sample.clustering)
.modify(_.staticList append "test")
.queryString

val chain = for {
store1 <- database.staticCollectionTable.store(sample).future()
store2 <- database.staticCollectionTable.store(sample2).future()
update <- database.staticCollectionTable.update.where(_.id eqs id)
def updateQuery: Future[ResultSet] = if (cassandraVersion.value >= Version.`3.0.0`) {
db.staticCollectionTable.update.where(_.id eqs id)
.and(_.clusteringId eqs sample.clustering)
.modify(_.staticList append "test")
.future()
} else {
db.staticCollectionTable.update.where(_.id eqs id)
.modify(_.staticList append "test")
.future()
}

val chain = for {
store1 <- db.staticCollectionTable.store(sample).future()
store2 <- db.staticCollectionTable.store(sample2).future()
update <- updateQuery

rec <- database.staticCollectionTable
.select
Expand All @@ -99,8 +101,8 @@ class StaticColumnTest extends PhantomSuite {
.one()
} yield rec

whenReady(chain) {
res => res.value.list shouldEqual sample.list ::: List("test")
whenReady(chain) { res =>
res.value.list shouldEqual sample.list ::: List("test")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.outworkers.phantom.builder.serializers
import java.util.concurrent.TimeUnit

import com.outworkers.phantom.builder.QueryBuilder
import com.outworkers.phantom.builder.primitives.Primitive
import com.outworkers.phantom.builder.query.SerializationTest
import com.outworkers.phantom.builder.syntax.CQLSyntax
import com.outworkers.phantom.dsl._
Expand All @@ -30,7 +31,9 @@ import scala.concurrent.duration._

class CreateQueryBuilderTest extends FreeSpec with Matchers with SerializationTest {

private[this] val BasicTable = TestDatabase.basicTable
def db: TestDatabase = TestDatabase

private[this] val BasicTable = db.basicTable
final val DefaultTtl = 500
final val OneDay = 86400

Expand Down Expand Up @@ -365,5 +368,62 @@ class CreateQueryBuilderTest extends FreeSpec with Matchers with SerializationTe
}
}

"should account for static modifiers on a collection" - {
"add a static column modifier on a simple collection" in {
val stringP = Primitive[String]

db.staticCollectionTable.staticList.cassandraType shouldEqual s"list<${stringP.cassandraType}> static"
}
}

"should generate a correct cassandra type for collections " - {
"freeze a collection type without a frozen inner type" in {
val qb = QueryBuilder.Collections.collectionType(
colType = CQLSyntax.Collections.list,
cassandraType = CQLSyntax.Types.BigInt,
shouldFreeze = true,
freezeInner = false,
static = false
).queryString

qb shouldEqual "frozen<list<bigint>>"
}

"freeze a collection type with a frozen inner type" in {

val tpe = QueryBuilder.Collections.tupleType(
CQLSyntax.Types.BigInt,
CQLSyntax.Types.Text
).queryString

val qb = QueryBuilder.Collections.collectionType(
colType = CQLSyntax.Collections.list,
cassandraType = tpe,
shouldFreeze = true,
freezeInner = true,
static = false
).queryString

qb shouldEqual "frozen<list<frozen<tuple<bigint, text>>>>"
}

"generate a frozen collection if its used as a partition key " in {

val stringP = Primitive[String]

val cType = db.primaryCollectionsTable.listIndex.cassandraType

cType shouldEqual s"frozen<list<${stringP.cassandraType}>>"
}

"generate a frozen collection if its used as a primary key " in {

val stringP = Primitive[String]

val cType = db.primaryCollectionsTable.setCol.cassandraType

cType shouldEqual s"frozen<set<${stringP.cassandraType}>>"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,26 @@ import com.outworkers.phantom.dsl._

case class CounterRecord(id: UUID, count: Long)

class CounterTableTest extends CassandraTable[ConcreteCounterTableTest, CounterRecord] {

abstract class CounterTableTest extends Table[
CounterTableTest,
CounterRecord
] with RootConnector {
object id extends UUIDColumn(this) with PartitionKey
object count_entries extends CounterColumn(this)
}

abstract class ConcreteCounterTableTest extends CounterTableTest with RootConnector {
override val tableName = "counter_column_tests"
}

class SecondaryCounterTable extends CassandraTable[ConcreteSecondaryCounterTable, CounterRecord] {
abstract class SecondaryCounterTable extends Table[
SecondaryCounterTable,
CounterRecord
] with RootConnector {
object id extends UUIDColumn(this) with PartitionKey
object count_entries extends CounterColumn(this)
}

abstract class ConcreteSecondaryCounterTable extends SecondaryCounterTable with RootConnector {
override val tableName = "secondary_column_tests"
}

class BrokenCounterTableTest extends CassandraTable[ConcreteBrokenCounterTableTest, CounterRecord] {

abstract class BrokenCounterTableTest extends CassandraTable[
BrokenCounterTableTest,
CounterRecord
] with RootConnector {
object id extends UUIDColumn(this) with PartitionKey
object count_entries extends CounterColumn(this)

}

abstract class ConcreteBrokenCounterTableTest extends BrokenCounterTableTest with RootConnector {
override val tableName = "counter_column_tests"
}

Loading

0 comments on commit 448906a

Please sign in to comment.