Skip to content

Commit 6035516

Browse files
sobychackoilayaperumalg
authored andcommitted
GH-2165: Simplify VectorStore delete method to return void
- Change VectorStore.delete() and related implementations to return void instead of Optional<Boolean> - Remove unnecessary boolean return values and success status checks across all vector store implementations - Clean up tests by removing redundant assertions - Implementations continue using runtime exceptions for error signaling Signed-off-by: Soby Chacko <[email protected]>
1 parent b466159 commit 6035516

File tree

26 files changed

+43
-127
lines changed

26 files changed

+43
-127
lines changed

spring-ai-core/src/main/java/org/springframework/ai/vectorstore/SimpleVectorStore.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,10 @@ public void doAdd(List<Document> documents) {
106106
}
107107

108108
@Override
109-
public Optional<Boolean> doDelete(List<String> idList) {
109+
public void doDelete(List<String> idList) {
110110
for (String id : idList) {
111111
this.store.remove(id);
112112
}
113-
return Optional.of(true);
114113
}
115114

116115
@Override

spring-ai-core/src/main/java/org/springframework/ai/vectorstore/VectorStore.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,8 @@ default void accept(List<Document> documents) {
5959
/**
6060
* Deletes documents from the vector store.
6161
* @param idList list of document ids for which documents will be removed.
62-
* @return Returns true if the documents were successfully deleted.
6362
*/
64-
@Nullable
65-
Optional<Boolean> delete(List<String> idList);
63+
void delete(List<String> idList);
6664

6765
/**
6866
* Deletes documents from the vector store based on filter criteria.

spring-ai-core/src/main/java/org/springframework/ai/vectorstore/observation/AbstractObservationVectorStore.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,13 @@ public void add(List<Document> documents) {
8787
}
8888

8989
@Override
90-
@Nullable
91-
public Optional<Boolean> delete(List<String> deleteDocIds) {
90+
public void delete(List<String> deleteDocIds) {
9291

9392
VectorStoreObservationContext observationContext = this
9493
.createObservationContextBuilder(VectorStoreObservationContext.Operation.DELETE.value())
9594
.build();
9695

97-
return VectorStoreObservationDocumentation.AI_VECTOR_STORE
96+
VectorStoreObservationDocumentation.AI_VECTOR_STORE
9897
.observation(this.customObservationConvention, DEFAULT_OBSERVATION_CONVENTION, () -> observationContext,
9998
this.observationRegistry)
10099
.observe(() -> this.doDelete(deleteDocIds));
@@ -140,9 +139,8 @@ public List<Document> similaritySearch(SearchRequest request) {
140139
/**
141140
* Perform the actual delete operation.
142141
* @param idList the list of document IDs to delete
143-
* @return true if the documents were successfully deleted
144142
*/
145-
public abstract Optional<Boolean> doDelete(List<String> idList);
143+
public abstract void doDelete(List<String> idList);
146144

147145
/**
148146
* Template method for concrete implementations to provide filter-based deletion

spring-ai-core/src/test/java/org/springframework/ai/vectorstore/SimpleVectorStoreTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
import static org.assertj.core.api.Assertions.assertThat;
4040
import static org.assertj.core.api.Assertions.assertThatThrownBy;
41+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
4142
import static org.mockito.ArgumentMatchers.any;
4243
import static org.mockito.Mockito.mock;
4344
import static org.mockito.Mockito.when;
@@ -112,8 +113,8 @@ void shouldDeleteDocuments() {
112113
@Test
113114
void shouldHandleDeleteOfNonexistentDocument() {
114115
this.vectorStore.delete(List.of("nonexistent-id"));
115-
// Should not throw exception and return true
116-
assertThat(this.vectorStore.delete(List.of("nonexistent-id")).get()).isTrue();
116+
// Should not throw exception
117+
assertDoesNotThrow(() -> this.vectorStore.delete(List.of("nonexistent-id")));
117118
}
118119

119120
@Test

vector-stores/spring-ai-azure-cosmos-db-store/src/main/java/org/springframework/ai/vectorstore/cosmosdb/CosmosDBVectorStore.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ public void doAdd(List<Document> documents) {
271271
}
272272

273273
@Override
274-
public Optional<Boolean> doDelete(List<String> idList) {
274+
public void doDelete(List<String> idList) {
275275
try {
276276
// Convert the list of IDs into bulk delete operations
277277
List<CosmosItemOperation> itemOperations = idList.stream()
@@ -285,12 +285,9 @@ public Optional<Boolean> doDelete(List<String> idList) {
285285
response.getResponse().getStatusCode()))
286286
.doOnError(error -> logger.error("Error deleting document: {}", error.getMessage()))
287287
.blockLast(); // This will block until all operations have finished
288-
289-
return Optional.of(true);
290288
}
291289
catch (Exception e) {
292290
logger.error("Exception while deleting documents: {}", e.getMessage());
293-
return Optional.of(false);
294291
}
295292
}
296293

vector-stores/spring-ai-azure-store/src/main/java/org/springframework/ai/vectorstore/azure/AzureVectorStore.java

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -188,31 +188,17 @@ public void doAdd(List<Document> documents) {
188188
}
189189

190190
@Override
191-
public Optional<Boolean> doDelete(List<String> documentIds) {
191+
public void doDelete(List<String> documentIds) {
192192

193193
Assert.notNull(documentIds, "The document ID list should not be null.");
194-
if (CollectionUtils.isEmpty(documentIds)) {
195-
return Optional.of(true); // nothing to do;
196-
}
197194

198195
final var searchDocumentIds = documentIds.stream().map(documentId -> {
199196
SearchDocument searchDocument = new SearchDocument();
200197
searchDocument.put(ID_FIELD_NAME, documentId);
201198
return searchDocument;
202199
}).toList();
203200

204-
var results = this.searchClient.deleteDocuments(searchDocumentIds);
205-
206-
boolean resSuccess = true;
207-
208-
for (IndexingResult result : results.getResults()) {
209-
if (!result.isSucceeded()) {
210-
resSuccess = false;
211-
break;
212-
}
213-
}
214-
215-
return Optional.of(resSuccess);
201+
this.searchClient.deleteDocuments(searchDocumentIds);
216202
}
217203

218204
@Override

vector-stores/spring-ai-cassandra-store/src/main/java/org/springframework/ai/vectorstore/cassandra/CassandraVectorStore.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ public void doAdd(List<Document> documents) {
305305
}
306306

307307
@Override
308-
public Optional<Boolean> doDelete(List<String> idList) {
308+
public void doDelete(List<String> idList) {
309309
CompletableFuture[] futures = new CompletableFuture[idList.size()];
310310
int i = 0;
311311
for (String id : idList) {
@@ -314,7 +314,6 @@ public Optional<Boolean> doDelete(List<String> idList) {
314314
futures[i++] = this.session.executeAsync(s).toCompletableFuture();
315315
}
316316
CompletableFuture.allOf(futures).join();
317-
return Optional.of(Boolean.TRUE);
318317
}
319318

320319
@Override
@@ -339,13 +338,7 @@ protected void doDelete(Filter.Expression filterExpression) {
339338
if (!matchingDocs.isEmpty()) {
340339
// Then delete those documents by ID
341340
List<String> idsToDelete = matchingDocs.stream().map(Document::getId).collect(Collectors.toList());
342-
343-
Optional<Boolean> result = delete(idsToDelete);
344-
345-
if (result.isPresent() && !result.get()) {
346-
throw new IllegalStateException("Failed to delete some documents");
347-
}
348-
341+
delete(idsToDelete);
349342
logger.debug("Deleted {} documents matching filter expression", idsToDelete.size());
350343
}
351344
}

vector-stores/spring-ai-chroma-store/src/main/java/org/springframework/ai/chroma/vectorstore/ChromaVectorStore.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,9 @@ public void doAdd(@NonNull List<Document> documents) {
158158
}
159159

160160
@Override
161-
public Optional<Boolean> doDelete(@NonNull List<String> idList) {
161+
public void doDelete(List<String> idList) {
162162
Assert.notNull(idList, "Document id list must not be null");
163-
int status = this.chromaApi.deleteEmbeddings(this.collectionId, new DeleteEmbeddingsRequest(idList));
164-
return Optional.of(status == 200);
163+
this.chromaApi.deleteEmbeddings(this.collectionId, new DeleteEmbeddingsRequest(idList));
165164
}
166165

167166
@Override

vector-stores/spring-ai-chroma-store/src/test/java/org/springframework/ai/chroma/vectorstore/ChromaVectorStoreIT.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ public void addAndSearch() {
8888
assertThat(resultDoc.getMetadata()).containsKeys("meta2", DocumentMetadata.DISTANCE.value());
8989

9090
// Remove all documents from the store
91-
assertThat(vectorStore.delete(this.documents.stream().map(doc -> doc.getId()).toList()))
92-
.isEqualTo(Optional.of(Boolean.TRUE));
91+
vectorStore.delete(this.documents.stream().map(doc -> doc.getId()).toList());
9392

9493
List<Document> results2 = vectorStore
9594
.similaritySearch(SearchRequest.builder().query("Great").topK(1).build());
@@ -118,7 +117,7 @@ public void simpleSearch() {
118117
assertThat(resultDoc.getText()).isEqualTo("The sky is blue because of Rayleigh scattering.");
119118

120119
// Remove all documents from the store
121-
assertThat(vectorStore.delete(List.of(document.getId()))).isEqualTo(Optional.of(Boolean.TRUE));
120+
vectorStore.delete(List.of(document.getId()));
122121

123122
results = vectorStore.similaritySearch(SearchRequest.builder().query("Why is the sky blue?").build());
124123
assertThat(results).hasSize(0);

vector-stores/spring-ai-coherence-store/src/main/java/org/springframework/ai/vectorstore/coherence/CoherenceVectorStore.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -178,21 +178,15 @@ public void doAdd(final List<Document> documents) {
178178
}
179179

180180
@Override
181-
public Optional<Boolean> doDelete(final List<String> idList) {
181+
public void doDelete(final List<String> idList) {
182182
var chunkIds = idList.stream().map(this::toChunkId).toList();
183-
Map<DocumentChunk.Id, Boolean> results = this.documentChunks.invokeAll(chunkIds, entry -> {
183+
this.documentChunks.invokeAll(chunkIds, entry -> {
184184
if (entry.isPresent()) {
185185
entry.remove(false);
186186
return true;
187187
}
188188
return false;
189189
});
190-
for (boolean r : results.values()) {
191-
if (!r) {
192-
return Optional.of(false);
193-
}
194-
}
195-
return Optional.of(true);
196190
}
197191

198192
@Override

0 commit comments

Comments
 (0)