Skip to content

Commit 1140747

Browse files
authored
feat: bulk delete documents (#77)
1 parent d0541f9 commit 1140747

File tree

6 files changed

+80
-0
lines changed

6 files changed

+80
-0
lines changed

document-store/src/integrationTest/java/org/hypertrace/core/documentstore/DocStoreTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Iterator;
2020
import java.util.List;
2121
import java.util.Map;
22+
import java.util.Set;
2223
import java.util.stream.IntStream;
2324
import java.util.stream.Stream;
2425
import org.apache.commons.lang3.RandomUtils;
@@ -981,6 +982,23 @@ public void testDelete(String dataStoreName) throws IOException {
981982
Assertions.assertEquals(collection.count(), 0);
982983
}
983984

985+
@ParameterizedTest
986+
@MethodSource("databaseContextProvider")
987+
public void testBulkDelete(String dataStoreName) throws IOException {
988+
Datastore datastore = datastoreMap.get(dataStoreName);
989+
Collection collection = datastore.getCollection(COLLECTION_NAME);
990+
SingleValueKey docKey1 = new SingleValueKey("default", "testKey1");
991+
collection.upsert(docKey1, Utils.createDocument("foo1", "bar1"));
992+
SingleValueKey docKey2 = new SingleValueKey("default", "testKey2");
993+
collection.upsert(docKey2, Utils.createDocument("foo2", "bar2"));
994+
SingleValueKey docKey3 = new SingleValueKey("default", "testKey3");
995+
collection.upsert(docKey3, Utils.createDocument("foo3", "bar3"));
996+
997+
Assertions.assertEquals(collection.count(), 3);
998+
collection.delete(Set.of(docKey1, docKey2));
999+
Assertions.assertEquals(collection.count(), 1);
1000+
}
1001+
9841002
@ParameterizedTest
9851003
@MethodSource("databaseContextProvider")
9861004
public void testDeleteAll(String dataStoreName) throws IOException {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.hypertrace.core.documentstore;
2+
3+
public class BulkDeleteResult extends DeleteResult {
4+
5+
public BulkDeleteResult(long deletedCount) {
6+
super(deletedCount);
7+
}
8+
}

document-store/src/main/java/org/hypertrace/core/documentstore/Collection.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.IOException;
44
import java.util.List;
55
import java.util.Map;
6+
import java.util.Set;
67

78
/** Interface spec for common operations on a collection of documents */
89
public interface Collection {
@@ -87,6 +88,13 @@ public interface Collection {
8788
*/
8889
boolean delete(Key key);
8990

91+
/**
92+
* Delete the documents for the given keys
93+
*
94+
* @param keys {@link Key}s of the document to be deleted
95+
*/
96+
BulkDeleteResult delete(Set<Key> keys);
97+
9098
/**
9199
* Deletes a sub document
92100
*
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.hypertrace.core.documentstore;
2+
3+
public class DeleteResult {
4+
private long deletedCount;
5+
6+
public DeleteResult(long deletedCount) {
7+
this.deletedCount = deletedCount;
8+
}
9+
10+
public long getDeletedCount() {
11+
return deletedCount;
12+
}
13+
}

document-store/src/main/java/org/hypertrace/core/documentstore/mongo/MongoCollection.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.bson.json.JsonMode;
4141
import org.bson.json.JsonWriterSettings;
4242
import org.hypertrace.core.documentstore.BulkArrayValueUpdateRequest;
43+
import org.hypertrace.core.documentstore.BulkDeleteResult;
4344
import org.hypertrace.core.documentstore.BulkUpdateRequest;
4445
import org.hypertrace.core.documentstore.BulkUpdateResult;
4546
import org.hypertrace.core.documentstore.CloseableIterator;
@@ -479,6 +480,12 @@ public boolean delete(Key key) {
479480
return deleteResult.getDeletedCount() > 0;
480481
}
481482

483+
@Override
484+
public BulkDeleteResult delete(Set<Key> keys) {
485+
DeleteResult deleteResult = collection.deleteMany(this.selectionCriteriaForKeys(keys));
486+
return new BulkDeleteResult(deleteResult.getDeletedCount());
487+
}
488+
482489
@Override
483490
public boolean deleteSubDoc(Key key, String subDocPath) {
484491
BasicDBObject unsetObject = new BasicDBObject("$unset", new BasicDBObject(subDocPath, ""));

document-store/src/main/java/org/hypertrace/core/documentstore/postgres/PostgresCollection.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.stream.Collectors;
2222
import lombok.SneakyThrows;
2323
import org.hypertrace.core.documentstore.BulkArrayValueUpdateRequest;
24+
import org.hypertrace.core.documentstore.BulkDeleteResult;
2425
import org.hypertrace.core.documentstore.BulkUpdateRequest;
2526
import org.hypertrace.core.documentstore.BulkUpdateResult;
2627
import org.hypertrace.core.documentstore.CloseableIterator;
@@ -344,6 +345,31 @@ public boolean delete(Key key) {
344345
return false;
345346
}
346347

348+
@Override
349+
public BulkDeleteResult delete(Set<Key> keys) {
350+
String ids =
351+
keys.stream().map(key -> "'" + key.toString() + "'").collect(Collectors.joining(", "));
352+
String space = " ";
353+
String deleteSQL =
354+
new StringBuilder("DELETE FROM")
355+
.append(space)
356+
.append(collectionName)
357+
.append(" WHERE ")
358+
.append(ID)
359+
.append(" IN ")
360+
.append("(")
361+
.append(ids)
362+
.append(")")
363+
.toString();
364+
try (PreparedStatement preparedStatement = client.prepareStatement(deleteSQL)) {
365+
int deletedCount = preparedStatement.executeUpdate();
366+
return new BulkDeleteResult(deletedCount);
367+
} catch (SQLException e) {
368+
LOGGER.error("SQLException deleting documents. keys: {}", keys, e);
369+
}
370+
return new BulkDeleteResult(0);
371+
}
372+
347373
@Override
348374
public boolean deleteSubDoc(Key key, String subDocPath) {
349375
String deleteSubDocSQL =

0 commit comments

Comments
 (0)