From c43d0d17468a13aaee6030d4e9f11b2def6095e1 Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Thu, 15 Aug 2013 19:24:02 +0200 Subject: [PATCH] Added infrastructure to figure out the number of unique values for a field on the atomic level and the highest number of atomic field values for all segments. This can use as a heuristic for initializing data structures. Also moved the load method from concrete classes to AbstractIndexFieldData class. --- .../fielddata/AbstractIndexFieldData.java | 35 +++++++++++++++++++ .../index/fielddata/AtomicFieldData.java | 5 +++ .../index/fielddata/IndexFieldData.java | 5 +++ .../plain/AbstractBytesIndexFieldData.java | 19 +++------- .../plain/DoubleArrayAtomicFieldData.java | 28 +++++++++++++-- .../plain/DoubleArrayIndexFieldData.java | 18 ++-------- .../plain/FSTBytesAtomicFieldData.java | 9 ++++- .../plain/FloatArrayAtomicFieldData.java | 28 +++++++++++++-- .../plain/FloatArrayIndexFieldData.java | 18 ++-------- .../GeoPointDoubleArrayAtomicFieldData.java | 28 +++++++++++++-- .../GeoPointDoubleArrayIndexFieldData.java | 18 ++-------- .../plain/PackedArrayAtomicFieldData.java | 28 +++++++++++++-- .../plain/PackedArrayIndexFieldData.java | 18 ++-------- .../plain/PagedBytesAtomicFieldData.java | 10 ++++++ .../AbstractStringFieldDataTests.java | 13 ++++++- 15 files changed, 191 insertions(+), 89 deletions(-) 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