|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.ml.common.transport.batch; |
| 7 | + |
| 8 | +import static org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken; |
| 9 | +import static org.opensearch.ml.common.utils.StringUtils.getOrderedMap; |
| 10 | + |
| 11 | +import java.io.IOException; |
| 12 | +import java.util.HashMap; |
| 13 | +import java.util.Map; |
| 14 | + |
| 15 | +import org.opensearch.core.common.io.stream.StreamInput; |
| 16 | +import org.opensearch.core.common.io.stream.StreamOutput; |
| 17 | +import org.opensearch.core.common.io.stream.Writeable; |
| 18 | +import org.opensearch.core.xcontent.ToXContentObject; |
| 19 | +import org.opensearch.core.xcontent.XContentBuilder; |
| 20 | +import org.opensearch.core.xcontent.XContentParser; |
| 21 | + |
| 22 | +import lombok.Builder; |
| 23 | +import lombok.Getter; |
| 24 | + |
| 25 | +/** |
| 26 | + * ML batch ingestion data: index, field mapping and input and out files. |
| 27 | + */ |
| 28 | +public class MLBatchIngestionInput implements ToXContentObject, Writeable { |
| 29 | + |
| 30 | + public static final String INDEX_NAME_FIELD = "index_name"; |
| 31 | + public static final String TEXT_EMBEDDING_FIELD_MAP_FIELD = "text_embedding_field_map"; |
| 32 | + public static final String DATA_SOURCE_FIELD = "data_source"; |
| 33 | + public static final String CONNECTOR_CREDENTIAL_FIELD = "credential"; |
| 34 | + @Getter |
| 35 | + private String indexName; |
| 36 | + @Getter |
| 37 | + private Map<String, String> fieldMapping; |
| 38 | + @Getter |
| 39 | + private Map<String, String> dataSources; |
| 40 | + @Getter |
| 41 | + private Map<String, String> credential; |
| 42 | + |
| 43 | + @Builder(toBuilder = true) |
| 44 | + public MLBatchIngestionInput( |
| 45 | + String indexName, |
| 46 | + Map<String, String> fieldMapping, |
| 47 | + Map<String, String> dataSources, |
| 48 | + Map<String, String> credential |
| 49 | + ) { |
| 50 | + this.indexName = indexName; |
| 51 | + this.fieldMapping = fieldMapping; |
| 52 | + this.dataSources = dataSources; |
| 53 | + this.credential = credential; |
| 54 | + } |
| 55 | + |
| 56 | + public static MLBatchIngestionInput parse(XContentParser parser) throws IOException { |
| 57 | + String indexName = null; |
| 58 | + Map<String, String> fieldMapping = null; |
| 59 | + Map<String, String> dataSources = null; |
| 60 | + Map<String, String> credential = new HashMap<>(); |
| 61 | + |
| 62 | + ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser); |
| 63 | + while (parser.nextToken() != XContentParser.Token.END_OBJECT) { |
| 64 | + String fieldName = parser.currentName(); |
| 65 | + parser.nextToken(); |
| 66 | + |
| 67 | + switch (fieldName) { |
| 68 | + case INDEX_NAME_FIELD: |
| 69 | + indexName = parser.text(); |
| 70 | + break; |
| 71 | + case TEXT_EMBEDDING_FIELD_MAP_FIELD: |
| 72 | + fieldMapping = getOrderedMap(parser.mapOrdered()); |
| 73 | + break; |
| 74 | + case CONNECTOR_CREDENTIAL_FIELD: |
| 75 | + credential = parser.mapStrings(); |
| 76 | + break; |
| 77 | + case DATA_SOURCE_FIELD: |
| 78 | + dataSources = parser.mapStrings(); |
| 79 | + break; |
| 80 | + default: |
| 81 | + parser.skipChildren(); |
| 82 | + break; |
| 83 | + } |
| 84 | + } |
| 85 | + return new MLBatchIngestionInput(indexName, fieldMapping, dataSources, credential); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 90 | + builder.startObject(); |
| 91 | + if (indexName != null) { |
| 92 | + builder.field(INDEX_NAME_FIELD, indexName); |
| 93 | + } |
| 94 | + if (fieldMapping != null) { |
| 95 | + builder.field(TEXT_EMBEDDING_FIELD_MAP_FIELD, fieldMapping); |
| 96 | + } |
| 97 | + if (dataSources != null) { |
| 98 | + builder.field(DATA_SOURCE_FIELD, dataSources); |
| 99 | + } |
| 100 | + if (credential != null) { |
| 101 | + builder.field(CONNECTOR_CREDENTIAL_FIELD, credential); |
| 102 | + } |
| 103 | + builder.endObject(); |
| 104 | + return builder; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public void writeTo(StreamOutput output) throws IOException { |
| 109 | + output.writeOptionalString(indexName); |
| 110 | + if (fieldMapping != null) { |
| 111 | + output.writeBoolean(true); |
| 112 | + output.writeMap(fieldMapping, StreamOutput::writeString, StreamOutput::writeString); |
| 113 | + } else { |
| 114 | + output.writeBoolean(false); |
| 115 | + } |
| 116 | + |
| 117 | + if (dataSources != null) { |
| 118 | + output.writeBoolean(true); |
| 119 | + output.writeMap(dataSources, StreamOutput::writeString, StreamOutput::writeString); |
| 120 | + } else { |
| 121 | + output.writeBoolean(false); |
| 122 | + } |
| 123 | + |
| 124 | + if (credential != null) { |
| 125 | + output.writeBoolean(true); |
| 126 | + output.writeMap(credential, StreamOutput::writeString, StreamOutput::writeString); |
| 127 | + } else { |
| 128 | + output.writeBoolean(false); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + public MLBatchIngestionInput(StreamInput input) throws IOException { |
| 133 | + indexName = input.readOptionalString(); |
| 134 | + if (input.readBoolean()) { |
| 135 | + fieldMapping = input.readMap(s -> s.readString(), s -> s.readString()); |
| 136 | + } |
| 137 | + if (input.readBoolean()) { |
| 138 | + dataSources = input.readMap(s -> s.readString(), s -> s.readString()); |
| 139 | + } |
| 140 | + if (input.readBoolean()) { |
| 141 | + credential = input.readMap(s -> s.readString(), s -> s.readString()); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | +} |
0 commit comments