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 *     Delprat Thierry
016 *     Delbosc Benoit
017 */
018
019package org.nuxeo.elasticsearch.work;
020
021import static org.nuxeo.elasticsearch.ElasticSearchConstants.REINDEX_USING_CHILDREN_TRAVERSAL_PROPERTY;
022
023import java.util.List;
024
025import org.nuxeo.ecm.core.work.api.Work;
026import org.nuxeo.ecm.core.work.api.WorkManager;
027import org.nuxeo.elasticsearch.api.ElasticSearchIndexing;
028import org.nuxeo.elasticsearch.commands.IndexingCommand;
029import org.nuxeo.runtime.api.Framework;
030
031/**
032 * Simple Indexing Worker
033 */
034public class IndexingWorker extends AbstractIndexingWorker implements Work {
035
036    private static final long serialVersionUID = -5141471452954319812L;
037
038    public IndexingWorker(String repositoryName, List<IndexingCommand> cmds) {
039        super(repositoryName, cmds);
040    }
041
042    @Override
043    public String getTitle() {
044        return " ElasticSearch indexing for docs: " + getCmdsDigest();
045    }
046
047    protected boolean needRecurse(IndexingCommand cmd) {
048        if (cmd.isRecurse()) {
049            switch (cmd.getType()) {
050                case INSERT:
051                case UPDATE:
052                case UPDATE_SECURITY:
053                    return true;
054                case DELETE:
055                    // recurse deletion is done atomically
056                    return false;
057            }
058        }
059        return false;
060    }
061
062    @Override
063    protected void doIndexingWork(ElasticSearchIndexing esi, List<IndexingCommand> cmds) {
064        esi.indexNonRecursive(cmds);
065        WorkManager wm = Framework.getLocalService(WorkManager.class);
066        for (IndexingCommand cmd : cmds) {
067            if (needRecurse(cmd)) {
068                Work subWorker;
069                boolean useChildrenWorker = Boolean.parseBoolean(Framework.getProperty(REINDEX_USING_CHILDREN_TRAVERSAL_PROPERTY,
070                        "false"));
071                if (useChildrenWorker) {
072                    subWorker = new ChildrenIndexingWorker(cmd);
073                } else {
074                    subWorker = new ScrollingIndexingWorker(cmd.getRepositoryName(), String.format(
075                            "SELECT ecm:uuid FROM Document WHERE ecm:ancestorId = '%s'", cmd.getTargetDocumentId()));
076                }
077                wm.schedule(subWorker);
078            }
079        }
080    }
081
082    public String getCmdsDigest() {
083        String ret = "";
084        for (IndexingCommand cmd : cmds) {
085            ret += " " + cmd.getId();
086        }
087        return ret;
088    }
089}