From 569d5773c90e952492421043fe17834f21883e51 Mon Sep 17 00:00:00 2001 From: groot Date: Wed, 16 Oct 2024 17:27:19 +0800 Subject: [PATCH] Remove Jackson dependence (#1109) Signed-off-by: yhmo --- .../java/io/milvus/v1/HighLevelExample.java | 3 +- examples/pom.xml | 5 ++ pom.xml | 21 ----- .../java/io/milvus/bulkwriter/Buffer.java | 5 +- .../java/io/milvus/bulkwriter/BulkImport.java | 15 ++-- .../java/io/milvus/bulkwriter/BulkWriter.java | 2 - .../client/AbstractMilvusGrpcClient.java | 4 +- .../io/milvus/common/utils/JacksonUtils.java | 83 ------------------- .../io/milvus/common/utils/JsonUtils.java | 72 ++++++++++++++++ .../milvus/orm/iterator/SearchIterator.java | 17 ++-- src/main/java/io/milvus/param/ParamUtils.java | 46 +++++----- .../param/dml/ranker/WeightedRanker.java | 6 +- .../vector/request/ranker/WeightedRanker.java | 4 +- .../java/io/milvus/v2/utils/ConvertUtils.java | 4 +- .../java/io/milvus/v2/utils/VectorUtils.java | 6 +- .../milvus/client/MilvusClientDockerTest.java | 58 +++++++------ .../v2/client/MilvusClientV2DockerTest.java | 62 +++++++------- .../milvus/v2/service/vector/VectorTest.java | 6 +- 18 files changed, 191 insertions(+), 228 deletions(-) delete mode 100644 src/main/java/io/milvus/common/utils/JacksonUtils.java create mode 100644 src/main/java/io/milvus/common/utils/JsonUtils.java diff --git a/examples/main/java/io/milvus/v1/HighLevelExample.java b/examples/main/java/io/milvus/v1/HighLevelExample.java index a432dcff9..279b390be 100644 --- a/examples/main/java/io/milvus/v1/HighLevelExample.java +++ b/examples/main/java/io/milvus/v1/HighLevelExample.java @@ -23,7 +23,6 @@ import com.google.common.collect.Lists; import io.milvus.client.MilvusServiceClient; import io.milvus.common.clientenum.ConsistencyLevelEnum; -import io.milvus.common.utils.JacksonUtils; import io.milvus.common.utils.VectorUtils; import io.milvus.grpc.*; import io.milvus.param.*; @@ -95,7 +94,7 @@ private R createCollection() { R response = milvusClient.createCollection(createSimpleCollectionParam); CommonUtils.handleResponseStatus(response); - System.out.println(JacksonUtils.toJsonString(response.getData())); + System.out.println(response); return response; } diff --git a/examples/pom.xml b/examples/pom.xml index 7b243e372..a67c8762f 100644 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -83,6 +83,11 @@ 1.7.36 compile + + com.fasterxml.jackson.dataformat + jackson-dataformat-csv + 2.16.1 + diff --git a/pom.xml b/pom.xml index ff82686c8..0f716a417 100644 --- a/pom.xml +++ b/pom.xml @@ -89,7 +89,6 @@ 2.19.1 1.1.0 5.10.1 - 2.16.1 2.10.1 1.9.10 4.11.0 @@ -126,21 +125,6 @@ slf4j-api ${slf4j.api.version} - - com.fasterxml.jackson.core - jackson-databind - ${jackson.version} - - - com.fasterxml.jackson.core - jackson-annotations - ${jackson.version} - - - com.fasterxml.jackson.core - jackson-core - ${jackson.version} - com.google.guava guava @@ -341,11 +325,6 @@ - - com.fasterxml.jackson.dataformat - jackson-dataformat-csv - ${jackson.version} - diff --git a/src/main/java/io/milvus/bulkwriter/Buffer.java b/src/main/java/io/milvus/bulkwriter/Buffer.java index 177734b9b..aef459827 100644 --- a/src/main/java/io/milvus/bulkwriter/Buffer.java +++ b/src/main/java/io/milvus/bulkwriter/Buffer.java @@ -19,11 +19,11 @@ package io.milvus.bulkwriter; -import com.google.gson.*; import com.google.common.collect.Lists; import io.milvus.bulkwriter.common.clientenum.BulkFileType; import io.milvus.common.utils.ExceptionUtils; import io.milvus.bulkwriter.common.utils.ParquetUtils; +import io.milvus.common.utils.JsonUtils; import io.milvus.grpc.DataType; import io.milvus.param.collection.CollectionSchemaParam; import io.milvus.param.collection.FieldType; @@ -57,7 +57,6 @@ public class Buffer { private Map> buffer; private Map fields; - private static final Gson GSON_INSTANCE = new Gson(); public Buffer(CollectionSchemaParam collectionSchema, BulkFileType fileType) { this.collectionSchema = collectionSchema; @@ -312,7 +311,7 @@ private static void addBinaryVector(Group group, String fieldName, ByteBuffer by private static void addSparseVector(Group group, String fieldName, SortedMap sparse) { // sparse vector is parsed as JSON format string in the server side - String jsonString = GSON_INSTANCE.toJson(sparse); + String jsonString = JsonUtils.toJson(sparse); group.append(fieldName, jsonString); } } diff --git a/src/main/java/io/milvus/bulkwriter/BulkImport.java b/src/main/java/io/milvus/bulkwriter/BulkImport.java index 9ffa8b258..aa830671f 100644 --- a/src/main/java/io/milvus/bulkwriter/BulkImport.java +++ b/src/main/java/io/milvus/bulkwriter/BulkImport.java @@ -19,24 +19,23 @@ package io.milvus.bulkwriter; -import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import io.milvus.bulkwriter.request.describe.BaseDescribeImportRequest; import io.milvus.bulkwriter.request.import_.BaseImportRequest; import io.milvus.bulkwriter.request.list.BaseListImportJobsRequest; import io.milvus.bulkwriter.response.RestfulResponse; +import io.milvus.common.utils.JsonUtils; import java.util.Map; public class BulkImport extends BaseBulkImport { - private static final Gson GSON_INSTANCE = new Gson(); public static String bulkImport(String url, BaseImportRequest request) { String requestURL = url + "/v2/vectordb/jobs/import/create"; - Map params = GSON_INSTANCE.fromJson(GSON_INSTANCE.toJson(request), new TypeToken>() {}.getType()); + Map params = JsonUtils.fromJson(JsonUtils.toJson(request), new TypeToken>() {}.getType()); String body = postRequest(requestURL, request.getApiKey(), params, 60 * 1000); - RestfulResponse response = GSON_INSTANCE.fromJson(body, new TypeToken>(){}.getType()); + RestfulResponse response = JsonUtils.fromJson(body, new TypeToken>(){}.getType()); handleResponse(requestURL, response); return body; } @@ -44,9 +43,9 @@ public static String bulkImport(String url, BaseImportRequest request) { public static String getImportProgress(String url, BaseDescribeImportRequest request) { String requestURL = url + "/v2/vectordb/jobs/import/describe"; - Map params = GSON_INSTANCE.fromJson(GSON_INSTANCE.toJson(request), new TypeToken>() {}.getType()); + Map params = JsonUtils.fromJson(JsonUtils.toJson(request), new TypeToken>() {}.getType()); String body = postRequest(requestURL, request.getApiKey(), params, 60 * 1000); - RestfulResponse response = GSON_INSTANCE.fromJson(body, new TypeToken>(){}.getType()); + RestfulResponse response = JsonUtils.fromJson(body, new TypeToken>(){}.getType()); handleResponse(requestURL, response); return body; } @@ -54,9 +53,9 @@ public static String getImportProgress(String url, BaseDescribeImportRequest req public static String listImportJobs(String url, BaseListImportJobsRequest request) { String requestURL = url + "/v2/vectordb/jobs/import/list"; - Map params = GSON_INSTANCE.fromJson(GSON_INSTANCE.toJson(request), new TypeToken>() {}.getType()); + Map params = JsonUtils.fromJson(JsonUtils.toJson(request), new TypeToken>() {}.getType()); String body = postRequest(requestURL, request.getApiKey(), params, 60 * 1000); - RestfulResponse response = GSON_INSTANCE.fromJson(body, new TypeToken>(){}.getType()); + RestfulResponse response = JsonUtils.fromJson(body, new TypeToken>(){}.getType()); handleResponse(requestURL, response); return body; } diff --git a/src/main/java/io/milvus/bulkwriter/BulkWriter.java b/src/main/java/io/milvus/bulkwriter/BulkWriter.java index f776b193f..69ca5325f 100644 --- a/src/main/java/io/milvus/bulkwriter/BulkWriter.java +++ b/src/main/java/io/milvus/bulkwriter/BulkWriter.java @@ -51,8 +51,6 @@ public abstract class BulkWriter { protected Buffer buffer; protected ReentrantLock bufferLock; - private static final Gson GSON_INSTANCE = new Gson(); - protected BulkWriter(CollectionSchemaParam collectionSchema, int chunkSize, BulkFileType fileType) { this.collectionSchema = collectionSchema; this.chunkSize = chunkSize; diff --git a/src/main/java/io/milvus/client/AbstractMilvusGrpcClient.java b/src/main/java/io/milvus/client/AbstractMilvusGrpcClient.java index 7f8655fd6..90c30fc28 100644 --- a/src/main/java/io/milvus/client/AbstractMilvusGrpcClient.java +++ b/src/main/java/io/milvus/client/AbstractMilvusGrpcClient.java @@ -22,7 +22,7 @@ import com.google.common.collect.Lists; import com.google.common.util.concurrent.*; import io.grpc.StatusRuntimeException; -import io.milvus.common.utils.JacksonUtils; +import io.milvus.common.utils.JsonUtils; import io.milvus.common.utils.VectorUtils; import io.milvus.exception.*; import io.milvus.grpc.*; @@ -3288,7 +3288,7 @@ public R search(SearchSimpleParam requestParam) { .withOutFields(requestParam.getOutputFields()) .withExpr(requestParam.getFilter()) .withTopK(requestParam.getLimit()) - .withParams(JacksonUtils.toJsonString(requestParam.getParams())) + .withParams(JsonUtils.toJson(requestParam.getParams())) .withConsistencyLevel(requestParam.getConsistencyLevel()) .build(); diff --git a/src/main/java/io/milvus/common/utils/JacksonUtils.java b/src/main/java/io/milvus/common/utils/JacksonUtils.java deleted file mode 100644 index a0669afb1..000000000 --- a/src/main/java/io/milvus/common/utils/JacksonUtils.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package io.milvus.common.utils; - -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.ObjectMapper; - -public class JacksonUtils { - - private static final ObjectMapper objectMapper; - - static { - objectMapper = new ObjectMapper(); - objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); - } - - public static T fromJson(String jsonStr, TypeReference typeRef) { - try { - return objectMapper.readValue(jsonStr, typeRef); - } catch (Exception e) { - throw new IllegalArgumentException("json deserialization error, e=", e); - } - } - - public static T fromJson(String jsonStr, Class type) { - try { - return objectMapper.readValue(jsonStr, type); - } catch (Exception e) { - throw new IllegalArgumentException("json deserialization error, e=", e); - } - } - - public static T fromJson(byte[] bytes, TypeReference typeRef) { - try { - return objectMapper.readValue(bytes, typeRef); - } catch (Exception e) { - throw new IllegalArgumentException("json deserialization error, e=", e); - } - } - - public static T fromJson(byte[] bytes, Class type) { - try { - return objectMapper.readValue(bytes, type); - } catch (Exception e) { - throw new IllegalArgumentException("json deserialization error, e=", e); - } - } - - public static String toJsonString(Object obj) { - try { - return objectMapper.writeValueAsString(obj); - } catch (Exception e) { - throw new IllegalArgumentException("json serialization error, e=", e); - } - } - - public static byte[] toJsonByte(Object obj) { - try { - return objectMapper.writeValueAsBytes(obj); - } catch (Exception e) { - throw new IllegalArgumentException("json serialization error, e=", e); - } - } - -} \ No newline at end of file diff --git a/src/main/java/io/milvus/common/utils/JsonUtils.java b/src/main/java/io/milvus/common/utils/JsonUtils.java new file mode 100644 index 000000000..824f454f7 --- /dev/null +++ b/src/main/java/io/milvus/common/utils/JsonUtils.java @@ -0,0 +1,72 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package io.milvus.common.utils; + +import com.google.gson.*; +import com.google.gson.reflect.TypeToken; + +import java.lang.reflect.Type; + +public class JsonUtils { + // Set ToNumberPolicy.LONG_OR_DOUBLE so that integer can be parsed as integer, float be parsed as float. + // Gson doc declared "Gson instances are Thread-safe so you can reuse them freely across multiple threads." + // So we can use it as a global static instance. + // https://www.javadoc.io/doc/com.google.code.gson/gson/2.10.1/com.google.gson/com/google/gson/Gson.html + private static final Gson GSON_INSTANCE = new GsonBuilder() + .setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE) + .create(); + + public static T fromJson(String jsonStr, Class classOfT) { + return GSON_INSTANCE.fromJson(jsonStr, classOfT); + } + + public static T fromJson(String jsonStr, Type typeOfT) { + return GSON_INSTANCE.fromJson(jsonStr, typeOfT); + } + + public static T fromJson(String jsonStr, TypeToken typeOfT) { + return GSON_INSTANCE.fromJson(jsonStr, typeOfT); + } + + + public static T fromJson(JsonElement jsonElement, Class classOfT) { + return GSON_INSTANCE.fromJson(jsonElement, classOfT); + } + + public static T fromJson(JsonElement jsonElement, Type typeOfT) { + return GSON_INSTANCE.fromJson(jsonElement, typeOfT); + } + + public static T fromJson(JsonElement jsonElement, TypeToken typeOfT) { + return GSON_INSTANCE.fromJson(jsonElement, typeOfT); + } + + public static String toJson(Object obj) { + return GSON_INSTANCE.toJson(obj); + } + + public static String toJson(JsonElement jsonElement) { + return GSON_INSTANCE.toJson(jsonElement); + } + + public static JsonElement toJsonTree(T obj) { + return GSON_INSTANCE.toJsonTree(obj, new TypeToken() {}.getType()); + } +} diff --git a/src/main/java/io/milvus/orm/iterator/SearchIterator.java b/src/main/java/io/milvus/orm/iterator/SearchIterator.java index b15cce790..3157cc1c8 100644 --- a/src/main/java/io/milvus/orm/iterator/SearchIterator.java +++ b/src/main/java/io/milvus/orm/iterator/SearchIterator.java @@ -2,10 +2,10 @@ import com.amazonaws.util.CollectionUtils; import com.amazonaws.util.StringUtils; -import com.fasterxml.jackson.core.type.TypeReference; import com.google.common.collect.Lists; +import com.google.gson.reflect.TypeToken; import io.milvus.common.utils.ExceptionUtils; -import io.milvus.common.utils.JacksonUtils; +import io.milvus.common.utils.JsonUtils; import io.milvus.exception.ParamException; import io.milvus.grpc.*; import io.milvus.param.MetricType; @@ -146,8 +146,7 @@ private void initParams() { if (null != searchIteratorParam.getParams() && !searchIteratorParam.getParams().isEmpty()) { params = new HashMap<>(); } - params = JacksonUtils.fromJson(searchIteratorParam.getParams(), new TypeReference>() { - }); + params = JsonUtils.fromJson(searchIteratorParam.getParams(), new TypeToken>() {}.getType()); } private void checkForSpecialIndexParam() { @@ -246,7 +245,7 @@ private SearchResultsWrapper executeNextSearch(Map params, Strin .withExpr(nextExpr) .withOutFields(searchIteratorParam.getOutFields()) .withRoundDecimal(searchIteratorParam.getRoundDecimal()) - .withParams(JacksonUtils.toJsonString(params)) + .withParams(JsonUtils.toJson(params)) .withMetricType(MetricType.valueOf(searchIteratorParam.getMetricType())) .withIgnoreGrowing(searchIteratorParam.isIgnoreGrowing()) .withIterator(Boolean.TRUE) @@ -403,8 +402,12 @@ private List trySearchFill() { private Map nextParams(int coefficient) { coefficient = Math.max(1, coefficient); - Map nextParams = JacksonUtils.fromJson(JacksonUtils.toJsonString(params), new TypeReference>() { - }); + + // Note: params is a Map to store the original parameters, we must not change the params. + // here we make a new object nextParams, to ensure is it a deep copy, we convert it to JSON string and + // convert back to a Map. + Map nextParams = JsonUtils.fromJson(JsonUtils.toJson(params), + new TypeToken>() {}.getType()); if (metricsPositiveRelated(metricType)) { float nextRadius = tailBand + width * coefficient; diff --git a/src/main/java/io/milvus/param/ParamUtils.java b/src/main/java/io/milvus/param/ParamUtils.java index 09ac855ad..a89d24b39 100644 --- a/src/main/java/io/milvus/param/ParamUtils.java +++ b/src/main/java/io/milvus/param/ParamUtils.java @@ -23,7 +23,7 @@ import com.google.gson.reflect.TypeToken; import com.google.protobuf.ByteString; import io.milvus.common.clientenum.ConsistencyLevelEnum; -import io.milvus.common.utils.JacksonUtils; +import io.milvus.common.utils.JsonUtils; import io.milvus.exception.ParamException; import io.milvus.grpc.*; import io.milvus.param.collection.FieldType; @@ -46,7 +46,6 @@ * Utility functions for param classes */ public class ParamUtils { - private static final Gson GSON_INSTANCE = new Gson(); private static HashMap getTypeErrorMsgForColumnInsert() { final HashMap typeErrMsg = new HashMap<>(); @@ -263,7 +262,7 @@ public static Object checkFieldValue(FieldType fieldSchema, JsonElement value) { } int dim = fieldSchema.getDimension(); try { - List vector = GSON_INSTANCE.fromJson(value, new TypeToken>() {}.getType()); + List vector = JsonUtils.fromJson(value, new TypeToken>() {}.getType()); if (vector.size() != dim) { String msg = "Incorrect dimension for field '%s': dimension: %d is not equal to field's dimension: %d"; throw new ParamException(String.format(msg, fieldSchema.getName(), vector.size(), dim)); @@ -282,7 +281,7 @@ public static Object checkFieldValue(FieldType fieldSchema, JsonElement value) { } int dim = fieldSchema.getDimension(); try { - byte[] v = GSON_INSTANCE.fromJson(value, new TypeToken() {}.getType()); + byte[] v = JsonUtils.fromJson(value, new TypeToken() {}.getType()); int real_dim = calculateBinVectorDim(dataType, v.length); if (real_dim != dim) { String msg = "Incorrect dimension for field '%s': dimension: %d is not equal to field's dimension: %d"; @@ -300,7 +299,7 @@ public static Object checkFieldValue(FieldType fieldSchema, JsonElement value) { } try { // return SortedMap for genFieldData() - return GSON_INSTANCE.fromJson(value, new TypeToken>() {}.getType()); + return JsonUtils.fromJson(value, new TypeToken>() {}.getType()); } catch (JsonSyntaxException e) { throw new ParamException(String.format("Unable to convert JsonObject to SortedMap for field '%s'. Reason: %s", fieldSchema.getName(), e.getCause().getMessage())); @@ -368,19 +367,19 @@ public static List convertJsonArray(JsonArray jsonArray, DataType elemen try { switch (elementType) { case Int64: - return GSON_INSTANCE.fromJson(jsonArray, new TypeToken>() {}.getType()); + return JsonUtils.fromJson(jsonArray, new TypeToken>() {}.getType()); case Int32: case Int16: case Int8: - return GSON_INSTANCE.fromJson(jsonArray, new TypeToken>() {}.getType()); + return JsonUtils.fromJson(jsonArray, new TypeToken>() {}.getType()); case Bool: - return GSON_INSTANCE.fromJson(jsonArray, new TypeToken>() {}.getType()); + return JsonUtils.fromJson(jsonArray, new TypeToken>() {}.getType()); case Float: - return GSON_INSTANCE.fromJson(jsonArray, new TypeToken>() {}.getType()); + return JsonUtils.fromJson(jsonArray, new TypeToken>() {}.getType()); case Double: - return GSON_INSTANCE.fromJson(jsonArray, new TypeToken>() {}.getType()); + return JsonUtils.fromJson(jsonArray, new TypeToken>() {}.getType()); case VarChar: - return GSON_INSTANCE.fromJson(jsonArray, new TypeToken>() {}.getType()); + return JsonUtils.fromJson(jsonArray, new TypeToken>() {}.getType()); default: throw new ParamException(String.format("Unsupported element type of Array field '%s'", fieldName)); } @@ -741,18 +740,19 @@ public static SearchRequest convertSearchParam(@NonNull SearchParam requestParam if (null != requestParam.getParams() && !requestParam.getParams().isEmpty()) { try { - Map paramMap = JacksonUtils.fromJson(requestParam.getParams(),Map.class); - String offset = paramMap.getOrDefault(Constant.OFFSET, 0).toString(); - builder.addSearchParams( - KeyValuePair.newBuilder() - .setKey(Constant.OFFSET) - .setValue(offset) - .build()); - builder.addSearchParams( - KeyValuePair.newBuilder() - .setKey(Constant.PARAMS) - .setValue(requestParam.getParams()) - .build()); + Map paramMap = JsonUtils.fromJson(requestParam.getParams(), + new TypeToken>() {}.getType()); + String offset = paramMap.getOrDefault(Constant.OFFSET, 0).toString(); + builder.addSearchParams( + KeyValuePair.newBuilder() + .setKey(Constant.OFFSET) + .setValue(offset) + .build()); + builder.addSearchParams( + KeyValuePair.newBuilder() + .setKey(Constant.PARAMS) + .setValue(requestParam.getParams()) + .build()); } catch (IllegalArgumentException e) { throw new ParamException(e.getMessage() + e.getCause().getMessage()); } diff --git a/src/main/java/io/milvus/param/dml/ranker/WeightedRanker.java b/src/main/java/io/milvus/param/dml/ranker/WeightedRanker.java index f890797d6..2c3bab0ec 100644 --- a/src/main/java/io/milvus/param/dml/ranker/WeightedRanker.java +++ b/src/main/java/io/milvus/param/dml/ranker/WeightedRanker.java @@ -19,8 +19,8 @@ package io.milvus.param.dml.ranker; -import com.google.gson.*; -import com.google.gson.reflect.TypeToken; +import com.google.gson.JsonObject; +import io.milvus.common.utils.JsonUtils; import io.milvus.exception.ParamException; import lombok.Getter; @@ -45,7 +45,7 @@ private WeightedRanker(@NonNull Builder builder) { @Override public Map getProperties() { JsonObject params = new JsonObject(); - params.add("weights", new Gson().toJsonTree(this.weights).getAsJsonArray()); + params.add("weights", JsonUtils.toJsonTree(this.weights).getAsJsonArray()); Map props = new HashMap<>(); props.put("strategy", "weighted"); diff --git a/src/main/java/io/milvus/v2/service/vector/request/ranker/WeightedRanker.java b/src/main/java/io/milvus/v2/service/vector/request/ranker/WeightedRanker.java index 9047663dc..191c728f5 100644 --- a/src/main/java/io/milvus/v2/service/vector/request/ranker/WeightedRanker.java +++ b/src/main/java/io/milvus/v2/service/vector/request/ranker/WeightedRanker.java @@ -19,8 +19,8 @@ package io.milvus.v2.service.vector.request.ranker; -import com.google.gson.Gson; import com.google.gson.JsonObject; +import io.milvus.common.utils.JsonUtils; import java.util.HashMap; import java.util.List; @@ -40,7 +40,7 @@ public WeightedRanker(List weights) { @Override public Map getProperties() { JsonObject params = new JsonObject(); - params.add("weights", new Gson().toJsonTree(this.weights).getAsJsonArray()); + params.add("weights", JsonUtils.toJsonTree(this.weights).getAsJsonArray()); Map props = new HashMap<>(); props.put("strategy", "weighted"); diff --git a/src/main/java/io/milvus/v2/utils/ConvertUtils.java b/src/main/java/io/milvus/v2/utils/ConvertUtils.java index be31225df..86613926b 100644 --- a/src/main/java/io/milvus/v2/utils/ConvertUtils.java +++ b/src/main/java/io/milvus/v2/utils/ConvertUtils.java @@ -19,8 +19,8 @@ package io.milvus.v2.utils; -import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; +import io.milvus.common.utils.JsonUtils; import io.milvus.grpc.*; import io.milvus.param.Constant; import io.milvus.param.ParamUtils; @@ -97,7 +97,7 @@ public DescribeIndexResp convertToDescribeIndexResp(List respo } else if (param.getKey().equals(Constant.MMAP_ENABLED)) { properties.put(param.getKey(), param.getValue()); } else if (param.getKey().equals(Constant.PARAMS)) { - extraParams = new Gson().fromJson(param.getValue(), new TypeToken>() {}.getType()); + extraParams = JsonUtils.fromJson(param.getValue(), new TypeToken>() {}.getType()); } } diff --git a/src/main/java/io/milvus/v2/utils/VectorUtils.java b/src/main/java/io/milvus/v2/utils/VectorUtils.java index f7f599946..59bfa5b83 100644 --- a/src/main/java/io/milvus/v2/utils/VectorUtils.java +++ b/src/main/java/io/milvus/v2/utils/VectorUtils.java @@ -19,13 +19,12 @@ package io.milvus.v2.utils; -import com.google.gson.Gson; import com.google.protobuf.ByteString; +import io.milvus.common.utils.JsonUtils; import io.milvus.v2.common.ConsistencyLevel; import io.milvus.exception.ParamException; import io.milvus.grpc.*; import io.milvus.param.Constant; -import io.milvus.param.MetricType; import io.milvus.param.ParamUtils; import io.milvus.v2.service.vector.request.*; import io.milvus.v2.service.vector.request.ranker.BaseRanker; @@ -37,7 +36,6 @@ import java.util.*; public class VectorUtils { - private static final Gson GSON_INSTANCE = new Gson(); public QueryRequest ConvertToGrpcQueryRequest(QueryReq request){ QueryRequest.Builder builder = QueryRequest.newBuilder() @@ -155,7 +153,7 @@ public SearchRequest ConvertToGrpcSearchRequest(SearchReq request) { if (null != request.getSearchParams()) { try { - String searchParams = GSON_INSTANCE.toJson(request.getSearchParams()); + String searchParams = JsonUtils.toJson(request.getSearchParams()); builder.addSearchParams( KeyValuePair.newBuilder() .setKey(Constant.PARAMS) diff --git a/src/test/java/io/milvus/client/MilvusClientDockerTest.java b/src/test/java/io/milvus/client/MilvusClientDockerTest.java index ead34c163..b2b182f83 100644 --- a/src/test/java/io/milvus/client/MilvusClientDockerTest.java +++ b/src/test/java/io/milvus/client/MilvusClientDockerTest.java @@ -28,6 +28,7 @@ import io.milvus.bulkwriter.common.utils.ParquetReaderUtils; import io.milvus.common.clientenum.ConsistencyLevelEnum; import io.milvus.common.utils.Float16Utils; +import io.milvus.common.utils.JsonUtils; import io.milvus.exception.ParamException; import io.milvus.grpc.*; import io.milvus.orm.iterator.QueryIterator; @@ -79,8 +80,6 @@ class MilvusClientDockerTest { protected static final float FLOAT16_PRECISION = 0.001f; protected static final float BFLOAT16_PRECISION = 0.01f; - protected static final Gson GSON_INSTANCE = new Gson(); - private static final Random RANDOM = new Random(); @Container @@ -477,22 +476,22 @@ private List generateRowsData(CollectionSchemaParam schema, int coun row.add(fieldType.getName(), info); break; case Array: - row.add(fieldType.getName(), GSON_INSTANCE.toJsonTree(generateRandomArray(fieldType))); + row.add(fieldType.getName(), JsonUtils.toJsonTree(generateRandomArray(fieldType))); break; case FloatVector: - row.add(fieldType.getName(), GSON_INSTANCE.toJsonTree(generateFloatVector())); + row.add(fieldType.getName(), JsonUtils.toJsonTree(generateFloatVector())); break; case BinaryVector: - row.add(fieldType.getName(), GSON_INSTANCE.toJsonTree(generateBinaryVector().array())); + row.add(fieldType.getName(), JsonUtils.toJsonTree(generateBinaryVector().array())); break; case Float16Vector: - row.add(fieldType.getName(), GSON_INSTANCE.toJsonTree(generateFloat16Vector().array())); + row.add(fieldType.getName(), JsonUtils.toJsonTree(generateFloat16Vector().array())); break; case BFloat16Vector: - row.add(fieldType.getName(), GSON_INSTANCE.toJsonTree(generateBFloat16Vector().array())); + row.add(fieldType.getName(), JsonUtils.toJsonTree(generateBFloat16Vector().array())); break; case SparseFloatVector: - row.add(fieldType.getName(), GSON_INSTANCE.toJsonTree(generateSparseVector())); + row.add(fieldType.getName(), JsonUtils.toJsonTree(generateSparseVector())); break; default: Assertions.fail(); @@ -1270,9 +1269,9 @@ void testFloat16Vector() { List vector = vectors.get(i + 5000); ByteBuffer fp16Vector = Float16Utils.f32VectorToFp16Buffer(vector); - row.add(DataType.Float16Vector.name(), GSON_INSTANCE.toJsonTree(fp16Vector.array())); + row.add(DataType.Float16Vector.name(), JsonUtils.toJsonTree(fp16Vector.array())); ByteBuffer bf16Vector = Float16Utils.f32VectorToBf16Buffer(vector); - row.add(DataType.BFloat16Vector.name(), GSON_INSTANCE.toJsonTree(bf16Vector.array())); + row.add(DataType.BFloat16Vector.name(), JsonUtils.toJsonTree(bf16Vector.array())); rows.add(row); } @@ -2244,7 +2243,7 @@ void testArrayField() { JsonObject row = new JsonObject(); row.addProperty("id", 10000L + (long)i); List vector = generateFloatVectors(1).get(0); - row.add(DataType.FloatVector.name(), GSON_INSTANCE.toJsonTree(vector)); + row.add(DataType.FloatVector.name(), JsonUtils.toJsonTree(vector)); List strArray = new ArrayList<>(); List intArray = new ArrayList<>(); @@ -2254,9 +2253,9 @@ void testArrayField() { intArray.add(i*10000 + k); floatArray.add((float)k/1000 + i); } - row.add(varcharArrayName, GSON_INSTANCE.toJsonTree(strArray)); - row.add(intArrayName, GSON_INSTANCE.toJsonTree(intArray)); - row.add(floatArrayName, GSON_INSTANCE.toJsonTree(floatArray)); + row.add(varcharArrayName, JsonUtils.toJsonTree(strArray)); + row.add(intArrayName, JsonUtils.toJsonTree(intArray)); + row.add(floatArrayName, JsonUtils.toJsonTree(floatArray)); rows.add(row); } @@ -2358,7 +2357,7 @@ void testUpsert() throws InterruptedException { JsonObject row = new JsonObject(); row.addProperty("id", i); List vector = generateFloatVectors(1).get(0); - row.add(DataType.FloatVector.name(), GSON_INSTANCE.toJsonTree(vector)); + row.add(DataType.FloatVector.name(), JsonUtils.toJsonTree(vector)); row.addProperty(DataType.VarChar.name(), String.format("name_%d", i)); row.addProperty("dynamic_value", String.format("dynamic_%d", i)); rows.add(row); @@ -2432,7 +2431,7 @@ void testUpsert() throws InterruptedException { JsonObject row = new JsonObject(); row.addProperty("id", rowCount + i); List vector = generateFloatVectors(1).get(0); - row.add(DataType.FloatVector.name(), GSON_INSTANCE.toJsonTree(vector)); + row.add(DataType.FloatVector.name(), JsonUtils.toJsonTree(vector)); row.addProperty(DataType.VarChar.name(), String.format("name_%d", rowCount + i)); rows.add(row); } @@ -2474,14 +2473,14 @@ void testUpsert() throws InterruptedException { JsonObject row = new JsonObject(); row.addProperty("id", 5L); List vector = generateFloatVectors(1).get(0); - row.add(DataType.FloatVector.name(), GSON_INSTANCE.toJsonTree(vector)); + row.add(DataType.FloatVector.name(), JsonUtils.toJsonTree(vector)); row.addProperty(DataType.VarChar.name(), "updated_5"); row.addProperty("dynamic_value", String.format("dynamic_%d", 5)); rows.add(row); row = new JsonObject(); row.addProperty("id", 18L); vector = generateFloatVectors(1).get(0); - row.add(DataType.FloatVector.name(), GSON_INSTANCE.toJsonTree(vector)); + row.add(DataType.FloatVector.name(), JsonUtils.toJsonTree(vector)); row.addProperty(DataType.VarChar.name(), "updated_18"); row.addProperty("dynamic_value", 18); rows.add(row); @@ -2669,7 +2668,7 @@ void testCollectionHighLevelGet(FieldType primaryField, FieldType vectorField) { row.addProperty(primaryField.getName(), String.valueOf(i)); } List vector = generateFloatVectors(1).get(0); - row.add(vectorField.getName(), GSON_INSTANCE.toJsonTree(vector)); + row.add(vectorField.getName(), JsonUtils.toJsonTree(vector)); rows.add(row); primaryIds.add(String.valueOf(i)); } @@ -2734,7 +2733,7 @@ void testCollectionHighLevelDelete(FieldType primaryField, FieldType vectorField row.addProperty(primaryField.getName(), String.valueOf(i)); } List vector = generateFloatVectors(1).get(0); - row.add(vectorField.getName(), GSON_INSTANCE.toJsonTree(vector)); + row.add(vectorField.getName(), JsonUtils.toJsonTree(vector)); rows.add(row); primaryIds.add(String.valueOf(i)); } @@ -2821,13 +2820,13 @@ public void testBulkWriter() { JsonObject obj = new JsonObject(); obj.addProperty("dummy", i); row.add(DataType.JSON.name(), obj); - row.add(DataType.Array.name() + "_varchar", GSON_INSTANCE.toJsonTree(Lists.newArrayList("aaa", "bbb", "ccc"))); - row.add(DataType.Array.name() + "_int32", GSON_INSTANCE.toJsonTree(Lists.newArrayList(5, 6, 3, 2, 1))); - row.add(DataType.Array.name() + "_float", GSON_INSTANCE.toJsonTree(Lists.newArrayList(0.5, 1.8))); - row.add(DataType.FloatVector.name(), GSON_INSTANCE.toJsonTree(generateFloatVector())); - row.add(DataType.BinaryVector.name(), GSON_INSTANCE.toJsonTree(generateBinaryVector().array())); - row.add(DataType.BFloat16Vector.name(), GSON_INSTANCE.toJsonTree(generateBFloat16Vector().array())); - row.add(DataType.SparseFloatVector.name(), GSON_INSTANCE.toJsonTree(generateSparseVector())); + row.add(DataType.Array.name() + "_varchar", JsonUtils.toJsonTree(Lists.newArrayList("aaa", "bbb", "ccc"))); + row.add(DataType.Array.name() + "_int32", JsonUtils.toJsonTree(Lists.newArrayList(5, 6, 3, 2, 1))); + row.add(DataType.Array.name() + "_float", JsonUtils.toJsonTree(Lists.newArrayList(0.5, 1.8))); + row.add(DataType.FloatVector.name(), JsonUtils.toJsonTree(generateFloatVector())); + row.add(DataType.BinaryVector.name(), JsonUtils.toJsonTree(generateBinaryVector().array())); + row.add(DataType.BFloat16Vector.name(), JsonUtils.toJsonTree(generateBFloat16Vector().array())); + row.add(DataType.SparseFloatVector.name(), JsonUtils.toJsonTree(generateSparseVector())); if (enabledDynamic) { row.addProperty("dynamic_1", i); @@ -2884,11 +2883,10 @@ public void testIterator() { // insert data int rowCount = 1000; List rows = new ArrayList<>(); - Gson gson = new Gson(); for (long i = 0L; i < rowCount; ++i) { JsonObject row = new JsonObject(); row.addProperty("id", Long.toString(i)); - row.add(DataType.FloatVector.name(), gson.toJsonTree(generateFloatVectors(1).get(0))); + row.add(DataType.FloatVector.name(), JsonUtils.toJsonTree(generateFloatVectors(1).get(0))); JsonObject json = new JsonObject(); if (i%2 == 0) { json.addProperty("even", true); @@ -3060,7 +3058,7 @@ void testCacheCollectionSchema() { // insert JsonObject row = new JsonObject(); - row.add("vector", GSON_INSTANCE.toJsonTree(generateFloatVectors(1).get(0))); + row.add("vector", JsonUtils.toJsonTree(generateFloatVectors(1).get(0))); R insertR = client.insert(InsertParam.newBuilder() .withCollectionName(randomCollectionName) .withRows(Collections.singletonList(row)) diff --git a/src/test/java/io/milvus/v2/client/MilvusClientV2DockerTest.java b/src/test/java/io/milvus/v2/client/MilvusClientV2DockerTest.java index e4bd1af44..0c7d757f7 100644 --- a/src/test/java/io/milvus/v2/client/MilvusClientV2DockerTest.java +++ b/src/test/java/io/milvus/v2/client/MilvusClientV2DockerTest.java @@ -24,6 +24,7 @@ import com.google.gson.reflect.TypeToken; import io.milvus.common.utils.Float16Utils; +import io.milvus.common.utils.JsonUtils; import io.milvus.orm.iterator.QueryIterator; import io.milvus.orm.iterator.SearchIterator; import io.milvus.param.Constant; @@ -75,8 +76,6 @@ class MilvusClientV2DockerTest { private static RandomStringGenerator generator; private static final int dimension = 256; - private static final Gson GSON_INSTANCE = new Gson(); - private static final Random RANDOM = new Random(); @Container @@ -244,7 +243,7 @@ private JsonArray generateRandomArray(CreateCollectionReq.FieldSchema field) { for (int i = 0; i < eleCnt; i++) { values.add(i%10 == 0); } - return GSON_INSTANCE.toJsonTree(values).getAsJsonArray(); + return JsonUtils.toJsonTree(values).getAsJsonArray(); } case Int8: case Int16: { @@ -252,42 +251,42 @@ private JsonArray generateRandomArray(CreateCollectionReq.FieldSchema field) { for (int i = 0; i < eleCnt; i++) { values.add((short)RANDOM.nextInt(256)); } - return GSON_INSTANCE.toJsonTree(values).getAsJsonArray(); + return JsonUtils.toJsonTree(values).getAsJsonArray(); } case Int32: { List values = new ArrayList<>(); for (int i = 0; i < eleCnt; i++) { values.add(RANDOM.nextInt()); } - return GSON_INSTANCE.toJsonTree(values).getAsJsonArray(); + return JsonUtils.toJsonTree(values).getAsJsonArray(); } case Int64: { List values = new ArrayList<>(); for (int i = 0; i < eleCnt; i++) { values.add(RANDOM.nextLong()); } - return GSON_INSTANCE.toJsonTree(values).getAsJsonArray(); + return JsonUtils.toJsonTree(values).getAsJsonArray(); } case Float: { List values = new ArrayList<>(); for (int i = 0; i < eleCnt; i++) { values.add(RANDOM.nextFloat()); } - return GSON_INSTANCE.toJsonTree(values).getAsJsonArray(); + return JsonUtils.toJsonTree(values).getAsJsonArray(); } case Double: { List values = new ArrayList<>(); for (int i = 0; i < eleCnt; i++) { values.add(RANDOM.nextDouble()); } - return GSON_INSTANCE.toJsonTree(values).getAsJsonArray(); + return JsonUtils.toJsonTree(values).getAsJsonArray(); } case VarChar: { List values = new ArrayList<>(); for (int i = 0; i < eleCnt; i++) { values.add(String.format("varchar_arr_%d", i)); } - return GSON_INSTANCE.toJsonTree(values).getAsJsonArray(); + return JsonUtils.toJsonTree(values).getAsJsonArray(); } default: Assertions.fail(); @@ -330,7 +329,7 @@ private List generateRandomData(CreateCollectionReq.CollectionSchema case JSON: { JsonObject jsonObj = new JsonObject(); jsonObj.addProperty(String.format("JSON_%d", i), i); - jsonObj.add("flags", GSON_INSTANCE.toJsonTree(new long[]{i, i+1, i + 2})); + jsonObj.add("flags", JsonUtils.toJsonTree(new long[]{i, i+1, i + 2})); row.add(field.getName(), jsonObj); break; } @@ -341,27 +340,27 @@ private List generateRandomData(CreateCollectionReq.CollectionSchema } case FloatVector: { List vector = generateFolatVector(); - row.add(field.getName(), GSON_INSTANCE.toJsonTree(vector)); + row.add(field.getName(), JsonUtils.toJsonTree(vector)); break; } case BinaryVector: { ByteBuffer vector = generateBinaryVector(); - row.add(field.getName(), GSON_INSTANCE.toJsonTree(vector.array())); + row.add(field.getName(), JsonUtils.toJsonTree(vector.array())); break; } case Float16Vector: { ByteBuffer vector = generateFloat16Vector(); - row.add(field.getName(), GSON_INSTANCE.toJsonTree(vector.array())); + row.add(field.getName(), JsonUtils.toJsonTree(vector.array())); break; } case BFloat16Vector: { ByteBuffer vector = generateBFloat16Vector(); - row.add(field.getName(), GSON_INSTANCE.toJsonTree(vector.array())); + row.add(field.getName(), JsonUtils.toJsonTree(vector.array())); break; } case SparseFloatVector: { SortedMap vector = generateSparseVector(); - row.add(field.getName(), GSON_INSTANCE.toJsonTree(vector)); + row.add(field.getName(), JsonUtils.toJsonTree(vector)); break; } default: @@ -395,13 +394,13 @@ private void verifyOutput(JsonObject row, Map entity) { Assertions.assertEquals(row.get("json_field").toString(), jsn.toString()); List arrInt = (List) entity.get("arr_int_field"); - List arrIntOri = GSON_INSTANCE.fromJson(row.get("arr_int_field"), new TypeToken>() {}.getType()); + List arrIntOri = JsonUtils.fromJson(row.get("arr_int_field"), new TypeToken>() {}.getType()); Assertions.assertEquals(arrIntOri, arrInt); List arrFloat = (List) entity.get("arr_float_field"); - List arrFloatOri = GSON_INSTANCE.fromJson(row.get("arr_float_field"), new TypeToken>() {}.getType()); + List arrFloatOri = JsonUtils.fromJson(row.get("arr_float_field"), new TypeToken>() {}.getType()); Assertions.assertEquals(arrFloatOri, arrFloat); List arrStr = (List) entity.get("arr_varchar_field"); - List arrStrOri = GSON_INSTANCE.fromJson(row.get("arr_varchar_field"), new TypeToken>() {}.getType()); + List arrStrOri = JsonUtils.fromJson(row.get("arr_varchar_field"), new TypeToken>() {}.getType()); Assertions.assertEquals(arrStrOri, arrStr); } @@ -531,7 +530,7 @@ void testFloatVectors() { for (int i = 0; i < nq; i++) { JsonObject row = data.get(RANDOM.nextInt((int)count)); targetIDs.add(row.get("id").getAsLong()); - List vector = GSON_INSTANCE.fromJson(row.get(vectorFieldName), new TypeToken>() {}.getType()); + List vector = JsonUtils.fromJson(row.get(vectorFieldName), new TypeToken>() {}.getType()); targetVectors.add(new FloatVec(vector)); } @@ -638,7 +637,7 @@ void testBinaryVectors() throws InterruptedException { for (int i = 0; i < nq; i++) { JsonObject row = data.get(RANDOM.nextInt((int)count)); targetIDs.add(row.get("id").getAsLong()); - byte[] vector = GSON_INSTANCE.fromJson(row.get(vectorFieldName), new TypeToken() {}.getType()); + byte[] vector = JsonUtils.fromJson(row.get(vectorFieldName), new TypeToken() {}.getType()); targetVectors.add(new BinaryVec(vector)); } SearchResp searchResp = client.search(SearchReq.builder() @@ -727,8 +726,8 @@ void testFloat16Vectors() { originVector.add((float)1/(i+1)); } System.out.println("Original float32 vector: " + originVector); - row.add(float16Field, GSON_INSTANCE.toJsonTree(Float16Utils.f32VectorToFp16Buffer(originVector).array())); - row.add(bfloat16Field, GSON_INSTANCE.toJsonTree(Float16Utils.f32VectorToBf16Buffer(originVector).array())); + row.add(float16Field, JsonUtils.toJsonTree(Float16Utils.f32VectorToFp16Buffer(originVector).array())); + row.add(bfloat16Field, JsonUtils.toJsonTree(Float16Utils.f32VectorToBf16Buffer(originVector).array())); UpsertResp upsertResp = client.upsert(UpsertReq.builder() .collectionName(randomCollectionName) @@ -845,7 +844,7 @@ void testSparseVectors() { for (int i = 0; i < nq; i++) { JsonObject row = data.get(RANDOM.nextInt((int)count)); targetIDs.add(row.get("id").getAsLong()); - SortedMap vector = GSON_INSTANCE.fromJson(row.get(vectorFieldName), new TypeToken>() {}.getType()); + SortedMap vector = JsonUtils.fromJson(row.get(vectorFieldName), new TypeToken>() {}.getType()); targetVectors.add(new SparseFloatVec(vector)); } SearchResp searchResp = client.search(SearchReq.builder() @@ -1011,11 +1010,10 @@ void testDeleteUpsert() { // insert List data = new ArrayList<>(); - Gson gson = new Gson(); for (int i = 0; i < 10; i++) { JsonObject row = new JsonObject(); row.addProperty("pk", String.format("pk_%d", i)); - row.add("float_vector", gson.toJsonTree(new float[]{(float)i, (float)(i + 1), (float)(i + 2), (float)(i + 3)})); + row.add("float_vector", JsonUtils.toJsonTree(new float[]{(float)i, (float)(i + 1), (float)(i + 2), (float)(i + 3)})); data.add(row); } @@ -1040,11 +1038,11 @@ void testDeleteUpsert() { List dataUpdate = new ArrayList<>(); JsonObject row1 = new JsonObject(); row1.addProperty("pk", "pk_5"); - row1.add("float_vector", gson.toJsonTree(new float[]{5.0f, 5.0f, 5.0f, 5.0f})); + row1.add("float_vector", JsonUtils.toJsonTree(new float[]{5.0f, 5.0f, 5.0f, 5.0f})); dataUpdate.add(row1); JsonObject row2 = new JsonObject(); row2.addProperty("pk", "pk_2"); - row2.add("float_vector", gson.toJsonTree(new float[]{2.0f, 2.0f, 2.0f, 2.0f})); + row2.add("float_vector", JsonUtils.toJsonTree(new float[]{2.0f, 2.0f, 2.0f, 2.0f})); dataUpdate.add(row2); UpsertResp upsertResp = client.upsert(UpsertReq.builder() .collectionName(randomCollectionName) @@ -1330,7 +1328,7 @@ void testCacheCollectionSchema() { // insert JsonObject row = new JsonObject(); - row.add("vector", GSON_INSTANCE.toJsonTree(generateFloatVectors(1).get(0))); + row.add("vector", JsonUtils.toJsonTree(generateFloatVectors(1).get(0))); InsertResp insertResp = client.insert(InsertReq.builder() .collectionName(randomCollectionName) .data(Collections.singletonList(row)) @@ -1360,7 +1358,7 @@ void testCacheCollectionSchema() { for (int i = 0; i < 100; ++i) { vector.add(RANDOM.nextFloat()); } - row.add("vector", GSON_INSTANCE.toJsonTree(vector)); + row.add("vector", JsonUtils.toJsonTree(vector)); insertResp = client.insert(InsertReq.builder() .collectionName(randomCollectionName) .data(Collections.singletonList(row)) @@ -1752,7 +1750,6 @@ void testMultiThreadsInsert() { System.out.println("Collection created"); try { - Gson gson = new Gson(); Random rand = new Random(); List threadList = new ArrayList<>(); for (int k = 0; k < 10; k++) { @@ -1764,7 +1761,7 @@ void testMultiThreadsInsert() { JsonObject obj = new JsonObject(); obj.addProperty("id", String.format("%d", i*cnt + j)); List vector = generateFolatVector(dim); - obj.add("vector", gson.toJsonTree(vector)); + obj.add("vector", JsonUtils.toJsonTree(vector)); obj.addProperty("dataTime", System.currentTimeMillis()); rows.add(obj); } @@ -1797,7 +1794,6 @@ void testMultiThreadsInsert() { } try { - Gson gson = new Gson(); Random rand = new Random(); List threadList = new ArrayList<>(); for (int k = 0; k < 10; k++) { @@ -1809,7 +1805,7 @@ void testMultiThreadsInsert() { JsonObject obj = new JsonObject(); obj.addProperty("id", String.format("%d", i*cnt + j)); List vector = generateFolatVector(dim); - obj.add("vector", gson.toJsonTree(vector)); + obj.add("vector", JsonUtils.toJsonTree(vector)); obj.addProperty("dataTime", System.currentTimeMillis()); rows.add(obj); } diff --git a/src/test/java/io/milvus/v2/service/vector/VectorTest.java b/src/test/java/io/milvus/v2/service/vector/VectorTest.java index e6f24546d..c1125e619 100644 --- a/src/test/java/io/milvus/v2/service/vector/VectorTest.java +++ b/src/test/java/io/milvus/v2/service/vector/VectorTest.java @@ -20,6 +20,7 @@ package io.milvus.v2.service.vector; import com.google.gson.*; +import io.milvus.common.utils.JsonUtils; import io.milvus.v2.BaseTest; import io.milvus.v2.service.vector.request.*; import io.milvus.v2.service.vector.request.data.FloatVec; @@ -40,13 +41,12 @@ class VectorTest extends BaseTest { void testInsert() { List data = new ArrayList<>(); - Gson gson = new Gson(); for (int i = 0; i < 100; i++) { JsonObject vector = new JsonObject(); List vectorList = new ArrayList<>(); vectorList.add(1.0f); vectorList.add(2.0f); - vector.add("vector", gson.toJsonTree(vectorList)); + vector.add("vector", JsonUtils.toJsonTree(vectorList)); vector.addProperty("id", (long) i); data.add(vector); } @@ -66,7 +66,7 @@ void testUpsert() { List vectorList = new ArrayList<>(); vectorList.add(2.0f); vectorList.add(3.0f); - jsonObject.add("vector", new Gson().toJsonTree(vectorList)); + jsonObject.add("vector", JsonUtils.toJsonTree(vectorList)); jsonObject.addProperty("id", 0L); UpsertReq request = UpsertReq.builder() .collectionName("test")