|
@@ -1,11 +1,6 @@
|
|
|
package io.milvus.client;
|
|
|
|
|
|
-import io.grpc.ManagedChannel;
|
|
|
-import io.grpc.ManagedChannelBuilder;
|
|
|
-import io.grpc.StatusRuntimeException;
|
|
|
-
|
|
|
-import io.milvus.client.params.*;
|
|
|
-import io.milvus.client.response.*;
|
|
|
+import io.grpc.*;
|
|
|
|
|
|
import javax.annotation.Nonnull;
|
|
|
import java.text.SimpleDateFormat;
|
|
@@ -14,25 +9,21 @@ import java.util.concurrent.TimeUnit;
|
|
|
import java.util.logging.Level;
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
|
-public class MilvusGrpcClient {
|
|
|
+public class MilvusGrpcClient implements MilvusClient {
|
|
|
|
|
|
private static final Logger logger = Logger.getLogger(MilvusGrpcClient.class.getName());
|
|
|
|
|
|
- private final ManagedChannel channel;
|
|
|
- private final io.milvus.client.grpc.MilvusServiceGrpc.MilvusServiceBlockingStub blockingStub;
|
|
|
- private final io.milvus.client.grpc.MilvusServiceGrpc.MilvusServiceFutureStub futureStub;
|
|
|
- private final io.milvus.client.grpc.MilvusServiceGrpc.MilvusServiceStub asyncStub;
|
|
|
+ private ManagedChannel channel = null;
|
|
|
+ private io.milvus.client.grpc.MilvusServiceGrpc.MilvusServiceBlockingStub blockingStub;
|
|
|
|
|
|
- public MilvusGrpcClient(String host, int port) {
|
|
|
- this(ManagedChannelBuilder.forAddress(host, port).usePlaintext());
|
|
|
- }
|
|
|
-
|
|
|
- public MilvusGrpcClient(ManagedChannelBuilder<?> channelBuilder) {
|
|
|
- channel = channelBuilder.build();
|
|
|
- blockingStub = io.milvus.client.grpc.MilvusServiceGrpc.newBlockingStub(channel);
|
|
|
- futureStub = io.milvus.client.grpc.MilvusServiceGrpc.newFutureStub(channel);
|
|
|
- asyncStub = io.milvus.client.grpc.MilvusServiceGrpc.newStub(channel);
|
|
|
- }
|
|
|
+// public MilvusGrpcClient(String host, int port) {
|
|
|
+// this(ManagedChannelBuilder.forAddress(host, port).usePlaintext());
|
|
|
+// }
|
|
|
+//
|
|
|
+// public MilvusGrpcClient(ManagedChannelBuilder<?> channelBuilder) {
|
|
|
+// channel = channelBuilder.build();
|
|
|
+// blockingStub = io.milvus.client.grpc.MilvusServiceGrpc.newBlockingStub(channel);
|
|
|
+// }
|
|
|
|
|
|
public void shutdown() throws InterruptedException {
|
|
|
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
|
|
@@ -41,20 +32,56 @@ public class MilvusGrpcClient {
|
|
|
|
|
|
///////////////////////Client Calls///////////////////////
|
|
|
|
|
|
- public Response createTable(@Nonnull TableSchema tableSchema) {
|
|
|
+ @Override
|
|
|
+ public Response connect(ConnectParam connectParam) {
|
|
|
+ if (channel != null) {
|
|
|
+ logWarning("You have already connected!");
|
|
|
+ return new Response(Response.Status.CONNECT_FAILED, "You have already connected!");
|
|
|
+ } else {
|
|
|
+ channel = ManagedChannelBuilder
|
|
|
+ .forAddress(connectParam.getHost(), Integer.parseInt(connectParam.getPort()))
|
|
|
+ .usePlaintext()
|
|
|
+ .build();
|
|
|
+ }
|
|
|
+
|
|
|
+ blockingStub = io.milvus.client.grpc.MilvusServiceGrpc.newBlockingStub(channel);
|
|
|
+ logInfo("Connected successfully!\n{0}", connectParam.toString());
|
|
|
+ return new Response(Response.Status.SUCCESS);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean connected() {
|
|
|
+ return channel != null && !channel.isShutdown() && !channel.isTerminated();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Response disconnect() {
|
|
|
+ if (!connected()) {
|
|
|
+ logWarning("You are not connected");
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ channel.shutdown();
|
|
|
+ }
|
|
|
+ return new Response(Response.Status.SUCCESS);
|
|
|
+ }
|
|
|
|
|
|
+ @Override
|
|
|
+ public Response createTable(@Nonnull TableSchemaParam tableSchemaParam) {
|
|
|
+ TableSchema tableSchema = tableSchemaParam.getTableSchema();
|
|
|
io.milvus.client.grpc.TableSchema request = io.milvus.client.grpc.TableSchema
|
|
|
- .newBuilder()
|
|
|
- .setTableName(tableSchema.getTableName())
|
|
|
- .setDimension(tableSchema.getDimension())
|
|
|
- .setIndexFileSize(tableSchema.getIndexFileSize())
|
|
|
- .setMetricType(tableSchema.getMetricType().getVal())
|
|
|
- .build();
|
|
|
+ .newBuilder()
|
|
|
+ .setTableName(tableSchema.getTableName())
|
|
|
+ .setDimension(tableSchema.getDimension())
|
|
|
+ .setIndexFileSize(tableSchema.getIndexFileSize())
|
|
|
+ .setMetricType(tableSchema.getMetricType().getVal())
|
|
|
+ .build();
|
|
|
|
|
|
io.milvus.client.grpc.Status response;
|
|
|
|
|
|
try {
|
|
|
- response = blockingStub.createTable(request);
|
|
|
+ response = blockingStub
|
|
|
+ .withDeadlineAfter(tableSchemaParam.getTimeout(), TimeUnit.SECONDS)
|
|
|
+ .createTable(request);
|
|
|
|
|
|
if (response.getErrorCode() == io.milvus.client.grpc.ErrorCode.SUCCESS) {
|
|
|
logInfo("Created table successfully!\n{0}", tableSchema.toString());
|
|
@@ -72,32 +99,38 @@ public class MilvusGrpcClient {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public HasTableResponse hasTable(@Nonnull String tableName) {
|
|
|
+ @Override
|
|
|
+ public HasTableResponse hasTable(@Nonnull TableParam tableParam) {
|
|
|
+ String tableName = tableParam.getTableName();
|
|
|
io.milvus.client.grpc.TableName request = io.milvus.client.grpc.TableName
|
|
|
- .newBuilder()
|
|
|
- .setTableName(tableName)
|
|
|
- .build();
|
|
|
+ .newBuilder()
|
|
|
+ .setTableName(tableName)
|
|
|
+ .build();
|
|
|
io.milvus.client.grpc.BoolReply response;
|
|
|
|
|
|
try {
|
|
|
- response = blockingStub.hasTable(request);
|
|
|
+ response = blockingStub
|
|
|
+ .withDeadlineAfter(tableParam.getTimeout(), TimeUnit.SECONDS)
|
|
|
+ .hasTable(request);
|
|
|
|
|
|
if (response.getStatus().getErrorCode() == io.milvus.client.grpc.ErrorCode.SUCCESS) {
|
|
|
logInfo("hasTable `{0}` = {1}", tableName, response.getBoolReply());
|
|
|
- return new HasTableResponse(Response.Status.SUCCESS, response.getBoolReply());
|
|
|
+ return new HasTableResponse(new Response(Response.Status.SUCCESS), response.getBoolReply());
|
|
|
} else {
|
|
|
logSevere("hasTable `{0}` failed:\n{1}", tableName, response.toString());
|
|
|
- return new HasTableResponse(Response.Status.valueOf(response.getStatus().getErrorCodeValue()),
|
|
|
- response.getStatus().getReason(),
|
|
|
- false);
|
|
|
+ return new HasTableResponse(new Response(Response.Status.valueOf(response.getStatus().getErrorCodeValue()),
|
|
|
+ response.getStatus().getReason()),
|
|
|
+ false);
|
|
|
}
|
|
|
} catch (StatusRuntimeException e) {
|
|
|
logSevere("hasTable RPC failed:\n{0}", e.getStatus().toString());
|
|
|
- return new HasTableResponse(Response.Status.RPC_ERROR, e.toString(), false);
|
|
|
+ return new HasTableResponse(new Response(Response.Status.RPC_ERROR, e.toString()), false);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public Response dropTable(@Nonnull String tableName) {
|
|
|
+ @Override
|
|
|
+ public Response dropTable(@Nonnull TableParam tableParam) {
|
|
|
+ String tableName = tableParam.getTableName();
|
|
|
io.milvus.client.grpc.TableName request = io.milvus.client.grpc.TableName
|
|
|
.newBuilder()
|
|
|
.setTableName(tableName)
|
|
@@ -105,7 +138,9 @@ public class MilvusGrpcClient {
|
|
|
io.milvus.client.grpc.Status response;
|
|
|
|
|
|
try {
|
|
|
- response = blockingStub.dropTable(request);
|
|
|
+ response = blockingStub
|
|
|
+ .withDeadlineAfter(tableParam.getTimeout(), TimeUnit.SECONDS)
|
|
|
+ .dropTable(request);
|
|
|
|
|
|
if (response.getErrorCode() == io.milvus.client.grpc.ErrorCode.SUCCESS) {
|
|
|
logInfo("Dropped table `{0}` successfully!", tableName);
|
|
@@ -120,6 +155,7 @@ public class MilvusGrpcClient {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
public Response createIndex(@Nonnull IndexParam indexParam) {
|
|
|
io.milvus.client.grpc.Index index = io.milvus.client.grpc.Index
|
|
|
.newBuilder()
|
|
@@ -135,7 +171,9 @@ public class MilvusGrpcClient {
|
|
|
io.milvus.client.grpc.Status response;
|
|
|
|
|
|
try {
|
|
|
- response = blockingStub.createIndex(request);
|
|
|
+ response = blockingStub
|
|
|
+ .withDeadlineAfter(indexParam.getTimeout(), TimeUnit.SECONDS)
|
|
|
+ .createIndex(request);
|
|
|
|
|
|
if (response.getErrorCode() == io.milvus.client.grpc.ErrorCode.SUCCESS) {
|
|
|
logInfo("Created index successfully!\n{0}", indexParam.toString());
|
|
@@ -150,8 +188,8 @@ public class MilvusGrpcClient {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
public InsertResponse insert(@Nonnull InsertParam insertParam) {
|
|
|
-
|
|
|
List<io.milvus.client.grpc.RowRecord> rowRecordList = new ArrayList<>();
|
|
|
for (List<Float> vectors : insertParam.getVectors()) {
|
|
|
io.milvus.client.grpc.RowRecord rowRecord = io.milvus.client.grpc.RowRecord
|
|
@@ -170,24 +208,27 @@ public class MilvusGrpcClient {
|
|
|
io.milvus.client.grpc.VectorIds response;
|
|
|
|
|
|
try {
|
|
|
- response = blockingStub.insert(request);
|
|
|
+ response = blockingStub
|
|
|
+ .withDeadlineAfter(insertParam.getTimeout(), TimeUnit.SECONDS)
|
|
|
+ .insert(request);
|
|
|
|
|
|
if (response.getStatus().getErrorCode() == io.milvus.client.grpc.ErrorCode.SUCCESS) {
|
|
|
logInfo("Inserted vectors successfully!");
|
|
|
Optional<List<Long>> resultVectorIds = Optional.ofNullable(response.getVectorIdArrayList());
|
|
|
- return new InsertResponse(Response.Status.SUCCESS, resultVectorIds.orElse(new ArrayList<>()));
|
|
|
+ return new InsertResponse(new Response(Response.Status.SUCCESS), resultVectorIds.orElse(new ArrayList<>()));
|
|
|
} else {
|
|
|
logSevere("Insert vectors failed");
|
|
|
- return new InsertResponse(Response.Status.valueOf(response.getStatus().getErrorCodeValue()),
|
|
|
- response.getStatus().getReason(),
|
|
|
+ return new InsertResponse(new Response(Response.Status.valueOf(response.getStatus().getErrorCodeValue()),
|
|
|
+ response.getStatus().getReason()),
|
|
|
new ArrayList<>());
|
|
|
}
|
|
|
} catch (StatusRuntimeException e) {
|
|
|
logSevere("insert RPC failed:\n{0}", e.getStatus().toString());
|
|
|
- return new InsertResponse(Response.Status.RPC_ERROR, e.toString(), new ArrayList<>());
|
|
|
+ return new InsertResponse(new Response(Response.Status.RPC_ERROR, e.toString()), new ArrayList<>());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
public SearchResponse search(@Nonnull SearchParam searchParam) {
|
|
|
|
|
|
List<io.milvus.client.grpc.RowRecord> queryRowRecordList = getQueryRowRecordList(searchParam);
|
|
@@ -206,25 +247,28 @@ public class MilvusGrpcClient {
|
|
|
io.milvus.client.grpc.TopKQueryResultList response;
|
|
|
|
|
|
try {
|
|
|
- response = blockingStub.search(request);
|
|
|
+ response = blockingStub
|
|
|
+ .withDeadlineAfter(searchParam.getTimeout(), TimeUnit.SECONDS)
|
|
|
+ .search(request);
|
|
|
|
|
|
if (response.getStatus().getErrorCode() == io.milvus.client.grpc.ErrorCode.SUCCESS) {
|
|
|
logInfo("Search completed successfully!");
|
|
|
|
|
|
List<List<SearchResponse.QueryResult>> queryResultsList = getQueryResultsList(response);
|
|
|
- return new SearchResponse(Response.Status.SUCCESS, queryResultsList);
|
|
|
+ return new SearchResponse(new Response(Response.Status.SUCCESS), queryResultsList);
|
|
|
} else {
|
|
|
logSevere("Search failed");
|
|
|
- return new SearchResponse(Response.Status.valueOf(response.getStatus().getErrorCodeValue()),
|
|
|
- response.getStatus().getReason(),
|
|
|
+ return new SearchResponse(new Response(Response.Status.valueOf(response.getStatus().getErrorCodeValue()),
|
|
|
+ response.getStatus().getReason()),
|
|
|
new ArrayList<>());
|
|
|
}
|
|
|
} catch (StatusRuntimeException e) {
|
|
|
logSevere("search RPC failed:\n{0}", e.getStatus().toString());
|
|
|
- return new SearchResponse(Response.Status.RPC_ERROR, e.toString(), new ArrayList<>());
|
|
|
+ return new SearchResponse(new Response(Response.Status.RPC_ERROR, e.toString()), new ArrayList<>());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
public SearchResponse searchInFiles(@Nonnull SearchInFilesParam searchInFilesParam) {
|
|
|
|
|
|
SearchParam searchParam = searchInFilesParam.getSearchParam();
|
|
@@ -251,26 +295,30 @@ public class MilvusGrpcClient {
|
|
|
io.milvus.client.grpc.TopKQueryResultList response;
|
|
|
|
|
|
try {
|
|
|
- response = blockingStub.searchInFiles(request);
|
|
|
+ response = blockingStub
|
|
|
+ .withDeadlineAfter(searchInFilesParam.getTimeout(), TimeUnit.SECONDS)
|
|
|
+ .searchInFiles(request);
|
|
|
|
|
|
if (response.getStatus().getErrorCode() == io.milvus.client.grpc.ErrorCode.SUCCESS) {
|
|
|
logInfo("Search in files {0} completed successfully!", searchInFilesParam.getFileIds());
|
|
|
|
|
|
List<List<SearchResponse.QueryResult>> queryResultsList = getQueryResultsList(response);
|
|
|
- return new SearchResponse(Response.Status.SUCCESS, queryResultsList);
|
|
|
+ return new SearchResponse(new Response(Response.Status.SUCCESS), queryResultsList);
|
|
|
} else {
|
|
|
logSevere("Search in files {0} failed", searchInFilesParam.getFileIds());
|
|
|
- return new SearchResponse(Response.Status.valueOf(response.getStatus().getErrorCodeValue()),
|
|
|
- response.getStatus().getReason(),
|
|
|
+ return new SearchResponse(new Response(Response.Status.valueOf(response.getStatus().getErrorCodeValue()),
|
|
|
+ response.getStatus().getReason()),
|
|
|
new ArrayList<>());
|
|
|
}
|
|
|
} catch (StatusRuntimeException e) {
|
|
|
logSevere("searchInFiles RPC failed:\n{0}", e.getStatus().toString());
|
|
|
- return new SearchResponse(Response.Status.RPC_ERROR, e.toString(), new ArrayList<>());
|
|
|
+ return new SearchResponse(new Response(Response.Status.RPC_ERROR, e.toString()), new ArrayList<>());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public DescribeTableResponse describeTable(@Nonnull String tableName) {
|
|
|
+ @Override
|
|
|
+ public DescribeTableResponse describeTable(@Nonnull TableParam tableParam) {
|
|
|
+ String tableName = tableParam.getTableName();
|
|
|
io.milvus.client.grpc.TableName request = io.milvus.client.grpc.TableName
|
|
|
.newBuilder()
|
|
|
.setTableName(tableName)
|
|
@@ -278,53 +326,30 @@ public class MilvusGrpcClient {
|
|
|
io.milvus.client.grpc.TableSchema response;
|
|
|
|
|
|
try {
|
|
|
- response = blockingStub.describeTable(request);
|
|
|
+ response = blockingStub
|
|
|
+ .withDeadlineAfter(tableParam.getTimeout(), TimeUnit.SECONDS)
|
|
|
+ .describeTable(request);
|
|
|
|
|
|
if (response.getStatus().getErrorCode() == io.milvus.client.grpc.ErrorCode.SUCCESS) {
|
|
|
TableSchema tableSchema = new TableSchema.Builder(response.getTableName(), response.getDimension())
|
|
|
- .withIndexFileSize(response.getIndexFileSize())
|
|
|
- .withMetricType(MetricType.valueOf(response.getMetricType()))
|
|
|
- .build();
|
|
|
+ .withIndexFileSize(response.getIndexFileSize())
|
|
|
+ .withMetricType(MetricType.valueOf(response.getMetricType()))
|
|
|
+ .build();
|
|
|
logInfo("Describe Table `{0}` returned:\n{1}", tableName, tableSchema);
|
|
|
- return new DescribeTableResponse(Response.Status.SUCCESS, tableSchema);
|
|
|
+ return new DescribeTableResponse(new Response(Response.Status.SUCCESS), tableSchema);
|
|
|
} else {
|
|
|
logSevere("Describe Table `{0}` failed:\n{1}", tableName, response.toString());
|
|
|
- return new DescribeTableResponse(Response.Status.valueOf(response.getStatus().getErrorCodeValue()),
|
|
|
- response.getStatus().getReason(),
|
|
|
- null);
|
|
|
+ return new DescribeTableResponse(new Response(Response.Status.valueOf(response.getStatus().getErrorCodeValue()),
|
|
|
+ response.getStatus().getReason()),
|
|
|
+ null);
|
|
|
}
|
|
|
} catch (StatusRuntimeException e) {
|
|
|
logSevere("describeTable RPC failed:\n{0}", e.getStatus().toString());
|
|
|
- return new DescribeTableResponse(Response.Status.RPC_ERROR, e.toString(), null);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public CountTableResponse countTable(@Nonnull String tableName) {
|
|
|
- io.milvus.client.grpc.TableName request = io.milvus.client.grpc.TableName
|
|
|
- .newBuilder()
|
|
|
- .setTableName(tableName)
|
|
|
- .build();
|
|
|
- io.milvus.client.grpc.TableRowCount response;
|
|
|
-
|
|
|
- try {
|
|
|
- response = blockingStub.countTable(request);
|
|
|
-
|
|
|
- if (response.getStatus().getErrorCode() == io.milvus.client.grpc.ErrorCode.SUCCESS) {
|
|
|
- long tableRowCount = response.getTableRowCount();
|
|
|
- logInfo("Table `{0}` has {1} rows", tableName, tableRowCount);
|
|
|
- return new CountTableResponse(Response.Status.SUCCESS, tableRowCount);
|
|
|
- } else {
|
|
|
- logSevere("Count Table `{0}` failed:\n{1}", tableName, response.toString());
|
|
|
- return new CountTableResponse(Response.Status.valueOf(response.getStatus().getErrorCodeValue()),
|
|
|
- response.getStatus().getReason(),
|
|
|
- 0);
|
|
|
- }
|
|
|
- } catch (StatusRuntimeException e) {
|
|
|
- logSevere("countTable RPC failed:\n{0}", e.getStatus().toString());
|
|
|
- return new CountTableResponse(Response.Status.RPC_ERROR, e.toString(), 0);
|
|
|
+ return new DescribeTableResponse(new Response(Response.Status.RPC_ERROR, e.toString()), null);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
public ShowTablesResponse showTables() {
|
|
|
io.milvus.client.grpc.Command request = io.milvus.client.grpc.Command
|
|
|
.newBuilder()
|
|
@@ -333,27 +358,59 @@ public class MilvusGrpcClient {
|
|
|
io.milvus.client.grpc.TableNameList response;
|
|
|
|
|
|
try {
|
|
|
- response = blockingStub.showTables(request);
|
|
|
+ response = blockingStub
|
|
|
+ .withDeadlineAfter(10, TimeUnit.SECONDS)
|
|
|
+ .showTables(request);
|
|
|
|
|
|
if (response.getStatus().getErrorCode() == io.milvus.client.grpc.ErrorCode.SUCCESS) {
|
|
|
List<String> tableNames = response.getTableNamesList();
|
|
|
logInfo("Current tables: {0}", tableNames.toString());
|
|
|
- return new ShowTablesResponse(Response.Status.SUCCESS, tableNames);
|
|
|
+ return new ShowTablesResponse(new Response(Response.Status.SUCCESS), tableNames);
|
|
|
} else {
|
|
|
logSevere("Show tables failed:\n{1}", response.toString());
|
|
|
- return new ShowTablesResponse(Response.Status.valueOf(response.getStatus().getErrorCodeValue()),
|
|
|
- response.getStatus().getReason(),
|
|
|
+ return new ShowTablesResponse(new Response(Response.Status.valueOf(response.getStatus().getErrorCodeValue()),
|
|
|
+ response.getStatus().getReason()),
|
|
|
new ArrayList<>());
|
|
|
}
|
|
|
} catch (StatusRuntimeException e) {
|
|
|
logSevere("showTables RPC failed:\n{0}", e.getStatus().toString());
|
|
|
- return new ShowTablesResponse(Response.Status.RPC_ERROR, e.toString(), new ArrayList<>());
|
|
|
+ return new ShowTablesResponse(new Response(Response.Status.RPC_ERROR, e.toString()), new ArrayList<>());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public GetTableRowCountResponse getTableRowCount(@Nonnull TableParam tableParam) {
|
|
|
+ String tableName = tableParam.getTableName();
|
|
|
+ io.milvus.client.grpc.TableName request = io.milvus.client.grpc.TableName
|
|
|
+ .newBuilder()
|
|
|
+ .setTableName(tableName)
|
|
|
+ .build();
|
|
|
+ io.milvus.client.grpc.TableRowCount response;
|
|
|
+
|
|
|
+ try {
|
|
|
+ response = blockingStub
|
|
|
+ .withDeadlineAfter(tableParam.getTimeout(), TimeUnit.SECONDS)
|
|
|
+ .countTable(request);
|
|
|
+
|
|
|
+ if (response.getStatus().getErrorCode() == io.milvus.client.grpc.ErrorCode.SUCCESS) {
|
|
|
+ long tableRowCount = response.getTableRowCount();
|
|
|
+ logInfo("Table `{0}` has {1} rows", tableName, tableRowCount);
|
|
|
+ return new GetTableRowCountResponse(new Response(Response.Status.SUCCESS), tableRowCount);
|
|
|
+ } else {
|
|
|
+ logSevere("Count Table `{0}` failed:\n{1}", tableName, response.toString());
|
|
|
+ return new GetTableRowCountResponse(new Response(Response.Status.valueOf(response.getStatus().getErrorCodeValue()),
|
|
|
+ response.getStatus().getReason()),
|
|
|
+ 0);
|
|
|
+ }
|
|
|
+ } catch (StatusRuntimeException e) {
|
|
|
+ logSevere("countTable RPC failed:\n{0}", e.getStatus().toString());
|
|
|
+ return new GetTableRowCountResponse(new Response(Response.Status.RPC_ERROR, e.toString()), 0);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//Cmd(Command) not implemented
|
|
|
|
|
|
- public Response deleteByRange(DeleteByRangeParam deleteByRangeParam) {
|
|
|
+ public Response deleteByRange(@Nonnull DeleteByRangeParam deleteByRangeParam) {
|
|
|
io.milvus.client.grpc.DeleteByRangeParam request = io.milvus.client.grpc.DeleteByRangeParam
|
|
|
.newBuilder()
|
|
|
.setRange(getRange(deleteByRangeParam.getDateRange()))
|
|
@@ -362,7 +419,9 @@ public class MilvusGrpcClient {
|
|
|
io.milvus.client.grpc.Status response;
|
|
|
|
|
|
try {
|
|
|
- response = blockingStub.deleteByRange(request);
|
|
|
+ response = blockingStub
|
|
|
+ .withDeadlineAfter(deleteByRangeParam.getTimeout(), TimeUnit.SECONDS)
|
|
|
+ .deleteByRange(request);
|
|
|
|
|
|
if (response.getErrorCode() == io.milvus.client.grpc.ErrorCode.SUCCESS) {
|
|
|
logInfo("Deleted vectors from table `{0}` in range {1} successfully!",
|
|
@@ -378,15 +437,19 @@ public class MilvusGrpcClient {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public Response preloadTable(String tableName) {
|
|
|
+ @Override
|
|
|
+ public Response preloadTable(@Nonnull TableParam tableParam) {
|
|
|
+ String tableName = tableParam.getTableName();
|
|
|
io.milvus.client.grpc.TableName request = io.milvus.client.grpc.TableName
|
|
|
- .newBuilder()
|
|
|
- .setTableName(tableName)
|
|
|
- .build();
|
|
|
+ .newBuilder()
|
|
|
+ .setTableName(tableName)
|
|
|
+ .build();
|
|
|
io.milvus.client.grpc.Status response;
|
|
|
|
|
|
try {
|
|
|
- response = blockingStub.preloadTable(request);
|
|
|
+ response = blockingStub
|
|
|
+ .withDeadlineAfter(tableParam.getTimeout(), TimeUnit.SECONDS)
|
|
|
+ .preloadTable(request);
|
|
|
|
|
|
if (response.getErrorCode() == io.milvus.client.grpc.ErrorCode.SUCCESS) {
|
|
|
logInfo("Preloaded table `{0}` successfully!", tableName);
|
|
@@ -401,7 +464,9 @@ public class MilvusGrpcClient {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public DescribeIndexResponse describeIndex(@Nonnull String tableName) {
|
|
|
+ @Override
|
|
|
+ public DescribeIndexResponse describeIndex(@Nonnull TableParam tableParam) {
|
|
|
+ String tableName = tableParam.getTableName();
|
|
|
io.milvus.client.grpc.TableName request = io.milvus.client.grpc.TableName
|
|
|
.newBuilder()
|
|
|
.setTableName(tableName)
|
|
@@ -409,27 +474,54 @@ public class MilvusGrpcClient {
|
|
|
io.milvus.client.grpc.IndexParam response;
|
|
|
|
|
|
try {
|
|
|
- response = blockingStub.describeIndex(request);
|
|
|
+ response = blockingStub
|
|
|
+ .withDeadlineAfter(tableParam.getTimeout(), TimeUnit.SECONDS)
|
|
|
+ .describeIndex(request);
|
|
|
|
|
|
if (response.getStatus().getErrorCode() == io.milvus.client.grpc.ErrorCode.SUCCESS) {
|
|
|
Index index = new Index.Builder()
|
|
|
- .withIndexType(IndexType.valueOf(response.getIndex().getIndexType()))
|
|
|
- .withNList(response.getIndex().getNlist())
|
|
|
- .build();
|
|
|
+ .withIndexType(IndexType.valueOf(response.getIndex().getIndexType()))
|
|
|
+ .withNList(response.getIndex().getNlist())
|
|
|
+ .build();
|
|
|
IndexParam indexParam = new IndexParam.Builder(response.getTableName())
|
|
|
- .withIndex(index)
|
|
|
- .build();
|
|
|
+ .withIndex(index)
|
|
|
+ .build();
|
|
|
logInfo("Describe index for table `{0}` returned:\n{1}", tableName, indexParam);
|
|
|
- return new DescribeIndexResponse(Response.Status.SUCCESS, indexParam);
|
|
|
+ return new DescribeIndexResponse(new Response(Response.Status.SUCCESS), indexParam);
|
|
|
} else {
|
|
|
logSevere("Describe index for table `{0}` failed:\n{1}", tableName, response.toString());
|
|
|
- return new DescribeIndexResponse(Response.Status.valueOf(response.getStatus().getErrorCodeValue()),
|
|
|
- response.getStatus().getReason(),
|
|
|
- null);
|
|
|
+ return new DescribeIndexResponse(new Response(Response.Status.valueOf(response.getStatus().getErrorCodeValue()),
|
|
|
+ response.getStatus().getReason()),
|
|
|
+ null);
|
|
|
}
|
|
|
} catch (StatusRuntimeException e) {
|
|
|
logSevere("describeIndex RPC failed:\n{0}", e.getStatus().toString());
|
|
|
- return new DescribeIndexResponse(Response.Status.RPC_ERROR, e.toString(), null);
|
|
|
+ return new DescribeIndexResponse(new Response(Response.Status.RPC_ERROR, e.toString()), null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Response dropIndex(@Nonnull TableParam tableParam) {
|
|
|
+ String tableName = tableParam.getTableName();
|
|
|
+ io.milvus.client.grpc.TableName request = io.milvus.client.grpc.TableName
|
|
|
+ .newBuilder()
|
|
|
+ .setTableName(tableName)
|
|
|
+ .build();
|
|
|
+ io.milvus.client.grpc.Status response;
|
|
|
+
|
|
|
+ try {
|
|
|
+ response = blockingStub.dropIndex(request);
|
|
|
+
|
|
|
+ if (response.getErrorCode() == io.milvus.client.grpc.ErrorCode.SUCCESS) {
|
|
|
+ logInfo("Dropped index for table `{0}` successfully!", tableName);
|
|
|
+ return new Response(Response.Status.SUCCESS);
|
|
|
+ } else {
|
|
|
+ logSevere("Drop index for table `{0}` failed", tableName);
|
|
|
+ return new Response(Response.Status.valueOf(response.getErrorCodeValue()), response.getReason());
|
|
|
+ }
|
|
|
+ } catch (StatusRuntimeException e) {
|
|
|
+ logSevere("dropIndex RPC failed:\n{0}", e.getStatus().toString());
|
|
|
+ return new Response(Response.Status.RPC_ERROR, e.toString());
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -492,29 +584,6 @@ public class MilvusGrpcClient {
|
|
|
return queryResultsList;
|
|
|
}
|
|
|
|
|
|
- public Response dropIndex(@Nonnull String tableName) {
|
|
|
- io.milvus.client.grpc.TableName request = io.milvus.client.grpc.TableName
|
|
|
- .newBuilder()
|
|
|
- .setTableName(tableName)
|
|
|
- .build();
|
|
|
- io.milvus.client.grpc.Status response;
|
|
|
-
|
|
|
- try {
|
|
|
- response = blockingStub.dropIndex(request);
|
|
|
-
|
|
|
- if (response.getErrorCode() == io.milvus.client.grpc.ErrorCode.SUCCESS) {
|
|
|
- logInfo("Dropped index for table `{0}` successfully!", tableName);
|
|
|
- return new Response(Response.Status.SUCCESS);
|
|
|
- } else {
|
|
|
- logSevere("Drop index for table `{0}` failed", tableName);
|
|
|
- return new Response(Response.Status.valueOf(response.getErrorCodeValue()), response.getReason());
|
|
|
- }
|
|
|
- } catch (StatusRuntimeException e) {
|
|
|
- logSevere("dropIndex RPC failed:\n{0}", e.getStatus().toString());
|
|
|
- return new Response(Response.Status.RPC_ERROR, e.toString());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
/////////////////////Log Functions//////////////////////
|
|
|
|
|
|
private static final String ANSI_RESET = "\u001B[0m";
|
|
@@ -550,19 +619,23 @@ public class MilvusGrpcClient {
|
|
|
|
|
|
//////////////////////////Main///////////////////////////
|
|
|
public static void main(String[] args) throws InterruptedException {
|
|
|
- MilvusGrpcClient client = new MilvusGrpcClient("192.168.1.188", 19531);
|
|
|
+ MilvusClient client = new MilvusGrpcClient();
|
|
|
+ ConnectParam connectParam = new ConnectParam.Builder().withHost("192.168.1.188").withPort("19530").build();
|
|
|
+ client.connect(connectParam);
|
|
|
|
|
|
try {
|
|
|
String tableName = "test_zhiru";
|
|
|
+ TableParam tableParam = new TableParam.Builder(tableName).withTimeout(20).build();
|
|
|
long dimension = 128;
|
|
|
TableSchema tableSchema = new TableSchema.Builder(tableName, dimension)
|
|
|
.withIndexFileSize(1024)
|
|
|
.withMetricType(MetricType.L2)
|
|
|
.build();
|
|
|
- Response createTableResponse = client.createTable(tableSchema);
|
|
|
+ TableSchemaParam tableSchemaParam = new TableSchemaParam.Builder(tableSchema).withTimeout(20).build();
|
|
|
+ Response createTableResponse = client.createTable(tableSchemaParam);
|
|
|
System.out.println(createTableResponse);
|
|
|
|
|
|
- HasTableResponse hasTableResponse = client.hasTable(tableName);
|
|
|
+ HasTableResponse hasTableResponse = client.hasTable(tableParam);
|
|
|
System.out.println(hasTableResponse);
|
|
|
|
|
|
Random random = new Random();
|
|
@@ -610,11 +683,11 @@ public class MilvusGrpcClient {
|
|
|
searchResponse = client.searchInFiles(searchInFilesParam);
|
|
|
System.out.println(searchResponse);
|
|
|
|
|
|
- DescribeTableResponse describeTableResponse = client.describeTable(tableName);
|
|
|
+ DescribeTableResponse describeTableResponse = client.describeTable(tableParam);
|
|
|
describeTableResponse.getTableSchema().ifPresent(System.out::println);
|
|
|
|
|
|
- CountTableResponse countTableResponse = client.countTable(tableName);
|
|
|
- System.out.println(countTableResponse);
|
|
|
+ GetTableRowCountResponse getTableRowCountResponse = client.getTableRowCount(tableParam);
|
|
|
+ System.out.println(getTableRowCountResponse);
|
|
|
|
|
|
ShowTablesResponse showTablesResponse = client.showTables();
|
|
|
System.out.println(showTablesResponse);
|
|
@@ -624,20 +697,20 @@ public class MilvusGrpcClient {
|
|
|
Response deleteByRangeResponse = client.deleteByRange(deleteByRangeParam);
|
|
|
System.out.println(deleteByRangeResponse);
|
|
|
|
|
|
- Response preloadTableResponse = client.preloadTable(tableName);
|
|
|
+ Response preloadTableResponse = client.preloadTable(tableParam);
|
|
|
System.out.println(preloadTableResponse);
|
|
|
|
|
|
- DescribeIndexResponse describeIndexResponse = client.describeIndex(tableName);
|
|
|
+ DescribeIndexResponse describeIndexResponse = client.describeIndex(tableParam);
|
|
|
describeIndexResponse.getIndexParam().ifPresent(System.out::println);
|
|
|
|
|
|
- Response dropIndexResponse = client.dropIndex(tableName);
|
|
|
+ Response dropIndexResponse = client.dropIndex(tableParam);
|
|
|
System.out.println(dropIndexResponse);
|
|
|
|
|
|
- Response dropTableResponse = client.dropTable(tableName);
|
|
|
+ Response dropTableResponse = client.dropTable(tableParam);
|
|
|
System.out.println(dropTableResponse);
|
|
|
|
|
|
} finally {
|
|
|
- client.shutdown();
|
|
|
+ client.disconnect();
|
|
|
}
|
|
|
}
|
|
|
}
|