Skip to content

Commit

Permalink
bugs fixed and upgrade to latest datastax driver for jdk 17 support
Browse files Browse the repository at this point in the history
  • Loading branch information
gengstrand committed Jan 6, 2024
1 parent 10a5086 commit 972d9e5
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 27 deletions.
6 changes: 3 additions & 3 deletions server/feed15/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ dependencies {
implementation("ch.qos.logback:logback-classic")
implementation("com.google.code.gson:gson:2.10.1")
implementation("redis.clients:jedis:3.2.0")
implementation("com.datastax.oss:java-driver-core:4.5.1")
implementation("com.datastax.oss:java-driver-query-builder:4.5.1")
implementation("com.datastax.oss:java-driver-mapper-runtime:4.5.1")
implementation("com.datastax.oss:java-driver-core:4.17.0")
implementation("com.datastax.oss:java-driver-query-builder:4.17.0")
implementation("com.datastax.oss:java-driver-mapper-runtime:4.17.0")
testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude(group = "org.mockito", module = "mockito-core")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,22 @@ class ParticipantController(private val participantService: ParticipantService)
fun getInbound(request: ServerRequest): Mono<ServerResponse> {
val id = request.pathVariable("id").toLongOrNull()
if (id == null) return ServerResponse.badRequest().body(fromValue("invalid id"))
val rv = participantService.getInbound(id)
return ServerResponse.ok().body(fromValue(rv))
return participantService.getInbound(id)
.flatMap { ServerResponse.ok().body(fromValue(it)) }
}

fun getOutbound(request: ServerRequest): Mono<ServerResponse> {
val id = request.pathVariable("id").toLongOrNull()
if (id == null) return ServerResponse.badRequest().body(fromValue("invalid id"))
val rv = participantService.getOutbound(id)
return ServerResponse.ok().body(fromValue(rv))
return participantService.getOutbound(id)
.flatMap { ServerResponse.ok().body(fromValue(it)) }
}

fun addOutbound(request: ServerRequest): Mono<ServerResponse> {
val id = request.pathVariable("id").toLongOrNull()
if (id == null) return ServerResponse.badRequest().body(fromValue("invalid id"))
return request.bodyToMono(OutboundModel::class.java)
.flatMap {
val ob = participantService.addOutbound(id, it)
ServerResponse.ok().body(fromValue(ob))
}
.flatMap { participantService.addOutbound(id, it) }
.flatMap { ServerResponse.ok().body(fromValue(it)) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ import info.glennengstrand.newsfeed.models.ParticipantModel
import mu.KotlinLogging
import org.springframework.stereotype.Component
import reactor.core.publisher.Mono
import java.time.format.DateTimeFormatter
import java.util.concurrent.CompletableFuture

@Component
class InboundDao {
private val n = NoSqlDao()
private val f = DateTimeFormatter.ofPattern("yyyy-MM-dd")
private val logger = KotlinLogging.logger {}
private val selectCql = """select toTimestamp(occurred) as occurred, fromparticipantid, subject, story
from Inbound where participantid = ? order by occurred desc"""
Expand All @@ -38,15 +36,15 @@ class InboundDao {
val from = ParticipantModel(id, "").link
return Mono.fromFuture {
CompletableFuture.supplyAsync<List<InboundModel>> {
val bs = selectStatement.bind(id)
val bs = selectStatement.bind(id.toInt())
val rs = session.execute(bs)
val rv = mutableListOf<InboundModel>()
rs.forEach {
rv.add(
InboundModel(
ParticipantModel(it.getLong(1), "").link,
ParticipantModel(it.getInt(1).toLong(), "").link,
from,
f.format(it.getInstant(0)),
n.format(it.getInstant(0)),
it.getString(2)!!,
it.getString(3)!!,
),
Expand All @@ -63,8 +61,8 @@ class InboundDao {
): Mono<InboundModel> {
val bs =
insertStatement.bind(
ParticipantModel.unlink(ib.to),
id,
ParticipantModel.unlink(ib.to).toInt(),
id.toInt(),
ib.subject,
ib.story,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package info.glennengstrand.newsfeed.daos

import com.datastax.oss.driver.api.core.CqlSessionBuilder
import java.net.InetSocketAddress
import java.time.Instant
import java.time.ZoneId
import java.time.format.DateTimeFormatter
import java.util.Locale
import java.util.concurrent.TimeUnit

open class NoSqlDao {
private val noSqlHost = System.getenv("NOSQL_HOST") ?: "localhost"
private val noSqlKeyspace = System.getenv("NOSQL_KEYSPACE") ?: "activity"
private val cp = InetSocketAddress(noSqlHost, 9042)
private val f =
DateTimeFormatter.ofPattern("yyyy-MM-dd")
.withLocale(Locale.US)
.withZone(ZoneId.systemDefault())

public val noSqlTtl = System.getenv("NOSQL_TTL")?.toLong() ?: TimeUnit.DAYS.toMillis(1)

public fun connect(f: (CqlSessionBuilder) -> Unit) {
return f(
CqlSessionBuilder()
.addContactPoint(cp)
.withLocalDatacenter("datacenter1")
.withKeyspace(noSqlKeyspace),
)
}

public fun format(v: Instant?): String = v?.let { f.format(it) } ?: f.format(Instant.now())
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ import info.glennengstrand.newsfeed.models.ParticipantModel
import mu.KotlinLogging
import org.springframework.stereotype.Component
import reactor.core.publisher.Mono
import java.time.format.DateTimeFormatter
import java.util.concurrent.CompletableFuture

@Component
class OutboundDao {
private val n = NoSqlDao()
private val f = DateTimeFormatter.ofPattern("yyyy-MM-dd")
private val logger = KotlinLogging.logger {}
private val selectCql = """select toTimestamp(occurred) as occurred, subject, story
from Outbound where participantid = ? order by occurred desc"""
Expand All @@ -38,14 +36,14 @@ class OutboundDao {
val from = ParticipantModel(id, "").link
return Mono.fromFuture {
CompletableFuture.supplyAsync<List<OutboundModel>> {
val bs = selectStatement.bind(id)
val bs = selectStatement.bind(id.toInt())
val rs = session.execute(bs)
val rv = mutableListOf<OutboundModel>()
rs.forEach {
rv.add(
OutboundModel(
from,
f.format(it.getInstant(0)),
n.format(it.getInstant(0)),
it.getString(1)!!,
it.getString(2)!!,
),
Expand All @@ -58,15 +56,15 @@ class OutboundDao {

fun addOutbound(
id: Long,
ib: OutboundModel,
ob: OutboundModel,
): Mono<OutboundModel> {
val bs =
insertStatement.bind(
id,
ib.subject,
ib.story,
id.toInt(),
ob.subject,
ob.story,
)
session.executeAsync(bs)
return Mono.just(ib)
return Mono.just(ob)
}
}

0 comments on commit 972d9e5

Please sign in to comment.