|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package io.milvus.v2.service.collection.request; |
| 21 | + |
| 22 | +import io.milvus.exception.ParamException; |
| 23 | +import io.milvus.v2.exception.ErrorCode; |
| 24 | +import io.milvus.v2.exception.MilvusClientException; |
| 25 | +import io.milvus.v2.service.collection.request.CreateCollectionReq.FieldSchema; |
| 26 | +import io.milvus.v2.utils.SchemaUtils; |
| 27 | + |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.HashMap; |
| 30 | +import java.util.List; |
| 31 | +import java.util.Map; |
| 32 | + |
| 33 | +public class AddCollectionStructFieldReq { |
| 34 | + private String collectionName; |
| 35 | + private String databaseName; |
| 36 | + private String fieldName; |
| 37 | + private String description; |
| 38 | + private Integer maxCapacity; |
| 39 | + private Boolean nullable; |
| 40 | + private List<FieldSchema> structFields; |
| 41 | + private Map<String, String> typeParams; |
| 42 | + |
| 43 | + private AddCollectionStructFieldReq(AddCollectionStructFieldReqBuilder builder) { |
| 44 | + this.collectionName = builder.collectionName; |
| 45 | + this.databaseName = builder.databaseName; |
| 46 | + this.fieldName = builder.fieldName; |
| 47 | + this.description = builder.description; |
| 48 | + this.maxCapacity = builder.maxCapacity; |
| 49 | + this.nullable = builder.nullable; |
| 50 | + this.structFields = builder.structFields; |
| 51 | + this.typeParams = builder.typeParams; |
| 52 | + } |
| 53 | + |
| 54 | + public String getCollectionName() { |
| 55 | + return collectionName; |
| 56 | + } |
| 57 | + |
| 58 | + public void setCollectionName(String collectionName) { |
| 59 | + this.collectionName = collectionName; |
| 60 | + } |
| 61 | + |
| 62 | + public String getDatabaseName() { |
| 63 | + return databaseName; |
| 64 | + } |
| 65 | + |
| 66 | + public void setDatabaseName(String databaseName) { |
| 67 | + this.databaseName = databaseName; |
| 68 | + } |
| 69 | + |
| 70 | + public String getFieldName() { |
| 71 | + return fieldName; |
| 72 | + } |
| 73 | + |
| 74 | + public void setFieldName(String fieldName) { |
| 75 | + this.fieldName = fieldName; |
| 76 | + } |
| 77 | + |
| 78 | + public String getDescription() { |
| 79 | + return description; |
| 80 | + } |
| 81 | + |
| 82 | + public void setDescription(String description) { |
| 83 | + this.description = description; |
| 84 | + } |
| 85 | + |
| 86 | + public Integer getMaxCapacity() { |
| 87 | + return maxCapacity; |
| 88 | + } |
| 89 | + |
| 90 | + public void setMaxCapacity(Integer maxCapacity) { |
| 91 | + this.maxCapacity = maxCapacity; |
| 92 | + } |
| 93 | + |
| 94 | + public Boolean getNullable() { |
| 95 | + return nullable; |
| 96 | + } |
| 97 | + |
| 98 | + public void setNullable(Boolean nullable) { |
| 99 | + this.nullable = nullable; |
| 100 | + } |
| 101 | + |
| 102 | + public List<FieldSchema> getStructFields() { |
| 103 | + return structFields; |
| 104 | + } |
| 105 | + |
| 106 | + public void setStructFields(List<FieldSchema> structFields) { |
| 107 | + this.structFields = structFields; |
| 108 | + } |
| 109 | + |
| 110 | + public Map<String, String> getTypeParams() { |
| 111 | + return typeParams; |
| 112 | + } |
| 113 | + |
| 114 | + public void setTypeParams(Map<String, String> typeParams) { |
| 115 | + this.typeParams = typeParams; |
| 116 | + } |
| 117 | + |
| 118 | + public CreateCollectionReq.StructFieldSchema toStructFieldSchema() { |
| 119 | + if (Boolean.FALSE.equals(nullable)) { |
| 120 | + throw new MilvusClientException(ErrorCode.INVALID_PARAMS, |
| 121 | + "Adding struct field to existing collection requires nullable=true"); |
| 122 | + } |
| 123 | + |
| 124 | + AddFieldReq addFieldReq = AddFieldReq.builder() |
| 125 | + .fieldName(fieldName) |
| 126 | + .description(description) |
| 127 | + .maxCapacity(maxCapacity) |
| 128 | + .structFields(structFields) |
| 129 | + .build(); |
| 130 | + try { |
| 131 | + CreateCollectionReq.StructFieldSchema structFieldSchema = SchemaUtils.convertFieldReqToStructFieldSchema(addFieldReq); |
| 132 | + structFieldSchema.setNullable(Boolean.TRUE); |
| 133 | + structFieldSchema.setTypeParams(typeParams); |
| 134 | + return structFieldSchema; |
| 135 | + } catch (ParamException e) { |
| 136 | + throw new MilvusClientException(ErrorCode.INVALID_PARAMS, e.getMessage()); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public String toString() { |
| 142 | + return "AddCollectionStructFieldReq{" + |
| 143 | + "collectionName='" + collectionName + '\'' + |
| 144 | + ", databaseName='" + databaseName + '\'' + |
| 145 | + ", fieldName='" + fieldName + '\'' + |
| 146 | + ", description='" + description + '\'' + |
| 147 | + ", maxCapacity=" + maxCapacity + |
| 148 | + ", nullable=" + nullable + |
| 149 | + ", structFields=" + structFields + |
| 150 | + ", typeParams=" + typeParams + |
| 151 | + '}'; |
| 152 | + } |
| 153 | + |
| 154 | + public static AddCollectionStructFieldReqBuilder builder() { |
| 155 | + return new AddCollectionStructFieldReqBuilder(); |
| 156 | + } |
| 157 | + |
| 158 | + public static class AddCollectionStructFieldReqBuilder { |
| 159 | + private String collectionName = ""; |
| 160 | + private String databaseName = ""; |
| 161 | + private String fieldName = ""; |
| 162 | + private String description = ""; |
| 163 | + private Integer maxCapacity; |
| 164 | + private Boolean nullable = Boolean.TRUE; |
| 165 | + private List<FieldSchema> structFields = new ArrayList<>(); |
| 166 | + private Map<String, String> typeParams = new HashMap<>(); |
| 167 | + |
| 168 | + private AddCollectionStructFieldReqBuilder() { |
| 169 | + } |
| 170 | + |
| 171 | + public AddCollectionStructFieldReqBuilder collectionName(String collectionName) { |
| 172 | + this.collectionName = collectionName; |
| 173 | + return this; |
| 174 | + } |
| 175 | + |
| 176 | + public AddCollectionStructFieldReqBuilder databaseName(String databaseName) { |
| 177 | + this.databaseName = databaseName; |
| 178 | + return this; |
| 179 | + } |
| 180 | + |
| 181 | + public AddCollectionStructFieldReqBuilder fieldName(String fieldName) { |
| 182 | + this.fieldName = fieldName; |
| 183 | + return this; |
| 184 | + } |
| 185 | + |
| 186 | + public AddCollectionStructFieldReqBuilder description(String description) { |
| 187 | + this.description = description; |
| 188 | + return this; |
| 189 | + } |
| 190 | + |
| 191 | + public AddCollectionStructFieldReqBuilder maxCapacity(Integer maxCapacity) { |
| 192 | + this.maxCapacity = maxCapacity; |
| 193 | + return this; |
| 194 | + } |
| 195 | + |
| 196 | + public AddCollectionStructFieldReqBuilder nullable(Boolean nullable) { |
| 197 | + this.nullable = nullable; |
| 198 | + return this; |
| 199 | + } |
| 200 | + |
| 201 | + public AddCollectionStructFieldReqBuilder structFields(List<FieldSchema> structFields) { |
| 202 | + this.structFields = structFields; |
| 203 | + return this; |
| 204 | + } |
| 205 | + |
| 206 | + public AddCollectionStructFieldReqBuilder addStructField(AddFieldReq addFieldReq) { |
| 207 | + if (this.structFields == null) { |
| 208 | + this.structFields = new ArrayList<>(); |
| 209 | + } |
| 210 | + this.structFields.add(SchemaUtils.convertFieldReqToFieldSchema(addFieldReq)); |
| 211 | + return this; |
| 212 | + } |
| 213 | + |
| 214 | + public AddCollectionStructFieldReqBuilder typeParams(Map<String, String> typeParams) { |
| 215 | + this.typeParams = typeParams; |
| 216 | + return this; |
| 217 | + } |
| 218 | + |
| 219 | + public AddCollectionStructFieldReqBuilder typeParam(String key, String value) { |
| 220 | + if (this.typeParams == null) { |
| 221 | + this.typeParams = new HashMap<>(); |
| 222 | + } |
| 223 | + this.typeParams.put(key, value); |
| 224 | + return this; |
| 225 | + } |
| 226 | + |
| 227 | + public AddCollectionStructFieldReq build() { |
| 228 | + return new AddCollectionStructFieldReq(this); |
| 229 | + } |
| 230 | + } |
| 231 | +} |
0 commit comments