Skip to content

Commit

Permalink
Feature/log4j embedded cassandra (outworkers#755)
Browse files Browse the repository at this point in the history
* Adding Log4J properties.

* Changing schema generation query.

* Using sequence everywhere.

* Skipping version bumps [version skip]

* Removing unused method

* Removing schema generation method from the table.
  • Loading branch information
alexflav23 authored Sep 27, 2017
1 parent 8505997 commit ca06db5
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
*/
package com.outworkers.phantom

import com.datastax.driver.core.Session
import com.outworkers.phantom.builder.QueryBuilder
import com.outworkers.phantom.builder.clauses.DeleteClause
import com.outworkers.phantom.builder.primitives.Primitive
import com.outworkers.phantom.builder.query.execution.{ExecutableCqlQuery, ExecutableStatements, FutureMonad, GuavaAdapter, QueryCollection}
import com.outworkers.phantom.builder.query.execution.{ExecutableCqlQuery, QueryCollection}
import com.outworkers.phantom.builder.query.sasi.Mode
import com.outworkers.phantom.builder.query.{RootCreateQuery, _}
import com.outworkers.phantom.column.AbstractColumn
Expand All @@ -29,8 +28,6 @@ import com.outworkers.phantom.macros.{==:==, SingleGeneric, TableHelper}
import org.slf4j.{Logger, LoggerFactory}
import shapeless.{Generic, HList}

import scala.concurrent.ExecutionContextExecutor

/**
* Main representation of a Cassandra table.
* @tparam T Type of this table.
Expand All @@ -56,16 +53,6 @@ abstract class CassandraTable[T <: CassandraTable[T, R], R](

lazy val logger: Logger = LoggerFactory.getLogger(getClass.getName.stripSuffix("$"))

def generateSchema[F[_]]()(
implicit session: Session,
keySpace: KeySpace,
ec: ExecutionContextExecutor,
monad: FutureMonad[F],
guavaAdapter: GuavaAdapter[F]
): F[Seq[ResultSet]] = {
new ExecutableStatements[F, Seq](autocreate(keySpace).queries).sequence()
}

def tableName: String = helper.tableName

def fromRow(r: Row): R = helper.fromRow(instance, r)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import com.outworkers.phantom.CassandraTable
import com.outworkers.phantom.builder._
import com.outworkers.phantom.builder.query.engine.CQLQuery
import com.outworkers.phantom.builder.query.execution.{ExecutableCqlQuery, QueryCollection}
import com.outworkers.phantom.builder.query.options.TablePropertyClause
import com.outworkers.phantom.builder.query.options.{CachingStrategies, TablePropertyClause}
import com.outworkers.phantom.builder.syntax.CQLSyntax
import com.outworkers.phantom.connectors.{KeySpace, SessionAugmenterImplicits}

Expand Down Expand Up @@ -161,7 +161,7 @@ object CreateQuery {

private[phantom] trait CreateImplicits extends TablePropertyClauses {

val Cache = Caching
val Cache: CachingStrategies = Caching

def apply[
T <: CassandraTable[T, _],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,16 @@ trait FutureMonad[F[_]] {
implicit ctx: ExecutionContextExecutor
): F[B]
}


object FutureMonadOps {
implicit class Ops[F[_], A](val source: F[A])(implicit monad: FutureMonad[F]) {
def map[B](f: A => B)(
implicit ctx: ExecutionContextExecutor
): F[B] = monad.map(source)(f)

def flatMap[B](fn: A => F[B])(
implicit ctx: ExecutionContextExecutor
): F[B]= monad.flatMap(source)(fn)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -239,25 +239,23 @@ abstract class QueryContext[P[_], F[_], Timeout](
override def future()(
implicit session: Session,
ctx: ExecutionContextExecutor
): F[Seq[ResultSet]] = new ExecutableStatements[F, Seq](query.queries).sequence()
): F[Seq[ResultSet]] = {
for {
tableCreationQuery <- adapter.fromGuava(query.executableQuery)
secondaryIndexes <- new ExecutableStatements(query.indexList).future()
sasiIndexes <- new ExecutableStatements(query.table.sasiQueries()).future()
} yield Seq(tableCreationQuery) ++ secondaryIndexes ++ sasiIndexes
}
}

implicit class CassandraTableStoreMethods[T <: CassandraTable[T, R], R](val table: CassandraTable[T, R]) {

def createSchemaAsync()(
implicit session: Session,
keySpace: KeySpace,
ec: ExecutionContextExecutor
): F[Seq[ResultSet]] = {
new ExecutableStatements[F, Seq](table.autocreate(keySpace).queries).sequence()
}

def createSchema(timeout: Timeout = defaultTimeout)(
implicit session: Session,
keySpace: KeySpace,
ec: ExecutionContextExecutor
): Seq[ResultSet] = {
await(new ExecutableStatements[F, Seq](table.autocreate(keySpace).queries).sequence(), timeout)
await(table.autocreate(keySpace).future(), timeout)
}

def storeRecord[V1, Repr <: HList, HL <: HList, Out <: HList](input: V1)(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ package object finagle extends TwitterQueryContext with DefaultImports {
implicit class SpoolSelectQueryOps[
P[_],
F[_],
Table <: CassandraTable[Table, _],
T <: CassandraTable[T, _],
Record,
Limit <: LimitBound,
Order <: OrderBound,
Status <: ConsistencyBound,
Chain <: WhereBound,
PS <: HList
](
val query: SelectQuery[Table, Record, Limit, Order, Status, Chain, PS]
val query: SelectQuery[T, Record, Limit, Order, Status, Chain, PS]
) extends AnyVal {
/**
* Produces a Twitter Spool of [R]ows
Expand Down Expand Up @@ -148,60 +148,60 @@ package object finagle extends TwitterQueryContext with DefaultImports {
}

implicit class InsertQueryAugmenter[
Table <: CassandraTable[Table, Record],
T <: CassandraTable[T, Record],
Record,
Status <: ConsistencyBound,
PS <: HList

](val query: InsertQuery[Table, Record, Status, PS]) extends AnyVal {
def ttl(duration: com.twitter.util.Duration): InsertQuery[Table, Record, Status, PS] = {
](val query: InsertQuery[T, Record, Status, PS]) extends AnyVal {
def ttl(duration: com.twitter.util.Duration): InsertQuery[T, Record, Status, PS] = {
query.ttl(duration.inSeconds)
}
}

implicit class UpdateQueryAugmenter[
Table <: CassandraTable[Table, _],
T <: CassandraTable[T, _],
Record,
Limit <: LimitBound,
Order <: OrderBound,
Status <: ConsistencyBound,
Chain <: WhereBound,
PS <: HList
](val query: UpdateQuery[Table, Record, Limit, Order, Status, Chain, PS]) extends AnyVal {
](val query: UpdateQuery[T, Record, Limit, Order, Status, Chain, PS]) extends AnyVal {

def ttl(duration: com.twitter.util.Duration): UpdateQuery[Table, Record, Limit, Order, Status, Chain, PS] = {
def ttl(duration: com.twitter.util.Duration): UpdateQuery[T, Record, Limit, Order, Status, Chain, PS] = {
query.ttl(duration.inSeconds)
}
}

implicit class AssignmentsUpdateQueryAugmenter[
Table <: CassandraTable[Table, _],
T <: CassandraTable[T, _],
Record,
Limit <: LimitBound,
Order <: OrderBound,
Status <: ConsistencyBound,
Chain <: WhereBound,
PS <: HList,
ModifiedPrepared <: HList
](val query: AssignmentsQuery[Table, Record, Limit, Order, Status, Chain, PS, ModifiedPrepared]) extends AnyVal {
](val query: AssignmentsQuery[T, Record, Limit, Order, Status, Chain, PS, ModifiedPrepared]) extends AnyVal {

def ttl(duration: TwitterDuration): AssignmentsQuery[Table, Record, Limit, Order, Status, Chain, PS, ModifiedPrepared] = {
def ttl(duration: TwitterDuration): AssignmentsQuery[T, Record, Limit, Order, Status, Chain, PS, ModifiedPrepared] = {
query.ttl(duration.inSeconds)
}
}

implicit class ConditionalUpdateQueryAugmenter[
Table <: CassandraTable[Table, _],
T <: CassandraTable[T, _],
Record,
Limit <: LimitBound,
Order <: OrderBound,
Status <: ConsistencyBound,
Chain <: WhereBound,
PS <: HList,
ModifiedPrepared <: HList
](val query: ConditionalQuery[Table, Record, Limit, Order, Status, Chain, PS, ModifiedPrepared]) extends AnyVal {
](val query: ConditionalQuery[T, Record, Limit, Order, Status, Chain, PS, ModifiedPrepared]) extends AnyVal {

def ttl(duration: TwitterDuration): ConditionalQuery[Table, Record, Limit, Order, Status, Chain, PS, ModifiedPrepared] = {
def ttl(duration: TwitterDuration): ConditionalQuery[T, Record, Limit, Order, Status, Chain, PS, ModifiedPrepared] = {
query.ttl(duration.inSeconds)
}
}
Expand Down
33 changes: 33 additions & 0 deletions phantom-sbt/src/main/resources/log4j.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# for production, you should probably set the root to INFO
# and the pattern to %c instead of %l. (%l is slower.)

# output messages into a rolling log file as well as stdout
log4j.rootLogger=ERROR,stdout,HColumnFamilyLogger

# stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c{3} - %m%n
log4j.appender.stdout.follow=true

log4j.appender.HColumnFamilyLogger=org.apache.log4j.ConsoleAppender
log4j.appender.HColumnFamilyLogger.layout=org.apache.log4j.PatternLayout
log4j.appender.HColumnFamilyLogger.layout.ConversionPattern=%m%n
log4j.category.HColumnFamilyLogger=ERROR
#log4j.category.org.apache=INFO, stdout
2 changes: 1 addition & 1 deletion project/Publishing.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import scala.util.Properties
object Publishing {

val defaultPublishingSettings = Seq(
version := "2.14.5"
version := "2.15.0"
)

lazy val noPublishSettings = Seq(
Expand Down

0 comments on commit ca06db5

Please sign in to comment.