Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-175 Adds schema class exists method #196

Merged
merged 4 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/main/java/io/weaviate/client/v1/schema/Schema.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package io.weaviate.client.v1.schema;

import io.weaviate.client.base.http.HttpClient;
import io.weaviate.client.Config;
import io.weaviate.client.v1.schema.api.ClassCreator;
import io.weaviate.client.v1.schema.api.ClassDeleter;
import io.weaviate.client.v1.schema.api.ClassExists;
import io.weaviate.client.v1.schema.api.ClassGetter;
import io.weaviate.client.v1.schema.api.PropertyCreator;
import io.weaviate.client.v1.schema.api.SchemaDeleter;
import io.weaviate.client.v1.schema.api.SchemaGetter;
import io.weaviate.client.v1.schema.api.ShardUpdater;
import io.weaviate.client.v1.schema.api.ShardsGetter;
import io.weaviate.client.v1.schema.api.ShardsUpdater;
import io.weaviate.client.Config;

public class Schema {
private final Config config;
Expand All @@ -29,6 +30,10 @@ public ClassGetter classGetter() {
return new ClassGetter(httpClient, config);
}

public ClassExists exists() {
return new ClassExists(new ClassGetter(httpClient, config));
}

public ClassCreator classCreator() {
return new ClassCreator(httpClient, config);
}
Expand Down
44 changes: 44 additions & 0 deletions src/main/java/io/weaviate/client/v1/schema/api/ClassExists.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.weaviate.client.v1.schema.api;

import io.weaviate.client.base.ClientResult;
import io.weaviate.client.base.Result;
import io.weaviate.client.base.WeaviateError;
import io.weaviate.client.base.WeaviateErrorMessage;
import io.weaviate.client.base.WeaviateErrorResponse;
import io.weaviate.client.v1.schema.model.WeaviateClass;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;

import java.util.Collections;

public class ClassExists implements ClientResult<Boolean> {
private final ClassGetter classGetter;
private String className;

public ClassExists(ClassGetter classGetter) {
this.classGetter = classGetter;
}

public ClassExists withClassName(String className) {
this.className = className;
return this;
}

@Override
public Result<Boolean> run() {
if (StringUtils.isEmpty(className)) {
WeaviateErrorMessage errorMessage = WeaviateErrorMessage.builder()
.message("classname cannot be empty").build();
WeaviateErrorResponse errors = WeaviateErrorResponse.builder()
.error(Collections.singletonList(errorMessage)).build();
return new Result<>(HttpStatus.SC_UNPROCESSABLE_ENTITY, null, errors);
}

Result<WeaviateClass> getterClass = classGetter.withClassName(className).run();
if (getterClass.hasErrors()) {
WeaviateError error = getterClass.getError();
return new Result<>(error.getStatusCode(), null, WeaviateErrorResponse.builder().error(error.getMessages()).build());
}
return new Result<>(HttpStatus.SC_OK, getterClass.getResult() != null, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import static org.assertj.core.api.InstanceOfAssertFactories.MAP;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -760,15 +761,14 @@ public void testSchemaGetShards() {
.build();
// when
Result<Boolean> createStatus = client.schema().classCreator().withClass(clazz).run();
Result<WeaviateClass> bandClass = client.schema().classGetter().withClassName(clazz.getClassName()).run();

Result<Boolean> bandClassStatus = client.schema().exists().withClassName(clazz.getClassName()).run();
Result<Boolean> nonExistentClassStatus = client.schema().exists().withClassName("nonExistentClass").run();
// then
assertNotNull(createStatus);
assertTrue(createStatus.getResult());
assertNotNull(bandClass);
assertNotNull(bandClass.getResult());
assertNull(bandClass.getError());

assertResultTrue(createStatus);
assertResultTrue(bandClassStatus);
assertNotNull(nonExistentClassStatus);
assertFalse(nonExistentClassStatus.getResult());
assertNull(nonExistentClassStatus.getError());
Result<Shard[]> shards = client.schema().shardsGetter()
.withClassName(clazz.getClassName()).run();
assertNotNull(shards);
Expand Down