Example usage for org.apache.lucene.index IndexReader getReaderCacheHelper

List of usage examples for org.apache.lucene.index IndexReader getReaderCacheHelper

Introduction

In this page you can find the example usage for org.apache.lucene.index IndexReader getReaderCacheHelper.

Prototype

public abstract CacheHelper getReaderCacheHelper();

Source Link

Document

Optional method: Return a CacheHelper that can be used to cache based on the content of this reader.

Usage

From source file:org.elasticsearch.common.lucene.uid.VersionsAndSeqNoResolver.java

License:Apache License

private static PerThreadIDVersionAndSeqNoLookup[] getLookupState(IndexReader reader, String uidField)
        throws IOException {
    // We cache on the top level
    // This means cache entries have a shorter lifetime, maybe as low as 1s with the
    // default refresh interval and a steady indexing rate, but on the other hand it
    // proved to be cheaper than having to perform a CHM and a TL get for every segment.
    // See https://github.com/elastic/elasticsearch/pull/19856.
    IndexReader.CacheHelper cacheHelper = reader.getReaderCacheHelper();
    CloseableThreadLocal<PerThreadIDVersionAndSeqNoLookup[]> ctl = lookupStates.get(cacheHelper.getKey());
    if (ctl == null) {
        // First time we are seeing this reader's core; make a new CTL:
        ctl = new CloseableThreadLocal<>();
        CloseableThreadLocal<PerThreadIDVersionAndSeqNoLookup[]> other = lookupStates
                .putIfAbsent(cacheHelper.getKey(), ctl);
        if (other == null) {
            // Our CTL won, we must remove it when the reader is closed:
            cacheHelper.addClosedListener(removeLookupState);
        } else {//from w  w w . jav  a  2  s . co m
            // Another thread beat us to it: just use their CTL:
            ctl = other;
        }
    }

    PerThreadIDVersionAndSeqNoLookup[] lookupState = ctl.get();
    if (lookupState == null) {
        lookupState = new PerThreadIDVersionAndSeqNoLookup[reader.leaves().size()];
        for (LeafReaderContext leaf : reader.leaves()) {
            lookupState[leaf.ord] = new PerThreadIDVersionAndSeqNoLookup(leaf.reader(), uidField);
        }
        ctl.set(lookupState);
    }

    if (lookupState.length != reader.leaves().size()) {
        throw new AssertionError(
                "Mismatched numbers of leaves: " + lookupState.length + " != " + reader.leaves().size());
    }

    if (lookupState.length > 0 && Objects.equals(lookupState[0].uidField, uidField) == false) {
        throw new AssertionError("Index does not consistently use the same uid field: [" + uidField + "] != ["
                + lookupState[0].uidField + "]");
    }

    return lookupState;
}