spring-projects / spring-projects/spring-data-mongodb

Ability to provide custom version check and increment strategies

Open
#3,644 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

status: feedback-provided status: waiting-for-triage
Dominant language
Java
Stars
1.7k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

Currently when the @Version annotation is used in a Document's field, that field gets used for optimistic locking, this is achieved by providing a Criteria along with the update, which checks that the version being updated matches the current version of the object, when the update succeeds then the version gets auto-incremented. There doesn't seem to be any simple way of overriding this behavior, other than providing a custom MongoTemplate, even then the code path to override becomes pretty significant, since this is pretty deep down into some private classes, it'd basically result in a lot of duplication just to change a couple of lines of code. The code that builds the version aware query lives in org.springframework.data.mongodb.core.EntityOperations$MappedEntity.getQueryForVersion and the version increment code lives in org.springframework.data.mongodb.core.EntityOperations$AdaptibleMappedEntity.incrementVersion. The ask is to be able to provide custom strategies that can be customized instead of the current hardcoded logic, for example something like this:

interface VersionQueryStrategy {
    Query getQueryForVersion(MongoPersistentEntity<?> entity, Object id, Object version);
}

interface VersionIncrementStrategy {
    <T> T incrementVersion(MongoPersistentEntity<?> entity, ConvertingPropertyAccessor<T> propertyAccessor, Number version)
}

Just to provide some context my use case revolves around using a timestamp that I receive externally as the version field, and my desired query would check that the current version is less than the new version, and the increment logic would be completely skipped since the value is managed externally.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with org.springframework.data.mongodb.core.EntityOperations$MappedEntity.getQueryForVersion and org.springframework.data.mongodb.core.EntityOperations$AdaptibleMappedEntity.incrementVersion, the two entry points named in the issue. Examine how version-aware queries and increments are currently built, then determine how configurable strategies would cover externally managed timestamps and skipped increments. Done means callers can provide both strategies without replacing MongoTemplate.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, mongodb
Domain
database
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.