davsclaus commented on code in PR #25863:
URL: https://github.com/apache/camel/pull/25863#discussion_r3901942920
##########
core/camel-api/src/main/java/org/apache/camel/spi/KeyValueRepository.java:
##########
@@ -95,21 +97,65 @@ public interface KeyValueRepository extends Service {
* The default implementation is not atomic. Implementations backed by
stores that support atomic compare-and-set
* operations should override this method for better concurrency
guarantees.
*
- * @param key the key
- * @param value the value to store
- * @param ttlMillis the time-to-live in milliseconds; {@code 0} or
negative means no expiration
- * @return the existing value if the key was already present, or
{@code null} if the put succeeded
+ * @param key the key
+ * @param value the value to store
+ * @param ttl the time-to-live; {@code null}, zero, or negative means
no expiration
+ * @return the existing value if the key was already present, or
{@code null} if the put succeeded
*/
@Nullable
- default Object putIfAbsent(String key, Object value, long ttlMillis) {
+ default Object putIfAbsent(String key, Object value, @Nullable Duration
ttl) {
Object existing = get(key);
if (existing != null) {
return existing;
}
- put(key, value, ttlMillis);
+ put(key, value, ttl);
return null;
}
+ /**
+ * Atomically replaces the value for the given key only if the current
value equals the expected old value
+ * (compare-and-swap).
+ * <p/>
+ * The default implementation is not atomic. Implementations backed by
stores that support atomic compare-and-swap
+ * operations (e.g., {@code ConcurrentMap.replace}, Hazelcast {@code
IMap.replace}) should override this method for
+ * better concurrency guarantees.
+ *
+ * @param key the key
+ * @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
+ */
+ default boolean replace(String key, Object expectedOldValue, Object
newValue, @Nullable Duration ttl) {
+ Object current = get(key);
+ if (current != null && Objects.equals(current, expectedOldValue)) {
+ put(key, newValue, ttl);
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Removes the entry for the given key only if the current value equals
the expected value (compare-and-swap).
+ * <p/>
+ * The default implementation is not atomic. Implementations backed by
stores that support atomic compare-and-remove
+ * operations (e.g., {@code ConcurrentMap.remove(key, value)}, Hazelcast
{@code IMap.remove(key, value)}) should
+ * override this method for better concurrency guarantees.
+ *
+ * @param key the key to remove
+ * @param expectedValue the value that must currently be associated with
the key
+ * @return {@code true} if the entry was removed, {@code
false} if the current value did not match or
+ * the key was not present
+ */
+ default boolean delete(String key, Object expectedValue) {
Review Comment:
Same as the `replace(...)` method above — this is also a new public method
on the interface and should have `@since 4.23` added as the last JavaDoc tag.
##########
core/camel-api/src/main/java/org/apache/camel/spi/KeyValueRepository.java:
##########
@@ -95,21 +97,65 @@ public interface KeyValueRepository extends Service {
* The default implementation is not atomic. Implementations backed by
stores that support atomic compare-and-set
* operations should override this method for better concurrency
guarantees.
*
- * @param key the key
- * @param value the value to store
- * @param ttlMillis the time-to-live in milliseconds; {@code 0} or
negative means no expiration
- * @return the existing value if the key was already present, or
{@code null} if the put succeeded
+ * @param key the key
+ * @param value the value to store
+ * @param ttl the time-to-live; {@code null}, zero, or negative means
no expiration
+ * @return the existing value if the key was already present, or
{@code null} if the put succeeded
*/
@Nullable
- default Object putIfAbsent(String key, Object value, long ttlMillis) {
+ default Object putIfAbsent(String key, Object value, @Nullable Duration
ttl) {
Object existing = get(key);
if (existing != null) {
return existing;
}
- put(key, value, ttlMillis);
+ put(key, value, ttl);
return null;
}
+ /**
+ * Atomically replaces the value for the given key only if the current
value equals the expected old value
+ * (compare-and-swap).
+ * <p/>
+ * The default implementation is not atomic. Implementations backed by
stores that support atomic compare-and-swap
+ * operations (e.g., {@code ConcurrentMap.replace}, Hazelcast {@code
IMap.replace}) should override this method for
+ * better concurrency guarantees.
+ *
+ * @param key the key
+ * @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
+ */
+ default boolean replace(String key, Object expectedOldValue, Object
newValue, @Nullable Duration ttl) {
Review Comment:
This is a new public method added to an existing `core/camel-api` interface.
Per CLAUDE.md's `@since` JavaDoc rule, please add `@since 4.23` as the last tag
in the JavaDoc block above (after `@return`).
--
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]