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:20:34 UTC

svn commit: r1704531 - in /james/mailbox/trunk/store/src: main/java/org/apache/james/mailbox/store/quota/ test/java/org/apache/james/mailbox/store/quota/

Author: btellier
Date: Tue Sep 22 10:20:31 2015
New Revision: 1704531

URL: http://svn.apache.org/viewvc?rev=1704531&view=rev
Log:
MAILBOX-64 Provide a Listening Quota updater and a fake implementation

Added:
    james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/quota/ListeningCurrentQuotaUpdater.java
    james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/quota/NoQuotaUpdater.java
    james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/quota/QuotaUpdater.java
    james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/quota/StoreCurrentQuotaManager.java
    james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/quota/ListeningCurrentQuotaUpdaterTest.java

Added: james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/quota/ListeningCurrentQuotaUpdater.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/quota/ListeningCurrentQuotaUpdater.java?rev=1704531&view=auto
==============================================================================
--- james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/quota/ListeningCurrentQuotaUpdater.java (added)
+++ james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/quota/ListeningCurrentQuotaUpdater.java Tue Sep 22 10:20:31 2015
@@ -0,0 +1,88 @@
+/****************************************************************
+ * 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.store.quota;
+
+import java.util.List;
+
+import org.apache.james.mailbox.MailboxListener;
+import org.apache.james.mailbox.exception.MailboxException;
+import org.apache.james.mailbox.model.QuotaRoot;
+import org.apache.james.mailbox.quota.QuotaRootResolver;
+
+import javax.inject.Inject;
+
+public class ListeningCurrentQuotaUpdater implements MailboxListener, QuotaUpdater {
+
+    private StoreCurrentQuotaManager currentQuotaManager;
+    private QuotaRootResolver quotaRootResolver;
+
+    @Inject
+    public void setQuotaRootResolver(QuotaRootResolver quotaRootResolver) {
+        this.quotaRootResolver = quotaRootResolver;
+    }
+
+    @Inject
+    public void setCurrentQuotaManager(StoreCurrentQuotaManager currentQuotaManager) {
+        this.currentQuotaManager = currentQuotaManager;
+    }
+
+    @Override
+    public void event(Event event) {
+        try {
+            QuotaRoot quotaRoot = quotaRootResolver.getQuotaRoot(event.getMailboxPath());
+            if (event instanceof Added) {
+                handleAddedEvent((Added) event, quotaRoot);
+            } else if (event instanceof Expunged) {
+                handleExpungedEvent((Expunged) event, quotaRoot);
+            }
+        } catch(MailboxException e) {
+            event.getSession().getLog().error("Error while updating quotas", e);
+        }
+    }
+
+    private void handleExpungedEvent(Expunged event, QuotaRoot quotaRoot) throws MailboxException {
+        Expunged expunged = event;
+        long addedSize = 0;
+        long addedCount = 0;
+        List<Long> uids = expunged.getUids();
+        for (Long uid : uids) {
+            addedSize += expunged.getMetaData(uid).getSize();
+            addedCount++;
+        }
+        // Expunge event can contain no data (expunge performed while no messages marked \Deleted)
+        if (addedCount != 0 && addedSize != 0) {
+            currentQuotaManager.decrease(quotaRoot, addedCount, addedSize);
+        }
+    }
+
+    private void handleAddedEvent(Added event, QuotaRoot quotaRoot) throws MailboxException {
+        Added added = event;
+        long addedSize = 0;
+        long addedCount = 0;
+        List<Long> uids = added.getUids();
+        for (Long uid : uids) {
+            addedSize += added.getMetaData(uid).getSize();
+            addedCount++;
+        }
+        if (addedCount != 0 && addedSize != 0) {
+            currentQuotaManager.increase(quotaRoot, addedCount, addedSize);
+        }
+    }
+
+}
\ No newline at end of file

Added: james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/quota/NoQuotaUpdater.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/quota/NoQuotaUpdater.java?rev=1704531&view=auto
==============================================================================
--- james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/quota/NoQuotaUpdater.java (added)
+++ james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/quota/NoQuotaUpdater.java Tue Sep 22 10:20:31 2015
@@ -0,0 +1,23 @@
+/****************************************************************
+ * 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.store.quota;
+
+public class NoQuotaUpdater implements QuotaUpdater {
+}

Added: james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/quota/QuotaUpdater.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/quota/QuotaUpdater.java?rev=1704531&view=auto
==============================================================================
--- james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/quota/QuotaUpdater.java (added)
+++ james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/quota/QuotaUpdater.java Tue Sep 22 10:20:31 2015
@@ -0,0 +1,23 @@
+/****************************************************************
+ * 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.store.quota;
+
+public interface QuotaUpdater {
+}

Added: james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/quota/StoreCurrentQuotaManager.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/quota/StoreCurrentQuotaManager.java?rev=1704531&view=auto
==============================================================================
--- james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/quota/StoreCurrentQuotaManager.java (added)
+++ james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/quota/StoreCurrentQuotaManager.java Tue Sep 22 10:20:31 2015
@@ -0,0 +1,32 @@
+/****************************************************************
+ * 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.store.quota;
+
+import org.apache.james.mailbox.exception.MailboxException;
+import org.apache.james.mailbox.model.QuotaRoot;
+import org.apache.james.mailbox.quota.CurrentQuotaManager;
+
+public interface StoreCurrentQuotaManager extends CurrentQuotaManager {
+
+    void increase(QuotaRoot quotaRoot, long count, long size) throws MailboxException;
+
+    void decrease(QuotaRoot quotaRoot, long count, long size) throws MailboxException;
+
+}

Added: james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/quota/ListeningCurrentQuotaUpdaterTest.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/quota/ListeningCurrentQuotaUpdaterTest.java?rev=1704531&view=auto
==============================================================================
--- james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/quota/ListeningCurrentQuotaUpdaterTest.java (added)
+++ james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/quota/ListeningCurrentQuotaUpdaterTest.java Tue Sep 22 10:20:31 2015
@@ -0,0 +1,185 @@
+/****************************************************************
+ * 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.store.quota;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import com.google.common.collect.Lists;
+import org.apache.james.mailbox.MailboxListener;
+import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.mailbox.model.MessageMetaData;
+import org.apache.james.mailbox.model.QuotaRoot;
+import org.apache.james.mailbox.quota.QuotaRootResolver;
+import org.apache.james.mailbox.store.SimpleMessageMetaData;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+
+import javax.mail.Flags;
+import java.util.Date;
+import java.util.List;
+
+public class ListeningCurrentQuotaUpdaterTest {
+
+    public static final int SIZE = 45;
+    public static final MailboxPath MAILBOX_PATH = new MailboxPath("#private", "benwa", "INBOX");
+    public static final QuotaRoot QUOTA_ROOT = QuotaRootImpl.quotaRoot("benwa");
+
+    private StoreCurrentQuotaManager mockedCurrentQuotaManager;
+    private QuotaRootResolver mockedQuotaRootResolver;
+    private ListeningCurrentQuotaUpdater testee;
+
+    @Before
+    public void setUp() throws Exception {
+        mockedQuotaRootResolver = mock(QuotaRootResolver.class);
+        mockedCurrentQuotaManager = mock(StoreCurrentQuotaManager.class);
+        testee = new ListeningCurrentQuotaUpdater();
+        testee.setCurrentQuotaManager(mockedCurrentQuotaManager);
+        testee.setQuotaRootResolver(mockedQuotaRootResolver);
+    }
+
+    @Test
+    public void addedEventShouldIncreaseCurrentQuotaValues() throws Exception {
+        MailboxListener.Added added = mock(MailboxListener.Added.class);
+        when(added.getMetaData(36)).thenAnswer(new Answer<MessageMetaData>() {
+            @Override
+            public MessageMetaData answer(InvocationOnMock invocationOnMock) throws Throwable {
+                return new SimpleMessageMetaData(36,0,new Flags(), SIZE, new Date());
+            }
+        });
+        when(added.getMetaData(38)).thenAnswer(new Answer<MessageMetaData>() {
+            @Override
+            public MessageMetaData answer(InvocationOnMock invocationOnMock) throws Throwable {
+                return new SimpleMessageMetaData(38,0,new Flags(), SIZE, new Date());
+            }
+        });
+        when(added.getUids()).thenAnswer(new Answer<List<Long>>() {
+            @Override
+            public List<Long> answer(InvocationOnMock invocationOnMock) throws Throwable {
+                return Lists.newArrayList(36L, 38L);
+            }
+        });
+        when(added.getMailboxPath()).thenAnswer(new Answer<MailboxPath>() {
+            @Override
+            public MailboxPath answer(InvocationOnMock invocationOnMock) throws Throwable {
+                return MAILBOX_PATH;
+            }
+        });
+        when(mockedQuotaRootResolver.getQuotaRoot(MAILBOX_PATH)).thenAnswer(new Answer<QuotaRoot>() {
+            @Override
+            public QuotaRoot answer(InvocationOnMock invocationOnMock) throws Throwable {
+                return QUOTA_ROOT;
+            }
+        });
+        testee.event(added);
+        verify(mockedCurrentQuotaManager).increase(QUOTA_ROOT, 2, 2 * SIZE);
+    }
+
+    @Test
+    public void expungedEventShouldDecreaseCurrentQuotaValues() throws Exception {
+        MailboxListener.Expunged expunged = mock(MailboxListener.Expunged.class);
+        when(expunged.getMetaData(36)).thenAnswer(new Answer<MessageMetaData>() {
+            @Override
+            public MessageMetaData answer(InvocationOnMock invocationOnMock) throws Throwable {
+                return new SimpleMessageMetaData(36,0,new Flags(), SIZE, new Date());
+            }
+        });
+        when(expunged.getMetaData(38)).thenAnswer(new Answer<MessageMetaData>() {
+            @Override
+            public MessageMetaData answer(InvocationOnMock invocationOnMock) throws Throwable {
+                return new SimpleMessageMetaData(38,0,new Flags(), SIZE, new Date());
+            }
+        });
+        when(expunged.getUids()).thenAnswer(new Answer<List<Long>>() {
+            @Override
+            public List<Long> answer(InvocationOnMock invocationOnMock) throws Throwable {
+                return Lists.newArrayList(36L, 38L);
+            }
+        });
+        when(expunged.getMailboxPath()).thenAnswer(new Answer<MailboxPath>() {
+            @Override
+            public MailboxPath answer(InvocationOnMock invocationOnMock) throws Throwable {
+                return MAILBOX_PATH;
+            }
+        });
+        when(mockedQuotaRootResolver.getQuotaRoot(MAILBOX_PATH)).thenAnswer(new Answer<QuotaRoot>() {
+            @Override
+            public QuotaRoot answer(InvocationOnMock invocationOnMock) throws Throwable {
+                return QUOTA_ROOT;
+            }
+        });
+        testee.event(expunged);
+        verify(mockedCurrentQuotaManager).decrease(QUOTA_ROOT, 2, 2 * SIZE);
+    }
+    @Test
+    public void emptyExpungedEventShouldNotTriggerDecrease() throws Exception {
+        MailboxListener.Expunged expunged = mock(MailboxListener.Expunged.class);
+        when(expunged.getUids()).thenAnswer(new Answer<List<Long>>() {
+            @Override
+            public List<Long> answer(InvocationOnMock invocationOnMock) throws Throwable {
+                return Lists.newArrayList();
+            }
+        });
+        when(expunged.getMailboxPath()).thenAnswer(new Answer<MailboxPath>() {
+            @Override
+            public MailboxPath answer(InvocationOnMock invocationOnMock) throws Throwable {
+                return MAILBOX_PATH;
+            }
+        });
+        when(mockedQuotaRootResolver.getQuotaRoot(MAILBOX_PATH)).thenAnswer(new Answer<QuotaRoot>() {
+            @Override
+            public QuotaRoot answer(InvocationOnMock invocationOnMock) throws Throwable {
+                return QUOTA_ROOT;
+            }
+        });
+        testee.event(expunged);
+        verify(mockedCurrentQuotaManager, never()).decrease(QUOTA_ROOT, 0, 0);
+    }
+
+    @Test
+    public void emptyAddedEventShouldNotTriggerDecrease() throws Exception {
+        MailboxListener.Added added = mock(MailboxListener.Added.class);
+        when(added.getUids()).thenAnswer(new Answer<List<Long>>() {
+            @Override
+            public List<Long> answer(InvocationOnMock invocationOnMock) throws Throwable {
+                return Lists.newArrayList();
+            }
+        });
+        when(added.getMailboxPath()).thenAnswer(new Answer<MailboxPath>() {
+            @Override
+            public MailboxPath answer(InvocationOnMock invocationOnMock) throws Throwable {
+                return MAILBOX_PATH;
+            }
+        });
+        when(mockedQuotaRootResolver.getQuotaRoot(MAILBOX_PATH)).thenAnswer(new Answer<QuotaRoot>() {
+            @Override
+            public QuotaRoot answer(InvocationOnMock invocationOnMock) throws Throwable {
+                return QUOTA_ROOT;
+            }
+        });
+        testee.event(added);
+        verify(mockedCurrentQuotaManager, never()).increase(QUOTA_ROOT, 0, 0);
+    }
+
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org