quantranhong1999 commented on code in PR #1849:
URL: https://github.com/apache/james-project/pull/1849#discussion_r1423503438


##########
server/data/data-postgres/src/main/java/org/apache/james/sieve/postgres/PostgresSieveRepository.java:
##########
@@ -100,166 +80,154 @@ private boolean overQuotaAfterModification(long 
usedSpace, long size, QuotaSizeL
     }
 
     @Override
-    public void putScript(Username username, ScriptName name, ScriptContent 
content) {
-        
transactionRunner.runAndHandleException(Throwing.<EntityManager>consumer(entityManager
 -> {
-            try {
-                haveSpace(username, name, content.length());
-                JPASieveScript jpaSieveScript = JPASieveScript.builder()
-                        .username(username.asString())
-                        .scriptName(name.getValue())
-                        .scriptContent(content)
-                        .build();
-                entityManager.persist(jpaSieveScript);
-            } catch (QuotaExceededException | StorageException e) {
-                rollbackTransactionIfActive(entityManager.getTransaction());
-                throw e;
-            }
-        }).sneakyThrow(), throwStorageExceptionConsumer("Unable to put script 
for user " + username.asString()));
+    public void putScript(Username username, ScriptName name, ScriptContent 
content) throws QuotaExceededException {
+        reThrowQuotaExceededException(() -> 
spaceThatWillBeUsedByNewScript(username, name, content.length())
+            .flatMap(spaceToUse -> throwOnOverQuota(username, spaceToUse)
+                .thenEmpty(Flux.merge(
+                        updateSpaceUsed(username, spaceToUse),
+                        
postgresSieveScriptDAO.upsertScript(PostgresSieveScript.builder()

Review Comment:
   On that, I will tend to rely on the number of inserted/updated rows which is 
always returned by Postgres to know if the upsert succeeded. RETURNING requires 
extra data transfer while we do not really need them in this case.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to