davsclaus commented on code in PR #26053:
URL: https://github.com/apache/camel/pull/26053#discussion_r3921465499


##########
components/camel-cassandraql/src/main/java/org/apache/camel/processor/keyvalue/cassandra/CassandraKeyValueRepository.java:
##########
@@ -319,6 +364,52 @@ public Object putIfAbsent(String key, Object value, 
Duration ttl) {
         return existingBuffer != null ? 
KeyValueRepositoryHelper.deserialize(existingBuffer) : null;
     }
 
+    /**
+     * Atomically replaces the value for the given key only if the current 
value equals the expected old value, using
+     * Cassandra's lightweight transaction ({@code UPDATE ... SET value = ? 
WHERE key = ? IF value = ?}).
+     *
+     * @param  expectedOldValue the value that must currently be associated 
with the key
+     * @param  newValue         the new value to store
+     * @param  ttl              the time-to-live for the new entry; {@code 
null}, zero, or negative means no expiration
+     * @return                  {@code true} if the value was replaced, {@code 
false} if the current value did not match
+     */
+    @Override
+    public boolean replace(String key, Object expectedOldValue, Object 
newValue, Duration ttl) {
+        LOGGER.debug("Replacing key {} if value matches, TTL {}", key, ttl);
+        ByteBuffer serializedNewValue = 
KeyValueRepositoryHelper.serializeToByteBuffer(newValue);
+        ByteBuffer serializedExpectedValue = 
KeyValueRepositoryHelper.serializeToByteBuffer(expectedOldValue);

Review Comment:
   The LWT `IF value = ?` compares the **serialized** `ByteBuffer` of 
`expectedOldValue` against the stored bytes, whereas the `KeyValueRepository` 
SPI default compares deserialized values with `Objects.equals(current, 
expectedOldValue)`. These match only if serialization is deterministic and the 
caller passes the exact value that was stored.
   
   Edge case: two objects that are `.equals()` but serialize to different bytes 
(e.g. maps/sets with different iteration order, or different concrete 
collection types) would return `false` here where the default 
`replace`/`delete` returns `true`. This is a reasonable and arguably more 
correct choice for server-side CAS — a short Javadoc note stating the 
comparison is by serialized value (same for `delete(key, expected)`) would make 
the contract explicit for callers.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to