checkAndMutate
checkAndMutate是checkAndSet的扩展版本:checkAndSet只允许set一个值,而checkAndMutate允许在单个原子操作中set或者del多个值。该接口从1.11.0版本开始提供。
为此,我们提供了一个包装类Mutations,用户可以预先设置需要实施的set或者del操作。
- class CheckAndMutateResult {
- /**
- * return value for checkAndMutate
- *
- * @param mutateSucceed true if mutate succeed.
- * @param checkValueReturned true if the check value is returned.
- * @param checkValueExist true if the check value is exist; can be used only when
- * checkValueReturned is true.
- * @param checkValue return the check value if exist; can be used only when checkValueExist is
- * true.
- */
- public boolean mutateSucceed;
- public boolean checkValueReturned;
- public boolean checkValueExist;
- public byte[] checkValue;
- }
- /**
- * atomically check and mutate by key, async version. if the check condition is satisfied, then
- * apply to mutate.
- *
- * @param hashKey the hash key to check and mutate.
- * @param checkSortKey the sort key to check.
- * @param checkType the check type.
- * @param checkOperand the check operand.
- * @param mutations the list of mutations to perform if check condition is satisfied.
- * @param options the check-and-mutate options.
- * @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 CheckAndMutateResult. 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.
- */
- Future<CheckAndMutateResult> asyncCheckAndMutate(
- byte[] hashKey,
- byte[] checkSortKey,
- CheckType checkType,
- byte[] checkOperand,
- Mutations mutations,
- CheckAndMutateOptions options,
- int timeout /*ms*/);
注:
- 参数:需传入TableName、HashKey、CheckSortKey、CheckType、CheckOperand、Mutations、Options。
- checkSortKey、checkType、checkOperand:用于指定检查的条件。
- mutations:用于指定条件检查成功后要实施的set或者del操作。
- options:其他选项,包括:
- setValueTTLSeconds:新值的TTL时间;0表示不设置TTL限制。
- returnCheckValue:是否需要返回CheckSortKey对应的value。
- 返回值:CheckAndMutateResult,包括:
- mutateSucceed:是否实施成功。
- checkValueReturned:是否返回了CheckSortKey对应的value。
- checkValueExist:CheckSortKey对应的value是否存在;该域只有在
checkValueReturned=true
时有意义。 - checkValue:CheckSortKey对应的value值;该域只有在
checkValueExist=true
时有意义。
- 异常:如果出现异常,譬如网络错误、超时错误、服务端错误等,会抛出 PException。另外以下情况也会抛出异常:
- 如果CheckType为
int compare
类型的操作,且CheckOperand或者CheckValue转换为int64时出错,譬如不是合法的数字或者超出int64范围。
- 如果CheckType为