Github user mbaechler commented on a diff in the pull request:

    https://github.com/apache/james-project/pull/13#discussion_r43605874
  
    --- Diff: 
mailbox/tool/src/main/java/org/apache/james/mailbox/indexer/ReIndexerImpl.java 
---
    @@ -0,0 +1,138 @@
    +/****************************************************************
    + * 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.indexer;
    +
    +import org.apache.james.mailbox.MailboxManager;
    +import org.apache.james.mailbox.MailboxSession;
    +import org.apache.james.mailbox.exception.MailboxException;
    +import org.apache.james.mailbox.indexer.events.FlagsMessageEvent;
    +import org.apache.james.mailbox.indexer.events.ImpactingEventType;
    +import org.apache.james.mailbox.indexer.events.ImpactingMessageEvent;
    +import org.apache.james.mailbox.indexer.registrations.GlobalRegistration;
    +import org.apache.james.mailbox.indexer.registrations.MailboxRegistration;
    +import org.apache.james.mailbox.model.MailboxPath;
    +import org.apache.james.mailbox.model.MessageRange;
    +import org.apache.james.mailbox.store.MailboxSessionMapperFactory;
    +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.MailboxId;
    +import org.apache.james.mailbox.store.mail.model.Message;
    +import org.apache.james.mailbox.store.search.ListeningMessageSearchIndex;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.util.Collection;
    +import java.util.Iterator;
    +import java.util.List;
    +
    +/**
    + * Note about live re-indexation handling :
    + *
    + *  - Data races may arise... If you modify the stored value between the 
received event check and the index operation,
    + *  you have an inconsistent behavior.
    + *
    + *  This class is more about supporting changes in real time for future 
indexed values. If you change a flags / delete
    + *  mails for instance, you will see it in the indexed value !
    + *
    + *  Why only care about updates and deletions ? Additions are already 
handled by the indexer that behaves normaly. We
    + *  should just "adapt" our indexed value to the latest value, if any. The 
normal indexer will take care of new stuff.
    + */
    +public class ReIndexerImpl<Id extends MailboxId> implements ReIndexer {
    +
    +    private static final Logger LOGGER = 
LoggerFactory.getLogger(ReIndexerImpl.class);
    +    public static final int LIMIT = 0;
    +
    +    private final MailboxManager mailboxManager;
    +    private final ListeningMessageSearchIndex<Id> messageSearchIndex;
    +    private final MailboxSessionMapperFactory<Id> 
mailboxSessionMapperFactory;
    +
    +    public ReIndexerImpl(MailboxManager mailboxManager,
    +                         ListeningMessageSearchIndex<Id> 
messageSearchIndex,
    +                         MailboxSessionMapperFactory<Id> 
mailboxSessionMapperFactory) {
    +        this.mailboxManager = mailboxManager;
    +        this.messageSearchIndex = messageSearchIndex;
    +        this.mailboxSessionMapperFactory = mailboxSessionMapperFactory;
    +    }
    +
    +    public void reIndex(MailboxPath path) throws MailboxException {
    +        MailboxSession mailboxSession = 
mailboxManager.createSystemSession("re-indexing", LOGGER);
    +        reIndex(path, mailboxSession);
    +    }
    +
    +
    +    public synchronized void reIndex() throws MailboxException {
    +        MailboxSession mailboxSession = 
mailboxManager.createSystemSession("re-indexing", LOGGER);
    +        List<MailboxPath> mailboxPaths = 
mailboxManager.list(mailboxSession);
    +        GlobalRegistration globalRegistration = new GlobalRegistration();
    +        mailboxManager.addGlobalListener(globalRegistration, 
mailboxSession);
    +        for (MailboxPath mailboxPath : mailboxPaths) {
    +            if (globalRegistration.indexThisPath(mailboxPath)) {
    +                reIndex(mailboxPath, mailboxSession);
    +            }
    +        }
    +        mailboxManager.removeGlobalListener(globalRegistration, 
mailboxSession);
    +    }
    +
    +    private void reIndex(MailboxPath path, MailboxSession mailboxSession) 
throws MailboxException {
    +        MailboxRegistration mailboxRegistration = new 
MailboxRegistration(path);
    +        mailboxManager.addListener(path, mailboxRegistration, 
mailboxSession);
    +        LOGGER.info("Intend to reindex " + path);
    +        Mailbox<Id> mailbox = 
mailboxSessionMapperFactory.getMailboxMapper(mailboxSession).findMailboxByPath(path);
    +        messageSearchIndex.delete(mailboxSession, mailbox, 
MessageRange.all());
    +        handleIterations(mailboxSession,
    +            mailboxRegistration,
    +            mailbox,
    +            mailboxSessionMapperFactory.getMessageMapper(mailboxSession)
    +                .findInMailbox(mailbox,
    +                    MessageRange.all(),
    +                    MessageMapper.FetchType.Full,
    +                    LIMIT));
    +        mailboxManager.removeListener(path, mailboxRegistration, 
mailboxSession);
    +        LOGGER.info("Finish to reindex " + path);
    +    }
    +
    +    private void handleIterations(MailboxSession mailboxSession, 
MailboxRegistration mailboxRegistration, Mailbox<Id> mailbox, 
Iterator<Message<Id>> iterator) throws MailboxException {
    +        while (iterator.hasNext()) {
    +            Message<Id> message = iterator.next();
    +            ImpactingMessageEvent impactingMessageEvent = 
findMostRelevant(mailboxRegistration.getImpactingEvents(message.getUid()));
    +            if (impactingMessageEvent == null) {
    +                messageSearchIndex.add(mailboxSession, mailbox, message);
    +            } else if (impactingMessageEvent instanceof FlagsMessageEvent) 
{
    +                message.setFlags(((FlagsMessageEvent) 
impactingMessageEvent).getFlags());
    +                messageSearchIndex.add(mailboxSession, mailbox, message);
    +            }
    +        }
    +    }
    +
    +    private ImpactingMessageEvent 
findMostRelevant(Collection<ImpactingMessageEvent> messageEvents) {
    +        if (messageEvents == null) {
    +            return null;
    +        }
    +        ImpactingMessageEvent last = null;
    +        for (ImpactingMessageEvent impactingMessageEvent : messageEvents) {
    +            if 
(impactingMessageEvent.getType().equals(ImpactingEventType.Deletion)) {
    +                return impactingMessageEvent;
    +            }
    +            last = impactingMessageEvent;
    --- End diff --
    
    if you get a list of messageEvents, you can just split your "find deletion" 
loop from the getLast by using Iterables.getLast()


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

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

Reply via email to