Search in sources :

Example 1 with Cache

use of org.elasticsearch.common.cache.Cache in project elasticsearch by elastic.

the class BitsetFilterCache method getAndLoadIfNotPresent.

private BitSet getAndLoadIfNotPresent(final Query query, final LeafReaderContext context) throws IOException, ExecutionException {
    final Object coreCacheReader = context.reader().getCoreCacheKey();
    final ShardId shardId = ShardUtils.extractShardId(context.reader());
    if (// can't require it because of the percolator
    shardId != null && indexSettings.getIndex().equals(shardId.getIndex()) == false) {
        // insanity
        throw new IllegalStateException("Trying to load bit set for index " + shardId.getIndex() + " with cache of index " + indexSettings.getIndex());
    }
    Cache<Query, Value> filterToFbs = loadedFilters.computeIfAbsent(coreCacheReader, key -> {
        context.reader().addCoreClosedListener(BitsetFilterCache.this);
        return CacheBuilder.<Query, Value>builder().build();
    });
    return filterToFbs.computeIfAbsent(query, key -> {
        final IndexReaderContext topLevelContext = ReaderUtil.getTopLevelContext(context);
        final IndexSearcher searcher = new IndexSearcher(topLevelContext);
        searcher.setQueryCache(null);
        final Weight weight = searcher.createNormalizedWeight(query, false);
        Scorer s = weight.scorer(context);
        final BitSet bitSet;
        if (s == null) {
            bitSet = null;
        } else {
            bitSet = BitSet.of(s.iterator(), context.reader().maxDoc());
        }
        Value value = new Value(bitSet, shardId);
        listener.onCache(shardId, value.bitset);
        return value;
    }).bitset;
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) Query(org.apache.lucene.search.Query) ShardId(org.elasticsearch.index.shard.ShardId) ReaderUtil(org.apache.lucene.index.ReaderUtil) CacheBuilder(org.elasticsearch.common.cache.CacheBuilder) Property(org.elasticsearch.common.settings.Setting.Property) Weight(org.apache.lucene.search.Weight) BitSet(org.apache.lucene.util.BitSet) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) IndexWarmer(org.elasticsearch.index.IndexWarmer) HashSet(java.util.HashSet) BitSetProducer(org.apache.lucene.search.join.BitSetProducer) Cache(org.elasticsearch.common.cache.Cache) TimeValue(org.elasticsearch.common.unit.TimeValue) IndexSettings(org.elasticsearch.index.IndexSettings) ThreadPool(org.elasticsearch.threadpool.ThreadPool) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) ShardUtils(org.elasticsearch.index.shard.ShardUtils) BitDocIdSet(org.apache.lucene.util.BitDocIdSet) TerminationHandle(org.elasticsearch.index.IndexWarmer.TerminationHandle) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) ObjectMapper(org.elasticsearch.index.mapper.ObjectMapper) Scorer(org.apache.lucene.search.Scorer) AbstractIndexComponent(org.elasticsearch.index.AbstractIndexComponent) Setting(org.elasticsearch.common.settings.Setting) Executor(java.util.concurrent.Executor) IndexShard(org.elasticsearch.index.shard.IndexShard) Set(java.util.Set) IOException(java.io.IOException) Engine(org.elasticsearch.index.engine.Engine) Objects(java.util.Objects) ExecutionException(java.util.concurrent.ExecutionException) ExceptionsHelper(org.elasticsearch.ExceptionsHelper) MapperService(org.elasticsearch.index.mapper.MapperService) CountDownLatch(java.util.concurrent.CountDownLatch) RemovalNotification(org.elasticsearch.common.cache.RemovalNotification) Supplier(org.apache.logging.log4j.util.Supplier) Closeable(java.io.Closeable) LeafReader(org.apache.lucene.index.LeafReader) Accountable(org.apache.lucene.util.Accountable) RemovalListener(org.elasticsearch.common.cache.RemovalListener) Queries(org.elasticsearch.common.lucene.search.Queries) IndexReaderContext(org.apache.lucene.index.IndexReaderContext) IndexSearcher(org.apache.lucene.search.IndexSearcher) IndexSearcher(org.apache.lucene.search.IndexSearcher) Query(org.apache.lucene.search.Query) TimeValue(org.elasticsearch.common.unit.TimeValue) BitSet(org.apache.lucene.util.BitSet) Scorer(org.apache.lucene.search.Scorer) IndexReaderContext(org.apache.lucene.index.IndexReaderContext) Weight(org.apache.lucene.search.Weight)

Aggregations

Closeable (java.io.Closeable)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 Objects (java.util.Objects)1 Set (java.util.Set)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutionException (java.util.concurrent.ExecutionException)1 Executor (java.util.concurrent.Executor)1 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)1 Supplier (org.apache.logging.log4j.util.Supplier)1 IndexReaderContext (org.apache.lucene.index.IndexReaderContext)1 LeafReader (org.apache.lucene.index.LeafReader)1 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)1 ReaderUtil (org.apache.lucene.index.ReaderUtil)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1 Query (org.apache.lucene.search.Query)1 Scorer (org.apache.lucene.search.Scorer)1 Weight (org.apache.lucene.search.Weight)1 BitSetProducer (org.apache.lucene.search.join.BitSetProducer)1 Accountable (org.apache.lucene.util.Accountable)1