Example usage for org.apache.lucene.index DirectoryReader openIfChanged

List of usage examples for org.apache.lucene.index DirectoryReader openIfChanged

Introduction

In this page you can find the example usage for org.apache.lucene.index DirectoryReader openIfChanged.

Prototype

public static DirectoryReader openIfChanged(DirectoryReader oldReader, IndexWriter writer) throws IOException 

Source Link

Document

Expert: If there changes (committed or not) in the IndexWriter versus what the provided reader is searching, then open and return a new IndexReader searching both committed and uncommitted changes from the writer; else, return null (though, the current implementation never returns null).

Usage

From source file:com.lucid.solr.sidecar.SidecarIndexReader.java

License:Apache License

private synchronized DirectoryReader openIfChangedInternal(IndexCommit commit, IndexWriter writer,
        boolean applyAllDeletes) throws CorruptIndexException, IOException {
    //LOG.info("SidecarIndexReader reopen:  ", new Exception());
    boolean rebuild = false;
    long modifiedTime = checkVersion();
    // check if the reader was modified
    boolean modified = false;
    DirectoryReader newMain;//w  ww .  j av a 2 s. co  m
    if (commit != null) {
        newMain = DirectoryReader.openIfChanged(main, commit);
    } else if (writer != null) { // nrt
        newMain = DirectoryReader.openIfChanged(main, writer, applyAllDeletes);
    } else {
        newMain = DirectoryReader.openIfChanged(main);
    }
    if (newMain != null) {
        // main will be closed when 'this' is closed
        rebuild = true;
    } else {
        newMain = main; // unchanged
    }

    //LOG.info(" modified=" + modified + ", rebuild=" + rebuild);
    if (modified || rebuild) {
        // must make private copy so that we don't modify the same subreaders
        // being used for concurrent searches on the existing reader.
        AtomicReader parallelReaders[] = new AtomicReader[this.parallelReaders.length];
        System.arraycopy(this.parallelReaders, 0, parallelReaders, 0, parallelReaders.length);
        if (parallel != null)
            parallel.close();
        //LOG.info(" - returning new from reopen()");
        if (!rebuild)
            main.incRef(); // passing same main, new reader from following reopen will close this
        if (rebuild && !modified && sidecarReaders != null) { // the same resources but changed main index
            //        if (sidecarReaders == null) {
            //          LOG.info(" - NRT reopen, missing sidecar data, single main index");
            //          return new SidecarIndexReader(factory, newMain, null, getSequentialSubReaders(newMain),
            //                  boostData, sidecarDir);
            //        }
            // try incremental nrt reopen
            AtomicReader[] newReaders = getSequentialSubReaders(newMain);
            if (writer != null) { // looks like nrt open - check it
                // there can be no new ones except maybe the last one
                Map<String, AtomicReader> newReadersMap = new HashMap<String, AtomicReader>();
                for (AtomicReader ar : newReaders) {
                    newReadersMap.put(ar.getCombinedCoreAndDeletesKey().toString(), ar);
                }
                for (int i = 0; i < mainReaders.length; i++) {
                    if (!newReadersMap.containsKey(mainReaders[i].getCombinedCoreAndDeletesKey().toString())) {
                        // this segment was dropped
                        //parallelReaders[i].close();
                        parallelReaders[i] = null;
                    } else {
                        // refresh this parallel reader
                        AtomicReader newReader = newReadersMap
                                .remove(mainReaders[i].getCombinedCoreAndDeletesKey().toString());
                        // verify that we have sidecar readers and that they span this segment too
                        // - the last segments could have originated from NRT
                        if (sidecarReaders != null && i < sidecarReaders.length) {
                            //sidecarReaders[i].incRef();
                            //parallelReaders[i].close();
                            parallelReaders[i] = new ParallelAtomicReader(newReader, sidecarReaders[i]);
                        } else {
                            parallelReaders[i] = newReader;
                        }
                    }
                }
                if (newReadersMap.size() <= 1) { // an nrt
                    List<AtomicReader> newParallelReaders = new ArrayList<AtomicReader>(newReaders.length);
                    for (int i = 0; i < parallelReaders.length; i++) {
                        if (parallelReaders[i] != null) { // refresh and add a parallel reader
                            newParallelReaders.add(parallelReaders[i]);
                        }
                    }
                    // and now simply add the new left-over nrt readers
                    //new Exception("### newReadersMap: " + newReadersMap).printStackTrace();
                    newParallelReaders.addAll(newReadersMap.values());
                    SidecarIndexReader newCIR = new SidecarIndexReader(factory, newMain, sidecarReaders,
                            (AtomicReader[]) newParallelReaders.toArray(new AtomicReader[0]), boostData,
                            sidecarDir);
                    LOG.info("Opened new NRT SidecarIndexReader");
                    return newCIR;
                }
            }
        }
        // else do a full rebuild
        LOG.info(" - full rebuild from reopen()");
        return factory.reopen(newMain, rebuild);
    } else {
        LOG.info(" - returning old from reopen()");
        return null;
    }
}

From source file:io.druid.extension.lucene.LuceneDruidSegment.java

License:Apache License

/**
 * Refreshes the internal index reader on a scheduled thread. This is a
 * controlled thread so it is ok to block this thread to make sure things are
 * in a good state./*from   w  w  w  .  j  a  v  a2 s . c o  m*/
 *
 * @throws IOException
 */
public void refreshRealtimeReader() throws IOException {
    synchronized (refreshLock) {
        if (!isOpen) {
            return;
        }
        if (ramWriter != null) {
            if (realtimeReader == null) {
                realtimeReader = DirectoryReader.open(ramWriter);
            } else {
                DirectoryReader newReader = DirectoryReader.openIfChanged(realtimeReader, ramWriter);
                if (newReader != null) {
                    DirectoryReader tmpReader = realtimeReader;
                    realtimeReader = newReader;
                    tmpReader.close();
                }
            }
        }
    }
}

From source file:org.neo4j.index.impl.lucene.legacy.WritableIndexReferenceFactory.java

License:Open Source License

/**
 * If nothing has changed underneath (since the searcher was last created
 * or refreshed) {@code searcher} is returned. But if something has changed a
 * refreshed searcher is returned. It makes use if the
 * {@link DirectoryReader#openIfChanged(DirectoryReader, IndexWriter, boolean)} which faster than opening an index
 * from/*from w w  w .j  a  v a 2s  .c  o  m*/
 * scratch.
 *
 * @param indexReference the {@link IndexReference} to refresh.
 * @return a refreshed version of the searcher or, if nothing has changed,
 *         {@code null}.
 * @throws RuntimeException if there's a problem with the index.
 */
@Override
IndexReference refresh(IndexReference indexReference) {
    try {
        DirectoryReader reader = (DirectoryReader) indexReference.getSearcher().getIndexReader();
        IndexWriter writer = indexReference.getWriter();
        IndexReader reopened = DirectoryReader.openIfChanged(reader, writer);
        if (reopened != null) {
            IndexSearcher newSearcher = newIndexSearcher(indexReference.getIdentifier(), reopened);
            indexReference.detachOrClose();
            return new WritableIndexReference(indexReference.getIdentifier(), newSearcher, writer);
        }
        return indexReference;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}