Add MongoCounterSupport helper #5127
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add MongoDB-backed Counter Support
This PR implements MongoDB-backed atomic counters as outlined in GH-4823, split into two logical commits for easier review.
Commit Structure
Commit 1: Internal helper implementation
Commit 2: Public API integration with MongoTemplate
This separation allows reviewing the core logic independently from the public API design.
What's included
Commit 1 - Internal Helper
MongoCounterSupport(package-private) - wrapsfindAndModifywith$inc,upsert, andreturnNewCommit 2 - Public API
MongoOperations.getNextCounterValue()- interface methodMongoTemplate.getNextCounterValue()- implementation delegating to MongoCounterSupportUsage
Counter document structure:
{_id: "counterName", count: 1}. First call returns 1, subsequent calls increment atomically.About the implementation
The original issue suggested wrapping the counter pattern in "something like a MongoSequence" and mentioned the relationship to #1842 (external ID generators) which might belong in data-commons.
Implementation decisions
MongoCounterSupportfollowing existing helper patterns, but open toMongoSequenceor alternativesMongoOperations.findAndModify()instead of the low-levelexecute()example for consistency with the codebaseThis is intentionally minimal. I'm planning to add custom increment amounts and additional features in follow-up PRs if this direction makes sense.
Looking for feedback
I'd appreciate thoughts on
MongoSequenceinstead ofMongoCounterSupport?Ready to adjust based on your feedback.
Closes #4823