Added: james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/SimpleMessageSearchIndex.java URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/SimpleMessageSearchIndex.java?rev=1133783&view=auto ============================================================================== --- james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/SimpleMessageSearchIndex.java (added) +++ james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/SimpleMessageSearchIndex.java Thu Jun 9 10:40:41 2011 @@ -0,0 +1,124 @@ +/**************************************************************** + * 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.search; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; + +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.SearchQuery.Criterion; +import org.apache.james.mailbox.SearchQuery.NumericRange; +import org.apache.james.mailbox.SearchQuery.UidCriterion; +import org.apache.james.mailbox.store.mail.MessageMapper; +import org.apache.james.mailbox.store.mail.MessageMapperFactory; +import org.apache.james.mailbox.store.mail.model.Mailbox; +import org.apache.james.mailbox.store.mail.model.Message; +import org.apache.james.mailbox.store.transaction.Mapper.MailboxMembershipCallback; + +/** + * {@link MessageSearchIndex} which just fetch {@link Message}'s from the {@link MessageMapper} and use {@link MessageSearcher} + * to match them against the {@link SearchQuery}. + * + * This works with every implementation but is SLOW. + * + * + * @param <Id> + */ +public class SimpleMessageSearchIndex<Id> implements MessageSearchIndex<Id>{ + + private final MessageMapperFactory<Id> factory; + public SimpleMessageSearchIndex(MessageMapperFactory<Id> factory) { + this.factory = factory; + } + + @Override + public Iterator<Long> search(MailboxSession session, Mailbox<Id> mailbox, SearchQuery query) throws MailboxException { + List<Criterion> crits = query.getCriterias(); + MessageMapper<Id> mapper = factory.getMessageMapper(session); + + // Ok we only search for a range so we can optimize the call + if (crits.size() == 1 && crits.get(0) instanceof UidCriterion) { + final List<Long> uids = new ArrayList<Long>(); + UidCriterion uidCrit = (UidCriterion) crits.get(0); + NumericRange[] ranges = uidCrit.getOperator().getRange(); + for (int i = 0; i < ranges.length; i++) { + NumericRange r = ranges[i]; + mapper.findInMailbox(mailbox, MessageRange.range(r.getLowValue(), r.getHighValue()), new MailboxMembershipCallback<Id>() { + + public void onMailboxMembers(List<Message<Id>> list) throws MailboxException { + for (int i = 0; i < list.size(); i++) { + long uid = list.get(i).getUid(); + if (uids.contains(uid) == false) { + uids.add(uid); + } + } + } + }); + } + Collections.sort(uids); + return uids.iterator(); + + + } else { + + final List<Message<Id>> hits = new ArrayList<Message<Id>>(); + + mapper.findInMailbox(mailbox, MessageRange.all(), new MailboxMembershipCallback<Id>() { + + public void onMailboxMembers(List<Message<Id>> list) throws MailboxException { + for (int i = 0; i < list.size(); i++) { + Message<Id> m = list.get(i); + if (hits.contains(m) == false) { + hits.add(m); + } + } + } + }); + Collections.sort(hits); + + Iterator<Message<?>> it = new Iterator<Message<?>>() { + final Iterator<Message<Id>> it = hits.iterator(); + public boolean hasNext() { + return it.hasNext(); + } + + public Message<?> next() { + return it.next(); + } + + public void remove() { + it.remove(); + } + + }; + + if (session == null) { + return new MessageSearches(it, query).iterator(); + } else { + return new MessageSearches(it, query, session.getLog()).iterator(); + } + } + } + +}
Modified: james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/lucene/LuceneMessageSearchIndex.java URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/lucene/LuceneMessageSearchIndex.java?rev=1133783&r1=1133782&r2=1133783&view=diff ============================================================================== --- james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/lucene/LuceneMessageSearchIndex.java (original) +++ james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/lucene/LuceneMessageSearchIndex.java Thu Jun 9 10:40:41 2011 @@ -53,9 +53,10 @@ import org.apache.james.mailbox.SearchQu import org.apache.james.mailbox.SearchQuery.NumericOperator; import org.apache.james.mailbox.SearchQuery.NumericRange; import org.apache.james.mailbox.SearchQuery.UidCriterion; +import org.apache.james.mailbox.store.mail.MessageMapperFactory; import org.apache.james.mailbox.store.mail.model.Mailbox; import org.apache.james.mailbox.store.mail.model.Message; -import org.apache.james.mailbox.store.search.MessageSearchIndex; +import org.apache.james.mailbox.store.search.ListeningMessageSearchIndex; import org.apache.james.mailbox.store.search.SearchUtil; import org.apache.james.mime4j.MimeException; import org.apache.james.mime4j.descriptor.BodyDescriptor; @@ -97,13 +98,13 @@ import org.apache.lucene.store.LockObtai import org.apache.lucene.util.Version; /** - * Lucene based {@link MessageSearchIndex} which offers message searching via a Lucene index + * Lucene based {@link ListeningMessageSearchIndex} which offers message searching via a Lucene index * * * @param <Id> */ -public class LuceneMessageSearchIndex<Id> implements MessageSearchIndex<Id>{ +public class LuceneMessageSearchIndex<Id> extends ListeningMessageSearchIndex<Id>{ /** * Default max query results @@ -266,17 +267,18 @@ public class LuceneMessageSearchIndex<Id private final static SortField FIRST_FROM_MAILBOX_DISPLAY_SORT_REVERSE = new SortField(FIRST_FROM_MAILBOX_DISPLAY_FIELD, SortField.LONG, true); - public LuceneMessageSearchIndex(Directory directory) throws CorruptIndexException, LockObtainFailedException, IOException { - this(directory, true); + public LuceneMessageSearchIndex(MessageMapperFactory<Id> factory, Directory directory) throws CorruptIndexException, LockObtainFailedException, IOException { + this(factory, directory, true); } - public LuceneMessageSearchIndex(Directory directory, boolean lenient) throws CorruptIndexException, LockObtainFailedException, IOException { - this(new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_31, createAnalyzer(lenient)))); + public LuceneMessageSearchIndex(MessageMapperFactory<Id> factory, Directory directory, boolean lenient) throws CorruptIndexException, LockObtainFailedException, IOException { + this(factory, new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_31, createAnalyzer(lenient)))); } - public LuceneMessageSearchIndex(IndexWriter writer) { + public LuceneMessageSearchIndex(MessageMapperFactory<Id> factory, IndexWriter writer) { + super(factory); this.writer = writer; } @@ -1036,6 +1038,7 @@ public class LuceneMessageSearchIndex<Id if (doc.getField(FLAGS_FIELD) == null) { doc.removeFields(FLAGS_FIELD); indexFlags(doc, f); + writer.updateDocument(new Term(ID_FIELD, doc.get(ID_FIELD)), doc); } @@ -1079,6 +1082,11 @@ public class LuceneMessageSearchIndex<Id for (int a = 0; a < userFlags.length; a++) { doc.add(new Field(FLAGS_FIELD, userFlags[a],Store.NO, Index.NOT_ANALYZED)); } + + // if no flags are there we just use a empty field + if (flags.length == 0 && userFlags.length == 0) { + doc.add(new Field(FLAGS_FIELD, "",Store.NO, Index.NOT_ANALYZED)); + } } /* * (non-Javadoc) Copied: james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/MailboxEventDispatcherFlagsTest.java (from r1132538, james/mailbox/trunk/api/src/test/java/org/apache/james/mailbox/util/MailboxEventDispatcherFlagsTest.java) URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/MailboxEventDispatcherFlagsTest.java?p2=james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/MailboxEventDispatcherFlagsTest.java&p1=james/mailbox/trunk/api/src/test/java/org/apache/james/mailbox/util/MailboxEventDispatcherFlagsTest.java&r1=1132538&r2=1133783&rev=1133783&view=diff ============================================================================== --- james/mailbox/trunk/api/src/test/java/org/apache/james/mailbox/util/MailboxEventDispatcherFlagsTest.java (original) +++ james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/MailboxEventDispatcherFlagsTest.java Thu Jun 9 10:40:41 2011 @@ -17,7 +17,7 @@ * under the License. * ****************************************************************/ -package org.apache.james.mailbox.util; +package org.apache.james.mailbox.store; import static org.junit.Assert.*; @@ -27,12 +27,13 @@ import java.util.Iterator; import javax.mail.Flags; import org.apache.james.mailbox.MailboxListener; -import org.apache.james.mailbox.MailboxPath; import org.apache.james.mailbox.MailboxSession; import org.apache.james.mailbox.MessageResult; import org.apache.james.mailbox.UpdatedFlags; import org.apache.james.mailbox.mock.MockMailboxSession; -import org.apache.james.mailbox.util.MailboxEventDispatcher; +import org.apache.james.mailbox.store.MailboxEventDispatcher; +import org.apache.james.mailbox.store.mail.model.Mailbox; +import org.apache.james.mailbox.util.EventCollector; import org.jmock.Expectations; import org.jmock.Mockery; import org.jmock.integration.junit4.JMock; @@ -44,7 +45,7 @@ import org.junit.runner.RunWith; @RunWith(JMock.class) public class MailboxEventDispatcherFlagsTest { - MailboxEventDispatcher dispatcher; + MailboxEventDispatcher<Long> dispatcher; EventCollector collector; @@ -62,10 +63,60 @@ public class MailboxEventDispatcherFlags private Mockery mockery = new JUnit4Mockery(); - private MailboxPath path = new MailboxPath(null, null, "test"); + private Mailbox<Long> mailbox = new Mailbox<Long>() { + + @Override + public Long getMailboxId() { + return 1L; + } + + @Override + public String getNamespace() { + return null; + } + + @Override + public void setNamespace(String namespace) { + } + + @Override + public String getUser() { + return null; + } + + @Override + public void setUser(String user) { + + } + + @Override + public String getName() { + return "test"; + } + + @Override + public void setName(String name) { + } + + @Override + public long getUidValidity() { + return 0; + } + + @Override + public long getLastKnownUid() { + return 0; + } + + @Override + public long getHighestKnownModSeq() { + return 0; + } + }; + @Before public void setUp() throws Exception { - dispatcher = new MailboxEventDispatcher(); + dispatcher = new MailboxEventDispatcher<Long>(); collector = new EventCollector(); dispatcher.addMailboxListener(collector); result = mockery.mock(MessageResult.class); @@ -77,7 +128,7 @@ public class MailboxEventDispatcherFlags @Test public void testShouldReturnNoChangesWhenSystemFlagsUnchanged() { - dispatcher.flagsUpdated(session, Arrays.asList(result.getUid()), path, Arrays.asList(new UpdatedFlags(result.getUid(), -1, new Flags( + dispatcher.flagsUpdated(session, Arrays.asList(result.getUid()), mailbox, Arrays.asList(new UpdatedFlags(result.getUid(), -1, new Flags( Flags.Flag.DELETED), new Flags(Flags.Flag.DELETED)))); assertEquals(1, collector.events.size()); assertTrue(collector.events.get(0) instanceof MailboxListener.FlagsUpdated); @@ -90,7 +141,7 @@ public class MailboxEventDispatcherFlags @Test public void testShouldShowAnsweredAdded() { - dispatcher.flagsUpdated(session, Arrays.asList(result.getUid()), path, Arrays.asList(new UpdatedFlags(result.getUid(), -1, new Flags(), + dispatcher.flagsUpdated(session, Arrays.asList(result.getUid()), mailbox, Arrays.asList(new UpdatedFlags(result.getUid(), -1, new Flags(), new Flags(Flags.Flag.ANSWERED)))); assertEquals(1, collector.events.size()); assertTrue(collector.events.get(0) instanceof MailboxListener.FlagsUpdated); @@ -105,7 +156,7 @@ public class MailboxEventDispatcherFlags @Test public void testShouldShowAnsweredRemoved() { - dispatcher.flagsUpdated(session, Arrays.asList(result.getUid()), path, Arrays.asList(new UpdatedFlags(result.getUid(), -1, new Flags( + dispatcher.flagsUpdated(session, Arrays.asList(result.getUid()), mailbox, Arrays.asList(new UpdatedFlags(result.getUid(), -1, new Flags( Flags.Flag.ANSWERED), new Flags()))); assertEquals(1, collector.events.size()); assertTrue(collector.events.get(0) instanceof MailboxListener.FlagsUpdated); @@ -120,7 +171,7 @@ public class MailboxEventDispatcherFlags @Test public void testShouldShowDeletedAdded() { - dispatcher.flagsUpdated(session, Arrays.asList(result.getUid()), path, Arrays.asList(new UpdatedFlags(result.getUid(), -1, new Flags(), + dispatcher.flagsUpdated(session, Arrays.asList(result.getUid()), mailbox, Arrays.asList(new UpdatedFlags(result.getUid(), -1, new Flags(), new Flags(Flags.Flag.DELETED)))); assertEquals(1, collector.events.size()); assertTrue(collector.events.get(0) instanceof MailboxListener.FlagsUpdated); @@ -135,7 +186,7 @@ public class MailboxEventDispatcherFlags @Test public void testShouldShowDeletedRemoved() { - dispatcher.flagsUpdated(session, Arrays.asList(result.getUid()), path, Arrays.asList(new UpdatedFlags(result.getUid(), -1, new Flags( + dispatcher.flagsUpdated(session, Arrays.asList(result.getUid()), mailbox, Arrays.asList(new UpdatedFlags(result.getUid(), -1, new Flags( Flags.Flag.DELETED), new Flags()))); assertEquals(1, collector.events.size()); assertTrue(collector.events.get(0) instanceof MailboxListener.FlagsUpdated); @@ -150,7 +201,7 @@ public class MailboxEventDispatcherFlags @Test public void testShouldShowDraftAdded() { - dispatcher.flagsUpdated(session, Arrays.asList(result.getUid()), path, Arrays.asList(new UpdatedFlags(result.getUid(), -1, new Flags(), + dispatcher.flagsUpdated(session, Arrays.asList(result.getUid()), mailbox, Arrays.asList(new UpdatedFlags(result.getUid(), -1, new Flags(), new Flags(Flags.Flag.DRAFT)))); assertEquals(1, collector.events.size()); assertTrue(collector.events.get(0) instanceof MailboxListener.FlagsUpdated); @@ -165,7 +216,7 @@ public class MailboxEventDispatcherFlags @Test public void testShouldShowDraftRemoved() { - dispatcher.flagsUpdated(session,Arrays.asList(result.getUid()), path, Arrays.asList(new UpdatedFlags(result.getUid(), -1, new Flags( + dispatcher.flagsUpdated(session,Arrays.asList(result.getUid()), mailbox, Arrays.asList(new UpdatedFlags(result.getUid(), -1, new Flags( Flags.Flag.DRAFT), new Flags()))); assertEquals(1, collector.events.size()); assertTrue(collector.events.get(0) instanceof MailboxListener.FlagsUpdated); @@ -180,7 +231,7 @@ public class MailboxEventDispatcherFlags @Test public void testShouldShowFlaggedAdded() { - dispatcher.flagsUpdated(session, Arrays.asList(result.getUid()), path, Arrays.asList(new UpdatedFlags(result.getUid(), -1, new Flags(), + dispatcher.flagsUpdated(session, Arrays.asList(result.getUid()), mailbox, Arrays.asList(new UpdatedFlags(result.getUid(), -1, new Flags(), new Flags(Flags.Flag.FLAGGED)))); assertEquals(1, collector.events.size()); assertTrue(collector.events.get(0) instanceof MailboxListener.FlagsUpdated); @@ -195,7 +246,7 @@ public class MailboxEventDispatcherFlags @Test public void testShouldShowFlaggedRemoved() { - dispatcher.flagsUpdated(session, Arrays.asList(result.getUid()), path, Arrays.asList(new UpdatedFlags(result.getUid(), -1, new Flags( + dispatcher.flagsUpdated(session, Arrays.asList(result.getUid()), mailbox, Arrays.asList(new UpdatedFlags(result.getUid(), -1, new Flags( Flags.Flag.FLAGGED), new Flags()))); assertEquals(1, collector.events.size()); assertTrue(collector.events.get(0) instanceof MailboxListener.FlagsUpdated); @@ -210,7 +261,7 @@ public class MailboxEventDispatcherFlags @Test public void testShouldShowRecentAdded() { - dispatcher.flagsUpdated(session, Arrays.asList(result.getUid()), path, Arrays.asList(new UpdatedFlags(result.getUid(), -1, new Flags(), + dispatcher.flagsUpdated(session, Arrays.asList(result.getUid()), mailbox, Arrays.asList(new UpdatedFlags(result.getUid(), -1, new Flags(), new Flags(Flags.Flag.RECENT)))); assertEquals(1, collector.events.size()); assertTrue(collector.events.get(0) instanceof MailboxListener.FlagsUpdated); @@ -225,7 +276,7 @@ public class MailboxEventDispatcherFlags @Test public void testShouldShowRecentRemoved() { - dispatcher.flagsUpdated(session, Arrays.asList(result.getUid()), path, Arrays.asList(new UpdatedFlags(result.getUid(), -1, new Flags( + dispatcher.flagsUpdated(session, Arrays.asList(result.getUid()), mailbox, Arrays.asList(new UpdatedFlags(result.getUid(), -1, new Flags( Flags.Flag.RECENT), new Flags()))); assertEquals(1, collector.events.size()); assertTrue(collector.events.get(0) instanceof MailboxListener.FlagsUpdated); @@ -240,7 +291,7 @@ public class MailboxEventDispatcherFlags @Test public void testShouldShowSeenAdded() { - dispatcher.flagsUpdated(session, Arrays.asList(result.getUid()), path, Arrays.asList(new UpdatedFlags(result.getUid(), -1, new Flags(), + dispatcher.flagsUpdated(session, Arrays.asList(result.getUid()), mailbox, Arrays.asList(new UpdatedFlags(result.getUid(), -1, new Flags(), new Flags(Flags.Flag.SEEN)))); assertEquals(1, collector.events.size()); assertTrue(collector.events.get(0) instanceof MailboxListener.FlagsUpdated); @@ -255,7 +306,7 @@ public class MailboxEventDispatcherFlags @Test public void testShouldShowSeenRemoved() { - dispatcher.flagsUpdated(session, Arrays.asList(result.getUid()), path, Arrays.asList(new UpdatedFlags(result.getUid(), -1, new Flags( + dispatcher.flagsUpdated(session, Arrays.asList(result.getUid()), mailbox, Arrays.asList(new UpdatedFlags(result.getUid(), -1, new Flags( Flags.Flag.SEEN), new Flags()))); assertEquals(1, collector.events.size()); assertTrue(collector.events.get(0) instanceof MailboxListener.FlagsUpdated); @@ -278,7 +329,7 @@ public class MailboxEventDispatcherFlags updated.add(Flags.Flag.DRAFT); updated.add(Flags.Flag.SEEN); - dispatcher.flagsUpdated(session, Arrays.asList(result.getUid()), path, Arrays.asList(new UpdatedFlags(result.getUid(), -1, originals, updated))); + dispatcher.flagsUpdated(session, Arrays.asList(result.getUid()), mailbox, Arrays.asList(new UpdatedFlags(result.getUid(), -1, originals, updated))); assertEquals(1, collector.events.size()); assertTrue(collector.events.get(0) instanceof MailboxListener.FlagsUpdated); MailboxListener.FlagsUpdated event = (MailboxListener.FlagsUpdated) collector.events Modified: james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/lucene/LuceneMessageSearchIndexTest.java URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/lucene/LuceneMessageSearchIndexTest.java?rev=1133783&r1=1133782&r2=1133783&view=diff ============================================================================== --- james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/lucene/LuceneMessageSearchIndexTest.java (original) +++ james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/lucene/LuceneMessageSearchIndexTest.java Thu Jun 9 10:40:41 2011 @@ -74,7 +74,7 @@ public class LuceneMessageSearchIndexTes @Before public void setUp() throws Exception { - index = new LuceneMessageSearchIndex<Long>(new RAMDirectory(), useLenient()); + index = new LuceneMessageSearchIndex<Long>(null, new RAMDirectory(), useLenient()); index.setEnableSuffixMatch(true); List<org.apache.james.mailbox.store.SimpleHeader> headersSubject = new ArrayList<org.apache.james.mailbox.store.SimpleHeader>(); headersSubject.add(new SimpleHeader("Subject", 1, "test")); --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org