Search in sources :

Example 1 with FilterIndexReaderByStringId

use of org.alfresco.repo.search.impl.lucene.FilterIndexReaderByStringId in project alfresco-repository by Alfresco.

the class IndexInfo method getMainIndexReferenceCountingReadOnlyIndexReader.

/**
 * Get the main index reader augmented with the specified TX data As above but we add the TX data
 *
 * @param id String
 * @param deleteOnlyNodes boolean
 * @return IndexReader
 * @throws IOException
 */
public IndexReader getMainIndexReferenceCountingReadOnlyIndexReader(String id, Set<String> deletions, Set<String> containerDeletions, boolean deleteOnlyNodes) throws IOException {
    if (id == null) {
        throw new IndexerException("\"null\" is not a valid identifier for a transaction");
    }
    getReadLock();
    try {
        if (indexIsShared && !checkVersion()) {
            releaseReadLock();
            getWriteLock();
            try {
                if (mainIndexReader != null) {
                    ((ReferenceCounting) mainIndexReader).setInvalidForReuse();
                }
                mainIndexReader = null;
            } finally {
                getReadLock();
                releaseWriteLock();
            }
        }
        if (mainIndexReader == null) {
            releaseReadLock();
            getWriteLock();
            try {
                if (mainIndexReader == null) {
                    // Sync with disk image if required
                    doWithFileLock(new LockWork<Object>() {

                        public Object doWork() throws Exception {
                            return null;
                        }

                        public boolean canRetry() {
                            return true;
                        }
                    });
                    mainIndexReader = createMainIndexReader();
                }
            } finally {
                getReadLock();
                releaseWriteLock();
            }
        }
        // Combine the index delta with the main index
        // Make sure the index is written to disk
        // TODO: Should use the in memory index but we often end up forcing
        // to disk anyway.
        // Is it worth it?
        // luceneIndexer.flushPending();
        IndexReader deltaReader = buildAndRegisterDeltaReader(id);
        IndexReader reader = null;
        if ((deletions == null || deletions.size() == 0) && (containerDeletions == null || containerDeletions.size() == 0)) {
            reader = new MultiReader(new IndexReader[] { mainIndexReader, deltaReader }, false);
        } else {
            IndexReader filterReader = new FilterIndexReaderByStringId("main+id", mainIndexReader, deletions, containerDeletions, deleteOnlyNodes);
            reader = new MultiReader(new IndexReader[] { filterReader, deltaReader }, false);
            // Cancel out extra incRef made by MultiReader
            filterReader.decRef();
        }
        // The reference count would have been incremented automatically by MultiReader /
        // FilterIndexReaderByStringId
        deltaReader.decRef();
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Main index reader references = " + ((ReferenceCounting) mainIndexReader).getReferenceCount());
        }
        reader = ReferenceCountingReadOnlyIndexReaderFactory.createReader(MAIN_READER + id, reader, false, config);
        ReferenceCounting refCounting = (ReferenceCounting) reader;
        reader.incRef();
        refCounting.setInvalidForReuse();
        return reader;
    } finally {
        releaseReadLock();
    }
}
Also used : FilterIndexReaderByStringId(org.alfresco.repo.search.impl.lucene.FilterIndexReaderByStringId) MultiReader(org.apache.lucene.index.MultiReader) IndexReader(org.apache.lucene.index.IndexReader) IndexerException(org.alfresco.repo.search.IndexerException) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) FileNotFoundException(java.io.FileNotFoundException) SAXPathException(org.jaxen.saxpath.SAXPathException) IndexerException(org.alfresco.repo.search.IndexerException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException)

Example 2 with FilterIndexReaderByStringId

use of org.alfresco.repo.search.impl.lucene.FilterIndexReaderByStringId in project alfresco-repository by Alfresco.

the class IndexInfo method createMainIndexReader.

private IndexReader createMainIndexReader() throws IOException {
    IndexReader reader = null;
    IndexReader oldReader = null;
    for (String id : indexEntries.keySet()) {
        IndexEntry entry = indexEntries.get(id);
        if (entry.getStatus().isCommitted()) {
            IndexReader subReader = getReferenceCountingIndexReader(id);
            if (reader == null) {
                reader = subReader;
            } else {
                boolean oldReaderIsSubReader = oldReader == null;
                oldReader = reader;
                reader = mainIndexReaders.get(id);
                if (reader == null) {
                    if (entry.getType() == IndexType.INDEX) {
                        reader = new MultiReader(new IndexReader[] { oldReader, subReader }, false);
                    } else if (entry.getType() == IndexType.DELTA) {
                        try {
                            IndexReader filterReader = new FilterIndexReaderByStringId(id, oldReader, getDeletions(entry.getName(), INDEX_INFO_DELETIONS), getDeletions(entry.getName(), INDEX_INFO_CONTAINER_DELETIONS), entry.isDeletOnlyNodes());
                            reader = new MultiReader(new IndexReader[] { filterReader, subReader }, false);
                            // Cancel out the incRef on the filter reader
                            filterReader.decRef();
                        } catch (IOException ioe) {
                            s_logger.error("Failed building filter reader beneath " + entry.getName(), ioe);
                            throw ioe;
                        }
                    }
                    reader = ReferenceCountingReadOnlyIndexReaderFactory.createReader(id + "multi", reader, true, config);
                    mainIndexReaders.put(id, reader);
                }
            }
        }
    }
    if (reader == null) {
        reader = IndexReader.open(emptyIndex);
    } else {
        // Keep this reader open whilst it is referenced by mainIndexReaders / referenceCountingReadOnlyIndexReaders
        reader.incRef();
    }
    reader = ReferenceCountingReadOnlyIndexReaderFactory.createReader(MAIN_READER, reader, false, config);
    return reader;
}
Also used : FilterIndexReaderByStringId(org.alfresco.repo.search.impl.lucene.FilterIndexReaderByStringId) MultiReader(org.apache.lucene.index.MultiReader) IndexReader(org.apache.lucene.index.IndexReader) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)2 FilterIndexReaderByStringId (org.alfresco.repo.search.impl.lucene.FilterIndexReaderByStringId)2 IndexReader (org.apache.lucene.index.IndexReader)2 MultiReader (org.apache.lucene.index.MultiReader)2 FileNotFoundException (java.io.FileNotFoundException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)1 IndexerException (org.alfresco.repo.search.IndexerException)1 CorruptIndexException (org.apache.lucene.index.CorruptIndexException)1 SAXPathException (org.jaxen.saxpath.SAXPathException)1