Skip to content

Commit becb5fa

Browse files
refactor findIndexWithDimensionAndType to avoid waiting on unrelated indexes
1 parent 96e6710 commit becb5fa

File tree

2 files changed

+10
-24
lines changed

2 files changed

+10
-24
lines changed

src/integration/java/io/pinecone/helpers/IndexManager.java

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,34 +36,20 @@ public static PineconeConnection createIndexIfNotExistsDataPlane(int dimension,
3636
return new PineconeConnection(config);
3737
}
3838

39-
public static String createIndexIfNotExistsControlPlane(Pinecone pinecone, int dimension, String indexType) throws IOException, InterruptedException {
40-
String indexName = findIndexWithDimensionAndType(pinecone, dimension, indexType);
41-
42-
return (indexName.isEmpty()) ? createNewIndex(pinecone, dimension, indexType) : indexName;
43-
}
44-
45-
public static String findIndexWithDimensionAndType(Pinecone pinecone, int dimension, String indexType)
46-
throws InterruptedException {
39+
public static String findIndexWithDimensionAndType(Pinecone pinecone, int dimension, String indexType) {
4740
String indexName = "";
48-
int i = 0;
4941
List<IndexModel> indexModels = pinecone.listIndexes().getIndexes();
5042
if(indexModels == null) {
5143
return indexName;
5244
}
53-
while (i < indexModels.size()) {
54-
IndexModel indexModel = waitUntilIndexIsReady(pinecone, indexModels.get(i).getName());
45+
46+
for (IndexModel indexModel : indexModels) {
5547
if (indexModel.getDimension() == dimension
56-
&&
57-
(
58-
indexType.equalsIgnoreCase(IndexModelSpec.SERIALIZED_NAME_POD)
59-
&& indexModel.getSpec().getPod() != null
60-
&& indexModel.getSpec().getPod().getReplicas() == 1
61-
&& indexModel.getSpec().getPod().getPodType().equalsIgnoreCase("p1.x1")
62-
)
63-
|| (indexType.equalsIgnoreCase(IndexModelSpec.SERIALIZED_NAME_SERVERLESS))) {
48+
&& (indexType.equalsIgnoreCase(IndexModelSpec.SERIALIZED_NAME_POD) && indexModel.getSpec().getPod() != null)
49+
|| (indexType.equalsIgnoreCase(IndexModelSpec.SERIALIZED_NAME_SERVERLESS) && indexModel.getSpec().getServerless() != null)
50+
) {
6451
return indexModel.getName();
6552
}
66-
i++;
6753
}
6854
return indexName;
6955
}
@@ -117,7 +103,7 @@ public static IndexModel waitUntilIndexIsReady(Pinecone pinecone, String indexNa
117103
return waitUntilIndexIsReady(pinecone, indexName, 300000);
118104
}
119105

120-
public static PineconeConnection createNewIndexAndConnect(Pinecone pinecone, String indexName, int dimension, IndexMetric metric, CreateIndexRequestSpec spec) throws InterruptedException, PineconeException {
106+
public static PineconeConnection createNewIndexAndConnect(Pinecone pinecone, String indexName, int dimension, IndexMetric metric, CreateIndexRequestSpec spec, boolean waitUntilIndexIsReady) throws InterruptedException, PineconeException {
121107
String apiKey = System.getenv("PINECONE_API_KEY");
122108
CreateIndexRequest createIndexRequest = new CreateIndexRequest().name(indexName).dimension(dimension).metric(metric).spec(spec);
123109
pinecone.createIndex(createIndexRequest);

src/integration/java/io/pinecone/integration/controlPlane/pod/CollectionTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public static void setUp() throws InterruptedException {
4545
new CreateIndexRequestSpecPod().pods(1).podType("p1.x1").replicas(1).environment(environment);
4646
CreateIndexRequestSpec spec = new CreateIndexRequestSpec().pod(podSpec);
4747
PineconeConnection dataPlaneConnection = createNewIndexAndConnect(controlPlaneClient, indexName, dimension,
48-
indexMetric, spec);
48+
indexMetric, spec, true);
4949
VectorServiceGrpc.VectorServiceBlockingStub blockingStub = dataPlaneConnection.getBlockingStub();
5050
indexes.add(indexName);
5151

@@ -119,7 +119,7 @@ public void testIndexFromCollectionHappyPath() throws InterruptedException {
119119
assertTrue(collection.getSize() > 0);
120120

121121
// Create index from collection
122-
String newIndexName = RandomStringBuilder.build("index-from-col", 5);
122+
String newIndexName = RandomStringBuilder.build("from-col", 5);
123123
logger.info("Creating index " + newIndexName + " from collection " + collectionName);
124124

125125
CreateIndexRequestSpecPod podSpec =
@@ -179,7 +179,7 @@ public void testIndexFromDifferentMetricCollection() throws InterruptedException
179179
new CreateIndexRequestSpecPod().environment(environment).sourceCollection(collectionName);
180180
CreateIndexRequestSpec spec = new CreateIndexRequestSpec().pod(podSpec);
181181
PineconeConnection dataPlaneConnection = createNewIndexAndConnect(controlPlaneClient, newIndexName, dimension
182-
, targetMetric, spec);
182+
, targetMetric, spec, false);
183183
indexes.add(newIndexName);
184184

185185
IndexModel newIndex = controlPlaneClient.describeIndex(newIndexName);

0 commit comments

Comments
 (0)