Skip to content

Commit

Permalink
Add support for gRPC Batch API
Browse files Browse the repository at this point in the history
  • Loading branch information
antas-marcin committed Sep 13, 2023
1 parent 4406016 commit 0e43bf7
Show file tree
Hide file tree
Showing 18 changed files with 42,331 additions and 12 deletions.
55 changes: 55 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@
<oauth2-oidc-sdk.version>10.14.2</oauth2-oidc-sdk.version>
<mock-server.version>5.15.0</mock-server.version>
<jackson.version>2.15.2</jackson.version>
<grpc-netty-shaded.version>1.58.0</grpc-netty-shaded.version>
<grpc-protobuf.version>1.58.0</grpc-protobuf.version>
<grpc-stub.version>1.58.0</grpc-stub.version>
<annotations-api.version>6.0.53</annotations-api.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -100,6 +104,28 @@
<artifactId>oauth2-oidc-sdk</artifactId>
<version>${oauth2-oidc-sdk.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty-shaded</artifactId>
<version>${grpc-netty-shaded.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>${grpc-protobuf.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>${grpc-stub.version}</version>
</dependency>
<dependency> <!-- necessary for Java 9+ -->
<groupId>org.apache.tomcat</groupId>
<artifactId>annotations-api</artifactId>
<version>${annotations-api.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
Expand Down Expand Up @@ -157,6 +183,13 @@
</dependencies>

<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.7.1</version>
</extension>
</extensions>
<pluginManagement>
<plugins>
<plugin>
Expand Down Expand Up @@ -346,6 +379,24 @@
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.1.1</version>
</plugin>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.6.1</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.24.0:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.58.0:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
<plugins>
Expand Down Expand Up @@ -373,6 +424,10 @@
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
</plugin>
<!-- <plugin>-->
<!-- <groupId>org.xolstice.maven.plugins</groupId>-->
<!-- <artifactId>protobuf-maven-plugin</artifactId>-->
<!-- </plugin>-->
</plugins>
</build>
</project>
33 changes: 32 additions & 1 deletion src/main/java/io/weaviate/client/Config.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package io.weaviate.client;

import java.util.Map;
import lombok.Getter;

public class Config {

private static final int DEFAULT_TIMEOUT_SECONDS = 60;
@Getter
private final String scheme;
private final String host;
private final String version;
Expand All @@ -15,6 +17,9 @@ public class Config {
private String proxyHost;
private int proxyPort;
private String proxyScheme;
private boolean useGRPC;
@Getter
private String grpcAddress;


public Config(String scheme, String host) {
Expand All @@ -35,10 +40,25 @@ public Config(String scheme, String host, Map<String, String> headers, int conne
this.socketTimeout = socketTimeout;
}

public Config(String scheme, String host, Map<String, String> headers, int timeout, boolean useGRPC) {
this.scheme = scheme;
this.host = host;
this.version = "v1";
this.headers = headers;
this.connectionTimeout = timeout;
this.connectionRequestTimeout = timeout;
this.socketTimeout = timeout;
this.useGRPC = useGRPC;
}

public String getBaseURL() {
return scheme + "://" + host + "/" + version;
}

public String getHost() {
return host;
}

public Map<String, String> getHeaders() {
return headers;
}
Expand Down Expand Up @@ -72,5 +92,16 @@ public int getProxyPort() {
public String getProxyScheme() {
return proxyScheme;
}


public boolean useGRPC() {
return this.useGRPC;
}

public void setUseGRPC(boolean useGRPC) {
this.useGRPC = useGRPC;
}

public void setGrpcAddress(String grpcAddress) {
this.grpcAddress = grpcAddress;
}
}
38 changes: 38 additions & 0 deletions src/main/java/io/weaviate/client/grpc/client/GrpcClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.weaviate.client.grpc.client;

import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.Metadata;
import io.grpc.stub.MetadataUtils;
import io.weaviate.client.Config;
import io.weaviate.client.grpc.protocol.WeaviateGrpc;
import java.util.Map;

public class GrpcClient {

public static WeaviateGrpc.WeaviateBlockingStub create(Config config) {
Metadata headers = new Metadata();
if (config.getHeaders() != null) {
for (Map.Entry<String, String> e : config.getHeaders().entrySet()) {
headers.put(Metadata.Key.of(e.getKey(), Metadata.ASCII_STRING_MARSHALLER), e.getValue());
}
}
ManagedChannel channel = ManagedChannelBuilder.forTarget(getAddress(config)).usePlaintext().build();
WeaviateGrpc.WeaviateBlockingStub blockingStub = WeaviateGrpc.newBlockingStub(channel);
return blockingStub.withInterceptors(MetadataUtils.newAttachHeadersInterceptor(headers));
}

private static String getAddress(Config config) {
if (config.getGrpcAddress() != null) {
return config.getGrpcAddress();
}
String host = config.getHost();
if (!host.contains(":")) {
if (config.getScheme() != null && config.getScheme().equals("https")) {
return String.format("%s:443", host);
}
return String.format("%s:80", host);
}
return host;
}
}
Loading

0 comments on commit 0e43bf7

Please sign in to comment.