001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.scheduler.jobs;
029
030import org.opencms.file.CmsObject;
031import org.opencms.main.OpenCms;
032import org.opencms.scheduler.I_CmsScheduledJob;
033import org.opencms.search.I_CmsIndexWriter;
034import org.opencms.search.I_CmsSearchIndex;
035import org.opencms.util.CmsStringUtil;
036
037import java.util.List;
038import java.util.Map;
039
040/**
041 * A schedulable OpenCms job that optimizes the Lucene based search indexes at runtime.<p>
042 *
043 * Only indexes which return <code>true</code> for
044 * {@link I_CmsSearchIndex#isUpdatedIncremental()} are being optimized.
045 * By default, all such indexes are optimized if this job is run.<p>
046 *
047 * Job parameters:<p>
048 * <dl>
049 * <dt><code>optimizeIndexes={comma separated list of index names}</code></dt>
050 * <dd>Specifies list of indexes to be optimized. Only the indexes in this list are being optimized.
051 * This parameter overrides an exclude list given with <code>excludeIndexes</code>.</dd>
052 * <dt><code>excludeIndexes={comma separated list of index names}</code></dt>
053 * <dd>Specifies list of indexes to be excluded from optimization.</dd>
054 * </dl>
055 *
056 * @since 8.5.0
057 */
058public class CmsSearchIndexOptimizeJob implements I_CmsScheduledJob {
059
060    /** Parameter to control which indexes are excluded from optimization. */
061    public static final String PARAM_INDEXES_EXCLUDED = "excludeIndexes";
062
063    /** Parameter to control which indexes are optimized. */
064    public static final String PARAM_INDEXES_OPTIMIZED = "optimizeIndexes";
065
066    /**
067     * @see org.opencms.scheduler.I_CmsScheduledJob#launch(CmsObject, Map)
068     */
069    public String launch(CmsObject cms, Map<String, String> parameters) throws Exception {
070
071        List<String> optimizeIndexes = null;
072        List<String> excludeIndexes = null;
073
074        String oi = parameters.get(PARAM_INDEXES_OPTIMIZED);
075        if (oi != null) {
076            optimizeIndexes = CmsStringUtil.splitAsList(oi, ',', true);
077            if (optimizeIndexes.isEmpty()) {
078                optimizeIndexes = null;
079            }
080        } else {
081            oi = parameters.get(PARAM_INDEXES_EXCLUDED);
082            if (oi != null) {
083                excludeIndexes = CmsStringUtil.splitAsList(oi, ',', true);
084                if (excludeIndexes.isEmpty()) {
085                    excludeIndexes = null;
086                }
087            }
088        }
089
090        for (I_CmsSearchIndex index : OpenCms.getSearchManager().getSearchIndexes()) {
091            if (index.isUpdatedIncremental()) {
092                // only indexes that are updated incremental need to be optimized
093
094                if (((optimizeIndexes == null) && (excludeIndexes == null))
095                    || ((optimizeIndexes != null) && optimizeIndexes.contains(index.getName()))
096                    || ((excludeIndexes != null) && !excludeIndexes.contains(index.getName()))) {
097                    // make sure index is either included or not excluded by name
098
099                    I_CmsIndexWriter writer = index.getIndexWriter(null, false);
100                    writer.optimize();
101                }
102            }
103        }
104
105        return null;
106    }
107}