From 8274655c077644d7878e69fde41787c031e5e274 Mon Sep 17 00:00:00 2001 From: Omar Sifontes Date: Tue, 1 Oct 2024 17:10:43 -0300 Subject: [PATCH 01/12] ajustando librerias para mongo7, ajustando url del repo nexus --- build.sbt | 17 +++++++++-------- .../src/main/scala/flowly/demo/MainTest.scala | 15 +++++++-------- .../flowly/mongodb/MongoDBRepository.scala | 14 ++++++++------ project/CommonSettings.scala | 4 ++-- 4 files changed, 26 insertions(+), 24 deletions(-) diff --git a/build.sbt b/build.sbt index 6ea5e80..abc3af3 100644 --- a/build.sbt +++ b/build.sbt @@ -17,7 +17,8 @@ lazy val `flowly-core` = project "org.specs2" %% "specs2-mock" % "4.6.0" % "test", "com.fasterxml.jackson.core" % "jackson-databind" % jacksonVersion, "com.fasterxml.jackson.datatype" % "jackson-datatype-jsr310" % jacksonVersion, - "com.fasterxml.jackson.module" %% "jackson-module-scala" % jacksonVersion + "com.fasterxml.jackson.module" %% "jackson-module-scala" % jacksonVersion, + "org.hamcrest" % "hamcrest-core" % "2.1" % "test" ) ) @@ -26,7 +27,7 @@ lazy val `flowly-mongodb` = project .settings( name := "flowly-mongodb", libraryDependencies ++= Seq( - "org.mongojack" % "mongojack" % "2.10.0", + "org.mongojack" % "mongojack" % "4.0.0", "com.fasterxml.jackson.core" % "jackson-databind" % jacksonVersion, "com.fasterxml.jackson.datatype" % "jackson-datatype-jsr310" % jacksonVersion, "com.fasterxml.jackson.module" %% "jackson-module-scala" % jacksonVersion @@ -35,11 +36,11 @@ lazy val `flowly-mongodb` = project .dependsOn(`flowly-core`) lazy val `flowly-demo` = project - .settings(CommonSettings.settings: _*) - .settings( - name := "flowly-demo", - packagedArtifacts := Map.empty - ) - .dependsOn(`flowly-core`, `flowly-mongodb`) + .settings(CommonSettings.settings: _*) + .settings( + name := "flowly-demo", + packagedArtifacts := Map.empty + ) + .dependsOn(`flowly-core`, `flowly-mongodb`) scalacOptions in Test ++= Seq("-Yrangepos") \ No newline at end of file diff --git a/flowly-demo/src/main/scala/flowly/demo/MainTest.scala b/flowly-demo/src/main/scala/flowly/demo/MainTest.scala index 57579fb..39d3dfb 100644 --- a/flowly-demo/src/main/scala/flowly/demo/MainTest.scala +++ b/flowly-demo/src/main/scala/flowly/demo/MainTest.scala @@ -18,12 +18,11 @@ package flowly.demo import java.io.IOError import java.time.Instant - import com.fasterxml.jackson.databind.{DeserializationFeature, ObjectMapper, SerializationFeature} import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule import com.fasterxml.jackson.module.scala.DefaultScalaModule import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper -import com.mongodb.MongoClient +import com.mongodb.client.MongoClients import flowly.core.context.{ExecutionContextFactory, Key, ReadableExecutionContext, WritableExecutionContext} import flowly.core.events.EventListener import flowly.core.repository.model.Attempts @@ -42,7 +41,7 @@ object MainTest extends App { trait RepositoryComponent { this: ObjectMapperRepositoryComponent => - val client = new MongoClient("localhost") + val client = MongoClients.create("localhost") lazy val repository = new MongoDBRepository(client, "flowly", "demo", objectMapperRepository) // lazy val repository = new InMemoryRepository } @@ -166,16 +165,16 @@ object MainTest extends App { object Components extends WorkflowComponent with FirstComponent with SecondComponent with ThirdComponent with DisjunctionComponent with BlockingDisjunctionComponent with BlockingComponent with Finish1Component with Finish2Component with ObjectMapperRepositoryComponent with ObjectMapperContextComponent with RepositoryComponent - val r = for { + val r = for { - id <- Components.workflow.init() + id <- Components.workflow.init() - result <- Components.workflow.execute(id) + result <- Components.workflow.execute(id) - } yield result + } yield result - println(r) + println(r) } diff --git a/flowly-mongodb/src/main/scala/flowly/mongodb/MongoDBRepository.scala b/flowly-mongodb/src/main/scala/flowly/mongodb/MongoDBRepository.scala index 1a2ad5c..e349b7c 100644 --- a/flowly-mongodb/src/main/scala/flowly/mongodb/MongoDBRepository.scala +++ b/flowly-mongodb/src/main/scala/flowly/mongodb/MongoDBRepository.scala @@ -5,7 +5,7 @@ import java.time.Instant import java.util.Date import com.fasterxml.jackson.databind._ import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper -import com.mongodb.MongoClient +import com.mongodb.client.MongoClient import com.mongodb.client.MongoCursor import com.mongodb.client.model.IndexOptions import flowly.core.compat.CompatUtils @@ -15,7 +15,7 @@ import flowly.core.repository.model.Session.{SessionId, Status} import flowly.core.{ErrorOr, SessionNotFound} import javax.persistence.{OptimisticLockException, PersistenceException} -import org.bson.Document +import org.bson.{Document, UuidRepresentation} import org.mongojack.JacksonMongoCollection import scala.language.implicitConversions @@ -26,8 +26,8 @@ class MongoDBRepository(client: MongoClient, databaseName: String, collectionNam protected val collection: JacksonMongoCollection[Session] = { val mongoCollection = client.getDatabase(databaseName).getCollection(collectionName) - val builder: JacksonMongoCollection.JacksonMongoCollectionBuilder[Session] = JacksonMongoCollection.builder() - val coll = builder.withObjectMapper(objectMapper).build(mongoCollection, classOf[Session]) + val builder: JacksonMongoCollection.JacksonMongoCollectionBuilder = JacksonMongoCollection.builder() + val coll = builder.withObjectMapper(objectMapper).build(client, databaseName, classOf[Session], UuidRepresentation.STANDARD) // Initialize sessionId index coll.createIndex(Document("sessionId" -> 1.asJava), new IndexOptions().unique(true)) @@ -61,7 +61,9 @@ class MongoDBRepository(client: MongoClient, databaseName: String, collectionNam private[flowly] def update(session: Session): ErrorOr[Session] = { Try { // Update will replace every document field and it is going to increment in one unit its version - val document = JacksonMongoCollection.convertToDocument(session, objectMapper, classOf[Session]) + val jsonString = objectMapper.writeValueAsString(session) + val document = org.bson.Document.parse(jsonString) + document.remove("version") val update = Document("$set" -> document, "$inc" -> Document("version" -> 1.asJava)) @@ -69,7 +71,7 @@ class MongoDBRepository(client: MongoClient, databaseName: String, collectionNam // Condition: there is a session with the same sessionId and version val query = Document("sessionId" -> session.sessionId, "version" -> session.version.asJava) - collection.findAndModify(query, Document(), Document(), collection.serializeFields(update), true, false) + collection.findOneAndUpdate(query, update) } match { case Success(null) => Left(new OptimisticLockException(s"Session ${session.sessionId} was modified by another transaction")) diff --git a/project/CommonSettings.scala b/project/CommonSettings.scala index 1de4012..600365b 100644 --- a/project/CommonSettings.scala +++ b/project/CommonSettings.scala @@ -5,11 +5,11 @@ object CommonSettings { val settings: Seq[Def.Setting[_]] = Seq(organization := "com.despegar.flowly", - publishTo := Some("Nexus Despegar" at s"https://cnd-backend/nexus/repository/${if (isSnapshot.value) "maven-snapshots" else "maven-releases"}/"), + publishTo := Some("Nexus Despegar" at s"https://backoffice-secure.despegar.com/nexus/repository/${if (isSnapshot.value) "maven-snapshots" else "maven-releases"}/"), resolvers += Opts.resolver.mavenLocalFile, resolvers += Resolver.mavenLocal, resolvers += "Typesafe repository" at "https://repo.typesafe.com/typesafe/releases/", - resolvers += "Nexus Public Repository" at "https://cnd-backend/nexus/repository/maven-all/", + resolvers += "Nexus Public Repository" at "https://backoffice-secure.despegar.com/nexus/repository/maven-all/", scalaVersion := "2.13.0", crossScalaVersions := Seq("2.12.8", "2.13.0")) } \ No newline at end of file From 7e74b2de2c0f1ffc85b490896850db78c54521e9 Mon Sep 17 00:00:00 2001 From: Omar Sifontes Date: Tue, 1 Oct 2024 17:17:52 -0300 Subject: [PATCH 02/12] . --- flowly-demo/src/main/scala/flowly/demo/MainTest.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flowly-demo/src/main/scala/flowly/demo/MainTest.scala b/flowly-demo/src/main/scala/flowly/demo/MainTest.scala index 39d3dfb..7e704ba 100644 --- a/flowly-demo/src/main/scala/flowly/demo/MainTest.scala +++ b/flowly-demo/src/main/scala/flowly/demo/MainTest.scala @@ -41,7 +41,7 @@ object MainTest extends App { trait RepositoryComponent { this: ObjectMapperRepositoryComponent => - val client = MongoClients.create("localhost") + val client = MongoClients.create("localhost:s") lazy val repository = new MongoDBRepository(client, "flowly", "demo", objectMapperRepository) // lazy val repository = new InMemoryRepository } From 51c906050d3ce37bcccbcc6120e26d534e186d9b Mon Sep 17 00:00:00 2001 From: Omar Sifontes Date: Tue, 1 Oct 2024 17:18:44 -0300 Subject: [PATCH 03/12] . --- flowly-demo/src/main/scala/flowly/demo/MainTest.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flowly-demo/src/main/scala/flowly/demo/MainTest.scala b/flowly-demo/src/main/scala/flowly/demo/MainTest.scala index 7e704ba..39d3dfb 100644 --- a/flowly-demo/src/main/scala/flowly/demo/MainTest.scala +++ b/flowly-demo/src/main/scala/flowly/demo/MainTest.scala @@ -41,7 +41,7 @@ object MainTest extends App { trait RepositoryComponent { this: ObjectMapperRepositoryComponent => - val client = MongoClients.create("localhost:s") + val client = MongoClients.create("localhost") lazy val repository = new MongoDBRepository(client, "flowly", "demo", objectMapperRepository) // lazy val repository = new InMemoryRepository } From 068b1ae3e99d234788197b37e0c770baf2f7a2a5 Mon Sep 17 00:00:00 2001 From: Omar Sifontes Date: Tue, 1 Oct 2024 17:26:45 -0300 Subject: [PATCH 04/12] mejoras en codigo --- .../src/main/scala/flowly/mongodb/MongoDBRepository.scala | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/flowly-mongodb/src/main/scala/flowly/mongodb/MongoDBRepository.scala b/flowly-mongodb/src/main/scala/flowly/mongodb/MongoDBRepository.scala index e349b7c..796a68b 100644 --- a/flowly-mongodb/src/main/scala/flowly/mongodb/MongoDBRepository.scala +++ b/flowly-mongodb/src/main/scala/flowly/mongodb/MongoDBRepository.scala @@ -24,10 +24,7 @@ import scala.util.{Failure, Success, Try} class MongoDBRepository(client: MongoClient, databaseName: String, collectionName: String, objectMapper: ObjectMapper with ScalaObjectMapper) extends Repository { protected val collection: JacksonMongoCollection[Session] = { - val mongoCollection = client.getDatabase(databaseName).getCollection(collectionName) - - val builder: JacksonMongoCollection.JacksonMongoCollectionBuilder = JacksonMongoCollection.builder() - val coll = builder.withObjectMapper(objectMapper).build(client, databaseName, classOf[Session], UuidRepresentation.STANDARD) + val coll = JacksonMongoCollection.builder().withObjectMapper(objectMapper).build(client, databaseName, classOf[Session], UuidRepresentation.STANDARD) // Initialize sessionId index coll.createIndex(Document("sessionId" -> 1.asJava), new IndexOptions().unique(true)) From 5cbdb3342e040276f8e6d02d15e122671625ed28 Mon Sep 17 00:00:00 2001 From: Omar Sifontes Date: Tue, 1 Oct 2024 17:27:36 -0300 Subject: [PATCH 05/12] mejoras en codigo --- flowly-demo/src/main/scala/flowly/demo/MainTest.scala | 6 ++---- .../src/main/scala/flowly/mongodb/MongoDBRepository.scala | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/flowly-demo/src/main/scala/flowly/demo/MainTest.scala b/flowly-demo/src/main/scala/flowly/demo/MainTest.scala index 39d3dfb..4746bdd 100644 --- a/flowly-demo/src/main/scala/flowly/demo/MainTest.scala +++ b/flowly-demo/src/main/scala/flowly/demo/MainTest.scala @@ -16,7 +16,6 @@ package flowly.demo * limitations under the License. */ -import java.io.IOError import java.time.Instant import com.fasterxml.jackson.databind.{DeserializationFeature, ObjectMapper, SerializationFeature} import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule @@ -26,15 +25,14 @@ import com.mongodb.client.MongoClients import flowly.core.context.{ExecutionContextFactory, Key, ReadableExecutionContext, WritableExecutionContext} import flowly.core.events.EventListener import flowly.core.repository.model.Attempts -import flowly.core.repository.{InMemoryRepository, Repository} +import flowly.core.repository.Repository import flowly.core.tasks.basic._ -import flowly.core.tasks.compose.{Alternative, Retry, Retryable} +import flowly.core.tasks.compose.{Retry, Retryable} import flowly.core.tasks.strategies.scheduling.SchedulingStrategy import flowly.core.tasks.strategies.stopping.StoppingStrategy import flowly.core.Workflow import flowly.mongodb.{CustomDateModule, MongoDBRepository} -import scala.util.Try object MainTest extends App { diff --git a/flowly-mongodb/src/main/scala/flowly/mongodb/MongoDBRepository.scala b/flowly-mongodb/src/main/scala/flowly/mongodb/MongoDBRepository.scala index 796a68b..67ad3e8 100644 --- a/flowly-mongodb/src/main/scala/flowly/mongodb/MongoDBRepository.scala +++ b/flowly-mongodb/src/main/scala/flowly/mongodb/MongoDBRepository.scala @@ -11,7 +11,7 @@ import com.mongodb.client.model.IndexOptions import flowly.core.compat.CompatUtils import flowly.core.repository.Repository import flowly.core.repository.model.{Session, Status} -import flowly.core.repository.model.Session.{SessionId, Status} +import flowly.core.repository.model.Session.SessionId import flowly.core.{ErrorOr, SessionNotFound} import javax.persistence.{OptimisticLockException, PersistenceException} From 9176197a4e38077ac08c3b8e46cf5e6e1b4f7817 Mon Sep 17 00:00:00 2001 From: Omar Sifontes Date: Wed, 2 Oct 2024 11:58:31 -0300 Subject: [PATCH 06/12] ajustando version --- version.sbt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version.sbt b/version.sbt index 93a38ed..f075ec9 100644 --- a/version.sbt +++ b/version.sbt @@ -1 +1 @@ -version in ThisBuild := "0.2.8-SNAPSHOT" +version in ThisBuild := "0.2.9-SNAPSHOT" From b134d2948213caa2f6ec7b48c0313b5e260f6f8e Mon Sep 17 00:00:00 2001 From: Omar Sifontes Date: Wed, 2 Oct 2024 14:27:22 -0300 Subject: [PATCH 07/12] correcciones para mantener la configuracion anterior a la migracion --- .../src/main/scala/flowly/mongodb/MongoDBRepository.scala | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/flowly-mongodb/src/main/scala/flowly/mongodb/MongoDBRepository.scala b/flowly-mongodb/src/main/scala/flowly/mongodb/MongoDBRepository.scala index 67ad3e8..56193bc 100644 --- a/flowly-mongodb/src/main/scala/flowly/mongodb/MongoDBRepository.scala +++ b/flowly-mongodb/src/main/scala/flowly/mongodb/MongoDBRepository.scala @@ -7,7 +7,7 @@ import com.fasterxml.jackson.databind._ import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper import com.mongodb.client.MongoClient import com.mongodb.client.MongoCursor -import com.mongodb.client.model.IndexOptions +import com.mongodb.client.model.{FindOneAndUpdateOptions, IndexOptions, UpdateOptions} import flowly.core.compat.CompatUtils import flowly.core.repository.Repository import flowly.core.repository.model.{Session, Status} @@ -67,8 +67,8 @@ class MongoDBRepository(client: MongoClient, databaseName: String, collectionNam // Condition: there is a session with the same sessionId and version val query = Document("sessionId" -> session.sessionId, "version" -> session.version.asJava) - - collection.findOneAndUpdate(query, update) + val updateOptions = new FindOneAndUpdateOptions().upsert(true) + collection.findOneAndUpdate(query, update, updateOptions) } match { case Success(null) => Left(new OptimisticLockException(s"Session ${session.sessionId} was modified by another transaction")) From 3aa9914e26963faf8226f4d012e7952607c6a95b Mon Sep 17 00:00:00 2001 From: Omar Sifontes Date: Wed, 2 Oct 2024 14:30:59 -0300 Subject: [PATCH 08/12] . --- flowly-demo/src/main/scala/flowly/demo/MainTest.scala | 10 +++++----- .../main/scala/flowly/mongodb/MongoDBRepository.scala | 6 ++---- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/flowly-demo/src/main/scala/flowly/demo/MainTest.scala b/flowly-demo/src/main/scala/flowly/demo/MainTest.scala index 4746bdd..ce2f639 100644 --- a/flowly-demo/src/main/scala/flowly/demo/MainTest.scala +++ b/flowly-demo/src/main/scala/flowly/demo/MainTest.scala @@ -163,16 +163,16 @@ object MainTest extends App { object Components extends WorkflowComponent with FirstComponent with SecondComponent with ThirdComponent with DisjunctionComponent with BlockingDisjunctionComponent with BlockingComponent with Finish1Component with Finish2Component with ObjectMapperRepositoryComponent with ObjectMapperContextComponent with RepositoryComponent - val r = for { + val r = for { - id <- Components.workflow.init() + id <- Components.workflow.init() - result <- Components.workflow.execute(id) + result <- Components.workflow.execute(id) - } yield result + } yield result - println(r) + println(r) } diff --git a/flowly-mongodb/src/main/scala/flowly/mongodb/MongoDBRepository.scala b/flowly-mongodb/src/main/scala/flowly/mongodb/MongoDBRepository.scala index 56193bc..e698c6e 100644 --- a/flowly-mongodb/src/main/scala/flowly/mongodb/MongoDBRepository.scala +++ b/flowly-mongodb/src/main/scala/flowly/mongodb/MongoDBRepository.scala @@ -7,7 +7,7 @@ import com.fasterxml.jackson.databind._ import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper import com.mongodb.client.MongoClient import com.mongodb.client.MongoCursor -import com.mongodb.client.model.{FindOneAndUpdateOptions, IndexOptions, UpdateOptions} +import com.mongodb.client.model.{FindOneAndUpdateOptions, IndexOptions} import flowly.core.compat.CompatUtils import flowly.core.repository.Repository import flowly.core.repository.model.{Session, Status} @@ -58,9 +58,7 @@ class MongoDBRepository(client: MongoClient, databaseName: String, collectionNam private[flowly] def update(session: Session): ErrorOr[Session] = { Try { // Update will replace every document field and it is going to increment in one unit its version - val jsonString = objectMapper.writeValueAsString(session) - val document = org.bson.Document.parse(jsonString) - + val document = org.bson.Document.parse(objectMapper.writeValueAsString(session)) document.remove("version") val update = Document("$set" -> document, "$inc" -> Document("version" -> 1.asJava)) From fdc33447b0d7faae634157ff867b1313d947c929 Mon Sep 17 00:00:00 2001 From: Omar Sifontes Date: Wed, 2 Oct 2024 15:05:51 -0300 Subject: [PATCH 09/12] ajustando config de mongo --- .../src/main/scala/flowly/mongodb/MongoDBRepository.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flowly-mongodb/src/main/scala/flowly/mongodb/MongoDBRepository.scala b/flowly-mongodb/src/main/scala/flowly/mongodb/MongoDBRepository.scala index e698c6e..53365b7 100644 --- a/flowly-mongodb/src/main/scala/flowly/mongodb/MongoDBRepository.scala +++ b/flowly-mongodb/src/main/scala/flowly/mongodb/MongoDBRepository.scala @@ -24,7 +24,7 @@ import scala.util.{Failure, Success, Try} class MongoDBRepository(client: MongoClient, databaseName: String, collectionName: String, objectMapper: ObjectMapper with ScalaObjectMapper) extends Repository { protected val collection: JacksonMongoCollection[Session] = { - val coll = JacksonMongoCollection.builder().withObjectMapper(objectMapper).build(client, databaseName, classOf[Session], UuidRepresentation.STANDARD) + val coll = JacksonMongoCollection.builder().withObjectMapper(objectMapper).build(client, databaseName, collectionName, classOf[Session], UuidRepresentation.STANDARD) // Initialize sessionId index coll.createIndex(Document("sessionId" -> 1.asJava), new IndexOptions().unique(true)) From a8990dfb71b87c7c6e26603571880257c0cd5987 Mon Sep 17 00:00:00 2001 From: Omar Sifontes Date: Thu, 17 Oct 2024 11:32:50 -0300 Subject: [PATCH 10/12] . --- build.sbt | 3 +-- .../src/main/scala/flowly/mongodb/MongoDBRepository.scala | 4 +++- version.sbt | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/build.sbt b/build.sbt index abc3af3..08a48e8 100644 --- a/build.sbt +++ b/build.sbt @@ -17,8 +17,7 @@ lazy val `flowly-core` = project "org.specs2" %% "specs2-mock" % "4.6.0" % "test", "com.fasterxml.jackson.core" % "jackson-databind" % jacksonVersion, "com.fasterxml.jackson.datatype" % "jackson-datatype-jsr310" % jacksonVersion, - "com.fasterxml.jackson.module" %% "jackson-module-scala" % jacksonVersion, - "org.hamcrest" % "hamcrest-core" % "2.1" % "test" + "com.fasterxml.jackson.module" %% "jackson-module-scala" % jacksonVersion ) ) diff --git a/flowly-mongodb/src/main/scala/flowly/mongodb/MongoDBRepository.scala b/flowly-mongodb/src/main/scala/flowly/mongodb/MongoDBRepository.scala index 53365b7..1fd54a3 100644 --- a/flowly-mongodb/src/main/scala/flowly/mongodb/MongoDBRepository.scala +++ b/flowly-mongodb/src/main/scala/flowly/mongodb/MongoDBRepository.scala @@ -58,7 +58,9 @@ class MongoDBRepository(client: MongoClient, databaseName: String, collectionNam private[flowly] def update(session: Session): ErrorOr[Session] = { Try { // Update will replace every document field and it is going to increment in one unit its version - val document = org.bson.Document.parse(objectMapper.writeValueAsString(session)) + //val document = org.bson.Document.parse(objectMapper.writeValueAsString(session)) + + val document = JacksonMongoCollection.convertToDocument(session, objectMapper, classOf[Session]) document.remove("version") val update = Document("$set" -> document, "$inc" -> Document("version" -> 1.asJava)) diff --git a/version.sbt b/version.sbt index f075ec9..c79914f 100644 --- a/version.sbt +++ b/version.sbt @@ -1 +1 @@ -version in ThisBuild := "0.2.9-SNAPSHOT" +version in ThisBuild := "0.2.9.2-SNAPSHOT" From 3f6ffdc41e61f6031af4b0e7b34f54c4246353db Mon Sep 17 00:00:00 2001 From: Omar Sifontes Date: Thu, 17 Oct 2024 12:00:54 -0300 Subject: [PATCH 11/12] . --- .../src/main/scala/flowly/mongodb/MongoDBRepository.scala | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/flowly-mongodb/src/main/scala/flowly/mongodb/MongoDBRepository.scala b/flowly-mongodb/src/main/scala/flowly/mongodb/MongoDBRepository.scala index 1fd54a3..143896d 100644 --- a/flowly-mongodb/src/main/scala/flowly/mongodb/MongoDBRepository.scala +++ b/flowly-mongodb/src/main/scala/flowly/mongodb/MongoDBRepository.scala @@ -57,10 +57,8 @@ class MongoDBRepository(client: MongoClient, databaseName: String, collectionNam private[flowly] def update(session: Session): ErrorOr[Session] = { Try { - // Update will replace every document field and it is going to increment in one unit its version - //val document = org.bson.Document.parse(objectMapper.writeValueAsString(session)) + val document = org.bson.Document.parse(objectMapper.writeValueAsString(session)) - val document = JacksonMongoCollection.convertToDocument(session, objectMapper, classOf[Session]) document.remove("version") val update = Document("$set" -> document, "$inc" -> Document("version" -> 1.asJava)) From a305ec9b3fb836d0d81c2bfa40fc3886cd83dc13 Mon Sep 17 00:00:00 2001 From: Omar Sifontes Date: Thu, 17 Oct 2024 12:56:18 -0300 Subject: [PATCH 12/12] ajustando maneja de armar la data para enviar a mongo --- build.sbt | 12 ++++++------ .../flowly/mongodb/MongoDBRepository.scala | 17 +++++++---------- version.sbt | 2 +- 3 files changed, 14 insertions(+), 17 deletions(-) diff --git a/build.sbt b/build.sbt index 08a48e8..5cfc6fa 100644 --- a/build.sbt +++ b/build.sbt @@ -35,11 +35,11 @@ lazy val `flowly-mongodb` = project .dependsOn(`flowly-core`) lazy val `flowly-demo` = project - .settings(CommonSettings.settings: _*) - .settings( - name := "flowly-demo", - packagedArtifacts := Map.empty - ) - .dependsOn(`flowly-core`, `flowly-mongodb`) + .settings(CommonSettings.settings: _*) + .settings( + name := "flowly-demo", + packagedArtifacts := Map.empty + ) + .dependsOn(`flowly-core`, `flowly-mongodb`) scalacOptions in Test ++= Seq("-Yrangepos") \ No newline at end of file diff --git a/flowly-mongodb/src/main/scala/flowly/mongodb/MongoDBRepository.scala b/flowly-mongodb/src/main/scala/flowly/mongodb/MongoDBRepository.scala index 143896d..e357f69 100644 --- a/flowly-mongodb/src/main/scala/flowly/mongodb/MongoDBRepository.scala +++ b/flowly-mongodb/src/main/scala/flowly/mongodb/MongoDBRepository.scala @@ -7,7 +7,7 @@ import com.fasterxml.jackson.databind._ import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper import com.mongodb.client.MongoClient import com.mongodb.client.MongoCursor -import com.mongodb.client.model.{FindOneAndUpdateOptions, IndexOptions} +import com.mongodb.client.model.{FindOneAndUpdateOptions, IndexOptions, ReplaceOptions} import flowly.core.compat.CompatUtils import flowly.core.repository.Repository import flowly.core.repository.model.{Session, Status} @@ -57,16 +57,13 @@ class MongoDBRepository(client: MongoClient, databaseName: String, collectionNam private[flowly] def update(session: Session): ErrorOr[Session] = { Try { - val document = org.bson.Document.parse(objectMapper.writeValueAsString(session)) - - document.remove("version") - - val update = Document("$set" -> document, "$inc" -> Document("version" -> 1.asJava)) - - // Condition: there is a session with the same sessionId and version val query = Document("sessionId" -> session.sessionId, "version" -> session.version.asJava) - val updateOptions = new FindOneAndUpdateOptions().upsert(true) - collection.findOneAndUpdate(query, update, updateOptions) + + // Update will replace every document field and it is going to increment in one unit its version + val newSession = session.copy(version = session.version + 1) + val updateOptions = new ReplaceOptions().upsert(true) + collection.replaceOne(query, newSession, updateOptions) + newSession } match { case Success(null) => Left(new OptimisticLockException(s"Session ${session.sessionId} was modified by another transaction")) diff --git a/version.sbt b/version.sbt index c79914f..a2926a4 100644 --- a/version.sbt +++ b/version.sbt @@ -1 +1 @@ -version in ThisBuild := "0.2.9.2-SNAPSHOT" +version in ThisBuild := "0.2.9.3-SNAPSHOT"