Repository: james-project Updated Branches: refs/heads/master 13989f999 -> 1d5744076
MAILBOX-351 ReIndexerPerformer should internally rely on MailboxId Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/655d35b3 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/655d35b3 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/655d35b3 Branch: refs/heads/master Commit: 655d35b38d2a3ed9577f9998e54c9398c7d91740 Parents: 13989f9 Author: Benoit Tellier <btell...@linagora.com> Authored: Fri Nov 23 15:03:31 2018 +0700 Committer: Benoit Tellier <btell...@linagora.com> Committed: Thu Nov 29 10:47:49 2018 +0700 ---------------------------------------------------------------------- .../tools/indexer/ReIndexerPerformer.java | 65 ++++++--------- .../registrations/GlobalRegistration.java | 62 --------------- .../registrations/GlobalRegistrationTest.java | 83 -------------------- 3 files changed, 24 insertions(+), 186 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/655d35b3/mailbox/tools/indexer/src/main/java/org/apache/mailbox/tools/indexer/ReIndexerPerformer.java ---------------------------------------------------------------------- diff --git a/mailbox/tools/indexer/src/main/java/org/apache/mailbox/tools/indexer/ReIndexerPerformer.java b/mailbox/tools/indexer/src/main/java/org/apache/mailbox/tools/indexer/ReIndexerPerformer.java index 92de5be..b86316f 100644 --- a/mailbox/tools/indexer/src/main/java/org/apache/mailbox/tools/indexer/ReIndexerPerformer.java +++ b/mailbox/tools/indexer/src/main/java/org/apache/mailbox/tools/indexer/ReIndexerPerformer.java @@ -19,9 +19,8 @@ package org.apache.mailbox.tools.indexer; -import java.util.List; import java.util.Optional; -import java.util.function.Function; +import java.util.stream.Stream; import javax.inject.Inject; @@ -30,6 +29,7 @@ import org.apache.james.mailbox.MailboxManager; import org.apache.james.mailbox.MailboxSession; import org.apache.james.mailbox.MessageUid; import org.apache.james.mailbox.exception.MailboxException; +import org.apache.james.mailbox.model.MailboxId; import org.apache.james.mailbox.model.MailboxMetaData; import org.apache.james.mailbox.model.MailboxPath; import org.apache.james.mailbox.model.MessageRange; @@ -40,20 +40,18 @@ import org.apache.james.mailbox.store.mail.model.Mailbox; import org.apache.james.mailbox.store.mail.model.MailboxMessage; import org.apache.james.mailbox.store.search.ListeningMessageSearchIndex; import org.apache.james.task.Task; -import org.apache.james.util.OptionalUtils; import org.apache.james.util.streams.Iterators; -import org.apache.mailbox.tools.indexer.registrations.GlobalRegistration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.github.fge.lambdas.Throwing; -import com.google.common.collect.ImmutableList; public class ReIndexerPerformer { private static final Logger LOGGER = LoggerFactory.getLogger(ReIndexerPerformer.class); private static final int NO_LIMIT = 0; private static final int SINGLE_MESSAGE = 1; + private static final String RE_INDEXING = "re-indexing"; private final MailboxManager mailboxManager; private final ListeningMessageSearchIndex messageSearchIndex; @@ -70,16 +68,19 @@ public class ReIndexerPerformer { Task.Result reIndex(MailboxPath path, ReprocessingContext reprocessingContext) throws MailboxException { MailboxSession mailboxSession = mailboxManager.createSystemSession(path.getUser()); - return reIndex(path, mailboxSession, reprocessingContext); + Mailbox mailbox = mailboxSessionMapperFactory.getMailboxMapper(mailboxSession).findMailboxByPath(path); + return reIndexSingleMailbox(mailbox.getMailboxId(), reprocessingContext); } Task.Result reIndex(ReprocessingContext reprocessingContext) throws MailboxException { - MailboxSession mailboxSession = mailboxManager.createSystemSession("re-indexing"); + MailboxSession mailboxSession = mailboxManager.createSystemSession(RE_INDEXING); LOGGER.info("Starting a full reindex"); - List<MailboxPath> mailboxPaths = mailboxManager.list(mailboxSession); + Stream<MailboxId> mailboxIds = mailboxSessionMapperFactory.getMailboxMapper(mailboxSession).list() + .stream() + .map(Mailbox::getMailboxId); try { - return reIndex(mailboxPaths, mailboxSession, reprocessingContext); + return reIndex(mailboxIds, reprocessingContext); } finally { LOGGER.info("Full reindex finished"); } @@ -88,14 +89,13 @@ public class ReIndexerPerformer { Task.Result reIndex(User user, ReprocessingContext reprocessingContext) throws MailboxException { MailboxSession mailboxSession = mailboxManager.createSystemSession(user.asString()); LOGGER.info("Starting a reindex for user {}", user.asString()); - List<MailboxPath> mailboxPaths = mailboxManager.search(MailboxQuery.privateMailboxesBuilder(mailboxSession) - .build(), mailboxSession) + + Stream<MailboxId> mailboxIds = mailboxManager.search(MailboxQuery.privateMailboxesBuilder(mailboxSession).build(), mailboxSession) .stream() - .map(MailboxMetaData::getPath) - .collect(ImmutableList.toImmutableList()); + .map(MailboxMetaData::getId); try { - return reIndex(mailboxPaths, mailboxSession, reprocessingContext); + return reIndex(mailboxIds, reprocessingContext); } finally { LOGGER.info("User {} reindex finished", user.asString()); } @@ -109,21 +109,13 @@ public class ReIndexerPerformer { return handleMessageReIndexing(mailboxSession, mailbox, uid); } - private Task.Result reIndex(List<MailboxPath> mailboxPaths, MailboxSession mailboxSession, ReprocessingContext reprocessingContext) throws MailboxException { - return wrapInGlobalRegistration(mailboxSession, - globalRegistration -> handleMultiMailboxesReindexingIterations(mailboxPaths, globalRegistration, reprocessingContext)); - } - - private Task.Result handleMultiMailboxesReindexingIterations(List<MailboxPath> mailboxPaths, GlobalRegistration globalRegistration, - ReprocessingContext reprocessingContext) { - return mailboxPaths.stream() - .map(globalRegistration::getPathToIndex) - .flatMap(OptionalUtils::toStream) - .map(path -> { + private Task.Result reIndex(Stream<MailboxId> mailboxIds, ReprocessingContext reprocessingContext) { + return mailboxIds + .map(mailboxId -> { try { - return reIndex(path, reprocessingContext); + return reIndexSingleMailbox(mailboxId, reprocessingContext); } catch (Throwable e) { - LOGGER.error("Error while proceeding to full reindexing on {}", path.asString(), e); + LOGGER.error("Error while proceeding to full reindexing on mailbox with mailboxId {}", mailboxId.serialize(), e); return Task.Result.PARTIAL; } }) @@ -131,9 +123,10 @@ public class ReIndexerPerformer { .orElse(Task.Result.COMPLETED); } - private Task.Result reIndex(MailboxPath path, MailboxSession mailboxSession, ReprocessingContext reprocessingContext) throws MailboxException { - LOGGER.info("Intend to reindex {}", path.asString()); - Mailbox mailbox = mailboxSessionMapperFactory.getMailboxMapper(mailboxSession).findMailboxByPath(path); + private Task.Result reIndexSingleMailbox(MailboxId mailboxId, ReprocessingContext reprocessingContext) throws MailboxException { + LOGGER.info("Intend to reindex mailbox with mailboxId {}", mailboxId.serialize()); + MailboxSession mailboxSession = mailboxManager.createSystemSession(RE_INDEXING); + Mailbox mailbox = mailboxSessionMapperFactory.getMailboxMapper(mailboxSession).findMailboxById(mailboxId); messageSearchIndex.deleteAll(mailboxSession, mailbox); try { return Iterators.toStream( @@ -145,7 +138,7 @@ public class ReIndexerPerformer { .reduce(Task::combine) .orElse(Task.Result.COMPLETED); } finally { - LOGGER.info("Finish to reindex {}", path.asString()); + LOGGER.info("Finish to reindex mailbox with mailboxId {}", mailboxId.serialize()); } } @@ -166,14 +159,4 @@ public class ReIndexerPerformer { .findInMailbox(mailbox, MessageRange.one(mUid), MessageMapper.FetchType.Full, SINGLE_MESSAGE)) .findFirst(); } - - private <T> T wrapInGlobalRegistration(MailboxSession session, Function<GlobalRegistration, T> function) throws MailboxException { - GlobalRegistration globalRegistration = new GlobalRegistration(); - mailboxManager.addGlobalListener(globalRegistration, session); - try { - return function.apply(globalRegistration); - } finally { - mailboxManager.removeGlobalListener(globalRegistration, session); - } - } } http://git-wip-us.apache.org/repos/asf/james-project/blob/655d35b3/mailbox/tools/indexer/src/main/java/org/apache/mailbox/tools/indexer/registrations/GlobalRegistration.java ---------------------------------------------------------------------- diff --git a/mailbox/tools/indexer/src/main/java/org/apache/mailbox/tools/indexer/registrations/GlobalRegistration.java b/mailbox/tools/indexer/src/main/java/org/apache/mailbox/tools/indexer/registrations/GlobalRegistration.java deleted file mode 100644 index 1cc7764..0000000 --- a/mailbox/tools/indexer/src/main/java/org/apache/mailbox/tools/indexer/registrations/GlobalRegistration.java +++ /dev/null @@ -1,62 +0,0 @@ -/**************************************************************** - * 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.mailbox.tools.indexer.registrations; - -import java.util.Optional; -import java.util.concurrent.ConcurrentHashMap; - -import org.apache.james.mailbox.Event; -import org.apache.james.mailbox.MailboxListener; -import org.apache.james.mailbox.model.MailboxPath; - -public class GlobalRegistration implements MailboxListener { - - private final ConcurrentHashMap<MailboxPath, Boolean> isPathDeleted; - private final ConcurrentHashMap<MailboxPath, MailboxPath> nameCorrespondence; - - public GlobalRegistration() { - this.isPathDeleted = new ConcurrentHashMap<>(); - this.nameCorrespondence = new ConcurrentHashMap<>(); - } - - public Optional<MailboxPath> getPathToIndex(MailboxPath mailboxPath) { - if (isPathDeleted.get(mailboxPath) != null) { - return Optional.empty(); - } - return Optional.of( - Optional.ofNullable(nameCorrespondence.get(mailboxPath)).orElse(mailboxPath)); - } - - @Override - public ListenerType getType() { - return ListenerType.EACH_NODE; - } - - @Override - public void event(Event event) { - if (event instanceof MailboxDeletion) { - MailboxDeletion mailboxDeletion = (MailboxDeletion) event; - isPathDeleted.put(mailboxDeletion.getMailboxPath(), true); - } else if (event instanceof MailboxRenamed) { - MailboxRenamed mailboxRenamed = (MailboxRenamed) event; - nameCorrespondence.put(mailboxRenamed.getMailboxPath(), ((MailboxRenamed) event).getNewPath()); - } - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/655d35b3/mailbox/tools/indexer/src/test/java/org/apache/mailbox/tools/indexer/registrations/GlobalRegistrationTest.java ---------------------------------------------------------------------- diff --git a/mailbox/tools/indexer/src/test/java/org/apache/mailbox/tools/indexer/registrations/GlobalRegistrationTest.java b/mailbox/tools/indexer/src/test/java/org/apache/mailbox/tools/indexer/registrations/GlobalRegistrationTest.java deleted file mode 100644 index e0b06f7..0000000 --- a/mailbox/tools/indexer/src/test/java/org/apache/mailbox/tools/indexer/registrations/GlobalRegistrationTest.java +++ /dev/null @@ -1,83 +0,0 @@ -/**************************************************************** - * 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.mailbox.tools.indexer.registrations; - -import static org.assertj.core.api.Assertions.assertThat; - -import java.util.Optional; - -import org.apache.james.core.quota.QuotaCount; -import org.apache.james.core.quota.QuotaSize; -import org.apache.james.mailbox.MailboxListener; -import org.apache.james.mailbox.mock.MockMailboxSession; -import org.apache.james.mailbox.model.MailboxPath; -import org.apache.james.mailbox.model.QuotaRoot; -import org.apache.james.mailbox.store.event.EventFactory; -import org.apache.james.mailbox.store.mail.model.impl.SimpleMailbox; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -public class GlobalRegistrationTest { - public static final MailboxPath INBOX = MailboxPath.forUser("btell...@apache.org", "INBOX"); - private static final MailboxPath NEW_PATH = MailboxPath.forUser("btell...@apache.org", "INBOX.new"); - private static final int UID_VALIDITY = 45; - private static final SimpleMailbox MAILBOX = new SimpleMailbox(INBOX, UID_VALIDITY); - private static final SimpleMailbox NEW_MAILBOX = new SimpleMailbox(NEW_PATH, UID_VALIDITY); - - private GlobalRegistration globalRegistration; - private EventFactory eventFactory; - private MockMailboxSession session; - - @BeforeEach - void setUp() { - eventFactory = new EventFactory(); - session = new MockMailboxSession("test"); - globalRegistration = new GlobalRegistration(); - } - - @Test - void pathToIndexShouldNotBeChangedByDefault() { - assertThat(globalRegistration.getPathToIndex(INBOX).get()).isEqualTo(INBOX); - } - - @Test - void pathToIndexShouldNotBeChangedByAddedEvents() { - MailboxListener.MailboxEvent event = eventFactory.mailboxAdded(session, MAILBOX); - globalRegistration.event(event); - assertThat(globalRegistration.getPathToIndex(INBOX).get()).isEqualTo(INBOX); - } - - @Test - void pathToIndexShouldBeNullifiedByDeletedEvents() { - QuotaRoot quotaRoot = QuotaRoot.quotaRoot("root", Optional.empty()); - QuotaCount quotaCount = QuotaCount.count(123); - QuotaSize quotaSize = QuotaSize.size(456); - MailboxListener.MailboxEvent event = eventFactory.mailboxDeleted(session, MAILBOX, quotaRoot, quotaCount, quotaSize); - globalRegistration.event(event); - assertThat(globalRegistration.getPathToIndex(INBOX)).isEqualTo(Optional.empty()); - } - - @Test - void pathToIndexShouldBeModifiedByRenamedEvents() { - MailboxListener.MailboxEvent event = eventFactory.mailboxRenamed(session, INBOX, NEW_MAILBOX); - globalRegistration.event(event); - assertThat(globalRegistration.getPathToIndex(INBOX).get()).isEqualTo(NEW_PATH); - } -} --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org