Skip to content
Closed
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Add support to list indexes
- Add support to configure index
- Add user-agent as a header
- Add integration tests for future stub

### v0.6.0
- Add async stub for data plane operations
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ The Java client doesn't support managing Pinecone services, only reading and wri

## Examples

- The most basic example usage is in `src/test/java/io/pinecone/PineconeClientLiveIntegTest.java`, covering most basic operations.
- For all data plane operations using:
- FutureStub: `src/integration/java/io/pinecone/DataPlane/FutureStubTest.java`
- SyncStub: `src/integration/java/io/pinecone/DataPlane/SyncStubTest.java`
- For control plane operations:
- CreateAndDeleteIndex: `src/integration/java/io/pinecone/ControlPlane/CreateAndDeleteIndexTest.java`
- ConfigureIndex: `src/integration/java/io/pinecone/ControlPlane/ConfigureIndexTest.java`

> [!NOTE]
> The java-basic-mvn example is outdated.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.pinecone.integration;
package io.pinecone.ControlPlane;

import io.pinecone.PineconeClientConfig;
import io.pinecone.PineconeClientLiveIntegTest;
import io.pinecone.PineconeIndexOperationClient;
import io.pinecone.exceptions.PineconeBadRequestException;
import io.pinecone.exceptions.PineconeNotFoundException;
Expand All @@ -20,7 +19,7 @@
public class ConfigureIndexTest {
private PineconeIndexOperationClient pinecone;
private String indexName;
private static final Logger logger = LoggerFactory.getLogger(PineconeClientLiveIntegTest.class);
private static final Logger logger = LoggerFactory.getLogger(ConfigureIndexTest.class);

@BeforeEach
public void setUp() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.pinecone;
package io.pinecone.ControlPlane;

import io.pinecone.PineconeClientConfig;
import io.pinecone.PineconeIndexOperationClient;
import io.pinecone.helpers.RandomStringBuilder;
import io.pinecone.model.CreateIndexRequest;
import io.pinecone.model.IndexMeta;
Expand All @@ -12,7 +14,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class PineconeIndexOperationsClientIntegrationTest {
public class CreateAndDeleteIndexTest {
private PineconeIndexOperationClient pinecone;

@BeforeEach
Expand Down
221 changes: 221 additions & 0 deletions src/integration/java/io/pinecone/DataPlane/FutureStubTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
package io.pinecone.DataPlane;

import com.google.common.primitives.Floats;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.protobuf.Struct;
import com.google.protobuf.Value;
import io.pinecone.*;
import io.pinecone.helpers.RandomStringBuilder;
import io.pinecone.model.IndexMeta;
import io.pinecone.proto.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;

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

public class FutureStubTest {

private static final Logger logger = LoggerFactory.getLogger(FutureStubTest.class);
public String indexName = "integ-test-sanity";

private PineconeClient dataPlaneClient;
private PineconeIndexOperationClient controlPlaneClient;
private PineconeConnection connection;
private String namespace;
private VectorServiceGrpc.VectorServiceFutureStub futureStub;

@BeforeEach
public void setUp() {
PineconeClientConfig config = new PineconeClientConfig()
.withApiKey(System.getenv("PINECONE_API_KEY"))
.withEnvironment(System.getenv("PINECONE_ENVIRONMENT"))
.withServerSideTimeoutSec(10);

controlPlaneClient = new PineconeIndexOperationClient(config);
dataPlaneClient = new PineconeClient(config);
namespace = RandomStringBuilder.build("ns", 8);

futureStub = connection.getFutureStub();
}

@Test
public void checkIndexSetup() throws Exception {
IndexMeta indexMeta = controlPlaneClient.describeIndex(indexName);
assertEquals(3, indexMeta.getDatabase().getDimension());

String host = indexMeta.getStatus().getHost();
connection = dataPlaneClient.connect(
new PineconeConnectionConfig()
.withConnectionUrl("https://" + host));
}

@Test
public void describeIndexStats() throws ExecutionException, InterruptedException {
DescribeIndexStatsRequest describeIndexRequest = DescribeIndexStatsRequest.newBuilder().build();
ListenableFuture<DescribeIndexStatsResponse> futureDescribeIndexResponse = futureStub.describeIndexStats(describeIndexRequest);

assertEquals(futureDescribeIndexResponse.get().getDimension(), 3);
}

@Test
public void sanity() throws Exception {
// upsert
float[][] upsertData = {{1.0F, 2.0F, 3.0F}, {4.0F, 5.0F, 6.0F}, {7.0F, 8.0F, 9.0F}};
List<String> upsertIds = Arrays.asList("v1", "v2", "v3");
List<Vector> upsertVectors = new ArrayList<>();

for (int i = 0; i < upsertData.length; i++) {
upsertVectors.add(Vector.newBuilder()
.addAllValues(Floats.asList(upsertData[i]))
.setMetadata(Struct.newBuilder()
.putFields("some_field", Value.newBuilder().setNumberValue(i).build())
.build())
.setId(upsertIds.get(i))
.build());
}

UpsertRequest request = UpsertRequest.newBuilder()
.addAllVectors(upsertVectors)
.setNamespace(namespace)
.build();

ListenableFuture<UpsertResponse> upsertFuture = futureStub.upsert(request);

UpsertResponse upsertResponse = upsertFuture.get();
logger.info("Put " + upsertResponse.getUpsertedCount() + " vectors into the index");
assertEquals(upsertResponse.getUpsertedCount(), 3);

// hybrid upsert
List<String> hybridsIds = Arrays.asList("v4", "v5", "v6");
List<Vector> hybridVectors = new ArrayList<>();
List<Integer> sparseIndices = Arrays.asList(0, 1, 2);
List<Float> sparseValues = Arrays.asList(0.11f, 0.22f, 0.33f);
for (int i = 0; i < hybridsIds.size(); i++) {
hybridVectors.add(
Vector.newBuilder()
.addAllValues(Floats.asList(upsertData[i]))
.setSparseValues(
SparseValues.newBuilder().addAllIndices(sparseIndices).addAllValues(sparseValues).build()
)
.setId(hybridsIds.get(i))
.build());
}

UpsertRequest hybridRequest = UpsertRequest.newBuilder()
.addAllVectors(hybridVectors)
.setNamespace(namespace)
.build();
ListenableFuture<UpsertResponse> hybridUpsertFuture = connection.getFutureStub().upsert(hybridRequest);

UpsertResponse hybridResponse = hybridUpsertFuture.get();
logger.info("Put " + hybridResponse.getUpsertedCount() + " vectors into the index");
assertEquals(hybridResponse.getUpsertedCount(), 3);

// fetch
List<String> ids = Arrays.asList("v1", "v2");
FetchRequest fetchRequest = FetchRequest.newBuilder().addAllIds(ids).setNamespace(namespace).build();
ListenableFuture<FetchResponse> fetchFuture = connection.getFutureStub().fetch(fetchRequest);
assert (fetchFuture.get().containsVectors("v1"));
assert (fetchFuture.get().containsVectors("v2"));


// Updates vector v1's values to 10.0, 11.0, and 12.0 from 1.0, 2.0, and 3.0
UpdateRequest updateRequest = UpdateRequest.newBuilder()
.setId("v1")
.setNamespace(namespace)
.addAllValues(Floats.asList(10F, 11F, 12F))
.build();
connection.getFutureStub().update(updateRequest);
fetchRequest = FetchRequest.newBuilder().addIds("v1").setNamespace(namespace).build();
connection.getFutureStub().fetch(fetchRequest);

// DEPRECATED: batch queries
float[] rawVector = {1.0F, 2.0F, 3.0F};
QueryVector queryVector = QueryVector.newBuilder()
.addAllValues(Floats.asList(rawVector))
.setFilter(Struct.newBuilder()
.putFields("some_field", Value.newBuilder()
.setStructValue(Struct.newBuilder()
.putFields("$lt", Value.newBuilder()
.setNumberValue(3)
.build()))
.build())
.build())
.setNamespace(namespace)
.build();

QueryRequest batchQueryRequest = QueryRequest.newBuilder()
.addQueries(queryVector)
.setNamespace(namespace)
.setTopK(2)
.setIncludeMetadata(true)
.build();

ListenableFuture<QueryResponse> queryFuture = connection.getFutureStub().query(batchQueryRequest);
QueryResponse queryResponse = queryFuture.get();
assertNotNull(queryResponse);
assertNotNull(queryResponse.getResultsList());
assertEquals(queryResponse.getResultsCount(), 1);

Iterable<Float> iterable = Arrays.asList(1.0F, 2.0F, 3.0F);
QueryRequest queryRequest = QueryRequest.newBuilder()
.addAllVector(iterable)
.setNamespace(namespace)
.setTopK(2)
.setIncludeMetadata(true)
.build();

// When querying using a single vector, we get matches instead of results
queryResponse = connection.getFutureStub().query(queryRequest).get();
assertNotNull(queryResponse);
assertNotNull(queryResponse.getMatchesList());
assertEquals(queryResponse.getMatchesCount(), 2);

// Query by id example
QueryRequest queryByIdRequest = QueryRequest.newBuilder()
.setId("v2")
.setNamespace(namespace)
.setTopK(2)
.setIncludeMetadata(true)
.build();

queryResponse = connection.getFutureStub().query(queryByIdRequest).get();
assertNotNull(queryResponse);
assertNotNull(queryResponse.getMatchesList());
assertEquals(queryResponse.getMatchesCount(), 2);

// Delete
String[] idsToDelete = {"v2"};
DeleteRequest deleteRequest = DeleteRequest.newBuilder()
.setNamespace(namespace)
.addAllIds(Arrays.asList(idsToDelete))
.setDeleteAll(false)
.build();

DeleteResponse deleteResponse = connection.getFutureStub().delete(deleteRequest).get();
fetchRequest = FetchRequest.newBuilder().addAllIds(ids).setNamespace(namespace).build();
FetchResponse fetchResponse = connection.getFutureStub().fetch(fetchRequest).get();
assertEquals(fetchResponse.getVectorsCount(), ids.size() - 1);

// Clear out the test
DeleteRequest deleteAllRequest = DeleteRequest.newBuilder()
.setNamespace(namespace)
.setDeleteAll(true)
.build();


connection.getFutureStub().delete(deleteAllRequest).get();

fetchRequest = FetchRequest.newBuilder().addAllIds(ids).setNamespace(namespace).build();
fetchResponse = connection.getFutureStub().fetch(fetchRequest).get();
assertEquals(fetchResponse.getVectorsCount(), 0);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package io.pinecone;
package io.pinecone.DataPlane;

import com.google.common.primitives.Floats;
import com.google.protobuf.Struct;
import com.google.protobuf.Value;
import io.pinecone.*;
import io.pinecone.helpers.RandomStringBuilder;
import io.pinecone.model.IndexMeta;
import io.pinecone.proto.*;
Expand All @@ -19,12 +20,13 @@
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class PineconeClientLiveIntegTest {
public class SyncStubTest {

public String indexName = "integ-test-sanity";

private static final Logger logger = LoggerFactory.getLogger(PineconeClientLiveIntegTest.class);
private static final Logger logger = LoggerFactory.getLogger(SyncStubTest.class);

private PineconeClient dataPlaneClient;
private PineconeIndexOperationClient controlPlaneClient;
Expand Down Expand Up @@ -78,7 +80,7 @@ public void sanity() throws Exception {

UpsertResponse upsertResponse = conn.getBlockingStub().upsert(request);
logger.info("Put " + upsertResponse.getUpsertedCount() + " vectors into the index");
assert (upsertResponse.getUpsertedCount() == 3);
assertEquals (upsertResponse.getUpsertedCount(), 3);

// hybrid upsert
List<String> hybridsIds = Arrays.asList("v4", "v5", "v6");
Expand All @@ -102,7 +104,7 @@ public void sanity() throws Exception {
.build();
UpsertResponse hybridResponse = conn.getBlockingStub().upsert(hybridRequest);
logger.info("Put " + hybridResponse.getUpsertedCount() + " vectors into the index");
assert (hybridResponse.getUpsertedCount() == 3);
assertEquals (hybridResponse.getUpsertedCount(), 3);

// fetch
List<String> ids = Arrays.asList("v1", "v2");
Expand Down Expand Up @@ -157,9 +159,9 @@ public void sanity() throws Exception {

// When querying using a single vector, we get matches instead of results
queryResponse = conn.getBlockingStub().query(queryRequest);
assertThat(queryResponse, notNullValue());
assertThat(queryResponse.getMatchesList(), notNullValue());
assertThat(queryResponse.getMatchesCount(), equalTo(2));
assertNotNull(queryResponse);
assertNotNull(queryResponse.getMatchesList());
assertEquals(queryResponse.getMatchesCount(), 2);

// Query by id example
QueryRequest queryByIdRequest = QueryRequest.newBuilder()
Expand All @@ -170,9 +172,9 @@ public void sanity() throws Exception {
.build();

queryResponse = conn.getBlockingStub().query(queryByIdRequest);
assertThat(queryResponse, notNullValue());
assertThat(queryResponse.getMatchesList(), notNullValue());
assertThat(queryResponse.getMatchesCount(), equalTo(2));
assertNotNull(queryResponse);
assertNotNull(queryResponse.getMatchesList());
assertEquals(queryResponse.getMatchesCount(), 2);

// Delete
String[] idsToDelete = {"v2"};
Expand All @@ -185,7 +187,7 @@ public void sanity() throws Exception {
conn.getBlockingStub().delete(deleteRequest);
fetchRequest = FetchRequest.newBuilder().addAllIds(ids).setNamespace(namespace).build();
fetchResponse = conn.getBlockingStub().fetch(fetchRequest);
assert (fetchResponse.getVectorsCount() == ids.size() - 1);
assertEquals (fetchResponse.getVectorsCount(), ids.size() - 1);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assertEquals (fetchResponse.getVectorsCount(), ids.size() - 1);
assertEquals(fetchResponse.getVectorsCount(), ids.size() - 1);


// Clear out the test
DeleteRequest deleteAllRequest = DeleteRequest.newBuilder()
Expand All @@ -196,6 +198,6 @@ public void sanity() throws Exception {
conn.getBlockingStub().delete(deleteAllRequest);
fetchRequest = FetchRequest.newBuilder().addAllIds(ids).setNamespace(namespace).build();
fetchResponse = conn.getBlockingStub().fetch(fetchRequest);
assert (fetchResponse.getVectorsCount() == 0);
assertEquals (fetchResponse.getVectorsCount(), 0);
}
}