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


##########
server/data/data-jmap-cassandra/src/main/java/org/apache/james/jmap/cassandra/upload/CassandraUploadUsageRepository.java:
##########
@@ -0,0 +1,67 @@
+/****************************************************************
+ * 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.jmap.cassandra.upload;
+
+import javax.inject.Inject;
+
+import org.apache.james.core.Username;
+import org.apache.james.core.quota.QuotaComponent;
+import org.apache.james.core.quota.QuotaSizeUsage;
+import org.apache.james.core.quota.QuotaType;
+import org.apache.james.jmap.api.upload.UploadUsageRepository;
+import org.apache.james.mailbox.cassandra.quota.CassandraQuotaCurrentValueDao;
+
+import reactor.core.publisher.Mono;
+
+public class CassandraUploadUsageRepository implements UploadUsageRepository {
+
+    private static final QuotaSizeUsage DEFAULT_QUOTA_SIZE_USAGE = 
QuotaSizeUsage.size(0);
+
+    private CassandraQuotaCurrentValueDao cassandraQuotaCurrentValueDao;
+
+    @Inject
+    public CassandraUploadUsageRepository(CassandraQuotaCurrentValueDao 
cassandraQuotaCurrentValueDao) {
+        this.cassandraQuotaCurrentValueDao = cassandraQuotaCurrentValueDao;
+    }
+
+    @Override
+    public Mono<Void> increaseSpace(Username username, QuotaSizeUsage usage) {
+        return 
cassandraQuotaCurrentValueDao.increase(CassandraQuotaCurrentValueDao.QuotaKey.of(QuotaComponent.JMAP_UPLOADS,
 username.asString(), QuotaType.SIZE),
+            usage.asLong());
+    }
+
+    @Override
+    public Mono<Void> decreaseSpace(Username username, QuotaSizeUsage usage) {
+        return 
cassandraQuotaCurrentValueDao.decrease(CassandraQuotaCurrentValueDao.QuotaKey.of(QuotaComponent.JMAP_UPLOADS,
 username.asString(), QuotaType.SIZE),
+            usage.asLong());
+    }
+
+    @Override
+    public Mono<QuotaSizeUsage> getSpaceUsage(Username username) {
+        return 
cassandraQuotaCurrentValueDao.getQuotaCurrentValue(CassandraQuotaCurrentValueDao.QuotaKey.of(QuotaComponent.JMAP_UPLOADS,
 username.asString(), QuotaType.SIZE))
+            .map(quotaCurrentValue -> 
QuotaSizeUsage.size(quotaCurrentValue.getCurrentValue())).defaultIfEmpty(DEFAULT_QUOTA_SIZE_USAGE);
+    }

Review Comment:
   From the ticket: 
   ```
       Publisher<QuotaSizeUsage> spaceUsag(User user); // Need to log WARNING 
upon negative values and sanitize them to ZERO
   ```
   
   Need to handle this?



##########
server/data/data-jmap/src/main/java/org/apache/james/jmap/memory/upload/InMemoryUploadUsageRepository.java:
##########
@@ -0,0 +1,74 @@
+/****************************************************************
+ * 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.jmap.memory.upload;
+
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicReference;
+
+import org.apache.james.core.Username;
+import org.apache.james.core.quota.QuotaSizeUsage;
+import org.apache.james.jmap.api.upload.UploadUsageRepository;
+
+import reactor.core.publisher.Mono;
+
+public class InMemoryUploadUsageRepository implements UploadUsageRepository {
+
+    private static final QuotaSizeUsage DEFAULT_QUOTA_SIZE_USAGE = 
QuotaSizeUsage.size(0);
+
+    private final Map<Username, AtomicReference<QuotaSizeUsage>> cache;
+
+    public InMemoryUploadUsageRepository() {
+        cache = new ConcurrentHashMap<>();
+    }
+
+    @Override
+    public Mono<Void> increaseSpace(Username username, QuotaSizeUsage usage) {
+        return updateSpace(username, usage.asLong());
+    }
+
+    @Override
+    public Mono<Void> decreaseSpace(Username username, QuotaSizeUsage usage) {
+        return updateSpace(username, Math.negateExact(usage.asLong()));
+    }
+
+    private Mono<Void> updateSpace(Username username, long amount) {
+        return Mono.fromRunnable(() -> {
+            AtomicReference<QuotaSizeUsage> quotaSizeUsageAtomicReference = 
cache.get(username);
+            if (Objects.isNull(quotaSizeUsageAtomicReference)) {
+                cache.put(username, new 
AtomicReference<>(QuotaSizeUsage.size(amount)));
+            } else {
+                quotaSizeUsageAtomicReference.updateAndGet(quotaSizeUsage -> 
quotaSizeUsage.add(amount));
+            }
+        });
+    }
+
+    @Override
+    public Mono<QuotaSizeUsage> getSpaceUsage(Username username) {
+        return Mono.just(cache.getOrDefault(username, new 
AtomicReference<>(DEFAULT_QUOTA_SIZE_USAGE))).map(quotaSizeUsageAtomicReference 
-> quotaSizeUsageAtomicReference.get());

Review Comment:
   ```suggestion
           return Mono.just(cache.getOrDefault(username, new 
AtomicReference<>(DEFAULT_QUOTA_SIZE_USAGE)))
                   .map(quotaSizeUsageAtomicReference -> 
quotaSizeUsageAtomicReference.get());
   ```
   
   "One dot per line" would be more readable IMO.



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