001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Thierry Delprat
016 *     Benoit Delbosc
017 */
018package org.nuxeo.elasticsearch.commands;
019
020import java.util.ArrayList;
021import java.util.HashSet;
022import java.util.List;
023import java.util.Set;
024
025import org.apache.commons.logging.Log;
026import org.apache.commons.logging.LogFactory;
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.elasticsearch.commands.IndexingCommand.Type;
029
030/*
031 * This class holds a list of indexing commands and manages de-duplication
032 */
033public class IndexingCommands {
034
035    protected final List<IndexingCommand> commands = new ArrayList<>();
036
037    protected final Set<Type> commandTypes = new HashSet<>();
038
039    protected DocumentModel targetDocument;
040
041    protected static final Log log = LogFactory.getLog(IndexingCommands.class);
042
043    protected IndexingCommands() {
044        //
045    }
046
047    public IndexingCommands(DocumentModel targetDocument) {
048        this.targetDocument = targetDocument;
049    }
050
051    public void add(Type type, boolean sync, boolean recurse) {
052        IndexingCommand cmd = new IndexingCommand(targetDocument, type, sync, recurse);
053        add(cmd);
054    }
055
056    protected IndexingCommand find(Type command) {
057        for (IndexingCommand cmd : commands) {
058            if (cmd.type == command) {
059                return cmd;
060            }
061        }
062        return null;
063    }
064
065    protected void add(IndexingCommand command) {
066        if (command == null) {
067            return;
068        }
069        if (commandTypes.contains(command.type)) {
070            IndexingCommand existing = find(command.type);
071            if (existing.merge(command)) {
072                return;
073            }
074        } else if (commandTypes.contains(Type.INSERT)) {
075            if (command.type == Type.DELETE) {
076                // index and delete in the same tx
077                clear();
078            } else if (command.isSync()) {
079                // switch to sync if possible
080                find(Type.INSERT).makeSync();
081            }
082            // we already have an index command, don't care about the new command
083            return;
084        }
085        if (command.type == Type.DELETE) {
086            // no need to keep event before delete.
087            clear();
088        }
089        commands.add(command);
090        commandTypes.add(command.type);
091    }
092
093    protected void clear() {
094        commands.clear();
095        commandTypes.clear();
096    }
097
098    public DocumentModel getTargetDocument() {
099        return targetDocument;
100    }
101
102    public boolean contains(Type command) {
103        return commandTypes.contains(command);
104    }
105
106    public List<IndexingCommand> getCommands() {
107        return commands;
108    }
109
110}