Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -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);
}
Expand All @@ -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;
}

Expand Down Expand Up @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method isn't needed, use ObjectUtils.nullSafeEquals(…) instead.

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}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
*
* @author Mark Paluch
* @author Sam Lightfoot
* @author Jeongjun Min
*/
class StatementFactoryUnitTests {

Expand Down Expand Up @@ -962,6 +963,19 @@ void shouldRenderSimilaritySelector() {
assertThat(statement.getNamedValues()).isEmpty();
}

@Test // GH-1525
void shouldCreateUpdateWithMultipleOperationsOnSameColumnDifferentKeys() {

Update update = Update.empty().set("map").atKey("key1").to("value1").set("map").atKey("key2").to("value2");

StatementBuilder<com.datastax.oss.driver.api.querybuilder.update.Update> updateStatementBuilder = statementFactory
.update(Query.empty(), update, personEntity);

String cql = updateStatementBuilder.build(ParameterHandling.INLINE).getQuery();

assertThat(cql).isEqualTo("UPDATE person SET map['key1']='value1', map['key2']='value2'");
}

@SuppressWarnings("unused")
static class Person {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*
* @author Mark Paluch
* @author Chema Vinacua
* @author Jeongjun Min
*/
class UpdateUnitTests {

Expand Down Expand Up @@ -153,4 +154,58 @@ void shouldCreateDecrementLongUpdate() {
assertThat(update.getUpdateOperations()).hasSize(1);
assertThat(update).hasToString("foo = foo - 2400000000");
}

@Test // GH-1525
void shouldAllowMultipleSetAtKeyOperationsOnSameColumn() {

Update update = Update.empty().set("foo").atKey("k1").to("v1").set("foo").atKey("k2").to("v2");

assertThat(update.getUpdateOperations()).hasSize(2);
assertThat(update).hasToString("foo['k1'] = 'v1', foo['k2'] = 'v2'");
}

@Test // GH-1525
void shouldUseLastWinsForDuplicateSetAtKeyOnSameKey() {

Update update = Update.empty().set("foo").atKey("k1").to("v1").set("foo").atKey("k1").to("v2");

assertThat(update.getUpdateOperations()).hasSize(1);
assertThat(update).hasToString("foo['k1'] = 'v2'");
}

@Test // GH-1525
void shouldAllowMultipleSetAtIndexOperationsOnSameColumn() {

Update update = Update.empty().set("foo").atIndex(1).to("A").set("foo").atIndex(2).to("B");

assertThat(update.getUpdateOperations()).hasSize(2);
assertThat(update).hasToString("foo[1] = 'A', foo[2] = 'B'");
}

@Test // GH-1525
void shouldUseLastWinsForDuplicateSetAtIndexOnSameIndex() {

Update update = Update.empty().set("foo").atIndex(1).to("A").set("foo").atIndex(1).to("B");

assertThat(update.getUpdateOperations()).hasSize(1);
assertThat(update).hasToString("foo[1] = 'B'");
}

@Test // GH-1525
void wholeColumnAndElementLevelShouldConflict_LastWinsWholeColumn() {

Update update = Update.empty().set("foo").atKey("k1").to("v1").set("foo", "ALL");

assertThat(update.getUpdateOperations()).hasSize(1);
assertThat(update).hasToString("foo = 'ALL'");
}

@Test // GH-1525
void wholeColumnAndElementLevelShouldConflict_LastWinsElementLevel() {

Update update = Update.empty().set("foo", "ALL").set("foo").atKey("k1").to("v1");

assertThat(update.getUpdateOperations()).hasSize(1);
assertThat(update).hasToString("foo['k1'] = 'v1'");
}
}