From be7e9c76face198a60aae24c863b39e22e7e6580 Mon Sep 17 00:00:00 2001 From: Yury-Fridlyand Date: Thu, 16 Feb 2023 17:28:47 -0800 Subject: [PATCH] Statically init instances of `OpenSearchDataType`. Signed-off-by: Yury-Fridlyand --- .../opensearch/data/type/OpenSearchDataType.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/opensearch/src/main/java/org/opensearch/sql/opensearch/data/type/OpenSearchDataType.java b/opensearch/src/main/java/org/opensearch/sql/opensearch/data/type/OpenSearchDataType.java index 07c86832b9..2fda12a567 100644 --- a/opensearch/src/main/java/org/opensearch/sql/opensearch/data/type/OpenSearchDataType.java +++ b/opensearch/src/main/java/org/opensearch/sql/opensearch/data/type/OpenSearchDataType.java @@ -14,6 +14,7 @@ import java.util.function.BiConsumer; import lombok.EqualsAndHashCode; import lombok.Getter; +import org.apache.commons.lang3.EnumUtils; import org.opensearch.sql.data.type.ExprCoreType; import org.opensearch.sql.data.type.ExprType; @@ -88,6 +89,14 @@ public ExprType getExprType() { */ private static final Map instances = new HashMap<>(); + static { + EnumUtils.getEnumList(MappingType.class).stream() + .filter(t -> t != MappingType.Invalid).forEach(t -> + instances.put(t.toString(), OpenSearchDataType.of(t))); + EnumUtils.getEnumList(ExprCoreType.class).forEach(t -> + instances.put(t.toString(), OpenSearchDataType.of(t))); + } + /** * A constructor function which builds proper `OpenSearchDataType` for given mapping `Type`. * @param mappingType A mapping type. @@ -112,7 +121,6 @@ public static OpenSearchDataType of(MappingType mappingType) { } res = new OpenSearchDataType(mappingType); res.exprCoreType = exprCoreType; - instances.put(mappingType.toString(), res); return res; } @@ -154,9 +162,7 @@ public static OpenSearchDataType of(ExprType type) { if (res != null) { return res; } - res = new OpenSearchDataType((ExprCoreType) type); - instances.put(type.toString(), res); - return res; + return new OpenSearchDataType((ExprCoreType) type); } protected OpenSearchDataType(ExprCoreType type) {