Skip to content
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
5 changes: 5 additions & 0 deletions src/main/java/io/pinecone/PineconeClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@ public PineconeConnection connect(String indexName) {
public PineconeConnection connect(PineconeConnectionConfig connectionConfig) {
return new PineconeConnection(config, connectionConfig);
Comment thread
rohanshah18 marked this conversation as resolved.
Resolved
}

public PineconeConnection connectWithUrl(String connectionUrl) {
Comment thread
rohanshah18 marked this conversation as resolved.
Resolved
return connect(new PineconeConnectionConfig()
.withConnectionUrl(connectionUrl));
}
}
4 changes: 0 additions & 4 deletions src/main/java/io/pinecone/PineconeClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,6 @@ public PineconeClientConfig withServerSideTimeoutSec(int serverSideTimeout) {
void validate() {
if (apiKey == null)
throw new PineconeValidationException("Invalid Pinecone config: missing apiKey");
if (environment == null)
Comment thread
rohanshah18 marked this conversation as resolved.
Resolved
throw new PineconeValidationException("Invalid Pinecone config: missing environment");
if (projectName == null)
throw new PineconeValidationException("Invalid Pinecone config: missing projectName ");
}

@Override
Expand Down
36 changes: 29 additions & 7 deletions src/main/java/io/pinecone/PineconeConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import io.grpc.ManagedChannel;
import io.grpc.Metadata;
import io.grpc.StatusRuntimeException;
import io.grpc.netty.GrpcSslContexts;
import io.grpc.netty.NegotiationType;
import io.grpc.netty.NettyChannelBuilder;
Expand Down Expand Up @@ -36,9 +35,10 @@ public class PineconeConnection implements AutoCloseable {
private VectorServiceGrpc.VectorServiceStub asyncStub;

public PineconeConnection(PineconeClientConfig clientConfig, PineconeConnectionConfig connectionConfig) {
connectionConfig.validate();
this.connectionConfig = connectionConfig;
this.clientConfig = clientConfig;
validateConfigs();

channel = connectionConfig.getCustomChannelBuilder() != null
? connectionConfig.getCustomChannelBuilder().apply(clientConfig, connectionConfig)
: buildChannel(clientConfig, connectionConfig);
Expand Down Expand Up @@ -113,15 +113,37 @@ private VectorServiceGrpc.VectorServiceBlockingStub applyDefaultBlockingStubConf
.withMaxOutboundMessageSize(DEFAULT_MAX_MESSAGE_SIZE);
}

private static String getEndpoint(PineconeClientConfig clientConfig, PineconeConnectionConfig connectionConfig) {
String endpoint = String.format("%s-%s.svc.%s.pinecone.io",
connectionConfig.getIndexName(),
clientConfig.getProjectName(),
clientConfig.getEnvironment());
static String getEndpoint(PineconeClientConfig clientConfig, PineconeConnectionConfig connectionConfig) {
String endpoint = (connectionConfig.getConnectionUrl() != null) ?
Comment thread
rohanshah18 marked this conversation as resolved.
Resolved
connectionConfig.getConnectionUrl().replaceFirst("https?://", "") :
Comment thread
rohanshah18 marked this conversation as resolved.
Resolved
String.format("%s-%s.svc.%s.pinecone.io",
connectionConfig.getIndexName(),
clientConfig.getProjectName(),
clientConfig.getEnvironment());

logger.debug("Pinecone endpoint is: " + endpoint);

return endpoint;
}

void validateConfigs() throws PineconeValidationException {
if (this.clientConfig == null) {
throw new PineconeValidationException("PineconeClientConfiguration may not be null");
}

if (this.connectionConfig == null) {
throw new PineconeValidationException("PineconeConnectionConfig may not be null");
}

this.clientConfig.validate();
this.connectionConfig.validate();

if (this.connectionConfig.getIndexName() != null) {
if (this.clientConfig.getEnvironment() == null || this.clientConfig.getProjectName() == null) {
throw new PineconeValidationException("Cannot connect with indexName "
+ this.connectionConfig.getIndexName()
+ " unless PineconeClientConfig contains projectName and environment");
}
}
}
}
19 changes: 16 additions & 3 deletions src/main/java/io/pinecone/PineconeConnectionConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ public class PineconeConnectionConfig {
*/
private String indexName;

private String connectionUrl;

/**
* Creates a new default config.
*/
public PineconeConnectionConfig() {}

protected PineconeConnectionConfig(PineconeConnectionConfig other) {
indexName = other.indexName;
connectionUrl = other.connectionUrl;
customChannelBuilder = other.customChannelBuilder;
}

Expand All @@ -42,6 +45,16 @@ public PineconeConnectionConfig withIndexName(String indexName) {
return config;
}

public String getConnectionUrl() {
return connectionUrl;
}

public PineconeConnectionConfig withConnectionUrl(String connectionUrl) {
PineconeConnectionConfig config = new PineconeConnectionConfig(this);
config.connectionUrl = connectionUrl;
return config;
}

Comment thread
rohanshah18 marked this conversation as resolved.
Resolved
public BiFunction<PineconeClientConfig, PineconeConnectionConfig, ManagedChannel> getCustomChannelBuilder() {
return customChannelBuilder;
}
Expand All @@ -53,16 +66,16 @@ public PineconeConnectionConfig withCustomChannelBuilder(BiFunction<PineconeClie
}

void validate() {
String messagePrefix = "Invalid Pinecone config: ";
if (indexName == null)
throw new PineconeValidationException(messagePrefix + "indexName must be specified");
if (indexName == null && connectionUrl == null)
throw new PineconeValidationException("Invalid PineconeConnectionConfig, indexName or connection url must be specified.");
}

@Override
public String toString() {
return "PineconeConnectionConfig("
+ "customChannelBuilder=" + getCustomChannelBuilder()
+ ", indexName=" + getIndexName()
+ ", connectionUrl=" + getConnectionUrl()
+ ")";
}
}
44 changes: 44 additions & 0 deletions src/test/java/io/pinecone/PineconeConnectionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.pinecone;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class PineconeConnectionTest {

@Test
void testGetEndpointWithConnectionUrlWithHttps() {
PineconeClientConfig clientConfig = new PineconeClientConfig().withApiKey("api-key");
PineconeConnectionConfig connectionConfig = new PineconeConnectionConfig()
.withConnectionUrl("https://steps-784-123-eqasas0aaaa1213aasasc-1223-f1eea9.svc.production.pinecone.io");

String endpoint = PineconeConnection.getEndpoint(clientConfig, connectionConfig);

assertEquals("steps-784-123-eqasas0aaaa1213aasasc-1223-f1eea9.svc.production.pinecone.io", endpoint);
}

@Test
void testGetEndpointWithConnectionUrlWithHttp() {
PineconeClientConfig clientConfig = new PineconeClientConfig().withApiKey("api-key");
PineconeConnectionConfig connectionConfig = new PineconeConnectionConfig()
.withConnectionUrl("http://steps-784-123-eqasas0aaaa1213aasasc-1223-f1eea9.svc.production.pinecone.io");

String endpoint = PineconeConnection.getEndpoint(clientConfig, connectionConfig);

assertEquals("steps-784-123-eqasas0aaaa1213aasasc-1223-f1eea9.svc.production.pinecone.io", endpoint);
}

@Test
void testGetEndpointWithoutConnectionUrl() {
PineconeClientConfig clientConfig = new PineconeClientConfig()
.withApiKey("secret-api-key")
.withEnvironment("aws-us-east4")
.withProjectName("fee911a");
PineconeConnectionConfig connectionConfig = new PineconeConnectionConfig()
.withIndexName("step-2");

String endpoint = PineconeConnection.getEndpoint(clientConfig, connectionConfig);

assertEquals("step-2-fee911a.svc.aws-us-east4.pinecone.io", endpoint);
}
}