diff --git a/src/main/java/org/elasticsearch/index/fielddata/AbstractIndexFieldData.java b/src/main/java/org/elasticsearch/index/fielddata/AbstractIndexFieldData.java index 77af03f40cd..3deaad3e440 100644 --- a/src/main/java/org/elasticsearch/index/fielddata/AbstractIndexFieldData.java +++ b/src/main/java/org/elasticsearch/index/fielddata/AbstractIndexFieldData.java @@ -1,17 +1,22 @@ package org.elasticsearch.index.fielddata; +import org.apache.lucene.index.AtomicReaderContext; import org.apache.lucene.index.IndexReader; +import org.elasticsearch.ElasticSearchException; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.AbstractIndexComponent; import org.elasticsearch.index.Index; import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.index.settings.IndexSettings; +import java.util.concurrent.atomic.AtomicLong; + /** */ public abstract class AbstractIndexFieldData extends AbstractIndexComponent implements IndexFieldData { private final FieldMapper.Names fieldNames; + private final AtomicLong highestUniqueValuesCount = new AtomicLong(); protected final FieldDataType fieldDataType; protected final IndexFieldDataCache cache; @@ -36,4 +41,34 @@ public abstract class AbstractIndexFieldData extends public void clear(IndexReader reader) { cache.clear(reader); } + + @Override + public long getHighestNumberOfSeenUniqueValues() { + return highestUniqueValuesCount.get(); + } + + @Override + public final FD load(AtomicReaderContext context) { + try { + FD fd = cache.load(context, this); + updateHighestSeenValuesCount(fd.getNumberUniqueValues()); + return fd; + } catch (Throwable e) { + if (e instanceof ElasticSearchException) { + throw (ElasticSearchException) e; + } else { + throw new ElasticSearchException(e.getMessage(), e); + } + } + } + + private void updateHighestSeenValuesCount(long newValuesCount) { + long current; + do { + if ((current = highestUniqueValuesCount.get()) >= newValuesCount) { + break; + } + } while (!highestUniqueValuesCount.compareAndSet(current, newValuesCount)); + } + } diff --git a/src/main/java/org/elasticsearch/index/fielddata/AtomicFieldData.java b/src/main/java/org/elasticsearch/index/fielddata/AtomicFieldData.java index d15497bd0e9..254e94b5d9c 100644 --- a/src/main/java/org/elasticsearch/index/fielddata/AtomicFieldData.java +++ b/src/main/java/org/elasticsearch/index/fielddata/AtomicFieldData.java @@ -39,6 +39,11 @@ public interface AtomicFieldData