Skip to content

Commit

Permalink
Revenj Scala v1.5
Browse files Browse the repository at this point in the history
Allow use of optimized persist function.
  • Loading branch information
zapov committed Dec 1, 2022
1 parent 6abe6b7 commit b83551e
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 3 deletions.
6 changes: 3 additions & 3 deletions scala/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ lazy val core = (project in file("revenj-core")
settings (commonSettings ++ publishSettings)
enablePlugins(SbtDslPlatformPlugin)
settings(
version := "1.4.0",
version := "1.5.0",
libraryDependencies ++= Seq(
"org.postgresql" % "postgresql" % "42.3.1",
"joda-time" % "joda-time" % "2.10.13", // TODO: will be removed
Expand All @@ -29,7 +29,7 @@ lazy val core = (project in file("revenj-core")
lazy val akka = (project in file("revenj-akka")
settings (commonSettings ++ publishSettings)
settings(
version := "1.4.0",
version := "1.5.0",
libraryDependencies ++= Seq(
"com.typesafe" % "config" % "1.3.4",
"com.typesafe.akka" %% "akka-http" % "10.2.7",
Expand All @@ -42,7 +42,7 @@ lazy val akka = (project in file("revenj-akka")
lazy val storage = (project in file("revenj-storage")
settings (commonSettings ++ publishSettings)
settings(
version := "1.4.0",
version := "1.5.0",
libraryDependencies ++= Seq(
"com.amazonaws" % "aws-java-sdk-s3" % "1.12.115",
"org.specs2" %% "specs2-scalacheck" % "4.13.0" % Test,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package net.revenj.database.postgres.converters

import net.revenj.database.postgres.PostgresWriter.{CollectionDiff, NewTuple}
import net.revenj.database.postgres.converters.PostgresTuple.buildNextEscape
import net.revenj.database.postgres.{PostgresReader, PostgresWriter}
import net.revenj.patterns.{Equality, Identifiable}
import org.postgresql.util.PGobject

import scala.collection.concurrent.TrieMap
import scala.collection.mutable
Expand Down Expand Up @@ -346,4 +349,110 @@ object ArrayTuple {
}
}
}

def toParameter(sw: PostgresWriter, arrayType: String, data: Iterable[Array[PostgresTuple]]): PGobject = {
sw.reset()
val array = new ArrayTuple(data.map(a => RecordTuple(a)).toArray)
val pgo = new PGobject
pgo.setType(arrayType)
array.buildTuple(sw, false)
pgo.setValue(sw.bufferToString())
sw.reset()
pgo
}

private def emptyPGO(arrayType: String): PGobject = {
val pgo = new PGobject
pgo.setType(arrayType)
pgo.setValue("NULL")
pgo
}

def toParameterSimple[T](
sw: PostgresWriter,
collection: scala.collection.Seq[T],
arrayType: String,
toTuple: T => PostgresTuple
): PGobject = {
if (collection.nonEmpty) {
toParameter(
sw,
arrayType,
collection.zipWithIndex.map { case (item, ind) =>
Array[PostgresTuple](
IntConverter.toTuple(ind),
toTuple(item))
})
} else {
emptyPGO(arrayType)
}
}

def toParameterPair[T](
sw: PostgresWriter,
collection: scala.collection.Seq[(T, T)],
arrayType: String,
toTupleUpdate: T => PostgresTuple,
toTupleTable: T => PostgresTuple
): PGobject = {
if(collection.nonEmpty) {
toParameter(
sw,
arrayType,
collection.zipWithIndex.map { case ((oldValue, newValue), ind) =>
Array[PostgresTuple](
IntConverter.toTuple(ind),
if (oldValue == null) PostgresTuple.NULL else toTupleUpdate(oldValue),
if (newValue == null) PostgresTuple.NULL else toTupleTable(newValue))
})
} else {
emptyPGO(arrayType)
}
}

def toParameterNew[T](
sw: PostgresWriter,
collection: scala.collection.Seq[NewTuple[T]],
arrayType: String,
toTuple: T => PostgresTuple
): PGobject = {
if(collection.nonEmpty) {
toParameter(
sw,
arrayType,
collection.map { tuple =>
Array[PostgresTuple](
IntConverter.toTuple(tuple.index),
IntConverter.toTuple(tuple.element),
toTuple(tuple.value))
})
} else {
emptyPGO(arrayType)
}
}

def toParameterDiff[T <: Equality[T] with Identifiable](
sw: PostgresWriter,
collection: scala.collection.Seq[CollectionDiff[T]],
arrayType: String,
toTuple: T => PostgresTuple
): PGobject = {
if(collection.nonEmpty) {
toParameter(
sw,
arrayType,
collection.map { it =>
Array[PostgresTuple](
IntConverter.toTuple(it.index),
IntConverter.toTuple(it.element),
if (it.oldValue.isEmpty) PostgresTuple.NULL else toTuple(it.oldValue.get),
if (it.changedValue.isEmpty) PostgresTuple.NULL else toTuple(it.changedValue.get),
if (it.newValue.isEmpty) PostgresTuple.NULL else toTuple(it.newValue.get),
BoolConverter.toTuple(it.isNew))
})
} else {
emptyPGO(arrayType)
}
}

}

0 comments on commit b83551e

Please sign in to comment.