001/* 002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Delprat Thierry 018 * Delbosc Benoit 019 */ 020 021package org.nuxeo.elasticsearch.work; 022 023import org.nuxeo.ecm.core.work.api.Work; 024import org.nuxeo.ecm.core.work.api.WorkManager; 025import org.nuxeo.elasticsearch.api.ElasticSearchIndexing; 026import org.nuxeo.elasticsearch.commands.IndexingCommand; 027import org.nuxeo.elasticsearch.commands.IndexingCommand.Type; 028import org.nuxeo.runtime.api.Framework; 029 030import java.util.List; 031 032import static org.nuxeo.elasticsearch.ElasticSearchConstants.REINDEX_USING_CHILDREN_TRAVERSAL_PROPERTY; 033 034/** 035 * Simple Indexing Worker 036 */ 037public class IndexingWorker extends AbstractIndexingWorker implements Work { 038 039 private static final long serialVersionUID = -5141471452954319812L; 040 041 public IndexingWorker(String repositoryName, List<IndexingCommand> cmds) { 042 super(repositoryName, cmds); 043 } 044 045 @Override 046 public String getTitle() { 047 return " ElasticSearch indexing for docs: " + getCmdsDigest(); 048 } 049 050 protected boolean needRecurse(IndexingCommand cmd) { 051 if (cmd.isRecurse()) { 052 switch (cmd.getType()) { 053 case INSERT: 054 case UPDATE: 055 case UPDATE_SECURITY: 056 case UPDATE_DIRECT_CHILDREN: 057 return true; 058 case DELETE: 059 // recurse deletion is done atomically 060 return false; 061 } 062 } 063 return false; 064 } 065 066 @Override 067 protected void doIndexingWork(ElasticSearchIndexing esi, List<IndexingCommand> cmds) { 068 esi.indexNonRecursive(cmds); 069 WorkManager wm = Framework.getLocalService(WorkManager.class); 070 for (IndexingCommand cmd : cmds) { 071 if (needRecurse(cmd)) { 072 wm.schedule(getWorker(cmd)); 073 } 074 } 075 } 076 077 private Work getWorker(IndexingCommand cmd) { 078 Work ret; 079 if (cmd.getType() == Type.UPDATE_DIRECT_CHILDREN) { 080 ret = new ScrollingIndexingWorker(cmd.getRepositoryName(), String.format( 081 "SELECT ecm:uuid FROM Document WHERE ecm:parentId = '%s'", cmd.getTargetDocumentId())); 082 } else { 083 boolean useChildrenWorker = Boolean.parseBoolean(Framework.getProperty(REINDEX_USING_CHILDREN_TRAVERSAL_PROPERTY, 084 "false")); 085 if (useChildrenWorker) { 086 ret = new ChildrenIndexingWorker(cmd); 087 } else { 088 ret = new ScrollingIndexingWorker(cmd.getRepositoryName(), String.format( 089 "SELECT ecm:uuid FROM Document WHERE ecm:ancestorId = '%s'", cmd.getTargetDocumentId())); 090 } 091 } 092 return ret; 093 } 094 095 public String getCmdsDigest() { 096 String ret = ""; 097 for (IndexingCommand cmd : cmds) { 098 ret += " " + cmd.getId(); 099 } 100 return ret; 101 } 102}