From ec060c0428662795335c31214c8a50cf9ec3c4ff Mon Sep 17 00:00:00 2001 From: Ryan Bogan Date: Tue, 7 May 2024 11:57:22 -0700 Subject: [PATCH] Extract if statement into ModelUtil method Signed-off-by: Ryan Bogan --- .../java/org/opensearch/knn/indices/ModelMetadata.java | 8 ++------ src/main/java/org/opensearch/knn/indices/ModelUtil.java | 6 ++++++ .../opensearch/knn/plugin/rest/RestTrainModelHandler.java | 5 ++--- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/opensearch/knn/indices/ModelMetadata.java b/src/main/java/org/opensearch/knn/indices/ModelMetadata.java index f749e3210..f3a5506cd 100644 --- a/src/main/java/org/opensearch/knn/indices/ModelMetadata.java +++ b/src/main/java/org/opensearch/knn/indices/ModelMetadata.java @@ -67,9 +67,7 @@ public ModelMetadata(StreamInput in) throws IOException { // Description and error may be empty. However, reading the string will work as long as they are not null // which is checked in constructor and setters this.description = in.readString(); - if (this.description.contains(",")) { - throw new IllegalArgumentException("Model description cannot contain any commas: ','."); - } + ModelUtil.blockCommasInModelDescription(this.description); this.error = in.readString(); if (IndexUtil.isVersionOnOrAfterMinRequiredVersion(in.getVersion(), IndexUtil.MODEL_NODE_ASSIGNMENT_KEY)) { @@ -126,9 +124,7 @@ public ModelMetadata( this.state = new AtomicReference<>(Objects.requireNonNull(modelState, "modelState must not be null")); this.timestamp = Objects.requireNonNull(timestamp, "timestamp must not be null"); this.description = Objects.requireNonNull(description, "description must not be null"); - if (this.description.contains(",")) { - throw new IllegalArgumentException("Model description cannot contain any commas: ','"); - } + ModelUtil.blockCommasInModelDescription(this.description); this.error = Objects.requireNonNull(error, "error must not be null"); this.trainingNodeAssignment = Objects.requireNonNull(trainingNodeAssignment, "node assignment must not be null"); this.methodComponentContext = Objects.requireNonNull(methodComponentContext, "method context must not be null"); diff --git a/src/main/java/org/opensearch/knn/indices/ModelUtil.java b/src/main/java/org/opensearch/knn/indices/ModelUtil.java index 3daaed138..4c6230a46 100644 --- a/src/main/java/org/opensearch/knn/indices/ModelUtil.java +++ b/src/main/java/org/opensearch/knn/indices/ModelUtil.java @@ -16,6 +16,12 @@ */ public class ModelUtil { + public static void blockCommasInModelDescription(String description) { + if (description.contains(",")) { + throw new IllegalArgumentException("Model description cannot contain any commas: ','"); + } + } + public static boolean isModelPresent(ModelMetadata modelMetadata) { return modelMetadata != null; } diff --git a/src/main/java/org/opensearch/knn/plugin/rest/RestTrainModelHandler.java b/src/main/java/org/opensearch/knn/plugin/rest/RestTrainModelHandler.java index 5ff978575..ebbd7fa9b 100644 --- a/src/main/java/org/opensearch/knn/plugin/rest/RestTrainModelHandler.java +++ b/src/main/java/org/opensearch/knn/plugin/rest/RestTrainModelHandler.java @@ -16,6 +16,7 @@ import org.opensearch.core.xcontent.XContentParser; import org.opensearch.index.mapper.NumberFieldMapper; import org.opensearch.knn.index.KNNMethodContext; +import org.opensearch.knn.indices.ModelUtil; import org.opensearch.knn.plugin.KNNPlugin; import org.opensearch.knn.plugin.transport.TrainingJobRouterAction; import org.opensearch.knn.plugin.transport.TrainingModelRequest; @@ -104,9 +105,7 @@ private TrainingModelRequest createTransportRequest(RestRequest restRequest) thr searchSize = (Integer) NumberFieldMapper.NumberType.INTEGER.parse(parser.objectBytes(), false); } else if (MODEL_DESCRIPTION.equals(fieldName) && ensureNotSet(fieldName, description)) { description = parser.textOrNull(); - if (description.contains(",")) { - throw new IllegalArgumentException("Model description cannot contain any commas: ','"); - } + ModelUtil.blockCommasInModelDescription(description); } else { throw new IllegalArgumentException("Unable to parse token. \"" + fieldName + "\" is not a valid " + "parameter."); }