Skip to content

Commit 40e0ee8

Browse files
authored
Merge pull request #248 from MamboIO/fix-246-and-247
Fix bugs related to index deletion (#246 #247)
2 parents b854cd1 + 0488466 commit 40e0ee8

2 files changed

Lines changed: 92 additions & 7 deletions

File tree

core/src/main/java/de/bwaldvogel/mongo/backend/AbstractMongoDatabase.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -427,25 +427,34 @@ private void dropIndexes(MongoCollection<P> collection, Document query) {
427427
Object index = query.get("index");
428428
Assert.notNull(index, () -> "Index name must not be null");
429429
MongoCollection<P> indexCollection = indexes.get();
430+
Document nsQuery = new Document("ns", collection.getFullName());
430431
if (Objects.equals(index, "*")) {
431-
for (Document indexDocument : indexCollection.queryAll()) {
432+
for (Document indexDocument : indexCollection.handleQuery(nsQuery)) {
432433
Document indexKeys = (Document) indexDocument.get("key");
433434
if (!isPrimaryKeyIndex(indexKeys)) {
434435
dropIndex(collection, indexDocument);
435436
}
436437
}
437-
} else if (index instanceof String) {
438-
dropIndex(collection, new Document("name", index));
439438
} else {
440-
Document indexKeys = (Document) index;
441-
Document indexQuery = new Document("key", indexKeys).append("ns", collection.getFullName());
442-
Document indexToDrop = CollectionUtils.getSingleElement(indexCollection.handleQuery(indexQuery),
443-
() -> new IndexNotFoundException(indexKeys));
439+
if (index instanceof String indexName) {
440+
nsQuery.append("name", indexName);
441+
} else {
442+
nsQuery.append("key", (Document) index);
443+
}
444+
Document indexToDrop = CollectionUtils.getSingleElement(indexCollection.handleQuery(nsQuery),
445+
() -> createIndexNotFoundException(index));
444446
int numDeleted = dropIndex(collection, indexToDrop);
445447
Assert.equals(numDeleted, 1, () -> "Expected one deleted document");
446448
}
447449
}
448450

451+
private static IndexNotFoundException createIndexNotFoundException(Object index) {
452+
if (index instanceof String indexName) {
453+
return new IndexNotFoundException("index not found with name [" + indexName + "]");
454+
}
455+
return new IndexNotFoundException((Document) index);
456+
}
457+
449458
private int dropIndex(MongoCollection<P> collection, Document indexDescription) {
450459
String indexName = (String) indexDescription.get("name");
451460
dropIndex(collection, indexName);

test-common/src/main/java/de/bwaldvogel/mongo/backend/AbstractBackendTest.java

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,82 @@ void testDropIndexes_twoIndexesWithTheSameKey() {
723723
);
724724
}
725725

726+
// https://github.com/bwaldvogel/mongo-java-server/issues/246
727+
@Test
728+
void testDropIndexes_string_twoIndexesWithTheSameKey() {
729+
collection.insertOne(json("_id: 1, c: 10"));
730+
731+
MongoCollection<Document> otherCollection = getCollection("other");
732+
otherCollection.insertOne(json("_id: 1, c: 10"));
733+
734+
String indexName = collection.createIndex(new Document("c", 1));
735+
otherCollection.createIndex(new Document("c", 1));
736+
737+
assertThat(collection.listIndexes())
738+
.containsExactlyInAnyOrder(
739+
json("key: {_id: 1}").append("name", "_id_").append("v", 2),
740+
json("key: {c: 1}").append("name", "c_1").append("v", 2)
741+
);
742+
743+
assertThat(otherCollection.listIndexes())
744+
.containsExactlyInAnyOrder(
745+
json("key: {_id: 1}").append("name", "_id_").append("v", 2),
746+
json("key: {c: 1}").append("name", "c_1").append("v", 2)
747+
);
748+
749+
collection.dropIndex(indexName);
750+
751+
assertThat(collection.listIndexes())
752+
.containsExactlyInAnyOrder(
753+
json("key: {_id: 1}").append("name", "_id_").append("v", 2)
754+
);
755+
756+
assertThat(otherCollection.listIndexes())
757+
.containsExactlyInAnyOrder(
758+
json("key: {_id: 1}").append("name", "_id_").append("v", 2),
759+
json("key: {c: 1}").append("name", "c_1").append("v", 2)
760+
);
761+
}
762+
763+
// https://github.com/bwaldvogel/mongo-java-server/issues/247
764+
@Test
765+
void testDropIndexes_all() {
766+
collection.insertOne(json("_id: 1, c: 10, d:1"));
767+
768+
MongoCollection<Document> otherCollection = getCollection("other");
769+
otherCollection.insertOne(json("_id: 1, c: 10"));
770+
771+
collection.createIndex(new Document("c", 1));
772+
collection.createIndex(new Document("d", 1));
773+
otherCollection.createIndex(new Document("c", 1));
774+
775+
assertThat(collection.listIndexes())
776+
.containsExactlyInAnyOrder(
777+
json("key: {_id: 1}").append("name", "_id_").append("v", 2),
778+
json("key: {c: 1}").append("name", "c_1").append("v", 2),
779+
json("key: {d: 1}").append("name", "d_1").append("v", 2)
780+
);
781+
782+
assertThat(otherCollection.listIndexes())
783+
.containsExactlyInAnyOrder(
784+
json("key: {_id: 1}").append("name", "_id_").append("v", 2),
785+
json("key: {c: 1}").append("name", "c_1").append("v", 2)
786+
);
787+
788+
collection.dropIndex("*");
789+
790+
assertThat(collection.listIndexes())
791+
.containsExactlyInAnyOrder(
792+
json("key: {_id: 1}").append("name", "_id_").append("v", 2)
793+
);
794+
795+
assertThat(otherCollection.listIndexes())
796+
.containsExactlyInAnyOrder(
797+
json("key: {_id: 1}").append("name", "_id_").append("v", 2),
798+
json("key: {c: 1}").append("name", "c_1").append("v", 2)
799+
);
800+
}
801+
726802
@Test
727803
public void testCurrentOperations() {
728804
Document currentOperations = getAdminDb().getCollection("$cmd.sys.inprog").find().first();

0 commit comments

Comments
 (0)