asyncCompareExchange
单HashKey数据的原子CAS操作。compareExchange的异步版本。
- public static class CompareExchangeResult {
- /**
- * return value for CompareExchange
- *
- * @param setSucceed true if set value succeed.
- * @param actualValue return the actual value if set value failed; null means the actual value is not exist.
- */
- boolean setSucceed;
- byte[] actualValue;
- }
- public static interface CompareExchangeListener extends GenericFutureListener<Future<CompareExchangeResult>> {
- /**
- * This function will be called when listened asyncCompareExchange future is done.
- *
- * @param future the listened future
- * @throws Exception throw exception if any error occurs.
- *
- * Notice: User shouldn't do any operations that may block or time-consuming
- */
- @Override
- public void operationComplete(Future<CompareExchangeResult> future) throws Exception;
- }
- /**
- * atomically compare and exchange value by key, async version.
- * <p>
- * - if the original value for the key is equal to the expected value, then update it with the desired value,
- * set CompareExchangeResult.setSucceed to true, and set CompareExchangeResult.actualValue to null because
- * the actual value must be equal to the desired value.
- * - if the original value for the key is not exist or not equal to the expected value, then set
- * CompareExchangeResult.setSucceed to false, and set the actual value in CompareExchangeResult.actualValue.
- * <p>
- * this method is very like the C++ function in {https://en.cppreference.com/w/cpp/atomic/atomic_compare_exchange}.
- *
- * @param hashKey the hash key to compare and exchange.
- * @param sortKey the sort key to compare and exchange.
- * @param expectedValue the value expected to be found for the key.
- * @param desiredValue the desired value to set if the original value for the key is equal to the expected value.
- * @param ttlSeconds time to live in seconds of the desired value, 0 means no ttl.
- * @param timeout how long will the operation timeout in milliseconds.
- * if timeout > 0, it is a timeout value for current op,
- * else the timeout value in the configuration file will be used.
- * @return the future for current op
- * <p>
- * Future return:
- * On success: return CompareExchangeResult.
- * On failure: a throwable, which is an instance of PException
- * <p>
- * Thread safety:
- * All the listeners for the same table are guaranteed to be dispatched in the same thread, so all the
- * listeners for the same future are guaranteed to be executed as the same order as the listeners added.
- * But listeners for different tables are not guaranteed to be dispatched in the same thread.
- */
- public Future<CompareExchangeResult> asyncCompareExchange(byte[] hashKey, byte[] sortKey,
- byte[] expectedValue, byte[] desiredValue,
- int ttlSeconds, int timeout/*ms*/);
注:
- 参数和返回值:参见同步接口compareExchange。