From 613b7462997ea41ed37bd1045a7de8937e3c5ef0 Mon Sep 17 00:00:00 2001 From: Shay Banon Date: Thu, 24 Jan 2013 09:33:08 +0100 Subject: [PATCH] move field data type to simply be type and settings --- .../common/settings/ImmutableSettings.java | 4 ++ .../index/fielddata/FieldDataType.java | 44 ++++++++++++------- .../fielddata/IndexFieldDataService.java | 28 ++++-------- .../index/fielddata/ordinals/Ordinals.java | 20 ++++----- .../plain/ByteArrayIndexFieldData.java | 2 +- .../plain/ConcreteBytesRefIndexFieldData.java | 2 +- .../plain/DoubleArrayIndexFieldData.java | 2 +- .../plain/FloatArrayIndexFieldData.java | 2 +- .../GeoPointDoubleArrayIndexFieldData.java | 2 +- .../plain/IntArrayIndexFieldData.java | 2 +- .../plain/LongArrayIndexFieldData.java | 2 +- .../plain/PagedBytesIndexFieldData.java | 2 +- .../plain/ShortArrayIndexFieldData.java | 2 +- .../index/mapper/internal/UidFieldMapper.java | 3 +- .../index/percolator/PercolatorExecutor.java | 3 +- .../ConcreteBytesStringFieldDataTests.java | 4 +- .../PagedBytesStringFieldDataTests.java | 4 +- 17 files changed, 67 insertions(+), 61 deletions(-) diff --git a/src/main/java/org/elasticsearch/common/settings/ImmutableSettings.java b/src/main/java/org/elasticsearch/common/settings/ImmutableSettings.java index 5194dfaf7bd..9bd6e023f0b 100644 --- a/src/main/java/org/elasticsearch/common/settings/ImmutableSettings.java +++ b/src/main/java/org/elasticsearch/common/settings/ImmutableSettings.java @@ -360,6 +360,10 @@ public class ImmutableSettings implements Settings { } } + public static Builder builder() { + return new Builder(); + } + /** * Returns a builder to be used in order to build settings. */ diff --git a/src/main/java/org/elasticsearch/index/fielddata/FieldDataType.java b/src/main/java/org/elasticsearch/index/fielddata/FieldDataType.java index e53f13a35fb..53b32977e05 100644 --- a/src/main/java/org/elasticsearch/index/fielddata/FieldDataType.java +++ b/src/main/java/org/elasticsearch/index/fielddata/FieldDataType.java @@ -19,42 +19,54 @@ package org.elasticsearch.index.fielddata; -import com.google.common.collect.ImmutableMap; -import org.elasticsearch.common.Nullable; +import org.elasticsearch.common.settings.ImmutableSettings; +import org.elasticsearch.common.settings.Settings; /** */ public class FieldDataType { private final String type; - @Nullable - private final String format; - private final ImmutableMap options; + private final Settings settings; public FieldDataType(String type) { - this(type, null, ImmutableMap.of()); + this(type, ImmutableSettings.Builder.EMPTY_SETTINGS); } - public FieldDataType(String type, String format) { - this(type, format, ImmutableMap.of()); + public FieldDataType(String type, Settings.Builder builder) { + this(type, builder.build()); } - public FieldDataType(String type, @Nullable String format, ImmutableMap options) { + public FieldDataType(String type, Settings settings) { this.type = type; - this.format = format; - this.options = options; + this.settings = settings; } public String getType() { return this.type; } - @Nullable - public String getFormat() { - return this.format; + public Settings getSettings() { + return this.settings; } - public ImmutableMap getOptions() { - return this.options; + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + FieldDataType that = (FieldDataType) o; + + if (!settings.equals(that.settings)) return false; + if (!type.equals(that.type)) return false; + + return true; + } + + @Override + public int hashCode() { + int result = type.hashCode(); + result = 31 * result + settings.hashCode(); + return result; } } diff --git a/src/main/java/org/elasticsearch/index/fielddata/IndexFieldDataService.java b/src/main/java/org/elasticsearch/index/fielddata/IndexFieldDataService.java index e4ba0b22b48..862e25cf86c 100644 --- a/src/main/java/org/elasticsearch/index/fielddata/IndexFieldDataService.java +++ b/src/main/java/org/elasticsearch/index/fielddata/IndexFieldDataService.java @@ -119,17 +119,11 @@ public class IndexFieldDataService extends AbstractIndexComponent { fieldData = loadedFieldData.get(fieldNames.indexName()); if (fieldData == null) { IndexFieldData.Builder builder = null; - if (type.getFormat() != null) { - builder = buildersByTypeAndFormat.get(Tuple.tuple(type.getType(), type.getFormat())); - if (builder == null) { - logger.warn("failed to find format [" + type.getFormat() + "] for field [" + fieldNames.fullName() + "], will use default"); - } - } - String format = indexSettings.get("index.fielddata.type." + type.getType() + ".format", null); + String format = type.getSettings().get("format", indexSettings.get("index.fielddata.type." + type.getType() + ".format", null)); if (format != null) { - builder = buildersByTypeAndFormat.get(Tuple.tuple(type.getType(), type.getFormat())); + builder = buildersByTypeAndFormat.get(Tuple.tuple(type.getType(), format)); if (builder == null) { - logger.warn("failed to find index level type format [" + format + "] for field [" + fieldNames.fullName() + "], will use default"); + logger.warn("failed to find format [" + format + "] for field [" + fieldNames.fullName() + "], will use default"); } } if (builder == null) { @@ -140,17 +134,13 @@ public class IndexFieldDataService extends AbstractIndexComponent { } IndexFieldDataCache cache; - if (type.getOptions().containsKey("cache")) { - String cacheType = type.getOptions().get("cache"); - if ("resident".equals(cacheType)) { - cache = new IndexFieldDataCache.Resident(); - } else if ("soft".equals(cacheType)) { - cache = new IndexFieldDataCache.Soft(); - } else { - throw new ElasticSearchIllegalArgumentException("cache type not supported [" + cacheType + "] for field [" + fieldNames.fullName() + "]"); - } - } else { + String cacheType = type.getSettings().get("cache", indexSettings.get("index.fielddata.cache", "resident")); + if ("resident".equals(cacheType)) { cache = new IndexFieldDataCache.Resident(); + } else if ("soft".equals(cacheType)) { + cache = new IndexFieldDataCache.Soft(); + } else { + throw new ElasticSearchIllegalArgumentException("cache type not supported [" + cacheType + "] for field [" + fieldNames.fullName() + "]"); } fieldData = builder.build(index, indexSettings, fieldNames, type, cache); diff --git a/src/main/java/org/elasticsearch/index/fielddata/ordinals/Ordinals.java b/src/main/java/org/elasticsearch/index/fielddata/ordinals/Ordinals.java index 2c276bbb810..63e363a60a8 100644 --- a/src/main/java/org/elasticsearch/index/fielddata/ordinals/Ordinals.java +++ b/src/main/java/org/elasticsearch/index/fielddata/ordinals/Ordinals.java @@ -19,10 +19,10 @@ package org.elasticsearch.index.fielddata.ordinals; +import org.elasticsearch.ElasticSearchIllegalArgumentException; +import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.fielddata.util.IntArrayRef; -import java.util.Map; - /** * A thread safe ordinals abstraction. Ordinals can only be positive integers. */ @@ -30,18 +30,16 @@ public interface Ordinals { static class Factories { - public static Ordinals createFromFlatOrdinals(int[][] ordinals, int numOrds, Map options) { - String multiOrdinals = options.get("multi_ordinals"); + public static Ordinals createFromFlatOrdinals(int[][] ordinals, int numOrds, Settings settings) { + String multiOrdinals = settings.get("multi_ordinals", "sparse"); if ("flat".equals(multiOrdinals)) { return new MultiFlatArrayOrdinals(ordinals, numOrds); + } else if ("sparse".equals(multiOrdinals)) { + int multiOrdinalsMaxDocs = settings.getAsInt("multi_ordinals_max_docs", 16777216 /*Equal to 64MB per storage array*/); + return new SparseMultiArrayOrdinals(ordinals, numOrds, multiOrdinalsMaxDocs); + } else { + throw new ElasticSearchIllegalArgumentException("no applicable fielddata multi_ordinals value, got [" + multiOrdinals + "]"); } - int multiOrdinalsMaxDocs = 16777216; // Equal to 64MB per storage array - String multiOrdinalsMaxDocsVal = options.get("multi_ordinals_max_docs"); - if (multiOrdinalsMaxDocsVal != null) { - multiOrdinalsMaxDocs = Integer.valueOf(multiOrdinalsMaxDocsVal); - } - - return new SparseMultiArrayOrdinals(ordinals, numOrds, multiOrdinalsMaxDocs); } } diff --git a/src/main/java/org/elasticsearch/index/fielddata/plain/ByteArrayIndexFieldData.java b/src/main/java/org/elasticsearch/index/fielddata/plain/ByteArrayIndexFieldData.java index 2efd0b3123d..374087c470a 100644 --- a/src/main/java/org/elasticsearch/index/fielddata/plain/ByteArrayIndexFieldData.java +++ b/src/main/java/org/elasticsearch/index/fielddata/plain/ByteArrayIndexFieldData.java @@ -149,7 +149,7 @@ public class ByteArrayIndexFieldData extends AbstractIndexFieldData implements Internal @Override public FieldDataType fieldDataType() { - return new FieldDataType("string", "paged_bytes"); + return new FieldDataType("string", ImmutableSettings.builder().put("format", "paged_bytes")); } @Override diff --git a/src/main/java/org/elasticsearch/index/percolator/PercolatorExecutor.java b/src/main/java/org/elasticsearch/index/percolator/PercolatorExecutor.java index ef92f2e2bbb..5091204de16 100644 --- a/src/main/java/org/elasticsearch/index/percolator/PercolatorExecutor.java +++ b/src/main/java/org/elasticsearch/index/percolator/PercolatorExecutor.java @@ -36,6 +36,7 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.logging.ESLogger; import org.elasticsearch.common.lucene.Lucene; +import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ConcurrentCollections; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -386,7 +387,7 @@ public class PercolatorExecutor extends AbstractIndexComponent { this.percolatorIndex = percolatorIndex; this.matches = matches; // TODO: when we move to a UID level mapping def on the index level, we can use that one, now, its per type, and we can't easily choose one - this.uidFieldData = percolatorIndex.fieldData().getForField(new FieldMapper.Names(UidFieldMapper.NAME), new FieldDataType("string", "paged_bytes")); + this.uidFieldData = percolatorIndex.fieldData().getForField(new FieldMapper.Names(UidFieldMapper.NAME), new FieldDataType("string", ImmutableSettings.builder().put("format", "paged_bytes"))); } @Override diff --git a/src/test/java/org/elasticsearch/test/unit/index/fielddata/ConcreteBytesStringFieldDataTests.java b/src/test/java/org/elasticsearch/test/unit/index/fielddata/ConcreteBytesStringFieldDataTests.java index 4877d30ae65..6b2cb5c78c1 100644 --- a/src/test/java/org/elasticsearch/test/unit/index/fielddata/ConcreteBytesStringFieldDataTests.java +++ b/src/test/java/org/elasticsearch/test/unit/index/fielddata/ConcreteBytesStringFieldDataTests.java @@ -19,7 +19,7 @@ package org.elasticsearch.test.unit.index.fielddata; -import com.google.common.collect.ImmutableMap; +import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.index.fielddata.FieldDataType; import org.testng.annotations.Test; @@ -30,6 +30,6 @@ public class ConcreteBytesStringFieldDataTests extends StringFieldDataTests { @Override protected FieldDataType getFieldDataType() { - return new FieldDataType("string", "concrete_bytes", ImmutableMap.of()); + return new FieldDataType("string", ImmutableSettings.builder().put("format", "concrete_bytes")); } } diff --git a/src/test/java/org/elasticsearch/test/unit/index/fielddata/PagedBytesStringFieldDataTests.java b/src/test/java/org/elasticsearch/test/unit/index/fielddata/PagedBytesStringFieldDataTests.java index f95e92e389a..4bd223b8910 100644 --- a/src/test/java/org/elasticsearch/test/unit/index/fielddata/PagedBytesStringFieldDataTests.java +++ b/src/test/java/org/elasticsearch/test/unit/index/fielddata/PagedBytesStringFieldDataTests.java @@ -19,7 +19,7 @@ package org.elasticsearch.test.unit.index.fielddata; -import com.google.common.collect.ImmutableMap; +import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.index.fielddata.FieldDataType; import org.testng.annotations.Test; @@ -30,6 +30,6 @@ public class PagedBytesStringFieldDataTests extends StringFieldDataTests { @Override protected FieldDataType getFieldDataType() { - return new FieldDataType("string", "paged_bytes", ImmutableMap.of()); + return new FieldDataType("string", ImmutableSettings.builder().put("format", "paged_bytes")); } }