hungphan227 commented on code in PR #1849:
URL: https://github.com/apache/james-project/pull/1849#discussion_r1425010478
##########
server/data/data-postgres/src/main/java/org/apache/james/sieve/postgres/PostgresSieveRepository.java:
##########
@@ -49,217 +43,167 @@
import org.apache.james.sieverepository.api.exception.QuotaExceededException;
import org.apache.james.sieverepository.api.exception.QuotaNotFoundException;
import org.apache.james.sieverepository.api.exception.ScriptNotFoundException;
-import org.apache.james.sieverepository.api.exception.StorageException;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.github.fge.lambdas.Throwing;
-import com.google.common.collect.ImmutableList;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
public class PostgresSieveRepository implements SieveRepository {
-
- private static final Logger LOGGER =
LoggerFactory.getLogger(PostgresSieveRepository.class);
-
- private final TransactionRunner transactionRunner;
private final PostgresSieveQuotaDAO postgresSieveQuotaDAO;
+ private final PostgresSieveScriptDAO postgresSieveScriptDAO;
@Inject
- public PostgresSieveRepository(EntityManagerFactory entityManagerFactory,
PostgresSieveQuotaDAO postgresSieveQuotaDAO) {
- this.transactionRunner = new TransactionRunner(entityManagerFactory);
+ public PostgresSieveRepository(PostgresSieveQuotaDAO postgresSieveQuotaDAO,
+ PostgresSieveScriptDAO
postgresSieveScriptDAO) {
this.postgresSieveQuotaDAO = postgresSieveQuotaDAO;
+ this.postgresSieveScriptDAO = postgresSieveScriptDAO;
}
@Override
- public void haveSpace(Username username, ScriptName name, long size)
throws QuotaExceededException, StorageException {
- long usedSpace = findAllSieveScriptsForUser(username).stream()
- .filter(sieveScript ->
!sieveScript.getScriptName().equals(name.getValue()))
- .mapToLong(JPASieveScript::getScriptSize)
- .sum();
-
- QuotaSizeLimit quota = limitToUser(username);
- if (overQuotaAfterModification(usedSpace, size, quota)) {
- throw new QuotaExceededException();
+ public void haveSpace(Username username, ScriptName name, long size)
throws QuotaExceededException {
+ long sizeDifference = spaceThatWillBeUsedByNewScript(username, name,
size).block();
+ throwOnOverQuota(username, sizeDifference);
+ }
+
+ @Override
+ public void putScript(Username username, ScriptName name, ScriptContent
content) throws QuotaExceededException {
+ long sizeDifference = spaceThatWillBeUsedByNewScript(username, name,
content.length()).block();
+ throwOnOverQuota(username, sizeDifference);
+ postgresSieveScriptDAO.upsertScript(PostgresSieveScript.builder()
+ .username(username.asString())
+ .scriptName(name.getValue())
+ .scriptContent(content.getValue())
+ .scriptSize(content.length())
+ .isActive(false)
+ .build())
+ .flatMap(upsertedScripts -> {
+ if (upsertedScripts > 0) {
+ return updateSpaceUsed(username, sizeDifference);
+ }
+ return Mono.empty();
+ })
+ .block();
+ }
+
+ private Mono<Void> updateSpaceUsed(Username username, long spaceToUse) {
+ if (spaceToUse == 0) {
+ return Mono.empty();
}
+ return postgresSieveQuotaDAO.updateSpaceUsed(username, spaceToUse);
}
- private QuotaSizeLimit limitToUser(Username username) {
- return postgresSieveQuotaDAO.getQuota(username)
- .filter(Optional::isPresent)
- .switchIfEmpty(postgresSieveQuotaDAO.getGlobalQuota())
- .block()
- .orElse(QuotaSizeLimit.unlimited());
+ private Mono<Long> spaceThatWillBeUsedByNewScript(Username username,
ScriptName name, long scriptSize) {
+ return postgresSieveScriptDAO.getScript(username, name)
+ .map(PostgresSieveScript::getScriptSize)
Review Comment:
should we just get script size from db? the whole record is unnecessary,
especially script content could be big (I guess :)) )
--
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]