asyncCompareExchange

单HashKey数据的原子CAS操作。compareExchange的异步版本。

  1. public static class CompareExchangeResult {
  2. /**
  3. * return value for CompareExchange
  4. *
  5. * @param setSucceed true if set value succeed.
  6. * @param actualValue return the actual value if set value failed; null means the actual value is not exist.
  7. */
  8. boolean setSucceed;
  9. byte[] actualValue;
  10. }
  11.  
  12. public static interface CompareExchangeListener extends GenericFutureListener<Future<CompareExchangeResult>> {
  13. /**
  14. * This function will be called when listened asyncCompareExchange future is done.
  15. *
  16. * @param future the listened future
  17. * @throws Exception throw exception if any error occurs.
  18. *
  19. * Notice: User shouldn't do any operations that may block or time-consuming
  20. */
  21. @Override
  22. public void operationComplete(Future<CompareExchangeResult> future) throws Exception;
  23. }
  24.  
  25. /**
  26. * atomically compare and exchange value by key, async version.
  27. * <p>
  28. * - if the original value for the key is equal to the expected value, then update it with the desired value,
  29. * set CompareExchangeResult.setSucceed to true, and set CompareExchangeResult.actualValue to null because
  30. * the actual value must be equal to the desired value.
  31. * - if the original value for the key is not exist or not equal to the expected value, then set
  32. * CompareExchangeResult.setSucceed to false, and set the actual value in CompareExchangeResult.actualValue.
  33. * <p>
  34. * this method is very like the C++ function in {https://en.cppreference.com/w/cpp/atomic/atomic_compare_exchange}.
  35. *
  36. * @param hashKey the hash key to compare and exchange.
  37. * @param sortKey the sort key to compare and exchange.
  38. * @param expectedValue the value expected to be found for the key.
  39. * @param desiredValue the desired value to set if the original value for the key is equal to the expected value.
  40. * @param ttlSeconds time to live in seconds of the desired value, 0 means no ttl.
  41. * @param timeout how long will the operation timeout in milliseconds.
  42. * if timeout > 0, it is a timeout value for current op,
  43. * else the timeout value in the configuration file will be used.
  44. * @return the future for current op
  45. * <p>
  46. * Future return:
  47. * On success: return CompareExchangeResult.
  48. * On failure: a throwable, which is an instance of PException
  49. * <p>
  50. * Thread safety:
  51. * All the listeners for the same table are guaranteed to be dispatched in the same thread, so all the
  52. * listeners for the same future are guaranteed to be executed as the same order as the listeners added.
  53. * But listeners for different tables are not guaranteed to be dispatched in the same thread.
  54. */
  55. public Future<CompareExchangeResult> asyncCompareExchange(byte[] hashKey, byte[] sortKey,
  56. byte[] expectedValue, byte[] desiredValue,
  57. int ttlSeconds, int timeout/*ms*/);

注: