Skip to content

Commit

Permalink
convert snapshot migration to use aggregation pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
dragonpoo committed Nov 20, 2024
1 parent 3a443bb commit 24ba4bd
Showing 1 changed file with 27 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

Expand Down Expand Up @@ -319,35 +320,43 @@ public void addTimeSeriesSnapshotHistory(MongockTemplate mongoTemplate, CommonCo

// Create the time-series collection if it doesn't exist
if (!mongoTemplate.collectionExists(ApplicationHistorySnapshotTS.class)) {
if(mongoVersion < 5) {
if (mongoVersion < 5) {
mongoTemplate.createCollection(ApplicationHistorySnapshotTS.class);
} else {
mongoTemplate.createCollection(ApplicationHistorySnapshotTS.class, CollectionOptions.empty().timeSeries("createdAt"));
}
}

Instant thresholdDate = Instant.now().minus(commonConfig.getQuery().getAppSnapshotKeepDuration(), ChronoUnit.DAYS);
List<ApplicationHistorySnapshot> snapshots = mongoTemplate.find(new Query().addCriteria(Criteria.where("createdAt").gte(thresholdDate)), ApplicationHistorySnapshot.class);
snapshots.forEach(snapshot -> {
ApplicationHistorySnapshotTS applicationHistorySnapshotTS = new ApplicationHistorySnapshotTS();
applicationHistorySnapshotTS.setApplicationId(snapshot.getApplicationId());
applicationHistorySnapshotTS.setDsl(snapshot.getDsl());
applicationHistorySnapshotTS.setContext(snapshot.getContext());
applicationHistorySnapshotTS.setCreatedAt(snapshot.getCreatedAt());
applicationHistorySnapshotTS.setCreatedBy(snapshot.getCreatedBy());
applicationHistorySnapshotTS.setModifiedBy(snapshot.getModifiedBy());
applicationHistorySnapshotTS.setUpdatedAt(snapshot.getUpdatedAt());
applicationHistorySnapshotTS.setId(snapshot.getId());
mongoTemplate.insert(applicationHistorySnapshotTS);
mongoTemplate.remove(snapshot);
});

// Ensure indexes if needed
// Use aggregation to move and transform data
Document match = new Document("$match",
new Document("createdAt", new Document("$gte", thresholdDate)));

Document project = new Document("$project", new Document()
.append("applicationId", 1)
.append("dsl", 1)
.append("context", 1)
.append("createdAt", 1)
.append("createdBy", 1)
.append("modifiedBy", 1)
.append("updatedAt", 1)
.append("id", "$_id")); // Map MongoDB's default `_id` to `id` if needed.

Document out = new Document("$out", "applicationHistorySnapshotTS"); // Target collection name

// Execute the aggregation pipeline
mongoTemplate.getDb()
.getCollection("applicationHistorySnapshot") // Original collection name
.aggregate(Arrays.asList(match, project, out))
.toCollection();

ensureIndexes(mongoTemplate, ApplicationHistorySnapshotTS.class,
makeIndex("applicationId"),
makeIndex("createdAt")
);
makeIndex("createdAt"));
}


private void addGidField(MongockTemplate mongoTemplate, String collectionName) {
// Create a query to match all documents
Query query = new Query();
Expand Down

0 comments on commit 24ba4bd

Please sign in to comment.