Browse Source

Simplify `hasCollection`

jianghua 4 years ago
parent
commit
c31b3afdc4

+ 0 - 54
src/main/java/io/milvus/client/HasCollectionResponse.java

@@ -1,54 +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.client;
-
-/**
- * Contains the returned <code>response</code> and <code>hasCollection</code> for <code>
- * hasCollection</code>
- */
-public class HasCollectionResponse {
-  private final Response response;
-  private final boolean hasCollection;
-
-  HasCollectionResponse(Response response, boolean hasCollection) {
-    this.response = response;
-    this.hasCollection = hasCollection;
-  }
-
-  /** @return <code>true</code> if the collection is present */
-  public boolean hasCollection() {
-    return hasCollection;
-  }
-
-  public Response getResponse() {
-    return response;
-  }
-
-  /** @return <code>true</code> if the response status equals SUCCESS */
-  public boolean ok() {
-    return response.ok();
-  }
-
-  @Override
-  public String toString() {
-    return String.format(
-        "HasCollectionResponse {%s, has collection = %s}", response.toString(), hasCollection);
-  }
-}

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

@@ -95,11 +95,9 @@ public interface MilvusClient {
    * Checks whether the collection exists
    *
    * @param collectionName collection to check
-   * @return <code>HasCollectionResponse</code>
-   * @see HasCollectionResponse
-   * @see Response
+   * @return true if the collection exists, false otherwise.
    */
-  HasCollectionResponse hasCollection(String collectionName);
+  boolean hasCollection(String collectionName);
 
   /**
    * Drops collection

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

@@ -249,36 +249,13 @@ abstract class AbstractMilvusGrpcClient implements MilvusClient {
   }
 
   @Override
-  public HasCollectionResponse hasCollection(@Nonnull String collectionName) {
-
-    if (!maybeAvailable()) {
-      logWarning("You are not connected to Milvus server");
-      return new HasCollectionResponse(new Response(Response.Status.CLIENT_NOT_CONNECTED), false);
-    }
-
-    CollectionName request = CollectionName.newBuilder().setCollectionName(collectionName).build();
-    BoolReply response;
-
-    try {
-      response = blockingStub().hasCollection(request);
-
-      if (response.getStatus().getErrorCode() == ErrorCode.SUCCESS) {
-        logInfo("hasCollection `{}` = {}", collectionName, response.getBoolReply());
-        return new HasCollectionResponse(
-            new Response(Response.Status.SUCCESS), response.getBoolReply());
-      } else {
-        logError("hasCollection `{}` failed:\n{}", collectionName, response.toString());
-        return new HasCollectionResponse(
-            new Response(
-                Response.Status.valueOf(response.getStatus().getErrorCodeValue()),
-                response.getStatus().getReason()),
-            false);
-      }
-    } catch (StatusRuntimeException e) {
-      logError("hasCollection RPC failed:\n{}", e.getStatus().toString());
-      return new HasCollectionResponse(
-          new Response(Response.Status.RPC_ERROR, e.toString()), false);
-    }
+  public boolean hasCollection(@Nonnull String collectionName) {
+    return translateExceptions(() -> {
+      CollectionName request = CollectionName.newBuilder().setCollectionName(collectionName).build();
+      BoolReply response = blockingStub().hasCollection(request);
+      checkResponseStatus(response.getStatus());
+      return response.getBoolReply();
+    });
   }
 
   @Override

+ 1 - 3
src/test/java/io/milvus/client/MilvusGrpcClientTest.java

@@ -338,9 +338,7 @@ class MilvusClientTest {
 
   @org.junit.jupiter.api.Test
   void hasCollection() {
-    HasCollectionResponse hasCollectionResponse = client.hasCollection(randomCollectionName);
-    assertTrue(hasCollectionResponse.ok());
-    assertTrue(hasCollectionResponse.hasCollection());
+    assertTrue(client.hasCollection(randomCollectionName));
   }
 
   @org.junit.jupiter.api.Test