-
Couldn't load subscription status.
- Fork 310
Support multiple Update operations for the same column name
#1596
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -17,10 +17,11 @@ | |
|
|
||
| import static org.springframework.data.cassandra.core.query.SerializationUtils.*; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import org.jspecify.annotations.Nullable; | ||
|
|
@@ -40,13 +41,14 @@ | |
| * | ||
| * @author Mark Paluch | ||
| * @author Chema Vinacua | ||
| * @author Jeongjun Min | ||
| * @since 2.0 | ||
| */ | ||
| public class Update { | ||
|
|
||
| private static final Update EMPTY = new Update(Collections.emptyMap()); | ||
| private static final Update EMPTY = new Update(Collections.emptyList()); | ||
|
|
||
| private final Map<ColumnName, AssignmentOp> updateOperations; | ||
| private final List<AssignmentOp> updateOperations; | ||
|
|
||
| /** | ||
| * Create an empty {@link Update} object. | ||
|
|
@@ -66,11 +68,9 @@ public static Update of(Iterable<AssignmentOp> assignmentOps) { | |
|
|
||
| Assert.notNull(assignmentOps, "Update operations must not be null"); | ||
|
|
||
| Map<ColumnName, AssignmentOp> updateOperations = assignmentOps instanceof Collection | ||
| ? new LinkedHashMap<>(((Collection<?>) assignmentOps).size()) | ||
| : new LinkedHashMap<>(); | ||
| List<AssignmentOp> updateOperations = new ArrayList<>(); | ||
|
|
||
| assignmentOps.forEach(assignmentOp -> updateOperations.put(assignmentOp.getColumnName(), assignmentOp)); | ||
| assignmentOps.forEach(updateOperations::add); | ||
|
|
||
| return new Update(updateOperations); | ||
| } | ||
|
|
@@ -84,7 +84,7 @@ public static Update update(String columnName, @Nullable Object value) { | |
| return empty().set(columnName, value); | ||
| } | ||
|
|
||
| private Update(Map<ColumnName, AssignmentOp> updateOperations) { | ||
| private Update(List<AssignmentOp> updateOperations) { | ||
| this.updateOperations = updateOperations; | ||
| } | ||
|
|
||
|
|
@@ -215,24 +215,56 @@ public Update decrement(String columnName, Number delta) { | |
| * @return {@link Collection} of update operations. | ||
| */ | ||
| public Collection<AssignmentOp> getUpdateOperations() { | ||
| return Collections.unmodifiableCollection(updateOperations.values()); | ||
| return Collections.unmodifiableCollection(updateOperations); | ||
| } | ||
|
|
||
| private Update add(AssignmentOp assignmentOp) { | ||
|
|
||
| Map<ColumnName, AssignmentOp> map = new LinkedHashMap<>(this.updateOperations.size() + 1); | ||
| List<AssignmentOp> list = new ArrayList<>(this.updateOperations.size() + 1); | ||
|
|
||
| map.putAll(this.updateOperations); | ||
| map.put(assignmentOp.getColumnName(), assignmentOp); | ||
| for (AssignmentOp existing : this.updateOperations) { | ||
| if (!conflicts(existing, assignmentOp)) { | ||
| list.add(existing); | ||
| } | ||
| } | ||
|
|
||
| return new Update(map); | ||
| list.add(assignmentOp); | ||
|
|
||
| return new Update(list); | ||
| } | ||
|
|
||
| /** | ||
| * Determine whether two assignment operations conflict and should not co-exist in a single {@link Update}. | ||
| * Conflicts are defined as whole-column operations on the same column or element-level operations targeting | ||
| * the same element (same map key or same list index) on the same column. In case of conflict, last-wins semantics | ||
| * apply and the incoming operation replaces the existing one. | ||
| */ | ||
| private static boolean conflicts(AssignmentOp existing, AssignmentOp incoming) { | ||
|
|
||
| if (!existing.getColumnName().equals(incoming.getColumnName())) { | ||
| return false; | ||
| } | ||
|
|
||
| if (existing instanceof SetAtKeyOp e && incoming instanceof SetAtKeyOp i) { | ||
| return equalsNullSafe(e.getKey(), i.getKey()); | ||
| } | ||
|
|
||
| if (existing instanceof SetAtIndexOp e && incoming instanceof SetAtIndexOp i) { | ||
| return e.getIndex() == i.getIndex(); | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return StringUtils.collectionToDelimitedString(updateOperations.values(), ", "); | ||
| private static boolean equalsNullSafe(@Nullable Object a, @Nullable Object b) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method isn't needed, use |
||
| return a == b || (a != null && a.equals(b)); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return StringUtils.collectionToDelimitedString(updateOperations, ", "); | ||
| } | ||
|
|
||
| /** | ||
| * Builder to add a single element/multiple elements to a collection associated with a {@link ColumnName}. | ||
| * | ||
|
|
||
This file contains hidden or 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 hidden or 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
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.