shikeying
2024-04-03 b77abcbc0f17070a2a970e0c4aa5837e90f28e1f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package com.walker.support.milvus;
 
//import io.milvus.param.ParamUtils;
 
import io.milvus.param.ParamUtils;
import org.checkerframework.checker.nullness.qual.NonNull;
 
import java.util.HashMap;
import java.util.Map;
 
public class FieldType {
//    private FieldType fieldType = null;
 
    private String name;
    private boolean primaryKey;
    private String description;
    private DataType dataType;
    private Map<String, String> typeParams;
    // 2024-03-28
    private DataType elementType;
    private boolean partitionKey;
 
    private FieldType(FieldType.Builder builder) {
        if (builder == null) {
            throw new NullPointerException("builder is marked non-null but is null");
        } else {
            this.name = builder.name;
            this.primaryKey = builder.primaryKey;
            this.description = builder.description;
            this.dataType = builder.dataType;
            this.typeParams = builder.typeParams;
//            this.autoID = builder.autoID;
            this.elementType = builder.elementType;
            this.partitionKey = builder.partitionKey;
        }
    }
 
    public static FieldType.Builder newBuilder() {
        return new FieldType.Builder();
    }
 
    @Override
    public String toString(){
        return new StringBuilder("[name=").append(this.name)
                .append(", isPrimary=").append(this.primaryKey)
                .append(", dataType=").append(this.dataType)
                .append(", summary=").append(this.description)
                .append("]").toString();
    }
 
    public String getName(){
        return this.name;
    }
    public boolean isPrimaryKey(){
        return this.primaryKey;
    }
    public String getDescription(){
        return this.description;
    }
    public DataType getDataType(){
        return this.dataType;
    }
    public Map<String, String> getTypeParams(){
        return this.typeParams;
    }
 
    public static final class Builder {
        private String name;
        private boolean primaryKey;
        private String description;
        private DataType dataType;
        private final Map<String, String> typeParams;
//        private boolean autoID;
        private DataType elementType;
        private boolean partitionKey;
 
        private Builder() {
            this.primaryKey = false;
            this.description = "";
            this.typeParams = new HashMap();
//            this.autoID = false;
            this.elementType = DataType.None;
            this.partitionKey = false;
        }
 
        public Builder withName(String name) {
            if (name == null) {
                throw new NullPointerException("name is marked non-null but is null");
            } else {
                this.name = name;
                return this;
            }
        }
 
        public Builder withPrimaryKey(boolean primaryKey) {
            this.primaryKey = primaryKey;
            return this;
        }
 
        public Builder withDescription(String description) {
            if (description == null) {
                throw new NullPointerException("description is marked non-null but is null");
            } else {
                this.description = description;
                return this;
            }
        }
 
        public Builder withDataType(DataType dataType) {
            if (dataType == null) {
                throw new NullPointerException("dataType is marked non-null but is null");
            } else {
                this.dataType = dataType;
                return this;
            }
        }
 
        public Builder withElementType(@NonNull DataType elementType) {
            if (elementType == null) {
                throw new NullPointerException("elementType is marked non-null but is null");
            } else {
                this.elementType = elementType;
                return this;
            }
        }
 
        public Builder addTypeParam(String key, String value) {
            if (key == null) {
                throw new NullPointerException("key is marked non-null but is null");
            } else if (value == null) {
                throw new NullPointerException("value is marked non-null but is null");
            } else {
                this.typeParams.put(key, value);
                return this;
            }
        }
 
        public Builder withTypeParams(Map<String, String> typeParams) {
            if (typeParams == null) {
                throw new NullPointerException("typeParams is marked non-null but is null");
            } else {
                Map var10001 = this.typeParams;
                typeParams.forEach(var10001::put);
                return this;
            }
        }
 
        public Builder withDimension(Integer dimension) {
            if (dimension == null) {
                throw new NullPointerException("dimension is marked non-null but is null");
            } else {
                this.typeParams.put("dim", dimension.toString());
                return this;
            }
        }
 
        public Builder withMaxLength(Integer maxLength) {
            if (maxLength == null) {
                throw new NullPointerException("maxLength is marked non-null but is null");
            } else {
                this.typeParams.put("max_length", maxLength.toString());
                return this;
            }
        }
 
//        public Builder withAutoID(boolean autoID) {
//            this.autoID = autoID;
//            return this;
//        }
        public Builder withPartitionKey(boolean partitionKey) {
            this.partitionKey = partitionKey;
            return this;
        }
 
        public FieldType build() throws ParamException {
            ParamUtils.CheckNullEmptyString(this.name, "Field name");
            if (this.dataType != null && this.dataType != DataType.None) {
                if (this.dataType == DataType.String) {
                    throw new io.milvus.exception.ParamException("String type is not supported, use Varchar instead");
                } else {
                    int len;
                    if (this.dataType == DataType.FloatVector || this.dataType == DataType.BinaryVector) {
                        if (!this.typeParams.containsKey("dim")) {
                            throw new io.milvus.exception.ParamException("Vector field dimension must be specified");
                        }
 
                        try {
                            len = Integer.parseInt((String)this.typeParams.get("dim"));
                            if (len <= 0) {
                                throw new io.milvus.exception.ParamException("Vector field dimension must be larger than zero");
                            }
                        } catch (NumberFormatException var3) {
                            throw new io.milvus.exception.ParamException("Vector field dimension must be an integer number");
                        }
                    }
 
                    if (this.dataType == DataType.VarChar) {
                        if (!this.typeParams.containsKey("max_length")) {
                            throw new io.milvus.exception.ParamException("Varchar field max length must be specified");
                        }
 
                        try {
                            len = Integer.parseInt((String)this.typeParams.get("max_length"));
                            if (len <= 0) {
                                throw new io.milvus.exception.ParamException("Varchar field max length must be larger than zero");
                            }
                        } catch (NumberFormatException var2) {
                            throw new io.milvus.exception.ParamException("Varchar field max length must be an integer number");
                        }
                    }
 
                    if (this.partitionKey) {
                        if (this.primaryKey) {
                            throw new io.milvus.exception.ParamException("Primary key field can not be partition key");
                        }
 
                        if (this.dataType != DataType.Long && this.dataType != DataType.VarChar) {
                            throw new io.milvus.exception.ParamException("Only Int64 and Varchar type field can be partition key");
                        }
                    }
 
                    if (this.dataType == DataType.Array) {
                        if (this.elementType == DataType.String) {
                            throw new io.milvus.exception.ParamException("String type is not supported, use Varchar instead");
                        }
 
                        if (this.elementType == DataType.None || this.elementType == DataType.Array || this.elementType == DataType.Json || this.elementType == DataType.String || this.elementType == DataType.FloatVector || this.elementType == DataType.Float16Vector || this.elementType == DataType.BinaryVector) {
                            throw new io.milvus.exception.ParamException("Unsupported element type");
                        }
 
                        if (!this.typeParams.containsKey("max_capacity")) {
                            throw new io.milvus.exception.ParamException("Array field max capacity must be specified");
                        }
 
                        if (this.elementType == DataType.VarChar && !this.typeParams.containsKey("max_length")) {
                            throw new io.milvus.exception.ParamException("Varchar array field max length must be specified");
                        }
                    }
 
                    return new FieldType(this);
                }
            } else {
                throw new io.milvus.exception.ParamException("Field data type is illegal");
            }
//            if (this.name == null || StringUtils.isBlank(this.name)) {
//                throw new ParamException(name + " cannot be null or empty");
//            }
//            if (this.dataType != null && this.dataType != DataType.None) {
//                if (this.dataType == DataType.String) {
//                    throw new ParamException("String type is not supported, use VarChar instead");
//                } else {
//                    int len;
//                    if (this.dataType == DataType.FloatVector || this.dataType == DataType.BinaryVector) {
//                        if (!this.typeParams.containsKey("dim")) {
//                            throw new ParamException("Vector field dimension must be specified");
//                        }
//
//                        try {
//                            len = Integer.parseInt((String)this.typeParams.get("dim"));
//                            if (len <= 0) {
//                                throw new ParamException("Vector field dimension must be larger than zero");
//                            }
//                        } catch (NumberFormatException var3) {
//                            throw new ParamException("Vector field dimension must be an integer number");
//                        }
//                    }
//                    return new FieldType(this);
//                }
//            } else {
//                throw new ParamException("Field data type is illegal");
//            }
        }
 
    }
}