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.search;
029
030import org.opencms.db.CmsPublishedResource;
031import org.opencms.main.CmsLog;
032import org.opencms.search.fields.CmsSearchField;
033
034import java.io.IOException;
035
036import org.apache.commons.logging.Log;
037import org.apache.lucene.document.Document;
038import org.apache.lucene.index.IndexWriter;
039import org.apache.lucene.index.Term;
040
041/**
042 * Delegates indexing to a standard Lucene IndexWriter.<p>
043 *
044 * @since 8.0.2
045 */
046public class CmsLuceneIndexWriter implements I_CmsIndexWriter {
047
048    /** The log object for this class. */
049    protected static final Log LOG = CmsLog.getLog(CmsLuceneIndexWriter.class);
050
051    /** The OpenCms search index instance this writer to supposed to write to. */
052    private CmsSearchIndex m_index;
053
054    /** The Lucene index writer to use. */
055    private final IndexWriter m_indexWriter;
056
057    /**
058     * Creates a new index writer based on the provided standard Lucene IndexWriter.<p>
059     *
060     * @param indexWriter the standard Lucene IndexWriter to use as delegate
061     */
062    public CmsLuceneIndexWriter(IndexWriter indexWriter) {
063
064        this(indexWriter, null);
065    }
066
067    /**
068     * Creates a new index writer based on the provided standard Lucene IndexWriter for the
069     * provided OpenCms search index instance.<p>
070     *
071     * The OpenCms search instance is currently used only for improved logging of the
072     * index operations.<p>
073     *
074     * @param indexWriter the standard Lucene IndexWriter to use as delegate
075     * @param index the OpenCms search index instance this writer to supposed to write to
076     */
077    public CmsLuceneIndexWriter(IndexWriter indexWriter, CmsSearchIndex index) {
078
079        m_indexWriter = indexWriter;
080        m_index = index;
081        if ((m_index != null) && LOG.isInfoEnabled()) {
082            LOG.info(
083                Messages.get().getBundle().key(
084                    Messages.LOG_INDEX_WRITER_MSG_CREATE_2,
085                    m_index.getName(),
086                    m_index.getPath()));
087        }
088    }
089
090    /**
091     * @see org.opencms.search.I_CmsIndexWriter#close()
092     */
093    public void close() throws IOException {
094
095        if ((m_index != null) && LOG.isInfoEnabled()) {
096            LOG.info(
097                Messages.get().getBundle().key(
098                    Messages.LOG_INDEX_WRITER_MSG_CLOSE_2,
099                    m_index.getName(),
100                    m_index.getPath()));
101        }
102        m_indexWriter.close();
103    }
104
105    /**
106     * @see org.opencms.search.I_CmsIndexWriter#commit()
107     */
108    public void commit() throws IOException {
109
110        if ((m_index != null) && LOG.isInfoEnabled()) {
111            LOG.info(
112                Messages.get().getBundle().key(
113                    Messages.LOG_INDEX_WRITER_MSG_COMMIT_2,
114                    m_index.getName(),
115                    m_index.getPath()));
116        }
117        m_indexWriter.commit();
118    }
119
120    /**
121     * @see org.opencms.search.I_CmsIndexWriter#deleteDocument(org.opencms.db.CmsPublishedResource)
122     */
123    public void deleteDocument(CmsPublishedResource resource) throws IOException {
124
125        // search for an exact match on the document root path
126        Term term = new Term(CmsSearchField.FIELD_PATH, resource.getRootPath());
127        if ((m_index != null) && LOG.isDebugEnabled()) {
128            LOG.debug(
129                Messages.get().getBundle().key(
130                    Messages.LOG_INDEX_WRITER_MSG_DOC_DELETE_3,
131                    resource.getRootPath(),
132                    m_index.getName(),
133                    m_index.getPath()));
134        }
135        m_indexWriter.deleteDocuments(term);
136    }
137
138    /**
139     * @see org.opencms.search.I_CmsIndexWriter#optimize()
140     *
141     * As optimize is deprecated with Lucene 3.5, this implementation
142     * actually calls {@link IndexWriter#forceMerge(int)}.<p>
143     */
144    public void optimize() throws IOException {
145
146        if ((m_index != null) && LOG.isInfoEnabled()) {
147            LOG.info(
148                Messages.get().getBundle().key(
149                    Messages.LOG_INDEX_WRITER_MSG_OPTIMIZE_2,
150                    m_index.getName(),
151                    m_index.getPath()));
152        }
153        int oldPriority = Thread.currentThread().getPriority();
154        // we don't want the priority too low as the process should complete as fast as possible
155        Thread.currentThread().setPriority(Thread.NORM_PRIORITY / 2);
156        m_indexWriter.forceMerge(5);
157        Thread.currentThread().setPriority(oldPriority);
158    }
159
160    /**
161     * @see org.opencms.search.I_CmsIndexWriter#updateDocument(java.lang.String, org.opencms.search.I_CmsSearchDocument)
162     */
163    public void updateDocument(String rootPath, I_CmsSearchDocument document) throws IOException {
164
165        Term pathTerm = new Term(CmsSearchField.FIELD_PATH, rootPath);
166        if ((m_index != null) && LOG.isDebugEnabled()) {
167            LOG.debug(
168                Messages.get().getBundle().key(
169                    Messages.LOG_INDEX_WRITER_MSG_DOC_UPDATE_3,
170                    rootPath,
171                    m_index.getName(),
172                    m_index.getPath()));
173        }
174        m_indexWriter.updateDocument(pathTerm, (Document)document.getDocument());
175    }
176}