chibenwa commented on code in PR #1706:
URL: https://github.com/apache/james-project/pull/1706#discussion_r1315456010


##########
mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/quota/CassandraPerUserMaxQuotaManagerV2.java:
##########
@@ -0,0 +1,360 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.mailbox.cassandra.quota;
+
+import static 
org.apache.james.backends.cassandra.components.CassandraQuotaLimitDao.QuotaLimitKey;
+import static org.apache.james.util.ReactorUtils.publishIfPresent;
+
+import java.util.Map;
+import java.util.Optional;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import javax.inject.Inject;
+
+import org.apache.commons.lang3.tuple.Pair;
+import org.apache.james.backends.cassandra.components.CassandraQuotaLimitDao;
+import org.apache.james.core.Domain;
+import org.apache.james.core.quota.QuotaComponent;
+import org.apache.james.core.quota.QuotaCountLimit;
+import org.apache.james.core.quota.QuotaLimit;
+import org.apache.james.core.quota.QuotaScope;
+import org.apache.james.core.quota.QuotaSizeLimit;
+import org.apache.james.core.quota.QuotaType;
+import org.apache.james.mailbox.model.Quota;
+import org.apache.james.mailbox.model.QuotaRoot;
+import org.apache.james.mailbox.quota.MaxQuotaManager;
+
+import com.google.common.collect.ImmutableMap;
+
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+public class CassandraPerUserMaxQuotaManagerV2 implements MaxQuotaManager {
+
+    private static final String GLOBAL_IDENTIFIER = "global";
+
+    private final CassandraQuotaLimitDao cassandraQuotaLimitDao;
+
+    @Inject
+    public CassandraPerUserMaxQuotaManagerV2(CassandraQuotaLimitDao 
cassandraQuotaLimitDao) {
+        this.cassandraQuotaLimitDao = cassandraQuotaLimitDao;
+    }
+
+    @Override
+    public void setMaxStorage(QuotaRoot quotaRoot, QuotaSizeLimit 
maxStorageQuota) {
+        setMaxStorageReactive(quotaRoot, maxStorageQuota).block();
+    }
+
+    @Override
+    public Mono<Void> setMaxStorageReactive(QuotaRoot quotaRoot, 
QuotaSizeLimit maxStorageQuota) {
+        return cassandraQuotaLimitDao.setQuotaLimit(QuotaLimit.builder()
+                .quotaScope(QuotaScope.USER)
+                .identifier(quotaRoot.getValue())
+                .quotaComponent(QuotaComponent.MAILBOX)
+                .quotaType(QuotaType.SIZE)
+                .quotaLimit(QuotaCodec.quotaValueToLong(maxStorageQuota))
+                .build());
+    }
+
+    @Override
+    public void setMaxMessage(QuotaRoot quotaRoot, QuotaCountLimit 
maxMessageCount) {
+        setMaxMessageReactive(quotaRoot, maxMessageCount).block();
+    }
+
+    @Override
+    public Mono<Void> setMaxMessageReactive(QuotaRoot quotaRoot, 
QuotaCountLimit maxMessageCount) {
+        return cassandraQuotaLimitDao.setQuotaLimit(QuotaLimit.builder()
+                .quotaScope(QuotaScope.USER)
+                .identifier(quotaRoot.getValue())
+                .quotaComponent(QuotaComponent.MAILBOX)
+                .quotaType(QuotaType.COUNT)
+                .quotaLimit(QuotaCodec.quotaValueToLong(maxMessageCount))
+                .build());
+    }
+
+    @Override
+    public void setDomainMaxMessage(Domain domain, QuotaCountLimit count) {
+        setDomainMaxMessageReactive(domain, count).block();
+    }
+
+    @Override
+    public Mono<Void> setDomainMaxMessageReactive(Domain domain, 
QuotaCountLimit count) {
+        return cassandraQuotaLimitDao.setQuotaLimit(QuotaLimit.builder()
+                .quotaScope(QuotaScope.DOMAIN)
+                .identifier(domain.asString())
+                .quotaComponent(QuotaComponent.MAILBOX)
+                .quotaType(QuotaType.COUNT)
+                .quotaLimit(QuotaCodec.quotaValueToLong(count))
+                .build());
+    }
+
+    @Override
+    public void setDomainMaxStorage(Domain domain, QuotaSizeLimit size) {
+        setDomainMaxStorageReactive(domain, size).block();
+    }
+
+    @Override
+    public Mono<Void> setDomainMaxStorageReactive(Domain domain, 
QuotaSizeLimit size) {
+        return cassandraQuotaLimitDao.setQuotaLimit(QuotaLimit.builder()
+                .quotaScope(QuotaScope.DOMAIN)
+                .identifier(domain.asString())
+                .quotaComponent(QuotaComponent.MAILBOX)
+                .quotaType(QuotaType.SIZE)
+                .quotaLimit(QuotaCodec.quotaValueToLong(size))
+                .build());
+    }
+
+    @Override
+    public void removeDomainMaxMessage(Domain domain) {
+        removeDomainMaxMessageReactive(domain).block();
+    }
+
+    @Override
+    public Mono<Void> removeDomainMaxMessageReactive(Domain domain) {
+        return 
cassandraQuotaLimitDao.deleteQuotaLimit(QuotaLimitKey.of(QuotaComponent.MAILBOX,
 QuotaScope.DOMAIN, domain.asString(), QuotaType.COUNT));
+    }
+
+    @Override
+    public void removeDomainMaxStorage(Domain domain) {
+        removeDomainMaxStorageReactive(domain).block();
+    }
+
+    @Override
+    public Mono<Void> removeDomainMaxStorageReactive(Domain domain) {
+        return 
cassandraQuotaLimitDao.deleteQuotaLimit(QuotaLimitKey.of(QuotaComponent.MAILBOX,
 QuotaScope.DOMAIN, domain.asString(), QuotaType.SIZE));
+    }
+
+    @Override
+    public Optional<QuotaCountLimit> getDomainMaxMessage(Domain domain) {
+        return getDomainMaxMessageReactive(domain).blockOptional();
+    }
+
+    @Override
+    public Mono<QuotaCountLimit> getDomainMaxMessageReactive(Domain domain) {
+        return getMaxMessageReactive(QuotaScope.DOMAIN, domain.asString());
+    }
+
+    @Override
+    public Optional<QuotaSizeLimit> getDomainMaxStorage(Domain domain) {
+        return getDomainMaxStorageReactive(domain).blockOptional();
+    }
+
+    @Override
+    public Mono<QuotaSizeLimit> getDomainMaxStorageReactive(Domain domain) {
+        return getMaxStorageReactive(QuotaScope.DOMAIN, domain.asString());
+    }
+
+    @Override
+    public void removeMaxMessage(QuotaRoot quotaRoot) {
+        removeMaxMessageReactive(quotaRoot).block();
+    }
+
+    @Override
+    public Mono<Void> removeMaxMessageReactive(QuotaRoot quotaRoot) {
+        return 
cassandraQuotaLimitDao.deleteQuotaLimit(QuotaLimitKey.of(QuotaComponent.MAILBOX,
 QuotaScope.USER, quotaRoot.getValue(), QuotaType.COUNT));
+    }
+
+    @Override
+    public void removeMaxStorage(QuotaRoot quotaRoot) {
+        removeMaxStorageReactive(quotaRoot).block();
+    }
+
+    @Override
+    public Mono<Void> removeMaxStorageReactive(QuotaRoot quotaRoot) {
+        return 
cassandraQuotaLimitDao.deleteQuotaLimit(QuotaLimitKey.of(QuotaComponent.MAILBOX,
 QuotaScope.USER, quotaRoot.getValue(), QuotaType.SIZE));
+    }
+
+    @Override
+    public void setGlobalMaxStorage(QuotaSizeLimit globalMaxStorage) {
+        setGlobalMaxStorageReactive(globalMaxStorage).block();
+    }
+
+    @Override
+    public Mono<Void> setGlobalMaxStorageReactive(QuotaSizeLimit 
globalMaxStorage) {
+        return cassandraQuotaLimitDao.setQuotaLimit(QuotaLimit.builder()
+                .quotaScope(QuotaScope.GLOBAL).identifier(GLOBAL_IDENTIFIER)
+                .quotaComponent(QuotaComponent.MAILBOX)
+                .quotaType(QuotaType.SIZE)
+                .quotaLimit(QuotaCodec.quotaValueToLong(globalMaxStorage))
+                .build());
+    }
+
+    @Override
+    public void removeGlobalMaxStorage() {
+        removeGlobalMaxStorageReactive().block();
+    }
+
+    @Override
+    public Mono<Void> removeGlobalMaxStorageReactive() {
+        return 
cassandraQuotaLimitDao.deleteQuotaLimit(QuotaLimitKey.of(QuotaComponent.MAILBOX,
 QuotaScope.GLOBAL, GLOBAL_IDENTIFIER, QuotaType.SIZE));
+    }
+
+    @Override
+    public void setGlobalMaxMessage(QuotaCountLimit globalMaxMessageCount) {
+        setGlobalMaxMessageReactive(globalMaxMessageCount).block();
+    }
+
+    @Override
+    public Mono<Void> setGlobalMaxMessageReactive(QuotaCountLimit 
globalMaxMessageCount) {
+        return cassandraQuotaLimitDao.setQuotaLimit(QuotaLimit.builder()
+                .quotaScope(QuotaScope.GLOBAL).identifier(GLOBAL_IDENTIFIER)
+                .quotaComponent(QuotaComponent.MAILBOX)
+                .quotaType(QuotaType.COUNT)
+                .quotaLimit(QuotaCodec.quotaValueToLong(globalMaxMessageCount))
+                .build());
+    }
+
+    @Override
+    public void removeGlobalMaxMessage() {
+        removeGlobalMaxMessageReactive().block();
+    }
+
+    @Override
+    public Mono<Void> removeGlobalMaxMessageReactive() {
+        return 
cassandraQuotaLimitDao.deleteQuotaLimit(QuotaLimitKey.of(QuotaComponent.MAILBOX,
 QuotaScope.GLOBAL, GLOBAL_IDENTIFIER, QuotaType.COUNT));
+    }
+
+    @Override
+    public Optional<QuotaSizeLimit> getGlobalMaxStorage() {
+        return getGlobalMaxStorageReactive().blockOptional();
+    }
+
+    @Override
+    public Mono<QuotaSizeLimit> getGlobalMaxStorageReactive() {
+        return getMaxStorageReactive(QuotaScope.GLOBAL, GLOBAL_IDENTIFIER);
+    }
+
+    @Override
+    public Optional<QuotaCountLimit> getGlobalMaxMessage() {
+        return getGlobalMaxMessageReactive().blockOptional();
+    }
+
+    @Override
+    public Mono<QuotaCountLimit> getGlobalMaxMessageReactive() {
+        return getMaxMessageReactive(QuotaScope.GLOBAL, GLOBAL_IDENTIFIER);
+    }
+
+    @Override
+    public Map<Quota.Scope, QuotaCountLimit> listMaxMessagesDetails(QuotaRoot 
quotaRoot) {
+        return listMaxMessagesDetailsReactive(quotaRoot).block();
+    }
+
+    @Override
+    public Mono<Map<Quota.Scope, QuotaCountLimit>> 
listMaxMessagesDetailsReactive(QuotaRoot quotaRoot) {
+        return Flux.merge(
+            getMaxMessageReactive(QuotaScope.USER, quotaRoot.getValue())
+                .map(limit -> Pair.of(Quota.Scope.User, limit)),
+            Mono.justOrEmpty(quotaRoot.getDomain())
+                .flatMap(domain -> getMaxMessageReactive(QuotaScope.DOMAIN, 
domain.asString()))
+                .map(limit -> Pair.of(Quota.Scope.Domain, limit)),
+            getGlobalMaxMessageReactive()
+                .map(limit -> Pair.of(Quota.Scope.Global, limit)))
+            .collect(ImmutableMap.toImmutableMap(
+                Pair::getKey,
+                Pair::getValue));
+    }
+
+    @Override
+    public Map<Quota.Scope, QuotaSizeLimit> listMaxStorageDetails(QuotaRoot 
quotaRoot) {
+        return listMaxStorageDetailsReactive(quotaRoot).block();
+    }
+
+    @Override
+    public Mono<Map<Quota.Scope, QuotaSizeLimit>> 
listMaxStorageDetailsReactive(QuotaRoot quotaRoot) {
+        return Flux.merge(
+            getMaxStorageReactive(QuotaScope.USER, quotaRoot.getValue())
+                .map(limit -> Pair.of(Quota.Scope.User, limit)),
+            Mono.justOrEmpty(quotaRoot.getDomain())
+                .flatMap(domain -> getMaxStorageReactive(QuotaScope.DOMAIN, 
domain.asString()))
+                .map(limit -> Pair.of(Quota.Scope.Domain, limit)),
+            getGlobalMaxStorageReactive()
+                .map(limit -> Pair.of(Quota.Scope.Global, limit)))
+            .collect(ImmutableMap.toImmutableMap(
+                Pair::getKey,
+                Pair::getValue));
+    }
+
+    @Override
+    public QuotaDetails quotaDetails(QuotaRoot quotaRoot) {
+        return quotaDetailsReactive(quotaRoot)
+            .block();
+    }
+
+    @Override
+    public Mono<QuotaDetails> quotaDetailsReactive(QuotaRoot quotaRoot) {

Review Comment:
   Ok apparently done, great!



-- 
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