Skip to content

Commit

Permalink
Remove Jackson dependence (#1109)
Browse files Browse the repository at this point in the history
Signed-off-by: yhmo <[email protected]>
  • Loading branch information
yhmo authored Oct 16, 2024
1 parent 05853a6 commit 569d577
Show file tree
Hide file tree
Showing 18 changed files with 191 additions and 228 deletions.
3 changes: 1 addition & 2 deletions examples/main/java/io/milvus/v1/HighLevelExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -95,7 +94,7 @@ private R<RpcStatus> createCollection() {

R<RpcStatus> response = milvusClient.createCollection(createSimpleCollectionParam);
CommonUtils.handleResponseStatus(response);
System.out.println(JacksonUtils.toJsonString(response.getData()));
System.out.println(response);
return response;
}

Expand Down
5 changes: 5 additions & 0 deletions examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@
<version>1.7.36</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-csv</artifactId>
<version>2.16.1</version>
</dependency>
</dependencies>

</project>
21 changes: 0 additions & 21 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@
<maven.surefire.plugin.version>2.19.1</maven.surefire.plugin.version>
<junit.platform.version>1.1.0</junit.platform.version>
<junit.jupiter.engine.version>5.10.1</junit.jupiter.engine.version>
<jackson.version>2.16.1</jackson.version>
<gson.version>2.10.1</gson.version>
<kotlin.version>1.9.10</kotlin.version>
<mockito.version>4.11.0</mockito.version>
Expand Down Expand Up @@ -126,21 +125,6 @@
<artifactId>slf4j-api</artifactId>
<version>${slf4j.api.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
Expand Down Expand Up @@ -341,11 +325,6 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-csv</artifactId>
<version>${jackson.version}</version>
</dependency>

<!-- storage sdk-->
<dependency>
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/io/milvus/bulkwriter/Buffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -57,7 +57,6 @@ public class Buffer {
private Map<String, List<Object>> buffer;
private Map<String, FieldType> fields;

private static final Gson GSON_INSTANCE = new Gson();

public Buffer(CollectionSchemaParam collectionSchema, BulkFileType fileType) {
this.collectionSchema = collectionSchema;
Expand Down Expand Up @@ -312,7 +311,7 @@ private static void addBinaryVector(Group group, String fieldName, ByteBuffer by

private static void addSparseVector(Group group, String fieldName, SortedMap<Long, Float> 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);
}
}
15 changes: 7 additions & 8 deletions src/main/java/io/milvus/bulkwriter/BulkImport.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,44 +19,43 @@

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<String, Object> params = GSON_INSTANCE.fromJson(GSON_INSTANCE.toJson(request), new TypeToken<Map<String, Object>>() {}.getType());
Map<String, Object> params = JsonUtils.fromJson(JsonUtils.toJson(request), new TypeToken<Map<String, Object>>() {}.getType());
String body = postRequest(requestURL, request.getApiKey(), params, 60 * 1000);
RestfulResponse<Object> response = GSON_INSTANCE.fromJson(body, new TypeToken<RestfulResponse<Object>>(){}.getType());
RestfulResponse<Object> response = JsonUtils.fromJson(body, new TypeToken<RestfulResponse<Object>>(){}.getType());
handleResponse(requestURL, response);
return body;
}

public static String getImportProgress(String url, BaseDescribeImportRequest request) {
String requestURL = url + "/v2/vectordb/jobs/import/describe";

Map<String, Object> params = GSON_INSTANCE.fromJson(GSON_INSTANCE.toJson(request), new TypeToken<Map<String, Object>>() {}.getType());
Map<String, Object> params = JsonUtils.fromJson(JsonUtils.toJson(request), new TypeToken<Map<String, Object>>() {}.getType());
String body = postRequest(requestURL, request.getApiKey(), params, 60 * 1000);
RestfulResponse<Object> response = GSON_INSTANCE.fromJson(body, new TypeToken<RestfulResponse<Object>>(){}.getType());
RestfulResponse<Object> response = JsonUtils.fromJson(body, new TypeToken<RestfulResponse<Object>>(){}.getType());
handleResponse(requestURL, response);
return body;
}

public static String listImportJobs(String url, BaseListImportJobsRequest request) {
String requestURL = url + "/v2/vectordb/jobs/import/list";

Map<String, Object> params = GSON_INSTANCE.fromJson(GSON_INSTANCE.toJson(request), new TypeToken<Map<String, Object>>() {}.getType());
Map<String, Object> params = JsonUtils.fromJson(JsonUtils.toJson(request), new TypeToken<Map<String, Object>>() {}.getType());
String body = postRequest(requestURL, request.getApiKey(), params, 60 * 1000);
RestfulResponse<Object> response = GSON_INSTANCE.fromJson(body, new TypeToken<RestfulResponse<Object>>(){}.getType());
RestfulResponse<Object> response = JsonUtils.fromJson(body, new TypeToken<RestfulResponse<Object>>(){}.getType());
handleResponse(requestURL, response);
return body;
}
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/io/milvus/bulkwriter/BulkWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/milvus/client/AbstractMilvusGrpcClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -3288,7 +3288,7 @@ public R<SearchResponse> 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();

Expand Down
83 changes: 0 additions & 83 deletions src/main/java/io/milvus/common/utils/JacksonUtils.java

This file was deleted.

72 changes: 72 additions & 0 deletions src/main/java/io/milvus/common/utils/JsonUtils.java
Original file line number Diff line number Diff line change
@@ -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> T fromJson(String jsonStr, Class<T> classOfT) {
return GSON_INSTANCE.fromJson(jsonStr, classOfT);
}

public static <T> T fromJson(String jsonStr, Type typeOfT) {
return GSON_INSTANCE.fromJson(jsonStr, typeOfT);
}

public static <T> T fromJson(String jsonStr, TypeToken<T> typeOfT) {
return GSON_INSTANCE.fromJson(jsonStr, typeOfT);
}


public static <T> T fromJson(JsonElement jsonElement, Class<T> classOfT) {
return GSON_INSTANCE.fromJson(jsonElement, classOfT);
}

public static <T> T fromJson(JsonElement jsonElement, Type typeOfT) {
return GSON_INSTANCE.fromJson(jsonElement, typeOfT);
}

public static <T> T fromJson(JsonElement jsonElement, TypeToken<T> 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 <T> JsonElement toJsonTree(T obj) {
return GSON_INSTANCE.toJsonTree(obj, new TypeToken<T>() {}.getType());
}
}
17 changes: 10 additions & 7 deletions src/main/java/io/milvus/orm/iterator/SearchIterator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -146,8 +146,7 @@ private void initParams() {
if (null != searchIteratorParam.getParams() && !searchIteratorParam.getParams().isEmpty()) {
params = new HashMap<>();
}
params = JacksonUtils.fromJson(searchIteratorParam.getParams(), new TypeReference<Map<String, Object>>() {
});
params = JsonUtils.fromJson(searchIteratorParam.getParams(), new TypeToken<Map<String, Object>>() {}.getType());
}

private void checkForSpecialIndexParam() {
Expand Down Expand Up @@ -246,7 +245,7 @@ private SearchResultsWrapper executeNextSearch(Map<String, Object> 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)
Expand Down Expand Up @@ -403,8 +402,12 @@ private List<QueryResultsWrapper.RowRecord> trySearchFill() {

private Map<String, Object> nextParams(int coefficient) {
coefficient = Math.max(1, coefficient);
Map<String, Object> nextParams = JacksonUtils.fromJson(JacksonUtils.toJsonString(params), new TypeReference<Map<String, Object>>() {
});

// Note: params is a Map<String, Object> 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<String, Object>.
Map<String, Object> nextParams = JsonUtils.fromJson(JsonUtils.toJson(params),
new TypeToken<Map<String, Object>>() {}.getType());

if (metricsPositiveRelated(metricType)) {
float nextRadius = tailBand + width * coefficient;
Expand Down
Loading

0 comments on commit 569d577

Please sign in to comment.