Skip to content

Commit

Permalink
Extract keyspace configuration to a separate class. (#539)
Browse files Browse the repository at this point in the history
The idea is to allow several instances to exist, i.e.
in `CreateJournalSchema` and `CreateSnapshotSchema`.
  • Loading branch information
rtar authored Dec 13, 2023
1 parent 9212f0a commit 92dc546
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@ import com.evolutiongaming.kafka.journal.eventual.cassandra.CassandraHelper._
import com.evolutiongaming.scassandra.CreateKeyspaceIfNotExists

trait CreateKeyspace[F[_]] {
def apply(config: SchemaConfig.Keyspace): F[Unit]
def apply(config: KeyspaceConfig): F[Unit]
}

object CreateKeyspace { self =>

def empty[F[_] : Applicative]: CreateKeyspace[F] = (_: SchemaConfig.Keyspace) => ().pure[F]

def empty[F[_] : Applicative]: CreateKeyspace[F] = (_: KeyspaceConfig) => ().pure[F]

def apply[F[_] : Monad : CassandraCluster : CassandraSession : LogOf]: CreateKeyspace[F] = new CreateKeyspace[F] {
def apply(config: SchemaConfig.Keyspace) = {

def apply(config: KeyspaceConfig) = {
if (config.autoCreate) {
val keyspace = config.name

Expand All @@ -36,7 +35,7 @@ object CreateKeyspace { self =>
result <- metadata.fold(create)(_ => ().pure[F])
} yield result
} else {
().pure[F]
().pure[F]
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.evolutiongaming.kafka.journal.eventual.cassandra

import com.evolutiongaming.scassandra.ReplicationStrategyConfig
import pureconfig.ConfigReader
import pureconfig.generic.semiauto.deriveReader

final case class KeyspaceConfig(
name: String = "journal",
replicationStrategy: ReplicationStrategyConfig = ReplicationStrategyConfig.Default,
autoCreate: Boolean = true
)

object KeyspaceConfig {

val default: KeyspaceConfig = KeyspaceConfig()

implicit val configReaderKeyspace: ConfigReader[KeyspaceConfig] = deriveReader
}
Original file line number Diff line number Diff line change
@@ -1,39 +1,24 @@
package com.evolutiongaming.kafka.journal.eventual.cassandra

import com.evolutiongaming.scassandra.ReplicationStrategyConfig
import pureconfig.ConfigReader
import pureconfig.generic.semiauto.deriveReader


final case class SchemaConfig(
keyspace: SchemaConfig.Keyspace = SchemaConfig.Keyspace.default,
keyspace: KeyspaceConfig = KeyspaceConfig.default,
journalTable: String = "journal",
metadataTable: String = "metadata",
metaJournalTable: String = "metajournal",
pointerTable: String = "pointer",
pointer2Table: String = "pointer2",
settingTable: String = "setting",
locksTable: String = "locks",
autoCreate: Boolean = true)

autoCreate: Boolean = true
)

object SchemaConfig {

val default: SchemaConfig = SchemaConfig()

implicit val configReaderSchemaConfig: ConfigReader[SchemaConfig] = deriveReader


final case class Keyspace(
name: String = "journal",
replicationStrategy: ReplicationStrategyConfig = ReplicationStrategyConfig.Default,
autoCreate: Boolean = true)

object Keyspace {

val default: Keyspace = Keyspace()


implicit val configReaderKeyspace: ConfigReader[Keyspace] = deriveReader
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class CreateSchemaSpec extends AnyFunSuite with Matchers { self =>
}

val createKeyspace: CreateKeyspace[StateT] = new CreateKeyspace[StateT] {
def apply(config: SchemaConfig.Keyspace) = {
def apply(config: KeyspaceConfig) = {
StateT { state =>
val state1 = state.add(Action.CreateKeyspace)
(state1, ())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class SchemaConfigSpec extends AnyFunSuite with Matchers {
test("apply from config") {
val config = ConfigFactory.parseURL(getClass.getResource("schema.conf"))
val expected = SchemaConfig(
keyspace = SchemaConfig.Keyspace(
keyspace = KeyspaceConfig(
name = "keyspace",
replicationStrategy = ReplicationStrategyConfig.Simple(3),
autoCreate = false),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ object ReadEventsApp extends IOApp {

val eventualCassandraConfig = EventualCassandraConfig(
schema = SchemaConfig(
keyspace = SchemaConfig.Keyspace(
keyspace = KeyspaceConfig(
name = "keyspace",
autoCreate = false),
autoCreate = false),
Expand Down

0 comments on commit 92dc546

Please sign in to comment.