Skip to content

convert snapshot migration to use aggregation pipeline #1324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 20, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
convert snapshot migration to use aggregation pipeline
  • Loading branch information
dragonpoo committed Nov 20, 2024
commit 24ba4bdabef1ee01f968455a42cbc1ccd8625254
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
Loading