-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(data): Fix conflict on index creation for the data collection. Ad…
…d migration command Signed-off-by: renaud colin <renaud.colin@inra.fr>
- Loading branch information
renaud colin
committed
Apr 9, 2024
1 parent
cd4d5f1
commit f05a611
Showing
10 changed files
with
203 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Migrate to 1.2.3 | ||
|
||
## Migration of data collection index | ||
|
||
- Run this command in order to delete previously created indexes and create new indexes required by OpenSILEX | ||
- Depending on your number of document in the `data` collection, it can take several minutes to complete | ||
|
||
```bash | ||
java -jar opensilex.jar --CONFIG_FILE=<config_file> system run-update org.opensilex.migration.MongoDbIndexesMigration --DEBUG | ||
``` |
82 changes: 82 additions & 0 deletions
82
opensilex-migration/src/main/java/org/opensilex/migration/MongoDbIndexesMigration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package org.opensilex.migration; | ||
|
||
import com.mongodb.client.MongoCollection; | ||
import com.mongodb.client.MongoDatabase; | ||
import com.mongodb.client.model.IndexOptions; | ||
import com.mongodb.client.model.Indexes; | ||
import org.apache.commons.collections4.SetUtils; | ||
import org.bson.Document; | ||
import org.bson.conversions.Bson; | ||
import org.opensilex.nosql.mongodb.service.v2.MongoDBServiceV2; | ||
import org.opensilex.update.AbstractOpenSilexModuleUpdate; | ||
import org.opensilex.update.OpensilexModuleUpdateException; | ||
|
||
import java.util.*; | ||
|
||
/** | ||
* @author rcolin | ||
*/ | ||
public class MongoDbIndexesMigration extends AbstractOpenSilexModuleUpdate { | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Update MongoDB indexes"; | ||
} | ||
|
||
@Override | ||
public void execute() throws OpensilexModuleUpdateException { | ||
|
||
MongoDBServiceV2 mongoDBServiceV2 = opensilex.getServiceInstance(MongoDBServiceV2.DEFAULT_SERVICE, MongoDBServiceV2.class); | ||
MongoDatabase database = mongoDBServiceV2.getDatabase(); | ||
|
||
// for each collection, ensure that indexes correspond to those that the OpenSILEX application register | ||
for(var indexesByCollection : mongoDBServiceV2.getIndexRegister().entrySet()){ | ||
|
||
// Get Set of required indexes, Set of existing index and Compute Set of indexes to create/delete by using set difference | ||
MongoCollection<?> collection = database.getCollection(indexesByCollection.getKey()); | ||
Map<Bson, IndexOptions> indexesOptions = indexesByCollection.getValue(); | ||
|
||
Set<Bson> existingIndexes = new HashSet<>(); | ||
collection.listIndexes().forEach(bson -> { | ||
Document existingIndex = bson.get("key", Document.class); | ||
existingIndexes.add(getIndexSpecificationWithoutOption(existingIndex)); | ||
}); | ||
|
||
// Delete old indexes and create new indexes | ||
Set<Bson> indexesToDelete = SetUtils.difference(existingIndexes, indexesOptions.keySet()); | ||
for(Bson index : indexesToDelete){ | ||
mongoDBServiceV2.dropIndex(collection, index); | ||
} | ||
|
||
Set<Bson> indexesToCreate = SetUtils.difference(indexesOptions.keySet(), existingIndexes); | ||
for(Bson index : indexesToCreate){ | ||
mongoDBServiceV2.createIndex(collection, index, indexesOptions.get(index)); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* @param existingIndex Document returned from a {@link MongoCollection#listIndexes()} | ||
* @return a Bson document matching the index format when passing to {@link MongoCollection#createIndex(Bson, IndexOptions)} or {@link MongoCollection#dropIndex(Bson)} | ||
*/ | ||
|
||
private Bson getIndexSpecificationWithoutOption(Document existingIndex){ | ||
|
||
List<Bson> indexes = new LinkedList<>(); | ||
existingIndex.keySet().forEach(indexField -> { | ||
int order = existingIndex.getInteger(indexField); | ||
if (order == 1) { | ||
indexes.add(Indexes.ascending(indexField)); | ||
} else { | ||
indexes.add(Indexes.descending(indexField)); | ||
} | ||
}); | ||
|
||
if (indexes.size() == 1) { | ||
return indexes.get(0); | ||
} else { | ||
return Indexes.compoundIndex(indexes); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.