akka.persistence.postgresql.journal.JournalTest.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of akka-persistence-postgresql_2.13 Show documentation
Show all versions of akka-persistence-postgresql_2.13 Show documentation
Reactive Akka Journal, Snapshot Store, and Persistence Query plugin for PostgreSQL
The newest version!
/*
* Copyright 2020-2021 Borislav Borisov.
*
* Licensed 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
*
* https://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.
*/
package akka.persistence.postgresql.journal
import akka.NotUsed
import akka.actor.ActorSystem
import akka.persistence.journal.Tagged
import akka.persistence.r2dbc.client.{R2dbc}
import akka.persistence.r2dbc.journal.JournalEntry
import akka.persistence.{AtomicWrite, PersistentRepr}
import akka.stream.Materializer
import akka.stream.scaladsl.{Sink, Source}
import com.typesafe.config.ConfigFactory
import io.r2dbc.postgresql.{PostgresqlConnectionConfiguration, PostgresqlConnectionFactory}
import java.lang.{Long => JLong}
import java.time.Instant
import reactor.core.publisher.Flux
import scala.collection.immutable.Seq
//import scala.jdk.CollectionConverters._
object JournalTest extends App {
val first = Seq(
AtomicWrite(Seq(
PersistentRepr(Tagged("foo", Set("FooTag", "FooTagTwo")), 1L, "foo"),
PersistentRepr(Tagged("foo2", Set("FooTag", "FooTagTwo")), 2L, "foo")
)),
AtomicWrite(PersistentRepr(Tagged("bar", Set("BarTag")), 2L, "bar"))
)
val second = Seq(AtomicWrite(PersistentRepr("bax", 3L, "bax")), AtomicWrite(PersistentRepr("baz", 4L, "baz")))
val cf = new PostgresqlConnectionFactory(PostgresqlConnectionConfiguration.builder()
.host("localhost")
.username("postgres")
.password("s3cr3t")
.database("db")
.preparedStatementCacheQueries(5000)
.build())
// allow-java-serialization = on
implicit val system = ActorSystem("ExampleSystem",
ConfigFactory.parseString(
"""
|akka.actor.allow-java-serialization = on
""".stripMargin
).withFallback(ConfigFactory.defaultApplication()).resolve()
)
implicit val mat = Materializer(system)
// val daoAkkaStreams = new PostgreSqlJournalDao(R2dbc(cf))
val pId = s"foo-${Instant.now.getEpochSecond}"
val events = List(
JournalEntry(1, pId, 1, "test-value".getBytes),
JournalEntry(3, pId, 2, "test-value-2".getBytes),
JournalEntry(5, pId, 3, "test-value-3".getBytes)
)
val events2 = List(
JournalEntry(7, pId, 4, "test-value-4".getBytes),
JournalEntry(9, pId, 5, "test-value-5".getBytes),
JournalEntry(11, pId, 6, "test-value-6".getBytes)
)
val eventsWithTags = List(
JournalEntry(1, pId, 1, "test-value".getBytes, tags = Set("FooEvent")),
JournalEntry(3, pId, 2, "test-value-2".getBytes, tags = Set("FooEvent")),
JournalEntry(5, pId, 3, "test-value-3".getBytes, tags = Set("FooEvent"))
)
// daoAkkaStreams.writeEvents(events)
// .runWith(Sink.foreach(x => {
// println(s"Returning $x from writeEvents")
// }))
// daoAkkaStreams.bindWriteEvents(events.asJava)
// .runWith(Sink.foreach(x => {
// println(s"Returning $x from writeEvents")
// }))
//
// daoAkkaStreams.bindWriteEvents(events2.asJava)
// .runWith(Sink.foreach(x => {
// println(s"Returning $x from writeEvents")
// }))
Thread.sleep(2000)
Some("foo")
println("And this: " + (Some("Karamba") ++ Some("Abc")).head)
val z = for {
x <- Some("Foo")
y <- Some("Karamba")
} yield { x + y }
println("Z is: " + z)
println("What do we have: " + Seq(Some("fun"), None, Some("more fun")).flatten.reduce(_ + ";" + _))
println("What do we have: " + Seq[Option[String]](None, None, None).flatten.reduceOption(_ + ";" + _))
val x: Option[String] = Some("Karamba")
println(x.fold("five")( _ + ";" + "five"))
/*
println("Running fetchEvents")
daoAkkaStreams.fetchEvents(pId, 1L, 5L, 10)
.runWith(Sink.foreach(x => {
println(s"Returning $x from fetchEvents")
}))
Thread.sleep(1500)
println(s"Running crafted fetch ${PostgreSqlJournalQueries.findEventsQuery(pId, 1, 5, 10)}")
cf.create().flatMapMany(connection =>
connection.createStatement(PostgreSqlJournalQueries.findEventsQuery(pId, 1, 5, 10))
.bind(0, pId)
.bind(1, 1)
.bind(2, 5)
.bind(3, 10)
.execute()
.flatMap(result => result.map((row, meta) => row.get("id", classOf[JLong])))
.flatMap(ignored =>
connection.createStatement(PostgreSqlJournalQueries.findEventsQuery(pId, 1, 5, 10))
.bind(0, pId)
.bind(1, 1)
.bind(2, 5)
.bind(3, 10)
.execute()
.flatMap(result => result.map((row, meta) => row.get("id", classOf[JLong])))
)
)
.subscribe((x: JLong) => println(s"CF X $x"))
Thread.sleep(1500)
*/
mat.shutdown()
system.terminate()
}