Compare Exchange: How to Delete Compare Exchange Value


  • Use DeleteCompareExchangeValueOperation to delete a Key and its Value.

  • The Key and its Value are deleted only if the index in the request matches the current index stored in the server for the specified key.

  • For an overview of the 'Compare Exchange' feature click: Compare Exchange Overview

  • In this page:


Syntax

Method:

public DeleteCompareExchangeValueOperation(Class<T> clazz, String key, long index)
Parameters
key String The key to be deleted
index long The version number of the value to be deleted

Returned object:

public class CompareExchangeResult<T> {
    private T value;
    private long index;
    private boolean successful;

    public T getValue() {
        return value;
    }

    public void setValue(T value) {
        this.value = value;
    }

    public long getIndex() {
        return index;
    }

    public void setIndex(long index) {
        this.index = index;
    }

    public boolean isSuccessful() {
        return successful;
    }

    public void setSuccessful(boolean successful) {
        this.successful = successful;
    }
}
Return Value
Successful boolean * True if the delete operation was successfully completed
* True if key doesn't exist
* False if the delete operation failed
Value T * The value that was deleted upon a successful delete
* 'null' if key doesn't exist
* The currently existing value on the server if delete operation failed
Index long * The next available version number upon success
* The next available version number if key doesn't exist
* The currently existing index on the server if the delete operation failed

Example

// First, get existing value
CompareExchangeValue<User> readResult
    = store.operations().send(
        new GetCompareExchangeValueOperation<>(User.class, "AdminUser"));

// Delete the key - use the index received from the 'Get' operation
CompareExchangeResult<User> deleteResult
    = store.operations().send(
        new DeleteCompareExchangeValueOperation<>(User.class, "AdminUser", readResult.getIndex()));

// The delete result is successful only if the index has not changed between the read and delete operations
boolean deleteResultSuccessful = deleteResult.isSuccessful();