Răsfoiți Sursa

Simplify `compact`

jianghua 4 ani în urmă
părinte
comite
16ad7b1da8

+ 11 - 39
src/main/java/io/milvus/client/CompactParam.java

@@ -19,53 +19,25 @@
 
 package io.milvus.client;
 
-import javax.annotation.Nonnull;
-
 /** Contains parameters for <code>compact</code> */
 public class CompactParam {
-  private final String collectionName;
-  private final double threshold;
+  private io.milvus.grpc.CompactParam.Builder builder;
 
-  private CompactParam(@Nonnull Builder builder) {
-    this.collectionName = builder.collectionName;
-    this.threshold = builder.threshold;
+  public static CompactParam create(String collectionName) {
+    return new CompactParam(collectionName);
   }
 
-  public String getCollectionName() {
-    return collectionName;
+  private CompactParam(String collectionName) {
+    builder = io.milvus.grpc.CompactParam.newBuilder();
+    builder.setCollectionName(collectionName).setThreshold(0.2);
   }
 
-  public double getThreshold() {
-    return threshold;
+  public CompactParam setThreshold(double threshold) {
+    builder.setThreshold(threshold);
+    return this;
   }
 
-  /** Builder for <code>CompactParam</code> */
-  public static class Builder {
-    // Required parameter
-    private final String collectionName;
-
-    // Optional parameter - initialized to default value
-    private double threshold = 0.2;
-
-    /** @param collectionName collection to compact */
-    public Builder(@Nonnull String collectionName) {
-      this.collectionName = collectionName;
-    }
-
-    /**
-     * Optional. Default to 0.2. Segment will compact if and only if the percentage of entities
-     * deleted exceeds the threshold.
-     *
-     * @param threshold The threshold
-     * @return <code>Builder</code>
-     */
-    public Builder withThreshold(double threshold) {
-      this.threshold = threshold;
-      return this;
-    }
-
-    public CompactParam build() {
-      return new CompactParam(this);
-    }
+  io.milvus.grpc.CompactParam grpc() {
+    return builder.build();
   }
 }

+ 2 - 5
src/main/java/io/milvus/client/MilvusClient.java

@@ -425,11 +425,9 @@ public interface MilvusClient {
    * </code>
    * </pre>
    *
-   * @return <code>Response</code>
    * @see CompactParam
-   * @see Response
    */
-  Response compact(CompactParam compactParam);
+  void compact(CompactParam compactParam);
 
   /**
    * Compacts the collection, erasing deleted data from disk and rebuild index in background (if the
@@ -448,8 +446,7 @@ public interface MilvusClient {
    *
    * @return a <code>ListenableFuture</code> object which holds the <code>Response</code>
    * @see CompactParam
-   * @see Response
    * @see ListenableFuture
    */
-  ListenableFuture<Response> compactAsync(CompactParam compactParam);
+  ListenableFuture<Void> compactAsync(CompactParam compactParam);
 }

+ 8 - 69
src/main/java/io/milvus/client/MilvusGrpcClient.java

@@ -532,78 +532,17 @@ abstract class AbstractMilvusGrpcClient implements MilvusClient {
   }
 
   @Override
-  public Response compact(CompactParam compactParam) {
-    if (!maybeAvailable()) {
-      logWarning("You are not connected to Milvus server");
-      return new Response(Response.Status.CLIENT_NOT_CONNECTED);
-    }
-
-    io.milvus.grpc.CompactParam request =
-        io.milvus.grpc.CompactParam.newBuilder()
-            .setCollectionName(compactParam.getCollectionName())
-            .setThreshold(compactParam.getThreshold())
-            .build();
-    Status response;
-
-    try {
-      response = blockingStub().compact(request);
-
-      if (response.getErrorCode() == ErrorCode.SUCCESS) {
-        logInfo("Compacted collection `{}` successfully!", compactParam.getCollectionName());
-        return new Response(Response.Status.SUCCESS);
-      } else {
-        logError("Compact collection `{}` failed:\n{}",
-            compactParam.getCollectionName(), response.toString());
-        return new Response(
-            Response.Status.valueOf(response.getErrorCodeValue()), response.getReason());
-      }
-    } catch (StatusRuntimeException e) {
-      logError("compact RPC failed:\n{}", e.getStatus().toString());
-      return new Response(Response.Status.RPC_ERROR, e.toString());
-    }
+  public void compact(CompactParam compactParam) {
+    translateExceptions(() -> Futures.getUnchecked(compactAsync(compactParam)));
   }
 
   @Override
-  public ListenableFuture<Response> compactAsync(@Nonnull CompactParam compactParam) {
-
-    if (!maybeAvailable()) {
-      logWarning("You are not connected to Milvus server");
-      return Futures.immediateFuture(new Response(Response.Status.CLIENT_NOT_CONNECTED));
-    }
-
-    io.milvus.grpc.CompactParam request =
-        io.milvus.grpc.CompactParam.newBuilder()
-            .setCollectionName(compactParam.getCollectionName())
-            .setThreshold(compactParam.getThreshold())
-            .build();
-
-    ListenableFuture<Status> response;
-
-    response = futureStub().compact(request);
-
-    Futures.addCallback(
-        response,
-        new FutureCallback<Status>() {
-          @Override
-          public void onSuccess(Status result) {
-            if (result.getErrorCode() == ErrorCode.SUCCESS) {
-              logInfo("Compacted collection `{}` successfully!",
-                  compactParam.getCollectionName());
-            } else {
-              logError("Compact collection `{}` failed:\n{}",
-                  compactParam.getCollectionName(), result.toString());
-            }
-          }
-
-          @Override
-          public void onFailure(Throwable t) {
-            logError("CompactAsync failed:\n{}", t.getMessage());
-          }
-        },
-        MoreExecutors.directExecutor());
-
-    return Futures.transform(
-        response, transformStatusToResponseFunc::apply, MoreExecutors.directExecutor());
+  public ListenableFuture<Void> compactAsync(@Nonnull CompactParam compactParam) {
+    return translateExceptions(() -> {
+      io.milvus.grpc.CompactParam request = compactParam.grpc();
+      ListenableFuture<Status> response = futureStub().compact(request);
+      return Futures.transform(response, this::checkResponseStatus, MoreExecutors.directExecutor());
+    });
   }
 
   ///////////////////// Util Functions/////////////////////

+ 2 - 4
src/test/java/io/milvus/client/MilvusGrpcClientTest.java

@@ -815,8 +815,7 @@ class MilvusClientTest {
 
     client.deleteEntityByID(randomCollectionName, entityIds.subList(0, size / 2));
     client.flush(randomCollectionName);
-    assertTrue(client.compact(
-        new CompactParam.Builder(randomCollectionName).withThreshold(0.2).build()).ok());
+    client.compact(CompactParam.create(randomCollectionName).setThreshold(0.2));
 
     collectionStats = client.getCollectionStats(randomCollectionName);
 
@@ -864,8 +863,7 @@ class MilvusClientTest {
 
     client.deleteEntityByID(randomCollectionName, entityIds.subList(0, size / 2));
     client.flush(randomCollectionName);
-    assertTrue(client.compactAsync(
-        new CompactParam.Builder(randomCollectionName).withThreshold(0.8).build()).get().ok());
+    client.compactAsync(CompactParam.create(randomCollectionName).setThreshold(0.8)).get();
 
     collectionStats = client.getCollectionStats(randomCollectionName);
     jsonInfo = new JSONObject(collectionStats);