Java 类org.apache.lucene.search.ControlledRealTimeReopenThread 实例源码

项目:flow    文件:LuceneFiler.java   
public LuceneFiler(@Nonnull Filer delegate, @Nonnull Config config) throws IOException {
    super(delegate);

    String path = config.getString("index.path");
    maxAge = config.getTime("index.maxAge", "-1");
    double maxMergeMb = config.getDouble("index.maxMergeMb", 4);
    double maxCachedMb = config.getDouble("index.maxCacheMb", 64);
    long targetMaxStale = config.getTime("index.targetMaxStale", "5s");
    long targetMinStale = config.getTime("index.targetMinStale", "1s");

    Directory dir = FSDirectory.open(new File(path).toPath());
    NRTCachingDirectory cachingDir = new NRTCachingDirectory(dir, maxMergeMb, maxCachedMb);
    IndexWriterConfig writerConfig = new IndexWriterConfig(null);
    writerConfig.setOpenMode(OpenMode.CREATE_OR_APPEND);

    writer = new TrackingIndexWriter(new IndexWriter(cachingDir, writerConfig));
    manager = new SearcherManager(writer.getIndexWriter(), true, new SearcherFactory());
    thread = new ControlledRealTimeReopenThread<>(writer, manager, targetMaxStale, targetMinStale);
    thread.start();
}
项目:r01fb    文件:LuceneIndex.java   
/**
 * Constructor based on an instance of the type responsible of the lucene index persistence
 */
@Inject
public LuceneIndex(final Directory luceneDirectory,
                   final Analyzer analyzer) {
    try {
        // [1]: Create the indexWriter
        _indexWriter = new IndexWriter(luceneDirectory,
                                       new IndexWriterConfig(LuceneConstants.VERSION,
                                                             analyzer));

        // [2a]: Create the TrackingIndexWriter to track changes to the delegated previously created IndexWriter 
        _trackingIndexWriter = new TrackingIndexWriter(_indexWriter);

        // [2b]: Create an IndexSearcher ReferenceManager to safely share IndexSearcher instances across
        //       multiple threads
        _indexSearcherReferenceManager = new SearcherManager(_indexWriter,
                                                             true,
                                                             null);

        // [3]: Create the ControlledRealTimeReopenThread that reopens the index periodically having into 
        //      account the changes made to the index and tracked by the TrackingIndexWriter instance
        //      The index is refreshed every 60sc when nobody is waiting 
        //      and every 100 millis whenever is someone waiting (see search method)
        //      (see http://lucene.apache.org/core/4_3_0/core/org/apache/lucene/search/NRTManagerReopenThread.html)
        _indexSearcherReopenThread = new ControlledRealTimeReopenThread<IndexSearcher>(_trackingIndexWriter,
                                                                                       _indexSearcherReferenceManager,
                                                                                       60.00,   // when there is nobody waiting
                                                                                       0.1);    // when there is someone waiting
        _indexSearcherReopenThread.start(); // start the refresher thread
    } catch (IOException ioEx) {
        throw new IllegalStateException("Lucene index could not be created: " + ioEx.getMessage());
    }
}
项目:cassandra-fhir-index    文件:LuceneService.java   
/**
 * Builds a new {@link FSIndex}.
 *
 * @param name
 *            the index name
 * @param mbeanName
 *            the JMX MBean object name
 * @param path
 *            the directory path
 * @param analyzer
 *            the index writer analyzer
 * @param refresh
 *            the index reader refresh frequency in seconds
 * @param ramBufferMB
 *            the index writer RAM buffer size in MB
 * @param maxMergeMB
 *            the directory max merge size in MB
 * @param maxCachedMB
 *            the directory max cache size in MB
 * @param refreshTask
 *            action to be done during refresh
 */
public void init(String name, String mbeanName, Path path, Analyzer analyzer, double refresh, int ramBufferMB,
        int maxMergeMB, int maxCachedMB, Runnable refreshTask) {
    try {

        this.path = path;
        this.name = name;

        // Open or create directory
        FSDirectory fsDirectory = FSDirectory.open(path);
        this.directory = new NRTCachingDirectory(fsDirectory, maxMergeMB, maxCachedMB);

        // Setup index writer
        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
        indexWriterConfig.setRAMBufferSizeMB(ramBufferMB);
        indexWriterConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
        indexWriterConfig.setUseCompoundFile(true);
        indexWriterConfig.setMergePolicy(new TieredMergePolicy());
        this.indexWriter = new IndexWriter(this.directory, indexWriterConfig);

        // Setup NRT search
        SearcherFactory searcherFactory = new SearcherFactory() {
            @Override
            public IndexSearcher newSearcher(IndexReader reader, IndexReader previousReader) {
                if (refreshTask != null) {
                    refreshTask.run();
                }
                IndexSearcher searcher = new IndexSearcher(reader);
                searcher.setSimilarity(new NoIDFSimilarity());
                return searcher;
            }
        };
        TrackingIndexWriter trackingWriter = new TrackingIndexWriter(this.indexWriter);
        this.searcherManager = new SearcherManager(this.indexWriter, true, searcherFactory);
        this.searcherReopener = new ControlledRealTimeReopenThread<>(trackingWriter, this.searcherManager, refresh,
                refresh);
        this.searcherReopener.start();

        // Register JMX MBean
        // mbean = new ObjectName(mbeanName);
        // ManagementFactory.getPlatformMBeanServer().registerMBean(service,
        // this.mbean);

    } catch (Exception e) {
        throw new FhirIndexException(e, "Error while creating index %s", name);
    }
}