Skip to content

Commit 847feaf

Browse files
committed
update upsert document method within jdbc state provider
1 parent aa14c78 commit 847feaf

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

jdbc/src/main/scala/app/softnetwork/persistence/jdbc/query/JdbcStateProvider.scala

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import app.softnetwork.persistence._
55
import app.softnetwork.persistence.jdbc.db.SlickDatabase
66
import app.softnetwork.persistence.model.{StateWrapper, Timestamped}
77
import app.softnetwork.persistence.query.JsonProvider
8-
import app.softnetwork.serialization.{serialization, updateCaseClass}
8+
import app.softnetwork.serialization.serialization
99
import org.json4s.Formats
1010
import slick.jdbc.JdbcProfile
1111

@@ -40,6 +40,8 @@ trait JdbcStateProvider[T <: Timestamped]
4040
case _ => table
4141
}
4242

43+
def excludedFields: Set[String] = Set("__serializedSizeMemoized")
44+
4345
implicit def executionContext: ExecutionContext
4446

4547
import api._
@@ -107,11 +109,19 @@ trait JdbcStateProvider[T <: Timestamped]
107109
* @return
108110
* whether the operation is successful or not
109111
*/
110-
override final def upsertDocument(uuid: String, data: String): Boolean = {
112+
override def upsertDocument(uuid: String, data: String): Boolean = {
111113
(loadDocument(uuid) match {
112114
case Some(document: T) =>
113115
implicit val manifest: Manifest[T] = manifestWrapper.wrapped
114-
Try(updateCaseClass(document, serialization.read[Map[String, Any]](data))) match {
116+
var state = serialization.read[Map[String, Any]](serialization.write(document))
117+
val updatedState = serialization.read[Map[String, Any]](data)
118+
for ((key, value) <- updatedState) {
119+
if (!excludedFields.contains(key)) {
120+
state = state + (key -> value)
121+
}
122+
}
123+
val updatedDocument = serialization.write(state)
124+
Try(serialization.read[T](updatedDocument)(formats, manifest)) match {
115125
case Success(updatedState: T) =>
116126
Some(updatedState)
117127
case Failure(e) =>

0 commit comments

Comments
 (0)