Skip to content

Commit 6c9bd5e

Browse files
committed
Support AddCollectionStructField interface
Signed-off-by: yhmo <yihua.mo@zilliz.com>
1 parent a873189 commit 6c9bd5e

8 files changed

Lines changed: 389 additions & 2 deletions

File tree

sdk-core/src/main/java/io/milvus/v2/client/MilvusClientV2.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,15 @@ public void addCollectionField(AddCollectionFieldReq request) {
489489
rpcUtils.retry(() -> collectionService.addCollectionField(this.getRpcStub(), request));
490490
}
491491

492+
/**
493+
* Add a new struct field to collection.
494+
*
495+
* @param request add new struct field request
496+
*/
497+
public void addCollectionStructField(AddCollectionStructFieldReq request) {
498+
rpcUtils.retry(() -> collectionService.addCollectionStructField(this.getRpcStub(), request));
499+
}
500+
492501
/**
493502
* Alter a field's properties.
494503
*

sdk-core/src/main/java/io/milvus/v2/service/collection/CollectionService.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,24 @@ public Void addCollectionField(MilvusServiceGrpc.MilvusServiceBlockingStub block
301301
return null;
302302
}
303303

304+
public Void addCollectionStructField(MilvusServiceGrpc.MilvusServiceBlockingStub blockingStub, AddCollectionStructFieldReq request) {
305+
String dbName = request.getDatabaseName();
306+
String collectionName = request.getCollectionName();
307+
String title = String.format("Add struct field to collection: '%s' in database: '%s'", collectionName, dbName);
308+
309+
AddCollectionStructFieldRequest.Builder builder = AddCollectionStructFieldRequest.newBuilder()
310+
.setCollectionName(collectionName)
311+
.setStructArrayFieldSchema(SchemaUtils.convertToGrpcStructFieldSchema(request.toStructFieldSchema()));
312+
if (StringUtils.isNotEmpty(dbName)) {
313+
builder.setDbName(dbName);
314+
}
315+
316+
Status response = blockingStub.addCollectionStructField(builder.build());
317+
rpcUtils.handleResponse(title, response);
318+
319+
return null;
320+
}
321+
304322
public Void alterCollectionField(MilvusServiceGrpc.MilvusServiceBlockingStub blockingStub, AlterCollectionFieldReq request) {
305323
String dbName = request.getDatabaseName();
306324
String collectionName = request.getCollectionName();
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
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.v2.exception.ErrorCode;
23+
import io.milvus.v2.exception.MilvusClientException;
24+
import io.milvus.v2.service.collection.request.CreateCollectionReq.FieldSchema;
25+
import io.milvus.v2.utils.SchemaUtils;
26+
27+
import java.util.ArrayList;
28+
import java.util.HashMap;
29+
import java.util.List;
30+
import java.util.Map;
31+
32+
public class AddCollectionStructFieldReq {
33+
private String collectionName;
34+
private String databaseName;
35+
private String fieldName;
36+
private String description;
37+
private Integer maxCapacity;
38+
private Boolean nullable;
39+
private List<FieldSchema> structFields;
40+
private Map<String, String> typeParams;
41+
42+
private AddCollectionStructFieldReq(AddCollectionStructFieldReqBuilder builder) {
43+
this.collectionName = builder.collectionName;
44+
this.databaseName = builder.databaseName;
45+
this.fieldName = builder.fieldName;
46+
this.description = builder.description;
47+
this.maxCapacity = builder.maxCapacity;
48+
this.nullable = builder.nullable;
49+
this.structFields = builder.structFields;
50+
this.typeParams = builder.typeParams;
51+
}
52+
53+
public String getCollectionName() {
54+
return collectionName;
55+
}
56+
57+
public void setCollectionName(String collectionName) {
58+
this.collectionName = collectionName;
59+
}
60+
61+
public String getDatabaseName() {
62+
return databaseName;
63+
}
64+
65+
public void setDatabaseName(String databaseName) {
66+
this.databaseName = databaseName;
67+
}
68+
69+
public String getFieldName() {
70+
return fieldName;
71+
}
72+
73+
public void setFieldName(String fieldName) {
74+
this.fieldName = fieldName;
75+
}
76+
77+
public String getDescription() {
78+
return description;
79+
}
80+
81+
public void setDescription(String description) {
82+
this.description = description;
83+
}
84+
85+
public Integer getMaxCapacity() {
86+
return maxCapacity;
87+
}
88+
89+
public void setMaxCapacity(Integer maxCapacity) {
90+
this.maxCapacity = maxCapacity;
91+
}
92+
93+
public Boolean getNullable() {
94+
return nullable;
95+
}
96+
97+
public void setNullable(Boolean nullable) {
98+
this.nullable = nullable;
99+
}
100+
101+
public List<FieldSchema> getStructFields() {
102+
return structFields;
103+
}
104+
105+
public void setStructFields(List<FieldSchema> structFields) {
106+
this.structFields = structFields;
107+
}
108+
109+
public Map<String, String> getTypeParams() {
110+
return typeParams;
111+
}
112+
113+
public void setTypeParams(Map<String, String> typeParams) {
114+
this.typeParams = typeParams;
115+
}
116+
117+
public CreateCollectionReq.StructFieldSchema toStructFieldSchema() {
118+
if (Boolean.FALSE.equals(nullable)) {
119+
throw new MilvusClientException(ErrorCode.INVALID_PARAMS,
120+
"Adding struct field to existing collection requires nullable=true");
121+
}
122+
return CreateCollectionReq.StructFieldSchema.builder()
123+
.name(fieldName)
124+
.description(description)
125+
.fields(structFields)
126+
.maxCapacity(maxCapacity)
127+
.nullable(Boolean.TRUE)
128+
.typeParams(typeParams)
129+
.build();
130+
}
131+
132+
@Override
133+
public String toString() {
134+
return "AddCollectionStructFieldReq{" +
135+
"collectionName='" + collectionName + '\'' +
136+
", databaseName='" + databaseName + '\'' +
137+
", fieldName='" + fieldName + '\'' +
138+
", description='" + description + '\'' +
139+
", maxCapacity=" + maxCapacity +
140+
", nullable=" + nullable +
141+
", structFields=" + structFields +
142+
", typeParams=" + typeParams +
143+
'}';
144+
}
145+
146+
public static AddCollectionStructFieldReqBuilder builder() {
147+
return new AddCollectionStructFieldReqBuilder();
148+
}
149+
150+
public static class AddCollectionStructFieldReqBuilder {
151+
private String collectionName = "";
152+
private String databaseName = "";
153+
private String fieldName = "";
154+
private String description = "";
155+
private Integer maxCapacity;
156+
private Boolean nullable = Boolean.TRUE;
157+
private List<FieldSchema> structFields = new ArrayList<>();
158+
private Map<String, String> typeParams = new HashMap<>();
159+
160+
private AddCollectionStructFieldReqBuilder() {
161+
}
162+
163+
public AddCollectionStructFieldReqBuilder collectionName(String collectionName) {
164+
this.collectionName = collectionName;
165+
return this;
166+
}
167+
168+
public AddCollectionStructFieldReqBuilder databaseName(String databaseName) {
169+
this.databaseName = databaseName;
170+
return this;
171+
}
172+
173+
public AddCollectionStructFieldReqBuilder fieldName(String fieldName) {
174+
this.fieldName = fieldName;
175+
return this;
176+
}
177+
178+
public AddCollectionStructFieldReqBuilder description(String description) {
179+
this.description = description;
180+
return this;
181+
}
182+
183+
public AddCollectionStructFieldReqBuilder maxCapacity(Integer maxCapacity) {
184+
this.maxCapacity = maxCapacity;
185+
return this;
186+
}
187+
188+
public AddCollectionStructFieldReqBuilder nullable(Boolean nullable) {
189+
this.nullable = nullable;
190+
return this;
191+
}
192+
193+
public AddCollectionStructFieldReqBuilder structFields(List<FieldSchema> structFields) {
194+
this.structFields = structFields;
195+
return this;
196+
}
197+
198+
public AddCollectionStructFieldReqBuilder addStructField(AddFieldReq addFieldReq) {
199+
if (this.structFields == null) {
200+
this.structFields = new ArrayList<>();
201+
}
202+
this.structFields.add(SchemaUtils.convertFieldReqToFieldSchema(addFieldReq));
203+
return this;
204+
}
205+
206+
public AddCollectionStructFieldReqBuilder typeParams(Map<String, String> typeParams) {
207+
this.typeParams = typeParams;
208+
return this;
209+
}
210+
211+
public AddCollectionStructFieldReqBuilder typeParam(String key, String value) {
212+
if (this.typeParams == null) {
213+
this.typeParams = new HashMap<>();
214+
}
215+
this.typeParams.put(key, value);
216+
return this;
217+
}
218+
219+
public AddCollectionStructFieldReq build() {
220+
return new AddCollectionStructFieldReq(this);
221+
}
222+
}
223+
}

sdk-core/src/main/java/io/milvus/v2/service/collection/request/CreateCollectionReq.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,12 +1056,16 @@ public static class StructFieldSchema {
10561056
private String description = "";
10571057
private List<CreateCollectionReq.FieldSchema> fields = new ArrayList<>();
10581058
private Integer maxCapacity;
1059+
private Boolean nullable = Boolean.FALSE;
1060+
private Map<String, String> typeParams = new HashMap<>();
10591061

10601062
private StructFieldSchema(StructFieldSchemaBuilder builder) {
10611063
this.name = builder.name;
10621064
this.description = builder.description;
10631065
this.fields = builder.fields;
10641066
this.maxCapacity = builder.maxCapacity;
1067+
this.nullable = builder.nullable;
1068+
this.typeParams = builder.typeParams;
10651069
}
10661070

10671071
public StructFieldSchema addField(AddFieldReq addFieldReq) {
@@ -1113,13 +1117,31 @@ public void setMaxCapacity(Integer maxCapacity) {
11131117
this.maxCapacity = maxCapacity;
11141118
}
11151119

1120+
public Boolean getNullable() {
1121+
return nullable;
1122+
}
1123+
1124+
public void setNullable(Boolean nullable) {
1125+
this.nullable = nullable;
1126+
}
1127+
1128+
public Map<String, String> getTypeParams() {
1129+
return typeParams;
1130+
}
1131+
1132+
public void setTypeParams(Map<String, String> typeParams) {
1133+
this.typeParams = typeParams;
1134+
}
1135+
11161136
@Override
11171137
public String toString() {
11181138
return "StructFieldSchema{" +
11191139
"name='" + name + '\'' +
11201140
", description='" + description + '\'' +
11211141
", fields=" + fields +
11221142
", maxCapacity=" + maxCapacity +
1143+
", nullable=" + nullable +
1144+
", typeParams=" + typeParams +
11231145
'}';
11241146
}
11251147

@@ -1132,6 +1154,8 @@ public static class StructFieldSchemaBuilder {
11321154
private String description = "";
11331155
private List<CreateCollectionReq.FieldSchema> fields = new ArrayList<>();
11341156
private Integer maxCapacity;
1157+
private Boolean nullable = Boolean.FALSE;
1158+
private Map<String, String> typeParams = new HashMap<>();
11351159

11361160
private StructFieldSchemaBuilder() {
11371161
}
@@ -1156,6 +1180,24 @@ public StructFieldSchemaBuilder maxCapacity(Integer maxCapacity) {
11561180
return this;
11571181
}
11581182

1183+
public StructFieldSchemaBuilder nullable(Boolean nullable) {
1184+
this.nullable = nullable;
1185+
return this;
1186+
}
1187+
1188+
public StructFieldSchemaBuilder typeParams(Map<String, String> typeParams) {
1189+
this.typeParams = typeParams;
1190+
return this;
1191+
}
1192+
1193+
public StructFieldSchemaBuilder typeParam(String key, String value) {
1194+
if (this.typeParams == null) {
1195+
this.typeParams = new HashMap<>();
1196+
}
1197+
this.typeParams.put(key, value);
1198+
return this;
1199+
}
1200+
11591201
public StructFieldSchema build() {
11601202
return new StructFieldSchema(this);
11611203
}

sdk-core/src/main/java/io/milvus/v2/utils/SchemaUtils.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,13 @@ public static StructArrayFieldSchema convertToGrpcStructFieldSchema(CreateCollec
158158
checkNullEmptyString(structSchema.getName(), "Field name");
159159
StructArrayFieldSchema.Builder builder = StructArrayFieldSchema.newBuilder()
160160
.setName(structSchema.getName())
161-
.setDescription(structSchema.getDescription());
161+
.setDescription(structSchema.getDescription())
162+
.setNullable(structSchema.getNullable());
163+
164+
List<KeyValuePair> typeParamsList = AssembleKvPair(structSchema.getTypeParams());
165+
if (CollectionUtils.isNotEmpty(typeParamsList)) {
166+
typeParamsList.forEach(builder::addTypeParams);
167+
}
162168

163169
for (CreateCollectionReq.FieldSchema field : structSchema.getFields()) {
164170
DataType actualType = DataType.Array;
@@ -286,7 +292,12 @@ public static CreateCollectionReq.StructFieldSchema convertFromGrpcStructFieldSc
286292
CreateCollectionReq.StructFieldSchema.StructFieldSchemaBuilder builder =
287293
CreateCollectionReq.StructFieldSchema.builder()
288294
.name(structSchema.getName())
289-
.description(structSchema.getDescription());
295+
.description(structSchema.getDescription())
296+
.nullable(structSchema.getNullable());
297+
List<KeyValuePair> structTypeParams = structSchema.getTypeParamsList();
298+
if (CollectionUtils.isNotEmpty(structTypeParams)) {
299+
structTypeParams.forEach((kv) -> builder.typeParam(kv.getKey(), kv.getValue()));
300+
}
290301
List<CreateCollectionReq.FieldSchema> fields = new ArrayList<>();
291302
for (FieldSchema fieldSchema : structSchema.getFieldsList()) {
292303
CreateCollectionReq.FieldSchema field = convertFromGrpcFieldSchema(fieldSchema);

sdk-core/src/test/java/io/milvus/v2/BaseTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ public void setUp() {
107107
// collection api
108108
when(blockingStub.showCollections(any(ShowCollectionsRequest.class))).thenReturn(ShowCollectionsResponse.newBuilder().setStatus(successStatus).addAllCollectionNames(Collections.singletonList("test")).build());
109109
when(blockingStub.createCollection(any(CreateCollectionRequest.class))).thenReturn(successStatus);
110+
when(blockingStub.addCollectionStructField(any())).thenReturn(successStatus);
110111
when(blockingStub.loadCollection(any())).thenReturn(successStatus);
111112
when(blockingStub.releaseCollection(any())).thenReturn(successStatus);
112113
when(blockingStub.getLoadState(any())).thenReturn(GetLoadStateResponse.newBuilder().setState(LoadState.LoadStateLoaded).setStatus(successStatus).build());

sdk-core/src/test/java/io/milvus/v2/client/MilvusClientV2Test.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,7 @@ void testV2BuilderClasses() {
535535

536536
// io.milvus/v2/service/collection
537537
VerifyClass(AddCollectionFieldReq.class.getName(), config);
538+
VerifyClass(AddCollectionStructFieldReq.class.getName(), config);
538539
config.setIgnoredMethods(Collections.singletonList("isEnableDefaultValue"));
539540
VerifyClass(AddFieldReq.class.getName(), config);
540541
config.clearIgnoredMethods();

0 commit comments

Comments
 (0)