Browse Source

Simplify `getCollectionStats`

jianghua 4 years ago
parent
commit
1dcc7d6e49

+ 1 - 3
src/main/java/io/milvus/client/MilvusClient.java

@@ -331,10 +331,8 @@ public interface MilvusClient {
    * result will be returned as JSON string.
    * result will be returned as JSON string.
    *
    *
    * @param collectionName collection to show info from
    * @param collectionName collection to show info from
-   * @return <code>Response</code>
-   * @see Response
    */
    */
-  Response getCollectionStats(String collectionName);
+  String getCollectionStats(String collectionName);
 
 
   /**
   /**
    * Gets entities data by id array
    * Gets entities data by id array

+ 7 - 28
src/main/java/io/milvus/client/MilvusGrpcClient.java

@@ -421,34 +421,13 @@ abstract class AbstractMilvusGrpcClient implements MilvusClient {
   }
   }
 
 
   @Override
   @Override
-  public Response getCollectionStats(String collectionName) {
-    if (!maybeAvailable()) {
-      logWarning("You are not connected to Milvus server");
-      return new Response(Response.Status.CLIENT_NOT_CONNECTED);
-    }
-
-    CollectionName request = CollectionName.newBuilder().setCollectionName(collectionName).build();
-    io.milvus.grpc.CollectionInfo response;
-
-    try {
-      response = blockingStub().showCollectionInfo(request);
-
-      if (response.getStatus().getErrorCode() == ErrorCode.SUCCESS) {
-        logInfo("getCollectionStats for `{}` returned successfully!", collectionName);
-        return new Response(Response.Status.SUCCESS, response.getJsonInfo());
-      } else {
-        logError(
-            "getCollectionStats for `{}` failed:\n{}",
-            collectionName,
-            response.getStatus().toString());
-        return new Response(
-            Response.Status.valueOf(response.getStatus().getErrorCodeValue()),
-            response.getStatus().getReason());
-      }
-    } catch (StatusRuntimeException e) {
-      logError("getCollectionStats RPC failed:\n{}", e.getStatus().toString());
-      return new Response(Response.Status.RPC_ERROR, e.toString());
-    }
+  public String getCollectionStats(String collectionName) {
+    return translateExceptions(() -> {
+      CollectionName request = CollectionName.newBuilder().setCollectionName(collectionName).build();
+      CollectionInfo response = blockingStub().showCollectionInfo(request);
+      checkResponseStatus(response.getStatus());
+      return response.getJsonInfo();
+    });
   }
   }
 
 
   @Override
   @Override

+ 30 - 45
src/test/java/io/milvus/client/MilvusGrpcClientTest.java

@@ -653,11 +653,8 @@ class MilvusClientTest {
 
 
     assertTrue(client.flush(randomCollectionName).ok());
     assertTrue(client.flush(randomCollectionName).ok());
 
 
-    Response getCollectionStatsResponse = client.getCollectionStats(randomCollectionName);
-    assertTrue(getCollectionStatsResponse.ok());
-
-    String jsonString = getCollectionStatsResponse.getMessage();
-    JSONObject jsonInfo = new JSONObject(jsonString);
+    String collectionStats = client.getCollectionStats(randomCollectionName);
+    JSONObject jsonInfo = new JSONObject(collectionStats);
     assertEquals(jsonInfo.getInt("row_count"), size);
     assertEquals(jsonInfo.getInt("row_count"), size);
 
 
     JSONArray partitions = jsonInfo.getJSONArray("partitions");
     JSONArray partitions = jsonInfo.getJSONArray("partitions");
@@ -744,16 +741,13 @@ class MilvusClientTest {
 
 
     assertTrue(client.flush(randomCollectionName).ok());
     assertTrue(client.flush(randomCollectionName).ok());
 
 
-    Response getCollectionStatsResponse = client.getCollectionStats(randomCollectionName);
-    assertTrue(getCollectionStatsResponse.ok());
-
-    JSONObject jsonInfo = new JSONObject(getCollectionStatsResponse.getMessage());
-    JSONObject segmentInfo =
-        jsonInfo
-            .getJSONArray("partitions")
-            .getJSONObject(0)
-            .getJSONArray("segments")
-            .getJSONObject(0);
+    String collectionStats = client.getCollectionStats(randomCollectionName);
+    JSONObject jsonInfo = new JSONObject(collectionStats);
+    JSONObject segmentInfo = jsonInfo
+        .getJSONArray("partitions")
+        .getJSONObject(0)
+        .getJSONArray("segments")
+        .getJSONObject(0);
 
 
     ListIDInSegmentResponse listIDInSegmentResponse =
     ListIDInSegmentResponse listIDInSegmentResponse =
         client.listIDInSegment(randomCollectionName, segmentInfo.getLong("id"));
         client.listIDInSegment(randomCollectionName, segmentInfo.getLong("id"));
@@ -822,15 +816,12 @@ class MilvusClientTest {
 
 
     assertTrue(client.flush(randomCollectionName).ok());
     assertTrue(client.flush(randomCollectionName).ok());
 
 
-    Response getCollectionStatsResponse = client.getCollectionStats(randomCollectionName);
-    assertTrue(getCollectionStatsResponse.ok());
-
-    JSONObject jsonInfo = new JSONObject(getCollectionStatsResponse.getMessage());
-    long previousSegmentSize =
-        jsonInfo
-            .getJSONArray("partitions")
-            .getJSONObject(0)
-            .getLong("data_size");
+    String collectionStats = client.getCollectionStats(randomCollectionName);
+    JSONObject jsonInfo = new JSONObject(collectionStats);
+    long previousSegmentSize = jsonInfo
+        .getJSONArray("partitions")
+        .getJSONObject(0)
+        .getLong("data_size");
 
 
     assertTrue(
     assertTrue(
         client.deleteEntityByID(randomCollectionName,
         client.deleteEntityByID(randomCollectionName,
@@ -839,15 +830,13 @@ class MilvusClientTest {
     assertTrue(client.compact(
     assertTrue(client.compact(
         new CompactParam.Builder(randomCollectionName).withThreshold(0.2).build()).ok());
         new CompactParam.Builder(randomCollectionName).withThreshold(0.2).build()).ok());
 
 
-    getCollectionStatsResponse = client.getCollectionStats(randomCollectionName);
-    assertTrue(getCollectionStatsResponse.ok());
+    collectionStats = client.getCollectionStats(randomCollectionName);
 
 
-    jsonInfo = new JSONObject(getCollectionStatsResponse.getMessage());
-    long currentSegmentSize =
-        jsonInfo
-            .getJSONArray("partitions")
-            .getJSONObject(0)
-            .getLong("data_size");
+    jsonInfo = new JSONObject(collectionStats);
+    long currentSegmentSize = jsonInfo
+        .getJSONArray("partitions")
+        .getJSONObject(0)
+        .getLong("data_size");
     assertTrue(currentSegmentSize < previousSegmentSize);
     assertTrue(currentSegmentSize < previousSegmentSize);
   }
   }
 
 
@@ -874,10 +863,8 @@ class MilvusClientTest {
 
 
     assertTrue(client.flush(randomCollectionName).ok());
     assertTrue(client.flush(randomCollectionName).ok());
 
 
-    Response getCollectionStatsResponse = client.getCollectionStats(randomCollectionName);
-    assertTrue(getCollectionStatsResponse.ok());
-
-    JSONObject jsonInfo = new JSONObject(getCollectionStatsResponse.getMessage());
+    String collectionStats = client.getCollectionStats(randomCollectionName);
+    JSONObject jsonInfo = new JSONObject(collectionStats);
     JSONObject segmentInfo =
     JSONObject segmentInfo =
         jsonInfo
         jsonInfo
             .getJSONArray("partitions")
             .getJSONArray("partitions")
@@ -894,15 +881,13 @@ class MilvusClientTest {
     assertTrue(client.compactAsync(
     assertTrue(client.compactAsync(
         new CompactParam.Builder(randomCollectionName).withThreshold(0.8).build()).get().ok());
         new CompactParam.Builder(randomCollectionName).withThreshold(0.8).build()).get().ok());
 
 
-    getCollectionStatsResponse = client.getCollectionStats(randomCollectionName);
-    assertTrue(getCollectionStatsResponse.ok());
-    jsonInfo = new JSONObject(getCollectionStatsResponse.getMessage());
-    segmentInfo =
-        jsonInfo
-            .getJSONArray("partitions")
-            .getJSONObject(0)
-            .getJSONArray("segments")
-            .getJSONObject(0);
+    collectionStats = client.getCollectionStats(randomCollectionName);
+    jsonInfo = new JSONObject(collectionStats);
+    segmentInfo = jsonInfo
+        .getJSONArray("partitions")
+        .getJSONObject(0)
+        .getJSONArray("segments")
+        .getJSONObject(0);
 
 
     long currentSegmentSize = segmentInfo.getLong("data_size");
     long currentSegmentSize = segmentInfo.getLong("data_size");
     assertFalse(currentSegmentSize < previousSegmentSize); // threshold 0.8 > 0.5, no compact
     assertFalse(currentSegmentSize < previousSegmentSize); // threshold 0.8 > 0.5, no compact