Skip to content

Commit

Permalink
Extract if statement into ModelUtil method
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Bogan <[email protected]>
  • Loading branch information
ryanbogan committed May 7, 2024
1 parent 92de7da commit ec060c0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
8 changes: 2 additions & 6 deletions src/main/java/org/opensearch/knn/indices/ModelMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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");
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/opensearch/knn/indices/ModelUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.");
}
Expand Down

0 comments on commit ec060c0

Please sign in to comment.