Example usage for org.apache.lucene.util CloseableThreadLocal get

List of usage examples for org.apache.lucene.util CloseableThreadLocal get

Introduction

In this page you can find the example usage for org.apache.lucene.util CloseableThreadLocal get.

Prototype

public T get() 

Source Link

Usage

From source file:org.codelibs.elasticsearch.common.lucene.uid.Versions.java

License:Apache License

private static PerThreadIDAndVersionLookup getLookupState(LeafReader reader) throws IOException {
    Object key = reader.getCoreCacheKey();
    CloseableThreadLocal<PerThreadIDAndVersionLookup> ctl = lookupStates.get(key);
    if (ctl == null) {
        // First time we are seeing this reader's core; make a
        // new CTL:
        ctl = new CloseableThreadLocal<>();
        CloseableThreadLocal<PerThreadIDAndVersionLookup> other = lookupStates.putIfAbsent(key, ctl);
        if (other == null) {
            // Our CTL won, we must remove it when the
            // core is closed:
            reader.addCoreClosedListener(removeLookupState);
        } else {/*from w ww .j  av a 2 s.co m*/
            // Another thread beat us to it: just use
            // their CTL:
            ctl = other;
        }
    }

    PerThreadIDAndVersionLookup lookupState = ctl.get();
    if (lookupState == null) {
        lookupState = new PerThreadIDAndVersionLookup(reader);
        ctl.set(lookupState);
    }

    return lookupState;
}

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 {/*w  ww. j a  v a2s  .c  om*/
            // 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;
}