You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2015/09/22 12:21:17 UTC
svn commit: r1704532 - in /james/mailbox/trunk/memory/src:
main/java/org/apache/james/mailbox/inmemory/quota/
test/java/org/apache/james/mailbox/inmemory/quota/
Author: btellier
Date: Tue Sep 22 10:21:15 2015
New Revision: 1704532
URL: http://svn.apache.org/viewvc?rev=1704532&view=rev
Log:
MAILBOX-64 Memory implementation for CurrentQuotaManager
Added:
james/mailbox/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/quota/InMemoryCurrentQuotaManager.java
james/mailbox/trunk/memory/src/test/java/org/apache/james/mailbox/inmemory/quota/InMemoryCurrentQuotaManagerTest.java
Modified:
james/mailbox/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/quota/InMemoryPerUserMaxQuotaManager.java
Added: james/mailbox/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/quota/InMemoryCurrentQuotaManager.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/quota/InMemoryCurrentQuotaManager.java?rev=1704532&view=auto
==============================================================================
--- james/mailbox/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/quota/InMemoryCurrentQuotaManager.java (added)
+++ james/mailbox/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/quota/InMemoryCurrentQuotaManager.java Tue Sep 22 10:21:15 2015
@@ -0,0 +1,118 @@
+/****************************************************************
+ * 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.inmemory.quota;
+
+import com.google.common.base.Preconditions;
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
+import org.apache.james.mailbox.MailboxManager;
+import org.apache.james.mailbox.exception.MailboxException;
+import org.apache.james.mailbox.model.QuotaRoot;
+import org.apache.james.mailbox.store.quota.CurrentQuotaCalculator;
+import org.apache.james.mailbox.store.quota.StoreCurrentQuotaManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.inject.Inject;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.atomic.AtomicLong;
+
+public class InMemoryCurrentQuotaManager implements StoreCurrentQuotaManager {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(InMemoryCurrentQuotaManager.class);
+
+ private final LoadingCache<QuotaRoot, Entry> quotaCache;
+
+ @Inject
+ public InMemoryCurrentQuotaManager(final CurrentQuotaCalculator quotaCalculator, final MailboxManager mailboxManager) {
+ this.quotaCache = CacheBuilder.<QuotaRoot, Entry>newBuilder().build(new CacheLoader<QuotaRoot, Entry>() {
+ @Override
+ public Entry load(QuotaRoot quotaRoot) throws Exception {
+ return new Entry(quotaCalculator.recalculateCurrentQuotas(quotaRoot, mailboxManager.createSystemSession(quotaRoot.getValue(), LOGGER)));
+ }
+ });
+ }
+
+
+ @Override
+ public void increase(QuotaRoot quotaRoot, long count, long size) throws MailboxException {
+ checkArguments(count, size);
+ doIncrease(quotaRoot, count, size);
+ }
+
+ @Override
+ public void decrease(QuotaRoot quotaRoot, long count, long size) throws MailboxException {
+ checkArguments(count, size);
+ doIncrease(quotaRoot, -count, -size);
+ }
+
+ @Override
+ public long getCurrentMessageCount(QuotaRoot quotaRoot) throws MailboxException {
+ try {
+ return quotaCache.get(quotaRoot).getCount().get();
+ } catch (ExecutionException e) {
+ throw new MailboxException("Exception caught", e);
+ }
+ }
+
+ @Override
+ public long getCurrentStorage(QuotaRoot quotaRoot) throws MailboxException {
+ try {
+ return quotaCache.get(quotaRoot).getSize().get();
+ } catch (ExecutionException e) {
+ throw new MailboxException("Exception caught", e);
+ }
+ }
+
+ private void doIncrease(QuotaRoot quotaRoot, long count, long size) throws MailboxException {
+ try {
+ Entry entry = quotaCache.get(quotaRoot);
+ entry.getCount().addAndGet(count);
+ entry.getSize().addAndGet(size);
+ } catch (ExecutionException e) {
+ throw new MailboxException("Exception caught", e);
+ }
+
+ }
+
+ private void checkArguments(long count, long size) {
+ Preconditions.checkArgument(count > 0, "Count should be positive");
+ Preconditions.checkArgument(size > 0, "Size should be positive");
+ }
+
+ class Entry {
+ private final AtomicLong count;
+ private final AtomicLong size;
+
+ public Entry(CurrentQuotaCalculator.CurrentQuotas currentQuotas) {
+ this.count = new AtomicLong(currentQuotas.getCount());
+ this.size = new AtomicLong(currentQuotas.getSize());
+ }
+
+ public AtomicLong getCount() {
+ return count;
+ }
+
+ public AtomicLong getSize() {
+ return size;
+ }
+ }
+}
Modified: james/mailbox/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/quota/InMemoryPerUserMaxQuotaManager.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/quota/InMemoryPerUserMaxQuotaManager.java?rev=1704532&r1=1704531&r2=1704532&view=diff
==============================================================================
--- james/mailbox/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/quota/InMemoryPerUserMaxQuotaManager.java (original)
+++ james/mailbox/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/quota/InMemoryPerUserMaxQuotaManager.java Tue Sep 22 10:21:15 2015
@@ -72,4 +72,13 @@ public class InMemoryPerUserMaxQuotaMana
userMaxMessage.put(quotaRoot.getValue(), maxMessageCount);
}
+ @Override
+ public long getDefaultMaxStorage() throws MailboxException {
+ return maxStorage;
+ }
+
+ @Override
+ public long getDefaultMaxMessage() throws MailboxException {
+ return maxMessage;
+ }
}
Added: james/mailbox/trunk/memory/src/test/java/org/apache/james/mailbox/inmemory/quota/InMemoryCurrentQuotaManagerTest.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/memory/src/test/java/org/apache/james/mailbox/inmemory/quota/InMemoryCurrentQuotaManagerTest.java?rev=1704532&view=auto
==============================================================================
--- james/mailbox/trunk/memory/src/test/java/org/apache/james/mailbox/inmemory/quota/InMemoryCurrentQuotaManagerTest.java (added)
+++ james/mailbox/trunk/memory/src/test/java/org/apache/james/mailbox/inmemory/quota/InMemoryCurrentQuotaManagerTest.java Tue Sep 22 10:21:15 2015
@@ -0,0 +1,126 @@
+/****************************************************************
+ * 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.inmemory.quota;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import org.apache.james.mailbox.MailboxManager;
+import org.apache.james.mailbox.model.QuotaRoot;
+import org.apache.james.mailbox.store.quota.CurrentQuotaCalculator;
+import org.apache.james.mailbox.store.quota.QuotaRootImpl;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+
+public class InMemoryCurrentQuotaManagerTest {
+
+ public static final QuotaRoot QUOTA_ROOT = QuotaRootImpl.quotaRoot("benwa");
+
+ private InMemoryCurrentQuotaManager testee;
+ private CurrentQuotaCalculator mockedCurrentQuotaCalculator;
+
+ @SuppressWarnings("unchecked")
+ @Before
+ public void setUp() throws Exception {
+ mockedCurrentQuotaCalculator = mock(CurrentQuotaCalculator.class);
+ MailboxManager mockedMailboxManager = mock(MailboxManager.class);
+ testee = new InMemoryCurrentQuotaManager(mockedCurrentQuotaCalculator, mockedMailboxManager);
+ }
+
+ @Test
+ public void getCurrentMessageCountShouldReturnRecalculateMessageCountWhenEntryIsNotInitialized() throws Exception {
+ when(mockedCurrentQuotaCalculator.recalculateCurrentQuotas(QUOTA_ROOT, null)).thenAnswer(new Answer<CurrentQuotaCalculator.CurrentQuotas>() {
+ @Override
+ public CurrentQuotaCalculator.CurrentQuotas answer(InvocationOnMock invocationOnMock) throws Throwable {
+ return new CurrentQuotaCalculator.CurrentQuotas(18, 512);
+ }
+ });
+ assertThat(testee.getCurrentMessageCount(QUOTA_ROOT)).isEqualTo(18);
+ }
+
+ @Test
+ public void getCurrentStorageShouldReturnRecalculateSizeWhenEntryIsNotInitialized() throws Exception {
+ when(mockedCurrentQuotaCalculator.recalculateCurrentQuotas(QUOTA_ROOT, null)).thenAnswer(new Answer<CurrentQuotaCalculator.CurrentQuotas>() {
+ @Override
+ public CurrentQuotaCalculator.CurrentQuotas answer(InvocationOnMock invocationOnMock) throws Throwable {
+ return new CurrentQuotaCalculator.CurrentQuotas(18, 512);
+ }
+ });
+ assertThat(testee.getCurrentStorage(QUOTA_ROOT)).isEqualTo(512);
+ }
+
+ @Test
+ public void getCurrentStorageShouldReRetrieveStoredQuotasWhenCalculateOnUnknownQuotaIsTrue() throws Exception {
+ when(mockedCurrentQuotaCalculator.recalculateCurrentQuotas(QUOTA_ROOT, null)).thenAnswer(new Answer<CurrentQuotaCalculator.CurrentQuotas>() {
+ @Override
+ public CurrentQuotaCalculator.CurrentQuotas answer(InvocationOnMock invocationOnMock) throws Throwable {
+ return new CurrentQuotaCalculator.CurrentQuotas(18, 512);
+ }
+ });
+ testee.increase(QUOTA_ROOT, 10, 100);
+ assertThat(testee.getCurrentMessageCount(QUOTA_ROOT)).isEqualTo(28);
+ assertThat(testee.getCurrentStorage(QUOTA_ROOT)).isEqualTo(612);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void increaseShouldThrowOnZeroCount() throws Exception {
+ testee.increase(QUOTA_ROOT, 0, 5);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void increaseShouldThrowOnNegativeCount() throws Exception {
+ testee.increase(QUOTA_ROOT, -1, 5);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void increaseShouldThrowOnZeroSize() throws Exception {
+ testee.increase(QUOTA_ROOT, 5, 0);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void increaseShouldThrowOnNegativeSize() throws Exception {
+ testee.increase(QUOTA_ROOT, 5, -1);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void decreaseShouldThrowOnZeroCount() throws Exception {
+ testee.decrease(QUOTA_ROOT, 0, 5);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void decreaseShouldThrowOnNegativeCount() throws Exception {
+ testee.decrease(QUOTA_ROOT, -1, 5);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void decreaseShouldThrowOnZeroSize() throws Exception {
+ testee.decrease(QUOTA_ROOT, 5, 0);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void decreaseShouldThrowOnNegativeSize() throws Exception {
+ testee.decrease(QUOTA_ROOT, 5, -1);
+ }
+
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org