Search in sources :

Example 1 with IndexModifier

use of org.apache.lucene.index.IndexModifier in project Openfire by igniterealtime.

the class ArchiveIndexer method rebuildIndex.

/**
     * Rebuilds the search index with all archived conversation data. This method returns
     * a Future that represents the status of the index rebuild process (also available
     * via {@link #getIndexRebuildProgress()}). The integer value
     * (values 0 through 100) represents the percentage of work done. If message archiving
     * is disabled, this method will return <tt>null</tt>.
     *
     * @return a Future to indicate the status of rebuilding the index or <tt>null</tt> if
     *      rebuilding the index is not possible.
     */
public synchronized Future<Integer> rebuildIndex() {
    // Immediately return if the service has been stopped.
    if (stopped) {
        return null;
    }
    // If a rebuild is already happening, return.
    if (rebuildInProgress) {
        return null;
    }
    rebuildInProgress = true;
    // Do nothing if archiving is disabled.
    if (!conversationManager.isArchivingEnabled()) {
        return null;
    }
    // Create a future to track the index rebuild progress.
    rebuildFuture = new RebuildFuture();
    // Create a runnable that will perform the actual rebuild work.
    Runnable rebuildTask = new Runnable() {

        public void run() {
            List<Long> conversationIDs = new ArrayList<Long>();
            Map<Long, Boolean> externalMetaData = new HashMap<Long, Boolean>();
            Connection con = null;
            PreparedStatement pstmt = null;
            ResultSet rs = null;
            try {
                con = DbConnectionManager.getConnection();
                pstmt = con.prepareStatement(ALL_CONVERSATIONS);
                rs = pstmt.executeQuery();
                while (rs.next()) {
                    long conversationID = rs.getLong(1);
                    conversationIDs.add(conversationID);
                    externalMetaData.put(conversationID, rs.getInt(2) == 1);
                }
            } catch (SQLException sqle) {
                Log.error(sqle.getMessage(), sqle);
            } finally {
                DbConnectionManager.closeConnection(rs, pstmt, con);
            }
            if (!conversationIDs.isEmpty()) {
                // Index the conversations.
                writerLock.lock();
                IndexModifier writer = null;
                try {
                    writer = new IndexModifier(directory, new StandardAnalyzer(), true);
                    long newestDate = indexConversations(conversationIDs, externalMetaData, writer, true);
                    writer.optimize();
                    // Done indexing so store a last modified date.
                    if (newestDate != -1) {
                        lastModified = newestDate;
                        indexProperties.setProperty("lastModified", Long.toString(lastModified));
                    }
                } catch (IOException ioe) {
                    Log.error(ioe.getMessage(), ioe);
                } finally {
                    if (writer != null) {
                        try {
                            writer.close();
                        } catch (Exception e) {
                            Log.error(e.getMessage(), e);
                        }
                    }
                    writerLock.unlock();
                }
            }
            // Done rebuilding the index, so reset state.
            rebuildFuture = null;
            rebuildInProgress = false;
        }
    };
    taskEngine.submit(rebuildTask);
    return rebuildFuture;
}
Also used : HashMap(java.util.HashMap) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) TimeoutException(java.util.concurrent.TimeoutException) SQLException(java.sql.SQLException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) IndexModifier(org.apache.lucene.index.IndexModifier) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) ResultSet(java.sql.ResultSet)

Example 2 with IndexModifier

use of org.apache.lucene.index.IndexModifier in project Openfire by igniterealtime.

the class ArchiveIndexer method updateIndex.

/**
     * Updates the search index with all new conversation data since the last index update.
     */
public void updateIndex() {
    // Immediately return if the service has been stopped.
    if (stopped) {
        return;
    }
    // Do nothing if archiving is disabled.
    if (!conversationManager.isArchivingEnabled()) {
        return;
    }
    // If we're currently rebuilding the index, return.
    if (rebuildInProgress) {
        return;
    }
    writerLock.lock();
    IndexModifier writer = null;
    try {
        writer = new IndexModifier(directory, new StandardAnalyzer(), false);
        List<Long> conversationIDs = new ArrayList<Long>();
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(NEW_CONVERSATIONS);
            pstmt.setLong(1, lastModified);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                conversationIDs.add(rs.getLong(1));
            }
        } catch (SQLException sqle) {
            Log.error(sqle.getMessage(), sqle);
        } finally {
            DbConnectionManager.closeConnection(rs, pstmt, con);
        }
        // updated since then.
        for (long conversationID : conversationIDs) {
            writer.deleteDocuments(new Term("conversationID", Long.toString(conversationID)));
        }
        // Load meta-data for each conversation.
        Map<Long, Boolean> externalMetaData = new HashMap<Long, Boolean>();
        for (long conversationID : conversationIDs) {
            try {
                con = DbConnectionManager.getConnection();
                pstmt = con.prepareStatement(CONVERSATION_METADATA);
                pstmt.setLong(1, conversationID);
                rs = pstmt.executeQuery();
                while (rs.next()) {
                    externalMetaData.put(conversationID, rs.getInt(1) == 1);
                }
            } catch (SQLException sqle) {
                Log.error(sqle.getMessage(), sqle);
            } finally {
                DbConnectionManager.closeConnection(rs, pstmt, con);
            }
        }
        // Now index all the new conversations.
        long newestDate = indexConversations(conversationIDs, externalMetaData, writer, false);
        writer.optimize();
        // Done indexing so store a last modified date.
        if (newestDate != -1) {
            lastModified = newestDate;
            indexProperties.setProperty("lastModified", Long.toString(lastModified));
        }
    } catch (IOException ioe) {
        Log.error(ioe.getMessage(), ioe);
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (Exception e) {
                Log.error(e.getMessage(), e);
            }
        }
        writerLock.unlock();
    }
}
Also used : SQLException(java.sql.SQLException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Term(org.apache.lucene.index.Term) IOException(java.io.IOException) TimeoutException(java.util.concurrent.TimeoutException) SQLException(java.sql.SQLException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) IndexModifier(org.apache.lucene.index.IndexModifier) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) ResultSet(java.sql.ResultSet)

Aggregations

IOException (java.io.IOException)2 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 ExecutionException (java.util.concurrent.ExecutionException)2 TimeoutException (java.util.concurrent.TimeoutException)2 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)2 IndexModifier (org.apache.lucene.index.IndexModifier)2 Term (org.apache.lucene.index.Term)1