james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nor...@apache.org
Subject svn commit: r1041402 [1/2] - in /james/imap/trunk: jcr/src/main/java/org/apache/james/mailbox/jcr/ jcr/src/main/java/org/apache/james/mailbox/jcr/mail/ jcr/src/main/java/org/apache/james/mailbox/jcr/mail/model/ jcr/src/test/java/org/apache/james/mailbo...
Date Thu, 02 Dec 2010 15:12:54 GMT
Author: norman
Date: Thu Dec  2 15:12:51 2010
New Revision: 1041402

URL: http://svn.apache.org/viewvc?rev=1041402&view=rev
Log:
Decouple next uid generation and use some kind of caching to make it more efficient. See IMAP-238

Added:
    james/imap/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/JCRCachingUidProvider.java
    james/imap/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/JPACachingUidProvider.java
    james/imap/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/InMemoryCachingUidProvider.java
    james/imap/trunk/store/src/main/java/org/apache/james/mailbox/store/CachingUidProvider.java
    james/imap/trunk/store/src/main/java/org/apache/james/mailbox/store/UidProvider.java
Removed:
    james/imap/trunk/store/src/main/java/org/apache/james/mailbox/store/LastUidTracker.java
Modified:
    james/imap/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/AbstractJCRMapper.java
    james/imap/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/JCRMailboxManager.java
    james/imap/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/JCRMailboxSessionMapperFactory.java
    james/imap/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/JCRMessageManager.java
    james/imap/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/JCRMessageMapper.java
    james/imap/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/model/JCRMailbox.java
    james/imap/trunk/jcr/src/test/java/org/apache/james/mailbox/jcr/JCRHostSystem.java
    james/imap/trunk/jcr/src/test/java/org/apache/james/mailbox/jcr/JCRStressTest.java
    james/imap/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/JPAMailboxManager.java
    james/imap/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/JPAMailboxSessionMapperFactory.java
    james/imap/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/JPAMessageManager.java
    james/imap/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/JPAMessageMapper.java
    james/imap/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/model/JPAMailbox.java
    james/imap/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/model/openjpa/AbstractJPAMailboxMembership.java
    james/imap/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/openjpa/OpenJPAMailboxManager.java
    james/imap/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/openjpa/OpenJPAMessageManager.java
    james/imap/trunk/jpa/src/test/java/org/apache/james/mailbox/jpa/JPAHostSystem.java
    james/imap/trunk/jpa/src/test/java/org/apache/james/mailbox/jpa/JPAStressTest.java
    james/imap/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/MaildirMailboxManager.java
    james/imap/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/MaildirMailboxSessionMapperFactory.java
    james/imap/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/MaildirMessageManager.java
    james/imap/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/MaildirStore.java
    james/imap/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/MaildirMailboxMapper.java
    james/imap/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/MaildirMessageMapper.java
    james/imap/trunk/maildir/src/test/java/org/apache/james/mailbox/functional/maildir/MaildirHostSystem.java
    james/imap/trunk/maildir/src/test/java/org/apache/james/mailbox/functional/maildir/MaildirStressTest.java
    james/imap/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/InMemoryMailboxManager.java
    james/imap/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/InMemoryStoreMessageManager.java
    james/imap/trunk/memory/src/test/java/org/apache/james/mailbox/inmemory/InMemoryHostSystem.java
    james/imap/trunk/protocol-tester/src/main/java/org/apache/james/imap/functional/AbstractStressTest.java
    james/imap/trunk/store/src/main/java/org/apache/james/mailbox/store/MapperStoreMessageManager.java
    james/imap/trunk/store/src/main/java/org/apache/james/mailbox/store/StoreMailboxManager.java
    james/imap/trunk/store/src/main/java/org/apache/james/mailbox/store/StoreMessageManager.java

Modified: james/imap/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/AbstractJCRMapper.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/AbstractJCRMapper.java?rev=1041402&r1=1041401&r2=1041402&view=diff
==============================================================================
--- james/imap/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/AbstractJCRMapper.java (original)
+++ james/imap/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/AbstractJCRMapper.java Thu Dec  2 15:12:51 2010
@@ -37,7 +37,7 @@ public abstract class AbstractJCRMapper 
 
     private final Log logger;
     private final MailboxSessionJCRRepository repository;
-    private final MailboxSession mSession;
+    protected final MailboxSession mSession;
     private final NodeLocker locker;
 
     

Added: james/imap/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/JCRCachingUidProvider.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/JCRCachingUidProvider.java?rev=1041402&view=auto
==============================================================================
--- james/imap/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/JCRCachingUidProvider.java (added)
+++ james/imap/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/JCRCachingUidProvider.java Thu Dec  2 15:12:51 2010
@@ -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.mailbox.jcr;
+
+import javax.jcr.NodeIterator;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+import javax.jcr.query.Query;
+import javax.jcr.query.QueryManager;
+import javax.jcr.query.QueryResult;
+
+import org.apache.jackrabbit.util.ISO9075;
+import org.apache.james.mailbox.MailboxException;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.jcr.mail.model.JCRMessage;
+import org.apache.james.mailbox.store.CachingUidProvider;
+import org.apache.james.mailbox.store.mail.model.Mailbox;
+
+/**
+ * Lazy-fetch the uid of the last insert Message if needed
+ * 
+ *
+ */
+public class JCRCachingUidProvider extends CachingUidProvider<String>{
+
+    private MailboxSessionJCRRepository repos;
+
+    public JCRCachingUidProvider(final MailboxSessionJCRRepository repos) {
+        this.repos = repos;
+    }
+    
+
+    
+    /*
+     * (non-Javadoc)
+     * @see org.apache.james.mailbox.store.CachingUidProvider#getLastUid(org.apache.james.mailbox.MailboxSession, org.apache.james.mailbox.store.mail.model.Mailbox)
+     */
+    protected long getLastUid(MailboxSession session, Mailbox<String> mailbox) throws MailboxException {
+        try {
+            Session s = repos.login(session);
+            // we use order by because without it count will always be 0 in jackrabbit
+            String queryString = "/jcr:root" + ISO9075.encodePath(s.getNodeByIdentifier(mailbox.getMailboxId()).getPath()) + "//element(*,jamesMailbox:message) order by @" + JCRMessage.UID_PROPERTY + " asc";
+            QueryManager manager = s.getWorkspace().getQueryManager();
+            Query q = manager.createQuery(queryString, Query.XPATH);
+            q.setLimit(1);
+            QueryResult result = q.execute();
+            NodeIterator nodes = result.getNodes();
+            if (nodes.hasNext()) {
+                return nodes.nextNode().getProperty(JCRMessage.UID_PROPERTY).getLong();
+            } else {
+                return 0;
+            }
+        } catch (RepositoryException e) {
+            throw new MailboxException("Unable to count unseen messages in mailbox " + mailbox, e);
+        }
+    }
+
+}

Modified: james/imap/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/JCRMailboxManager.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/JCRMailboxManager.java?rev=1041402&r1=1041401&r2=1041402&view=diff
==============================================================================
--- james/imap/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/JCRMailboxManager.java (original)
+++ james/imap/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/JCRMailboxManager.java Thu Dec  2 15:12:51 2010
@@ -18,8 +18,6 @@
  ****************************************************************/
 package org.apache.james.mailbox.jcr;
 
-import java.util.concurrent.atomic.AtomicLong;
-
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.james.mailbox.MailboxException;
@@ -31,6 +29,7 @@ import org.apache.james.mailbox.store.JV
 import org.apache.james.mailbox.store.MailboxPathLocker;
 import org.apache.james.mailbox.store.MapperStoreMessageManager;
 import org.apache.james.mailbox.store.StoreMailboxManager;
+import org.apache.james.mailbox.store.UidProvider;
 import org.apache.james.mailbox.store.mail.model.Mailbox;
 import org.apache.james.mailbox.util.MailboxEventDispatcher;
 
@@ -43,19 +42,19 @@ public class JCRMailboxManager extends S
     private final JCRMailboxSessionMapperFactory mapperFactory;
     private final Log logger = LogFactory.getLog(JCRMailboxManager.class);
     
-    public JCRMailboxManager(JCRMailboxSessionMapperFactory mapperFactory, final Authenticator authenticator) {
-	    this(mapperFactory, authenticator, new JVMMailboxPathLocker());
+    public JCRMailboxManager(JCRMailboxSessionMapperFactory mapperFactory, final Authenticator authenticator, final UidProvider<String> uidProvider) {
+	    this(mapperFactory, authenticator, uidProvider, new JVMMailboxPathLocker());
     }
 
-    public JCRMailboxManager(JCRMailboxSessionMapperFactory mapperFactory, final Authenticator authenticator, final MailboxPathLocker locker) {
-        super(mapperFactory, authenticator, locker);
+    public JCRMailboxManager(JCRMailboxSessionMapperFactory mapperFactory, final Authenticator authenticator, final UidProvider<String> uidProvider, final MailboxPathLocker locker) {
+        super(mapperFactory, authenticator, uidProvider, locker);
         this.mapperFactory = mapperFactory;
     }
 
     
     @Override
-    protected MapperStoreMessageManager<String> createMessageManager(AtomicLong lastUid, MailboxEventDispatcher dispatcher, Mailbox<String> mailboxEntity, MailboxSession session) throws MailboxException{
-        return new JCRMessageManager(mapperFactory, lastUid, dispatcher, (JCRMailbox) mailboxEntity, logger, getDelimiter());
+    protected MapperStoreMessageManager<String> createMessageManager(UidProvider<String> uidProvider, MailboxEventDispatcher dispatcher, Mailbox<String> mailboxEntity, MailboxSession session) throws MailboxException{
+        return new JCRMessageManager(mapperFactory, uidProvider, dispatcher, (JCRMailbox) mailboxEntity, logger, getDelimiter());
     }
 
     @Override

Modified: james/imap/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/JCRMailboxSessionMapperFactory.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/JCRMailboxSessionMapperFactory.java?rev=1041402&r1=1041401&r2=1041402&view=diff
==============================================================================
--- james/imap/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/JCRMailboxSessionMapperFactory.java (original)
+++ james/imap/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/JCRMailboxSessionMapperFactory.java Thu Dec  2 15:12:51 2010
@@ -28,6 +28,7 @@ import org.apache.james.mailbox.jcr.mail
 import org.apache.james.mailbox.jcr.mail.JCRMessageMapper;
 import org.apache.james.mailbox.jcr.user.JCRSubscriptionMapper;
 import org.apache.james.mailbox.store.MailboxSessionMapperFactory;
+import org.apache.james.mailbox.store.UidProvider;
 import org.apache.james.mailbox.store.mail.MailboxMapper;
 import org.apache.james.mailbox.store.mail.MessageMapper;
 import org.apache.james.mailbox.store.user.SubscriptionMapper;
@@ -45,17 +46,19 @@ public class JCRMailboxSessionMapperFact
     private final static int DEFAULT_SCALING = 2;
     private int scaling;
     private int messageScaling;
+    private UidProvider<String> provider;
 
-    public JCRMailboxSessionMapperFactory(final MailboxSessionJCRRepository repository, final NodeLocker locker) {
-        this(repository, locker, DEFAULT_SCALING, JCRMessageMapper.MESSAGE_SCALE_DAY);
+    public JCRMailboxSessionMapperFactory(final MailboxSessionJCRRepository repository, final NodeLocker locker, final UidProvider<String> uidProvider) {
+        this(repository, locker, uidProvider, DEFAULT_SCALING, JCRMessageMapper.MESSAGE_SCALE_DAY);
     }
 
-    public JCRMailboxSessionMapperFactory(final MailboxSessionJCRRepository repository, final NodeLocker locker, final int scaling, final int messageScaling) {
+    public JCRMailboxSessionMapperFactory(final MailboxSessionJCRRepository repository, final NodeLocker locker, final UidProvider<String> provider, final int scaling, final int messageScaling) {
         this.repository = repository;
         this.logger = LogFactory.getLog(JCRMailboxSessionMapperFactory.class);
         this.locker = locker;
         this.scaling = scaling;
         this.messageScaling = messageScaling;
+        this.provider = provider;
     }
     
     @Override
@@ -66,7 +69,7 @@ public class JCRMailboxSessionMapperFact
 
     @Override
     public MessageMapper<String> createMessageMapper(MailboxSession session) throws MailboxException {
-        JCRMessageMapper messageMapper = new JCRMessageMapper(repository, session, locker, logger, messageScaling);
+        JCRMessageMapper messageMapper = new JCRMessageMapper(repository, session, locker, provider, logger, messageScaling);
         return messageMapper;
     }
 

Modified: james/imap/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/JCRMessageManager.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/JCRMessageManager.java?rev=1041402&r1=1041401&r2=1041402&view=diff
==============================================================================
--- james/imap/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/JCRMessageManager.java (original)
+++ james/imap/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/JCRMessageManager.java Thu Dec  2 15:12:51 2010
@@ -22,7 +22,6 @@ import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
-import java.util.concurrent.atomic.AtomicLong;
 
 import javax.mail.Flags;
 
@@ -32,6 +31,7 @@ import org.apache.james.mailbox.jcr.mail
 import org.apache.james.mailbox.jcr.mail.model.JCRMailbox;
 import org.apache.james.mailbox.jcr.mail.model.JCRMessage;
 import org.apache.james.mailbox.store.MapperStoreMessageManager;
+import org.apache.james.mailbox.store.UidProvider;
 import org.apache.james.mailbox.store.mail.model.Header;
 import org.apache.james.mailbox.store.mail.model.MailboxMembership;
 import org.apache.james.mailbox.store.mail.model.PropertyBuilder;
@@ -45,9 +45,9 @@ public class JCRMessageManager extends M
 
     private final Log log;
 
-    public JCRMessageManager(JCRMailboxSessionMapperFactory mapperFactory, final AtomicLong lastUid,
+    public JCRMessageManager(JCRMailboxSessionMapperFactory mapperFactory, final UidProvider<String> uidProvider,
             final MailboxEventDispatcher dispatcher, final JCRMailbox mailbox, final Log log, final char delimiter) throws MailboxException {
-        super(mapperFactory, lastUid, dispatcher, mailbox);
+        super(mapperFactory, uidProvider, dispatcher, mailbox);
         this.log = log;
     }
 

Modified: james/imap/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/JCRMessageMapper.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/JCRMessageMapper.java?rev=1041402&r1=1041401&r2=1041402&view=diff
==============================================================================
--- james/imap/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/JCRMessageMapper.java (original)
+++ james/imap/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/JCRMessageMapper.java Thu Dec  2 15:12:51 2010
@@ -28,7 +28,6 @@ import java.util.List;
 import javax.jcr.ItemNotFoundException;
 import javax.jcr.Node;
 import javax.jcr.NodeIterator;
-import javax.jcr.Property;
 import javax.jcr.RepositoryException;
 import javax.jcr.query.Query;
 import javax.jcr.query.QueryManager;
@@ -49,9 +48,9 @@ import org.apache.james.mailbox.jcr.Abst
 import org.apache.james.mailbox.jcr.MailboxSessionJCRRepository;
 import org.apache.james.mailbox.jcr.NodeLocker;
 import org.apache.james.mailbox.jcr.NodeLocker.NodeLockedExecution;
-import org.apache.james.mailbox.jcr.mail.model.JCRMailbox;
 import org.apache.james.mailbox.jcr.mail.model.JCRMessage;
 import org.apache.james.mailbox.store.SearchQueryIterator;
+import org.apache.james.mailbox.store.UidProvider;
 import org.apache.james.mailbox.store.mail.MessageMapper;
 import org.apache.james.mailbox.store.mail.model.Mailbox;
 import org.apache.james.mailbox.store.mail.model.MailboxMembership;
@@ -100,6 +99,8 @@ public class JCRMessageMapper extends Ab
     public final static int MESSAGE_SCALE_MINUTE = 5;
 
     private final int scaleType;
+
+    private UidProvider<String> uidGenerator;
     
 
     /**
@@ -108,8 +109,8 @@ public class JCRMessageMapper extends Ab
      * 
      * In this case {@link #MESSAGE_SCALE_DAY} is used
      */
-    public JCRMessageMapper(final MailboxSessionJCRRepository repos, MailboxSession session, NodeLocker locker, final Log logger) {
-        this(repos, session, locker, logger, MESSAGE_SCALE_DAY);
+    public JCRMessageMapper(final MailboxSessionJCRRepository repos, MailboxSession session, NodeLocker locker, final UidProvider<String> uidGenerator, final Log logger) {
+        this(repos, session, locker, uidGenerator, logger, MESSAGE_SCALE_DAY);
     }
 
     /**
@@ -122,9 +123,10 @@ public class JCRMessageMapper extends Ab
      * @param scaleType the scale type to use when storing messages. See {@link #MESSAGE_SCALE_NONE}, {@link #MESSAGE_SCALE_YEAR}, {@link #MESSAGE_SCALE_MONTH}, {@link #MESSAGE_SCALE_DAY},
      *                  {@link #MESSAGE_SCALE_HOUR}, {@link #MESSAGE_SCALE_MINUTE}  
      */
-    public JCRMessageMapper(final MailboxSessionJCRRepository repos, MailboxSession session, NodeLocker locker, final Log logger, int scaleType) {
+    public JCRMessageMapper(final MailboxSessionJCRRepository repos, MailboxSession session, NodeLocker locker, final UidProvider<String> uidGenerator, final Log logger, int scaleType) {
         super(repos, session, locker, logger);
         this.scaleType = scaleType;
+        this.uidGenerator = uidGenerator;
     }
     
     /**
@@ -496,7 +498,8 @@ public class JCRMessageMapper extends Ab
                 Node node = null;
                 Node mailboxNode = getSession().getNodeByIdentifier(mailbox.getMailboxId());
 
-                final long nextUid = reserveNextUid((JCRMailbox) mailbox);
+                final long nextUid = uidGenerator.nextUid(mSession, mailbox);
+
                 
                 NodeLocker locker = getNodeLocker();
 
@@ -700,7 +703,7 @@ public class JCRMessageMapper extends Ab
      */
     public long copy(Mailbox<String> mailbox, MailboxMembership<String> oldmessage) throws MailboxException{
         try {
-            long uid = reserveNextUid((JCRMailbox) mailbox);
+            long uid = uidGenerator.nextUid(mSession, mailbox);
             String newMessagePath = getSession().getNodeByIdentifier(mailbox.getMailboxId()).getPath() + NODE_DELIMITER + String.valueOf(uid);
             getSession().getWorkspace().copy(((JCRMessage)oldmessage).getNode().getPath(), getSession().getNodeByIdentifier(mailbox.getMailboxId()).getPath() + NODE_DELIMITER + String.valueOf(uid));
             Node node = getSession().getNode(newMessagePath);
@@ -709,30 +712,9 @@ public class JCRMessageMapper extends Ab
             return uid;
         } catch (RepositoryException e) {
             throw new MailboxException("Unable to copy message " +oldmessage + " in mailbox " + mailbox, e);
-        } catch (InterruptedException e) {
-            throw new MailboxException("Unable to copy message " +oldmessage + " in mailbox " + mailbox, e);
         }
     }
     
-    protected long reserveNextUid(Mailbox<String> mailbox) throws RepositoryException, InterruptedException {
-        long result = getNodeLocker().execute(new NodeLocker.NodeLockedExecution<Long>() {
-
-            public Long execute(Node node) throws RepositoryException {
-                Property uidProp = node.getProperty(JCRMailbox.LASTUID_PROPERTY);
-                long uid = uidProp.getLong();
-                uid++;
-                uidProp.setValue(uid);
-                uidProp.getSession().save();
-                return uid;
-            }
-
-            public boolean isDeepLocked() {
-                return true;
-            }
-            
-        }, getSession().getNodeByIdentifier(mailbox.getMailboxId()), Long.class);
-        return result;
-        
-    }
+    
 
 }

Modified: james/imap/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/model/JCRMailbox.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/model/JCRMailbox.java?rev=1041402&r1=1041401&r2=1041402&view=diff
==============================================================================
--- james/imap/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/model/JCRMailbox.java (original)
+++ james/imap/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/model/JCRMailbox.java Thu Dec  2 15:12:51 2010
@@ -42,11 +42,9 @@ public class JCRMailbox implements Mailb
     public final static String NAMESPACE_PROPERTY = "jamesMailbox:mailboxNamespace";
     public final static String NAME_PROPERTY = "jamesMailbox:mailboxName";
     public final static String UIDVALIDITY_PROPERTY = "jamesMailbox:mailboxUidValidity";
-    public final static String LASTUID_PROPERTY = "jamesMailbox:mailboxLastUid";
 
     private String name;
     private long uidValidity;
-    private long lastUid = 0;
     private final Log logger;
     private Node node;
 
@@ -54,10 +52,6 @@ public class JCRMailbox implements Mailb
     private String namespace;
     private String user;
     
-    public JCRMailbox(final MailboxPath path, final long uidValidity, final long lastUid, Log logger) {
-        this(path, uidValidity, logger);
-        this.lastUid = lastUid;
-    }
     
     public JCRMailbox( final MailboxPath path, final long uidValidity, Log logger) {
         this.name = path.getName();
@@ -76,21 +70,6 @@ public class JCRMailbox implements Mailb
         return logger;
     }
 
-    /*
-     * (non-Javadoc)
-     * @see org.apache.james.mailbox.store.mail.model.Mailbox#getLastUid()
-     */
-    public long getLastUid() {
-        if (isPersistent()) {
-            try {
-                return node.getProperty(LASTUID_PROPERTY).getLong();
-            } catch (RepositoryException e) {
-                logger.error("Unable to access property " + LASTUID_PROPERTY, e);
-            }
-        }
-        return lastUid;
-    }
-
    
     /*
      * (non-Javadoc)
@@ -165,7 +144,6 @@ public class JCRMailbox implements Mailb
     public void  merge(Node node) throws RepositoryException {
         node.setProperty(NAME_PROPERTY, getName());
         node.setProperty(UIDVALIDITY_PROPERTY, getUidValidity());
-        node.setProperty(LASTUID_PROPERTY, getLastUid());   
         String user = getUser();
         if (user == null) {
             user = "";
@@ -181,7 +159,6 @@ public class JCRMailbox implements Mailb
             + "mailboxUID = " + this.getMailboxId() + TAB
             + "name = " + this.getName() + TAB
             + "uidValidity = " + this.getUidValidity() + TAB
-            + "lastUid = " + this.getLastUid() + TAB
             + " )";
         return retValue;
     }

Modified: james/imap/trunk/jcr/src/test/java/org/apache/james/mailbox/jcr/JCRHostSystem.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/jcr/src/test/java/org/apache/james/mailbox/jcr/JCRHostSystem.java?rev=1041402&r1=1041401&r2=1041402&view=diff
==============================================================================
--- james/imap/trunk/jcr/src/test/java/org/apache/james/mailbox/jcr/JCRHostSystem.java (original)
+++ james/imap/trunk/jcr/src/test/java/org/apache/james/mailbox/jcr/JCRHostSystem.java Thu Dec  2 15:12:51 2010
@@ -70,9 +70,10 @@ public class JCRHostSystem extends ImapH
             JCRUtils.registerCnd(repository, workspace, user, pass);
             
             userManager = new InMemoryUserManager();
-            JCRMailboxSessionMapperFactory mf = new JCRMailboxSessionMapperFactory(sessionRepos, new JCRVmNodeLocker());
+            JCRCachingUidProvider uidProvider = new JCRCachingUidProvider(sessionRepos);
+            JCRMailboxSessionMapperFactory mf = new JCRMailboxSessionMapperFactory(sessionRepos, new JCRVmNodeLocker(), uidProvider);
 
-            mailboxManager = new JCRMailboxManager(mf, userManager);
+            mailboxManager = new JCRMailboxManager(mf, userManager, uidProvider);
             final ImapProcessor defaultImapProcessorFactory = DefaultImapProcessorFactory.createDefaultProcessor(mailboxManager, new JCRSubscriptionManager(mf));
             resetUserMetaData();
             MailboxSession session = mailboxManager.createSystemSession("test", new SimpleLog("TestLog"));

Modified: james/imap/trunk/jcr/src/test/java/org/apache/james/mailbox/jcr/JCRStressTest.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/jcr/src/test/java/org/apache/james/mailbox/jcr/JCRStressTest.java?rev=1041402&r1=1041401&r2=1041402&view=diff
==============================================================================
--- james/imap/trunk/jcr/src/test/java/org/apache/james/mailbox/jcr/JCRStressTest.java (original)
+++ james/imap/trunk/jcr/src/test/java/org/apache/james/mailbox/jcr/JCRStressTest.java Thu Dec  2 15:12:51 2010
@@ -60,10 +60,12 @@ public class JCRStressTest extends Abstr
 
         // Register imap cnd file
         JCRUtils.registerCnd(repository, workspace, user, pass);
-
         MailboxSessionJCRRepository sessionRepos = new GlobalMailboxSessionJCRRepository(repository, workspace, user, pass);
-        JCRMailboxSessionMapperFactory mf = new JCRMailboxSessionMapperFactory(sessionRepos, new JCRVmNodeLocker());
-        mailboxManager = new JCRMailboxManager(mf, null);
+
+        JCRCachingUidProvider uidProvider = new JCRCachingUidProvider(sessionRepos);
+
+        JCRMailboxSessionMapperFactory mf = new JCRMailboxSessionMapperFactory(sessionRepos, new JCRVmNodeLocker(), uidProvider);
+        mailboxManager = new JCRMailboxManager(mf, null, uidProvider);
 
     }
     

Added: james/imap/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/JPACachingUidProvider.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/JPACachingUidProvider.java?rev=1041402&view=auto
==============================================================================
--- james/imap/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/JPACachingUidProvider.java (added)
+++ james/imap/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/JPACachingUidProvider.java Thu Dec  2 15:12:51 2010
@@ -0,0 +1,72 @@
+/****************************************************************
+ * 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.jpa;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.LockModeType;
+import javax.persistence.NoResultException;
+import javax.persistence.PersistenceException;
+
+import org.apache.james.mailbox.MailboxException;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.store.CachingUidProvider;
+import org.apache.james.mailbox.store.mail.model.Mailbox;
+import org.apache.james.mailbox.store.mail.model.MailboxMembership;
+
+/**
+ * Lazy-lookup last used uid via JPA 
+ * 
+ *
+ */
+public class JPACachingUidProvider extends CachingUidProvider<Long>{
+
+    private final EntityManagerFactory factory;
+    
+    public JPACachingUidProvider(EntityManagerFactory factory) {
+        this.factory = factory;
+    }
+    
+    
+    /*
+     * (non-Javadoc)
+     * @see org.apache.james.mailbox.store.CachingUidProvider#getLastUid(org.apache.james.mailbox.MailboxSession, org.apache.james.mailbox.store.mail.model.Mailbox)
+     */
+    @SuppressWarnings("unchecked")
+    protected long getLastUid(MailboxSession session, Mailbox<Long> mailbox) throws MailboxException{
+        EntityManager em = factory.createEntityManager();
+        try {
+            em.getTransaction().begin();
+            MailboxMembership<Long> m = (MailboxMembership<Long>) em.createNamedQuery("findLastUidInMailbox").setParameter("idParam", mailbox.getMailboxId()).setLockMode(LockModeType.PESSIMISTIC_WRITE).getSingleResult();
+            em.getTransaction().commit();
+            return m.getUid();
+        } catch (NoResultException e) {
+            em.getTransaction().rollback();
+            return 0;
+        } catch (PersistenceException e) {
+            em.getTransaction().rollback();
+            throw new MailboxException("Unable to retrieve last uid for mailbox " + mailbox);
+        } finally {
+            if (em.isOpen()) {
+                em.close();
+            }
+        }
+    }
+
+}

Modified: james/imap/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/JPAMailboxManager.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/JPAMailboxManager.java?rev=1041402&r1=1041401&r2=1041402&view=diff
==============================================================================
--- james/imap/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/JPAMailboxManager.java (original)
+++ james/imap/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/JPAMailboxManager.java Thu Dec  2 15:12:51 2010
@@ -26,6 +26,7 @@ import org.apache.james.mailbox.jpa.mail
 import org.apache.james.mailbox.store.Authenticator;
 import org.apache.james.mailbox.store.MailboxPathLocker;
 import org.apache.james.mailbox.store.StoreMailboxManager;
+import org.apache.james.mailbox.store.UidProvider;
 import org.apache.james.mailbox.store.mail.model.Mailbox;
 import org.apache.james.mailbox.store.transaction.TransactionalMapper;
 
@@ -35,8 +36,8 @@ import org.apache.james.mailbox.store.tr
 public abstract class JPAMailboxManager extends StoreMailboxManager<Long> {
     
     public JPAMailboxManager(JPAMailboxSessionMapperFactory mailboxSessionMapperFactory,
-            final Authenticator authenticator, final MailboxPathLocker locker) {
-        super(mailboxSessionMapperFactory, authenticator, locker);
+            final Authenticator authenticator, UidProvider<Long> uidProvider, final MailboxPathLocker locker) {
+        super(mailboxSessionMapperFactory, authenticator, uidProvider, locker);
     }
     
     @Override

Modified: james/imap/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/JPAMailboxSessionMapperFactory.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/JPAMailboxSessionMapperFactory.java?rev=1041402&r1=1041401&r2=1041402&view=diff
==============================================================================
--- james/imap/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/JPAMailboxSessionMapperFactory.java (original)
+++ james/imap/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/JPAMailboxSessionMapperFactory.java Thu Dec  2 15:12:51 2010
@@ -27,6 +27,7 @@ import org.apache.james.mailbox.jpa.mail
 import org.apache.james.mailbox.jpa.mail.JPAMessageMapper;
 import org.apache.james.mailbox.jpa.user.JPASubscriptionMapper;
 import org.apache.james.mailbox.store.MailboxSessionMapperFactory;
+import org.apache.james.mailbox.store.UidProvider;
 import org.apache.james.mailbox.store.mail.MailboxMapper;
 import org.apache.james.mailbox.store.mail.MessageMapper;
 import org.apache.james.mailbox.store.user.SubscriptionMapper;
@@ -39,14 +40,16 @@ public class JPAMailboxSessionMapperFact
 
     private final EntityManagerFactory entityManagerFactory;
     private final char delimiter;
+    private UidProvider<Long> uidGenerator;
 
-    public JPAMailboxSessionMapperFactory(EntityManagerFactory entityManagerFactory) {
-        this(entityManagerFactory, MailboxConstants.DEFAULT_DELIMITER);
+    public JPAMailboxSessionMapperFactory(EntityManagerFactory entityManagerFactory, UidProvider<Long> uidProvider) {
+        this(entityManagerFactory, uidProvider, MailboxConstants.DEFAULT_DELIMITER);
     }
 
-    public JPAMailboxSessionMapperFactory(EntityManagerFactory entityManagerFactory, char delimiter) {
+    public JPAMailboxSessionMapperFactory(EntityManagerFactory entityManagerFactory, UidProvider<Long> uidGenerator, char delimiter) {
         this.entityManagerFactory = entityManagerFactory;
         this.delimiter = delimiter;
+        this.uidGenerator = uidGenerator;
     }
     
     @Override
@@ -56,7 +59,7 @@ public class JPAMailboxSessionMapperFact
 
     @Override
     public MessageMapper<Long> createMessageMapper(MailboxSession session) {
-        return new JPAMessageMapper(entityManagerFactory);
+        return new JPAMessageMapper(session, entityManagerFactory, uidGenerator);
     }
 
     @Override

Modified: james/imap/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/JPAMessageManager.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/JPAMessageManager.java?rev=1041402&r1=1041401&r2=1041402&view=diff
==============================================================================
--- james/imap/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/JPAMessageManager.java (original)
+++ james/imap/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/JPAMessageManager.java Thu Dec  2 15:12:51 2010
@@ -22,7 +22,6 @@ import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
-import java.util.concurrent.atomic.AtomicLong;
 
 import javax.mail.Flags;
 
@@ -30,6 +29,7 @@ import org.apache.james.mailbox.MailboxE
 import org.apache.james.mailbox.jpa.mail.model.JPAHeader;
 import org.apache.james.mailbox.jpa.mail.model.openjpa.JPAMailboxMembership;
 import org.apache.james.mailbox.store.MapperStoreMessageManager;
+import org.apache.james.mailbox.store.UidProvider;
 import org.apache.james.mailbox.store.mail.model.Header;
 import org.apache.james.mailbox.store.mail.model.Mailbox;
 import org.apache.james.mailbox.store.mail.model.MailboxMembership;
@@ -41,9 +41,9 @@ import org.apache.james.mailbox.util.Mai
  */
 public class JPAMessageManager extends MapperStoreMessageManager<Long> {
     
-    public JPAMessageManager(JPAMailboxSessionMapperFactory mapperFactory, AtomicLong lastUid,
+    public JPAMessageManager(JPAMailboxSessionMapperFactory mapperFactory, UidProvider<Long> uidProvider,
             final MailboxEventDispatcher dispatcher,final Mailbox<Long> mailbox) throws MailboxException {
-        super(mapperFactory, lastUid, dispatcher, mailbox);     
+        super(mapperFactory, uidProvider, dispatcher, mailbox);     
     }
     
     @Override

Modified: james/imap/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/JPAMessageMapper.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/JPAMessageMapper.java?rev=1041402&r1=1041401&r2=1041402&view=diff
==============================================================================
--- james/imap/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/JPAMessageMapper.java (original)
+++ james/imap/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/JPAMessageMapper.java Thu Dec  2 15:12:51 2010
@@ -21,24 +21,23 @@ package org.apache.james.mailbox.jpa.mai
 import java.util.Iterator;
 import java.util.List;
 
-import javax.persistence.EntityManager;
 import javax.persistence.EntityManagerFactory;
-import javax.persistence.LockModeType;
 import javax.persistence.PersistenceException;
 import javax.persistence.Query;
 
 import org.apache.james.mailbox.MailboxException;
+import org.apache.james.mailbox.MailboxSession;
 import org.apache.james.mailbox.MessageRange;
-import org.apache.james.mailbox.SearchQuery;
 import org.apache.james.mailbox.MessageRange.Type;
+import org.apache.james.mailbox.SearchQuery;
 import org.apache.james.mailbox.SearchQuery.Criterion;
 import org.apache.james.mailbox.SearchQuery.NumericRange;
 import org.apache.james.mailbox.jpa.JPATransactionalMapper;
-import org.apache.james.mailbox.jpa.mail.model.JPAMailbox;
 import org.apache.james.mailbox.jpa.mail.model.openjpa.AbstractJPAMailboxMembership;
 import org.apache.james.mailbox.jpa.mail.model.openjpa.JPAMailboxMembership;
 import org.apache.james.mailbox.jpa.mail.model.openjpa.JPAStreamingMailboxMembership;
 import org.apache.james.mailbox.store.SearchQueryIterator;
+import org.apache.james.mailbox.store.UidProvider;
 import org.apache.james.mailbox.store.mail.MessageMapper;
 import org.apache.james.mailbox.store.mail.model.Mailbox;
 import org.apache.james.mailbox.store.mail.model.MailboxMembership;
@@ -50,8 +49,13 @@ import org.apache.james.mailbox.store.ma
  */
 public class JPAMessageMapper extends JPATransactionalMapper implements MessageMapper<Long> {
     
-    public JPAMessageMapper(final EntityManagerFactory entityManagerFactory) {
+    private final UidProvider<Long> uidGenerator;
+    private MailboxSession session;
+
+    public JPAMessageMapper(final MailboxSession session, final EntityManagerFactory entityManagerFactory, UidProvider<Long> uidGenerator) {
         super(entityManagerFactory);
+        this.session = session;
+        this.uidGenerator = uidGenerator;
     }
 
     /**
@@ -301,7 +305,7 @@ public class JPAMessageMapper extends JP
         try {
             
             if (message.getUid() == 0) {
-                ((AbstractJPAMailboxMembership) message).setUid(reserveUid((JPAMailbox)mailbox));
+                ((AbstractJPAMailboxMembership) message).setUid(uidGenerator.nextUid(session,mailbox));
             }
             getEntityManager().persist(message);
             return message.getUid();
@@ -325,21 +329,4 @@ public class JPAMessageMapper extends JP
         return save(mailbox, copy);
     }
     
-    /**
-     * Reserve the uid for the next {@link MailboxMembership} in the given {@link JPAMailbox}.
-     * This is done by using a row lock.
-     * 
-     * @param mailbox
-     * @return uid
-     */
-    protected long reserveUid(JPAMailbox mailbox) {
-        EntityManager manager = getEntityManager();
-
-        // we need to set a pessimistic write lock to be sure we don't get any problems with dirty reads etc.
-        JPAMailbox m = manager.find(JPAMailbox.class, mailbox.getMailboxId(), LockModeType.PESSIMISTIC_WRITE);
-        manager.refresh(m);
-        m.consumeUid();
-        manager.persist(m);
-        return m.getLastUid();
-    }
 }

Modified: james/imap/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/model/JPAMailbox.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/model/JPAMailbox.java?rev=1041402&r1=1041401&r2=1041402&view=diff
==============================================================================
--- james/imap/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/model/JPAMailbox.java (original)
+++ james/imap/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/model/JPAMailbox.java Thu Dec  2 15:12:51 2010
@@ -61,10 +61,6 @@ public class JPAMailbox implements Mailb
     /** The value for the uidValidity field */
     @Basic(optional=false) private long uidValidity;
 
-    
-    /** The value for the lastUid field */
-    @Basic(optional=false) private long lastUid = 0;
-    
     @Basic(optional=false) @Column(nullable = true)  private String user;
     @Basic(optional=false) @Column(nullable = false) private String namespace;
 
@@ -85,13 +81,6 @@ public class JPAMailbox implements Mailb
     }
 
     /**
-     * @see org.apache.james.mailbox.store.mail.model.Mailbox#getLastUid()
-     */
-    public long getLastUid() {
-        return lastUid;
-    }
-
-    /**
      * @see org.apache.james.mailbox.store.mail.model.Mailbox#getMailboxId()
      */
     public Long getMailboxId() {
@@ -113,12 +102,6 @@ public class JPAMailbox implements Mailb
         return uidValidity;
     }
 
-    /**
-     * @see org.apache.james.mailbox.store.mail.model.Mailbox#consumeUid()
-     */
-    public void consumeUid() {
-        ++lastUid;
-    }
     
     /**
      * @see org.apache.james.mailbox.store.mail.model.Mailbox#setName(java.lang.String)
@@ -134,7 +117,6 @@ public class JPAMailbox implements Mailb
             + "mailboxId = " + this.mailboxId + TAB
             + "name = " + this.name + TAB
             + "uidValidity = " + this.uidValidity + TAB
-            + "lastUid = " + this.lastUid + TAB
             + " )";
         return retValue;
     }
@@ -193,8 +175,4 @@ public class JPAMailbox implements Mailb
     public void setUser(String user) {
         this.user = user;
     }
-
-    public void setLastUid(long uid) {
-        this.lastUid = uid;
-    }
 }

Modified: james/imap/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/model/openjpa/AbstractJPAMailboxMembership.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/model/openjpa/AbstractJPAMailboxMembership.java?rev=1041402&r1=1041401&r2=1041402&view=diff
==============================================================================
--- james/imap/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/model/openjpa/AbstractJPAMailboxMembership.java (original)
+++ james/imap/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/model/openjpa/AbstractJPAMailboxMembership.java Thu Dec  2 15:12:51 2010
@@ -66,7 +66,9 @@ import org.apache.openjpa.persistence.jd
     @NamedQuery(name="countMessagesInMailbox",
             query="SELECT COUNT(membership) FROM Membership membership WHERE membership.mailboxId = :idParam"),                    
     @NamedQuery(name="deleteMessages",
-            query="DELETE FROM Membership membership WHERE membership.mailboxId = :idParam") 
+            query="DELETE FROM Membership membership WHERE membership.mailboxId = :idParam"),
+    @NamedQuery(name="findLastUidInMailbox",
+            query="SELECT membership FROM Membership membership WHERE membership.mailboxId = :idParam ORDER BY membership.uid ASC")
 })
 public abstract class AbstractJPAMailboxMembership extends AbstractMailboxMembership<Long> {
 

Modified: james/imap/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/openjpa/OpenJPAMailboxManager.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/openjpa/OpenJPAMailboxManager.java?rev=1041402&r1=1041401&r2=1041402&view=diff
==============================================================================
--- james/imap/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/openjpa/OpenJPAMailboxManager.java (original)
+++ james/imap/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/openjpa/OpenJPAMailboxManager.java Thu Dec  2 15:12:51 2010
@@ -20,8 +20,6 @@
 package org.apache.james.mailbox.jpa.openjpa;
 
 
-import java.util.concurrent.atomic.AtomicLong;
-
 import org.apache.james.mailbox.MailboxException;
 import org.apache.james.mailbox.MailboxSession;
 import org.apache.james.mailbox.jpa.JPAMailboxManager;
@@ -30,6 +28,7 @@ import org.apache.james.mailbox.store.Au
 import org.apache.james.mailbox.store.JVMMailboxPathLocker;
 import org.apache.james.mailbox.store.MailboxPathLocker;
 import org.apache.james.mailbox.store.MapperStoreMessageManager;
+import org.apache.james.mailbox.store.UidProvider;
 import org.apache.james.mailbox.store.mail.model.Mailbox;
 import org.apache.james.mailbox.util.MailboxEventDispatcher;
 
@@ -41,18 +40,18 @@ public class OpenJPAMailboxManager exten
 
     private boolean useStreaming;
 
-    public OpenJPAMailboxManager(JPAMailboxSessionMapperFactory mapperFactory, Authenticator authenticator, MailboxPathLocker locker, boolean useStreaming) {
-        super(mapperFactory, authenticator, locker);
+    public OpenJPAMailboxManager(JPAMailboxSessionMapperFactory mapperFactory, Authenticator authenticator, UidProvider<Long> uidProvider, MailboxPathLocker locker, boolean useStreaming) {
+        super(mapperFactory, authenticator, uidProvider, locker);
         this.useStreaming = useStreaming;
     }
 
-    public OpenJPAMailboxManager(JPAMailboxSessionMapperFactory mapperFactory, Authenticator authenticator) {
-        this(mapperFactory, authenticator, new JVMMailboxPathLocker(), false);
+    public OpenJPAMailboxManager(JPAMailboxSessionMapperFactory mapperFactory, Authenticator authenticator, UidProvider<Long> uidProvider) {
+        this(mapperFactory, authenticator, uidProvider, new JVMMailboxPathLocker(), false);
     }
 
     @Override
-    protected MapperStoreMessageManager<Long> createMessageManager(AtomicLong lastUid, MailboxEventDispatcher dispatcher, Mailbox<Long> mailboxRow, MailboxSession session) throws MailboxException {
-        MapperStoreMessageManager<Long> result =  new OpenJPAMessageManager((JPAMailboxSessionMapperFactory) mailboxSessionMapperFactory, lastUid, dispatcher, mailboxRow, useStreaming);
+    protected MapperStoreMessageManager<Long> createMessageManager(UidProvider<Long> uidProvider, MailboxEventDispatcher dispatcher, Mailbox<Long> mailboxRow, MailboxSession session) throws MailboxException {
+        MapperStoreMessageManager<Long> result =  new OpenJPAMessageManager((JPAMailboxSessionMapperFactory) mailboxSessionMapperFactory, uidProvider, dispatcher, mailboxRow, useStreaming);
         return result;
     }
 }

Modified: james/imap/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/openjpa/OpenJPAMessageManager.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/openjpa/OpenJPAMessageManager.java?rev=1041402&r1=1041401&r2=1041402&view=diff
==============================================================================
--- james/imap/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/openjpa/OpenJPAMessageManager.java (original)
+++ james/imap/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/openjpa/OpenJPAMessageManager.java Thu Dec  2 15:12:51 2010
@@ -23,7 +23,6 @@ import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
-import java.util.concurrent.atomic.AtomicLong;
 
 import javax.mail.Flags;
 
@@ -32,6 +31,7 @@ import org.apache.james.mailbox.jpa.JPAM
 import org.apache.james.mailbox.jpa.JPAMessageManager;
 import org.apache.james.mailbox.jpa.mail.model.JPAHeader;
 import org.apache.james.mailbox.jpa.mail.model.openjpa.JPAStreamingMailboxMembership;
+import org.apache.james.mailbox.store.UidProvider;
 import org.apache.james.mailbox.store.mail.model.Header;
 import org.apache.james.mailbox.store.mail.model.Mailbox;
 import org.apache.james.mailbox.store.mail.model.MailboxMembership;
@@ -46,14 +46,14 @@ public class OpenJPAMessageManager exten
 
     private final boolean useStreaming;
 
-    public OpenJPAMessageManager(JPAMailboxSessionMapperFactory mapperFactory, AtomicLong lastUid,
+    public OpenJPAMessageManager(JPAMailboxSessionMapperFactory mapperFactory, UidProvider<Long> uidProvider,
             MailboxEventDispatcher dispatcher, Mailbox<Long> mailbox) throws MailboxException {
-        this(mapperFactory, lastUid, dispatcher, mailbox, false);
+        this(mapperFactory, uidProvider, dispatcher, mailbox, false);
     }
 
-    public OpenJPAMessageManager(JPAMailboxSessionMapperFactory mapperFactory, AtomicLong lastUid,
+    public OpenJPAMessageManager(JPAMailboxSessionMapperFactory mapperFactory, UidProvider<Long> uidProvider,
             MailboxEventDispatcher dispatcher, Mailbox<Long> mailbox, final boolean useStreaming) throws MailboxException {
-        super(mapperFactory, lastUid, dispatcher, mailbox);
+        super(mapperFactory, uidProvider, dispatcher, mailbox);
         this.useStreaming = useStreaming;
     }
 

Modified: james/imap/trunk/jpa/src/test/java/org/apache/james/mailbox/jpa/JPAHostSystem.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/jpa/src/test/java/org/apache/james/mailbox/jpa/JPAHostSystem.java?rev=1041402&r1=1041401&r2=1041402&view=diff
==============================================================================
--- james/imap/trunk/jpa/src/test/java/org/apache/james/mailbox/jpa/JPAHostSystem.java (original)
+++ james/imap/trunk/jpa/src/test/java/org/apache/james/mailbox/jpa/JPAHostSystem.java Thu Dec  2 15:12:51 2010
@@ -77,8 +77,10 @@ public class JPAHostSystem extends ImapH
                 JPASubscription.class.getName() + ")");
         userManager = new InMemoryUserManager();
         entityManagerFactory = OpenJPAPersistence.getEntityManagerFactory(properties);
-        JPAMailboxSessionMapperFactory mf = new JPAMailboxSessionMapperFactory(entityManagerFactory);
-        mailboxManager = new OpenJPAMailboxManager(mf, userManager);
+        JPACachingUidProvider uidProvider = new JPACachingUidProvider(entityManagerFactory);
+
+        JPAMailboxSessionMapperFactory mf = new JPAMailboxSessionMapperFactory(entityManagerFactory, uidProvider);
+        mailboxManager = new OpenJPAMailboxManager(mf, userManager, uidProvider);
         SubscriptionManager subscriptionManager = new JPASubscriptionManager(mf);
         final ImapProcessor defaultImapProcessorFactory = DefaultImapProcessorFactory.createDefaultProcessor(mailboxManager, subscriptionManager);
         resetUserMetaData();

Modified: james/imap/trunk/jpa/src/test/java/org/apache/james/mailbox/jpa/JPAStressTest.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/jpa/src/test/java/org/apache/james/mailbox/jpa/JPAStressTest.java?rev=1041402&r1=1041401&r2=1041402&view=diff
==============================================================================
--- james/imap/trunk/jpa/src/test/java/org/apache/james/mailbox/jpa/JPAStressTest.java (original)
+++ james/imap/trunk/jpa/src/test/java/org/apache/james/mailbox/jpa/JPAStressTest.java Thu Dec  2 15:12:51 2010
@@ -73,8 +73,9 @@ public class JPAStressTest extends Abstr
         properties.put("openjpa.LockTimeout", locktimeout + "");
        
         entityManagerFactory = OpenJPAPersistence.getEntityManagerFactory(properties);
-        JPAMailboxSessionMapperFactory mf = new JPAMailboxSessionMapperFactory(entityManagerFactory);
-        mailboxManager = new OpenJPAMailboxManager(mf, null);
+        JPACachingUidProvider uidProvider = new JPACachingUidProvider(entityManagerFactory);
+        JPAMailboxSessionMapperFactory mf = new JPAMailboxSessionMapperFactory(entityManagerFactory, uidProvider);
+        mailboxManager = new OpenJPAMailboxManager(mf, null, uidProvider);
         
         // Set the lock timeout via SQL because of a bug in openJPA
         // https://issues.apache.org/jira/browse/OPENJPA-1656

Modified: james/imap/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/MaildirMailboxManager.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/MaildirMailboxManager.java?rev=1041402&r1=1041401&r2=1041402&view=diff
==============================================================================
--- james/imap/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/MaildirMailboxManager.java (original)
+++ james/imap/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/MaildirMailboxManager.java Thu Dec  2 15:12:51 2010
@@ -18,8 +18,6 @@
  ****************************************************************/
 package org.apache.james.mailbox.maildir;
 
-import java.util.concurrent.atomic.AtomicLong;
-
 import org.apache.james.mailbox.MailboxException;
 import org.apache.james.mailbox.MailboxPath;
 import org.apache.james.mailbox.MailboxSession;
@@ -30,6 +28,7 @@ import org.apache.james.mailbox.store.Ma
 import org.apache.james.mailbox.store.MailboxSessionMapperFactory;
 import org.apache.james.mailbox.store.MapperStoreMessageManager;
 import org.apache.james.mailbox.store.StoreMailboxManager;
+import org.apache.james.mailbox.store.UidProvider;
 import org.apache.james.mailbox.store.mail.model.Mailbox;
 import org.apache.james.mailbox.util.MailboxEventDispatcher;
 
@@ -37,20 +36,20 @@ public class MaildirMailboxManager exten
 
     public MaildirMailboxManager(
             MailboxSessionMapperFactory<Integer> mailboxSessionMapperFactory,
-            Authenticator authenticator) {
-        this(mailboxSessionMapperFactory, authenticator, new JVMMailboxPathLocker());
+            Authenticator authenticator, MaildirStore store) {
+        this(mailboxSessionMapperFactory, authenticator, store, new JVMMailboxPathLocker());
     }
 
     public MaildirMailboxManager(
             MailboxSessionMapperFactory<Integer> mailboxSessionMapperFactory,
-            Authenticator authenticator, MailboxPathLocker locker) {
-        super(mailboxSessionMapperFactory, authenticator, locker);
+            Authenticator authenticator, MaildirStore store, MailboxPathLocker locker) {
+        super(mailboxSessionMapperFactory, authenticator, store, locker);
     }
 
     @Override
-    protected MapperStoreMessageManager<Integer> createMessageManager(AtomicLong lastUid, MailboxEventDispatcher dispatcher,
+    protected MapperStoreMessageManager<Integer> createMessageManager(UidProvider<Integer> uidProvider, MailboxEventDispatcher dispatcher,
             Mailbox<Integer> mailboxEntiy, MailboxSession session) throws MailboxException {
-        return new MaildirMessageManager((MailboxSessionMapperFactory<Integer>)mailboxSessionMapperFactory, lastUid, dispatcher, mailboxEntiy);
+        return new MaildirMessageManager((MailboxSessionMapperFactory<Integer>)mailboxSessionMapperFactory, uidProvider, dispatcher, mailboxEntiy);
     }
 
     @Override

Modified: james/imap/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/MaildirMailboxSessionMapperFactory.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/MaildirMailboxSessionMapperFactory.java?rev=1041402&r1=1041401&r2=1041402&view=diff
==============================================================================
--- james/imap/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/MaildirMailboxSessionMapperFactory.java (original)
+++ james/imap/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/MaildirMailboxSessionMapperFactory.java Thu Dec  2 15:12:51 2010
@@ -32,28 +32,28 @@ import org.apache.james.mailbox.store.us
 public class MaildirMailboxSessionMapperFactory extends
         MailboxSessionMapperFactory<Integer> {
 
-    private final String root;
+    private final MaildirStore store;
     
-    public MaildirMailboxSessionMapperFactory(String root) {
-        this.root = root;
+    public MaildirMailboxSessionMapperFactory(MaildirStore store) {
+        this.store = store;
     }
     
     @Override
     protected MailboxMapper<Integer> createMailboxMapper(MailboxSession session)
             throws MailboxException {
-        return new MaildirMailboxMapper(root);
+        return new MaildirMailboxMapper(store);
     }
 
     @Override
     protected MessageMapper<Integer> createMessageMapper(MailboxSession session)
             throws MailboxException {
-        return new MaildirMessageMapper(root);
+        return new MaildirMessageMapper(store);
     }
 
     @Override
     protected SubscriptionMapper createSubscriptionMapper(MailboxSession session)
             throws SubscriptionException {
-        return new MaildirSubscriptionMapper(root);
+        return new MaildirSubscriptionMapper(store.getMaildirLocation());
     }
 
 }

Modified: james/imap/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/MaildirMessageManager.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/MaildirMessageManager.java?rev=1041402&r1=1041401&r2=1041402&view=diff
==============================================================================
--- james/imap/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/MaildirMessageManager.java (original)
+++ james/imap/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/MaildirMessageManager.java Thu Dec  2 15:12:51 2010
@@ -22,7 +22,6 @@ import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
-import java.util.concurrent.atomic.AtomicLong;
 
 import javax.mail.Flags;
 
@@ -31,6 +30,7 @@ import org.apache.james.mailbox.maildir.
 import org.apache.james.mailbox.maildir.mail.model.MaildirMessage;
 import org.apache.james.mailbox.store.MapperStoreMessageManager;
 import org.apache.james.mailbox.store.MessageMapperFactory;
+import org.apache.james.mailbox.store.UidProvider;
 import org.apache.james.mailbox.store.mail.model.Header;
 import org.apache.james.mailbox.store.mail.model.Mailbox;
 import org.apache.james.mailbox.store.mail.model.MailboxMembership;
@@ -39,10 +39,10 @@ import org.apache.james.mailbox.util.Mai
 
 public class MaildirMessageManager extends MapperStoreMessageManager<Integer> {
 
-    public MaildirMessageManager(MessageMapperFactory<Integer> mapperFactory, AtomicLong lastUid,
+    public MaildirMessageManager(MessageMapperFactory<Integer> mapperFactory, UidProvider<Integer> uidProvider,
             MailboxEventDispatcher dispatcher, Mailbox<Integer> mailboxEntiy)
     throws MailboxException {
-        super(mapperFactory, lastUid, dispatcher, mailboxEntiy);
+        super(mapperFactory, uidProvider, dispatcher, mailboxEntiy);
     }
 
     @Override

Modified: james/imap/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/MaildirStore.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/MaildirStore.java?rev=1041402&r1=1041401&r2=1041402&view=diff
==============================================================================
--- james/imap/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/MaildirStore.java (original)
+++ james/imap/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/MaildirStore.java Thu Dec  2 15:12:51 2010
@@ -24,10 +24,12 @@ import java.io.IOException;
 import org.apache.james.mailbox.MailboxException;
 import org.apache.james.mailbox.MailboxNotFoundException;
 import org.apache.james.mailbox.MailboxPath;
+import org.apache.james.mailbox.MailboxSession;
 import org.apache.james.mailbox.maildir.mail.model.MaildirMailbox;
+import org.apache.james.mailbox.store.UidProvider;
 import org.apache.james.mailbox.store.mail.model.Mailbox;
 
-public class MaildirStore {
+public class MaildirStore implements UidProvider<Integer>{
 
     public static final String PATH_USER = "%user";
     public static final String PATH_DOMAIN = "%domain";
@@ -52,6 +54,9 @@ public class MaildirStore {
         this.maildirLocation = maildirLocation;
     }
     
+    public String getMaildirLocation() {
+        return maildirLocation;
+    }
     /**
      * Create a {@link MaildirFolder} for a mailbox
      * @param mailbox
@@ -196,4 +201,20 @@ public class MaildirStore {
     public String getFolderName(MailboxPath mailboxPath) {
         return getFolderName(mailboxPath.getNamespace(), mailboxPath.getUser(), mailboxPath.getName());
     }
+
+    public long nextUid(MailboxSession session, Mailbox<Integer> mailbox) throws MailboxException {
+        try {
+            return createMaildirFolder(mailbox).getLastUid() +1;
+        } catch (IOException e) {
+            throw new MailboxException("Unable to generate next uid", e);
+        }
+    }
+
+    public long lastUid(MailboxSession session, Mailbox<Integer> mailbox) throws MailboxException {
+        try {
+            return createMaildirFolder(mailbox).getLastUid();
+        } catch (IOException e) {
+            throw new MailboxException("Unable to get last uid", e);
+        }
+    }
 }

Modified: james/imap/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/MaildirMailboxMapper.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/MaildirMailboxMapper.java?rev=1041402&r1=1041401&r2=1041402&view=diff
==============================================================================
--- james/imap/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/MaildirMailboxMapper.java (original)
+++ james/imap/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/MaildirMailboxMapper.java Thu Dec  2 15:12:51 2010
@@ -50,8 +50,8 @@ public class MaildirMailboxMapper extend
      */
     private ArrayList<Mailbox<Integer>> mailboxCache = new ArrayList<Mailbox<Integer>>();
     
-    public MaildirMailboxMapper(String maildirLocation) {
-        this.maildirStore = new MaildirStore(maildirLocation);
+    public MaildirMailboxMapper(MaildirStore maildirStore) {
+        this.maildirStore = maildirStore;
     }
 
     /*

Modified: james/imap/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/MaildirMessageMapper.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/MaildirMessageMapper.java?rev=1041402&r1=1041401&r2=1041402&view=diff
==============================================================================
--- james/imap/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/MaildirMessageMapper.java (original)
+++ james/imap/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/MaildirMessageMapper.java Thu Dec  2 15:12:51 2010
@@ -55,8 +55,8 @@ public class MaildirMessageMapper extend
     private final MaildirStore maildirStore;
     private final int BUF_SIZE = 2048;
 
-    public MaildirMessageMapper(String  maildirLocation) {
-        this.maildirStore = new MaildirStore(maildirLocation);
+    public MaildirMessageMapper(MaildirStore  maildirStore) {
+        this.maildirStore = maildirStore;
     }
     
 

Modified: james/imap/trunk/maildir/src/test/java/org/apache/james/mailbox/functional/maildir/MaildirHostSystem.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/maildir/src/test/java/org/apache/james/mailbox/functional/maildir/MaildirHostSystem.java?rev=1041402&r1=1041401&r2=1041402&view=diff
==============================================================================
--- james/imap/trunk/maildir/src/test/java/org/apache/james/mailbox/functional/maildir/MaildirHostSystem.java (original)
+++ james/imap/trunk/maildir/src/test/java/org/apache/james/mailbox/functional/maildir/MaildirHostSystem.java Thu Dec  2 15:12:51 2010
@@ -29,6 +29,7 @@ import org.apache.james.imap.main.Defaul
 import org.apache.james.imap.processor.main.DefaultImapProcessorFactory;
 import org.apache.james.mailbox.maildir.MaildirMailboxManager;
 import org.apache.james.mailbox.maildir.MaildirMailboxSessionMapperFactory;
+import org.apache.james.mailbox.maildir.MaildirStore;
 import org.apache.james.mailbox.maildir.MaildirSubscriptionManager;
 import org.apache.james.test.functional.HostSystem;
 
@@ -47,9 +48,10 @@ public class MaildirHostSystem extends I
     
     public MaildirHostSystem() {
         userManager = new InMemoryUserManager();
-        mailboxSessionMapperFactory = new MaildirMailboxSessionMapperFactory(MAILDIR_HOME + "/%user");
+        MaildirStore store = new MaildirStore(MAILDIR_HOME + "/%user");
+        mailboxSessionMapperFactory = new MaildirMailboxSessionMapperFactory(store);
         MaildirSubscriptionManager sm = new MaildirSubscriptionManager(mailboxSessionMapperFactory);
-        mailboxManager = new MaildirMailboxManager(mailboxSessionMapperFactory, userManager);
+        mailboxManager = new MaildirMailboxManager(mailboxSessionMapperFactory, userManager, store);
         
         final ImapProcessor defaultImapProcessorFactory = DefaultImapProcessorFactory.createDefaultProcessor(mailboxManager, sm);
         configure(new DefaultImapDecoderFactory().buildImapDecoder(),

Modified: james/imap/trunk/maildir/src/test/java/org/apache/james/mailbox/functional/maildir/MaildirStressTest.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/maildir/src/test/java/org/apache/james/mailbox/functional/maildir/MaildirStressTest.java?rev=1041402&r1=1041401&r2=1041402&view=diff
==============================================================================
--- james/imap/trunk/maildir/src/test/java/org/apache/james/mailbox/functional/maildir/MaildirStressTest.java (original)
+++ james/imap/trunk/maildir/src/test/java/org/apache/james/mailbox/functional/maildir/MaildirStressTest.java Thu Dec  2 15:12:51 2010
@@ -27,6 +27,7 @@ import org.apache.james.mailbox.MailboxE
 import org.apache.james.mailbox.MailboxManager;
 import org.apache.james.mailbox.maildir.MaildirMailboxManager;
 import org.apache.james.mailbox.maildir.MaildirMailboxSessionMapperFactory;
+import org.apache.james.mailbox.maildir.MaildirStore;
 import org.junit.After;
 import org.junit.Before;
 
@@ -37,8 +38,10 @@ public class MaildirStressTest extends A
     
     @Before
     public void setUp() {
-        MaildirMailboxSessionMapperFactory mf = new MaildirMailboxSessionMapperFactory(MAILDIR_HOME + "/%user");
-        mailboxManager = new MaildirMailboxManager(mf, null);
+        MaildirStore store = new MaildirStore(MAILDIR_HOME + "/%user");
+
+        MaildirMailboxSessionMapperFactory mf = new MaildirMailboxSessionMapperFactory(store);
+        mailboxManager = new MaildirMailboxManager(mf, null, store);
     }
     
     @After

Added: james/imap/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/InMemoryCachingUidProvider.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/InMemoryCachingUidProvider.java?rev=1041402&view=auto
==============================================================================
--- james/imap/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/InMemoryCachingUidProvider.java (added)
+++ james/imap/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/InMemoryCachingUidProvider.java Thu Dec  2 15:12:51 2010
@@ -0,0 +1,37 @@
+/****************************************************************
+ * 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;
+
+import org.apache.james.mailbox.MailboxException;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.store.CachingUidProvider;
+import org.apache.james.mailbox.store.mail.model.Mailbox;
+
+public class InMemoryCachingUidProvider extends CachingUidProvider<Long>{
+
+    /*
+     * (non-Javadoc)
+     * @see org.apache.james.mailbox.store.CachingUidProvider#getLastUid(org.apache.james.mailbox.MailboxSession, org.apache.james.mailbox.store.mail.model.Mailbox)
+     */
+    protected long getLastUid(MailboxSession session, Mailbox<Long> mailbox) throws MailboxException {
+        return 0;
+    }
+
+}

Modified: james/imap/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/InMemoryMailboxManager.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/InMemoryMailboxManager.java?rev=1041402&r1=1041401&r2=1041402&view=diff
==============================================================================
--- james/imap/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/InMemoryMailboxManager.java (original)
+++ james/imap/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/InMemoryMailboxManager.java Thu Dec  2 15:12:51 2010
@@ -19,8 +19,6 @@
 
 package org.apache.james.mailbox.inmemory;
 
-import java.util.concurrent.atomic.AtomicLong;
-
 import org.apache.james.mailbox.MailboxException;
 import org.apache.james.mailbox.MailboxPath;
 import org.apache.james.mailbox.MailboxSession;
@@ -30,18 +28,19 @@ import org.apache.james.mailbox.store.JV
 import org.apache.james.mailbox.store.MailboxSessionMapperFactory;
 import org.apache.james.mailbox.store.MapperStoreMessageManager;
 import org.apache.james.mailbox.store.StoreMailboxManager;
+import org.apache.james.mailbox.store.UidProvider;
 import org.apache.james.mailbox.store.mail.model.Mailbox;
 import org.apache.james.mailbox.util.MailboxEventDispatcher;
 
 public class InMemoryMailboxManager extends StoreMailboxManager<Long> {
 
-    public InMemoryMailboxManager(MailboxSessionMapperFactory<Long> mapperFactory, Authenticator authenticator) {
-        super(mapperFactory, authenticator, new JVMMailboxPathLocker());
+    public InMemoryMailboxManager(MailboxSessionMapperFactory<Long> mapperFactory, Authenticator authenticator, UidProvider<Long> uidProvider) {
+        super(mapperFactory, authenticator, uidProvider, new JVMMailboxPathLocker());
     }
 
     @Override
-    protected MapperStoreMessageManager<Long> createMessageManager(AtomicLong lastUid, MailboxEventDispatcher dispatcher, Mailbox<Long> mailboxRow, MailboxSession session) throws MailboxException {
-        return new InMemoryStoreMessageManager((MailboxSessionMapperFactory<Long>)mailboxSessionMapperFactory, lastUid, dispatcher, (InMemoryMailbox)mailboxRow);
+    protected MapperStoreMessageManager<Long> createMessageManager(UidProvider<Long> uidProvider, MailboxEventDispatcher dispatcher, Mailbox<Long> mailboxRow, MailboxSession session) throws MailboxException {
+        return new InMemoryStoreMessageManager((MailboxSessionMapperFactory<Long>)mailboxSessionMapperFactory, uidProvider, dispatcher, (InMemoryMailbox)mailboxRow);
     }
 
     @Override

Modified: james/imap/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/InMemoryStoreMessageManager.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/InMemoryStoreMessageManager.java?rev=1041402&r1=1041401&r2=1041402&view=diff
==============================================================================
--- james/imap/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/InMemoryStoreMessageManager.java (original)
+++ james/imap/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/InMemoryStoreMessageManager.java Thu Dec  2 15:12:51 2010
@@ -23,7 +23,6 @@ import java.io.ByteArrayOutputStream;
 import java.io.InputStream;
 import java.util.Date;
 import java.util.List;
-import java.util.concurrent.atomic.AtomicLong;
 
 import javax.mail.Flags;
 
@@ -33,6 +32,7 @@ import org.apache.james.mailbox.inmemory
 import org.apache.james.mailbox.inmemory.mail.model.SimpleMailboxMembership;
 import org.apache.james.mailbox.store.MailboxSessionMapperFactory;
 import org.apache.james.mailbox.store.MapperStoreMessageManager;
+import org.apache.james.mailbox.store.UidProvider;
 import org.apache.james.mailbox.store.mail.model.Header;
 import org.apache.james.mailbox.store.mail.model.MailboxMembership;
 import org.apache.james.mailbox.store.mail.model.PropertyBuilder;
@@ -40,9 +40,9 @@ import org.apache.james.mailbox.util.Mai
 
 public class InMemoryStoreMessageManager extends MapperStoreMessageManager<Long> {
 
-    public InMemoryStoreMessageManager(MailboxSessionMapperFactory<Long> mapperFactory, AtomicLong lastUid,
+    public InMemoryStoreMessageManager(MailboxSessionMapperFactory<Long> mapperFactory, UidProvider<Long> uidProvider,
             MailboxEventDispatcher dispatcher, InMemoryMailbox mailbox) throws MailboxException {
-        super(mapperFactory, lastUid, dispatcher,mailbox);
+        super(mapperFactory, uidProvider, dispatcher,mailbox);
     }
     
     @Override
@@ -70,8 +70,7 @@ public class InMemoryStoreMessageManager
             byteContent = new byte[0];
         }
         InMemoryMailbox mailbox = (InMemoryMailbox) getMailboxEntity();
-        ((InMemoryMailbox) mailbox).consumeUid();
-        return new SimpleMailboxMembership(internalDate, mailbox.getLastUid(), size, bodyStartOctet, byteContent, flags, headers, propertyBuilder, mailbox.getMailboxId());
+        return new SimpleMailboxMembership(internalDate, uidProvider.nextUid(null, mailbox), size, bodyStartOctet, byteContent, flags, headers, propertyBuilder, mailbox.getMailboxId());
     }
     
 }

Modified: james/imap/trunk/memory/src/test/java/org/apache/james/mailbox/inmemory/InMemoryHostSystem.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/memory/src/test/java/org/apache/james/mailbox/inmemory/InMemoryHostSystem.java?rev=1041402&r1=1041401&r2=1041402&view=diff
==============================================================================
--- james/imap/trunk/memory/src/test/java/org/apache/james/mailbox/inmemory/InMemoryHostSystem.java (original)
+++ james/imap/trunk/memory/src/test/java/org/apache/james/mailbox/inmemory/InMemoryHostSystem.java Thu Dec  2 15:12:51 2010
@@ -58,7 +58,8 @@ public class InMemoryHostSystem extends 
     private void initFields() {
         userManager = new InMemoryUserManager();
         factory = new InMemoryMailboxSessionMapperFactory();
-        mailboxManager = new InMemoryMailboxManager(factory, userManager);
+        InMemoryCachingUidProvider uidProvider = new InMemoryCachingUidProvider();
+        mailboxManager = new InMemoryMailboxManager(factory, userManager, uidProvider);
         final ImapProcessor defaultImapProcessorFactory = DefaultImapProcessorFactory.createDefaultProcessor(mailboxManager, new InMemorySubscriptionManager(factory));
         configure(new DefaultImapDecoderFactory().buildImapDecoder(),
                 new DefaultImapEncoderFactory().buildImapEncoder(),

Modified: james/imap/trunk/protocol-tester/src/main/java/org/apache/james/imap/functional/AbstractStressTest.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/protocol-tester/src/main/java/org/apache/james/imap/functional/AbstractStressTest.java?rev=1041402&r1=1041401&r2=1041402&view=diff
==============================================================================
--- james/imap/trunk/protocol-tester/src/main/java/org/apache/james/imap/functional/AbstractStressTest.java (original)
+++ james/imap/trunk/protocol-tester/src/main/java/org/apache/james/imap/functional/AbstractStressTest.java Thu Dec  2 15:12:51 2010
@@ -38,7 +38,7 @@ import org.junit.Test;
 
 public abstract class AbstractStressTest {
 
-    private final static int APPEND_OPERATIONS = 100;
+    private final static int APPEND_OPERATIONS = 200;
     
     
     protected abstract MailboxManager getMailboxManager();
@@ -46,7 +46,7 @@ public abstract class AbstractStressTest
     @Test
     public void testStessTest() throws InterruptedException, MailboxException {
        
-        final CountDownLatch latch = new CountDownLatch(200);
+        final CountDownLatch latch = new CountDownLatch(APPEND_OPERATIONS);
         final ExecutorService pool = Executors.newFixedThreadPool(APPEND_OPERATIONS/2);
         
         MailboxSession session = getMailboxManager().createSystemSession("test", new SimpleLog("Test"));
@@ -58,7 +58,7 @@ public abstract class AbstractStressTest
         final AtomicBoolean fail = new AtomicBoolean(false);
         
         // fire of 1000 append operations
-        for (int i = 0 ; i < 200; i++) {
+        for (int i = 0 ; i < APPEND_OPERATIONS; i++) {
             pool.execute(new Runnable() {
                 
                 public void run() {

Added: james/imap/trunk/store/src/main/java/org/apache/james/mailbox/store/CachingUidProvider.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/store/src/main/java/org/apache/james/mailbox/store/CachingUidProvider.java?rev=1041402&view=auto
==============================================================================
--- james/imap/trunk/store/src/main/java/org/apache/james/mailbox/store/CachingUidProvider.java (added)
+++ james/imap/trunk/store/src/main/java/org/apache/james/mailbox/store/CachingUidProvider.java Thu Dec  2 15:12:51 2010
@@ -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;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicLong;
+
+import org.apache.james.mailbox.MailboxException;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.store.mail.model.Mailbox;
+
+/**
+ * {@link UidProvider} which lazy retrieve the last used uid from a {@link Mailbox} and then use a in-memory cache to handle 
+ * futher increments.
+ * 
+ *
+ * @param <Id>
+ */
+public abstract class CachingUidProvider<Id> implements UidProvider<Id>{
+
+    private final Map<Id, AtomicLong> uids = new HashMap<Id, AtomicLong>();
+    
+    /*
+     * (non-Javadoc)
+     * @see org.apache.james.mailbox.store.UidProvider#nextUid(org.apache.james.mailbox.MailboxSession, org.apache.james.mailbox.store.mail.model.Mailbox)
+     */
+    public long nextUid(MailboxSession session, Mailbox<Id> mailbox) throws MailboxException {       
+        return retrieveLastUid(session, mailbox).incrementAndGet();
+    }
+    
+    /*
+     * (non-Javadoc)
+     * @see org.apache.james.mailbox.store.UidProvider#lastUid(org.apache.james.mailbox.MailboxSession, org.apache.james.mailbox.store.mail.model.Mailbox)
+     */
+    public long lastUid(MailboxSession session, Mailbox<Id> mailbox) throws MailboxException {
+        return retrieveLastUid(session, mailbox).get();
+    }
+
+    /**
+     * Retrieve the last uid for the {@link Mailbox} from cache or via lazy lookup.
+     * 
+     * @param session
+     * @param mailbox
+     * @return lastUid
+     * @throws MailboxException
+     */
+    protected AtomicLong retrieveLastUid(MailboxSession session, Mailbox<Id> mailbox) throws MailboxException {
+        AtomicLong uid;
+        synchronized (uids) {
+            uid = uids.get(mailbox.getMailboxId());
+            if (uid == null) {
+                uid = new AtomicLong(getLastUid(session, mailbox));
+                uids.put(mailbox.getMailboxId(), uid);
+            }
+            
+        }
+        return uid;
+    }
+    
+    /**
+     * Return the last used uid for the given {@link Mailbox}. This method is called in a lazy fashion. So when the first uid is needed for a {@link Mailbox}
+     * it will get called to get the last used. After that it will stored in memory and just increment there on each {@link #nextUid(MailboxSession, Mailbox)} call.
+     * 
+     * @param session
+     * @param mailbox
+     * @return lastUid
+     * @throws MailboxException
+     */
+    protected abstract long getLastUid(MailboxSession session, Mailbox<Id> mailbox) throws MailboxException;
+
+}

Modified: james/imap/trunk/store/src/main/java/org/apache/james/mailbox/store/MapperStoreMessageManager.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/store/src/main/java/org/apache/james/mailbox/store/MapperStoreMessageManager.java?rev=1041402&r1=1041401&r2=1041402&view=diff
==============================================================================
--- james/imap/trunk/store/src/main/java/org/apache/james/mailbox/store/MapperStoreMessageManager.java (original)
+++ james/imap/trunk/store/src/main/java/org/apache/james/mailbox/store/MapperStoreMessageManager.java Thu Dec  2 15:12:51 2010
@@ -24,7 +24,6 @@ import java.util.Collection;
 import java.util.Iterator;
 import java.util.List;
 import java.util.TreeSet;
-import java.util.concurrent.atomic.AtomicLong;
 
 import javax.mail.Flags;
 import javax.mail.MessagingException;
@@ -54,8 +53,8 @@ public abstract class MapperStoreMessage
     private MessageMapperFactory<Id> mapperFactory;
 
     
-    public MapperStoreMessageManager(MessageMapperFactory<Id> mapperFactory, final AtomicLong lastUid, final MailboxEventDispatcher dispatcher, final Mailbox<Id> mailbox) throws MailboxException {
-        super(lastUid, dispatcher, mailbox);
+    public MapperStoreMessageManager(MessageMapperFactory<Id> mapperFactory, final UidProvider<Id> uidProvider, final MailboxEventDispatcher dispatcher, final Mailbox<Id> mailbox) throws MailboxException {
+        super(uidProvider, dispatcher, mailbox);
         this.mapperFactory = mapperFactory;
     }
 

Modified: james/imap/trunk/store/src/main/java/org/apache/james/mailbox/store/StoreMailboxManager.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/store/src/main/java/org/apache/james/mailbox/store/StoreMailboxManager.java?rev=1041402&r1=1041401&r2=1041402&view=diff
==============================================================================
--- james/imap/trunk/store/src/main/java/org/apache/james/mailbox/store/StoreMailboxManager.java (original)
+++ james/imap/trunk/store/src/main/java/org/apache/james/mailbox/store/StoreMailboxManager.java Thu Dec  2 15:12:51 2010
@@ -24,9 +24,6 @@ import java.util.Collections;
 import java.util.List;
 import java.util.Locale;
 import java.util.Random;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ConcurrentMap;
-import java.util.concurrent.atomic.AtomicLong;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -74,15 +71,16 @@ public abstract class StoreMailboxManage
     private final static Random RANDOM = new Random();
     
     private Log log = LogFactory.getLog("org.apache.james.imap");
-    private ConcurrentMap<MailboxPath, AtomicLong> lastUids = new ConcurrentHashMap<MailboxPath, AtomicLong>();
 
     private MailboxPathLocker locker;
+
+    private UidProvider<Id> uidProvider;
     
-    public StoreMailboxManager(MailboxMapperFactory<Id> mailboxSessionMapperFactory, final Authenticator authenticator, final MailboxPathLocker locker) {
+    public StoreMailboxManager(MailboxMapperFactory<Id> mailboxSessionMapperFactory, final Authenticator authenticator, final UidProvider<Id> uidProvider,final MailboxPathLocker locker) {
         this.authenticator = authenticator;
         this.locker = locker;
         this.mailboxSessionMapperFactory = mailboxSessionMapperFactory;
-        
+        this.uidProvider = uidProvider;
         // The dispatcher need to have the delegating listener added
         dispatcher.addMailboxListener(delegatingListener);
     }
@@ -178,7 +176,7 @@ public abstract class StoreMailboxManage
      * @param mailboxRow
      * @return storeMailbox
      */
-    protected abstract StoreMessageManager<Id> createMessageManager(AtomicLong lastUid,MailboxEventDispatcher dispatcher, Mailbox<Id> mailboxRow, MailboxSession session) throws MailboxException;
+    protected abstract StoreMessageManager<Id> createMessageManager(UidProvider<Id> uidProvider,MailboxEventDispatcher dispatcher, Mailbox<Id> mailboxRow, MailboxSession session) throws MailboxException;
 
     /**
      * Create a Mailbox for the given namespace
@@ -204,29 +202,12 @@ public abstract class StoreMailboxManage
         } else {
             getLog().debug("Loaded mailbox " + mailboxPath);
             
-            // Get the lastuid for the mailbox and register the LastUidTracker to update it when messages 
-            // are added to the mailbox
-            final AtomicLong lastUid = getLastUid(mailboxPath);
-            StoreMessageManager<Id>  m = createMessageManager(lastUid, dispatcher, mailboxRow, session);
-            addListener(mailboxPath, new LastUidTracker(lastUids, session), session);
+            StoreMessageManager<Id>  m = createMessageManager(uidProvider, dispatcher, mailboxRow, session);
             return m;
         }
     }
 
     
-    /**
-     * Return the lastUid for the mailbox. 
-     * 
-     * 
-     * @param mailboxId
-     * @return lastUid
-     * 
-     * TODO: Maybe we should do something smart here and remove it from the {@link ConcurrentHashMap} once its not needed anymore
-     */
-    private AtomicLong getLastUid(MailboxPath path) {
-        lastUids.putIfAbsent(path, new AtomicLong(0));
-        return lastUids.get(path); 
-    }
     
     /*
      * (non-Javadoc)



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


Mime
View raw message