checkAndSet
单HashKey数据的原子CAS操作(可以理解为单行原子操作)。详细说明参见单行原子操作。
该操作先对某个SortKey(称之为CheckSortKey)的value做条件检查:
- 如果检查的条件满足,则将另一个SortKey(称之为SetSortKey)的value设置为新值。
- 如果检查的条件不满足,则不执行set操作。CheckSortKey和SetSortKey可以相同也可以不同。
用户还可以设置CheckAndSetOptions.returnCheckValue
来获取CheckSortKey对应的value。如果CheckSortKey和SetSortKey相同并且set成功,则获取set之前的旧值。
- public enum CheckType {
- CT_NO_CHECK(0),
- // appearance
- CT_VALUE_NOT_EXIST(1), // value is not exist
- CT_VALUE_NOT_EXIST_OR_EMPTY(2), // value is not exist or value is empty
- CT_VALUE_EXIST(3), // value is exist
- CT_VALUE_NOT_EMPTY(4), // value is exist and not empty
- // match
- CT_VALUE_MATCH_ANYWHERE(5), // operand matches anywhere in value
- CT_VALUE_MATCH_PREFIX(6), // operand matches prefix in value
- CT_VALUE_MATCH_POSTFIX(7), // operand matches postfix in value
- // bytes compare
- CT_VALUE_BYTES_LESS(8), // bytes compare: value < operand
- CT_VALUE_BYTES_LESS_OR_EQUAL(9), // bytes compare: value <= operand
- CT_VALUE_BYTES_EQUAL(10), // bytes compare: value == operand
- CT_VALUE_BYTES_GREATER_OR_EQUAL(11), // bytes compare: value >= operand
- CT_VALUE_BYTES_GREATER(12), // bytes compare: value > operand
- // int compare: first transfer bytes to int64; then compare by int value
- CT_VALUE_INT_LESS(13), // int compare: value < operand
- CT_VALUE_INT_LESS_OR_EQUAL(14), // int compare: value <= operand
- CT_VALUE_INT_EQUAL(15), // int compare: value == operand
- CT_VALUE_INT_GREATER_OR_EQUAL(16), // int compare: value >= operand
- CT_VALUE_INT_GREATER(17); // int compare: value > operand
- }
- public class CheckAndSetOptions {
- public int setValueTTLSeconds = 0; // time to live in seconds of the set value, 0 means no ttl.
- public boolean returnCheckValue = false; // if return the check value in results.
- }
- public class CheckAndSetResult {
- /**
- * return value for checkAndSet
- *
- * @param setSucceed true if set value 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.
- */
- boolean setSucceed;
- boolean checkValueReturned;
- boolean checkValueExist;
- byte[] checkValue;
- }
- /**
- * Atomically check and set value by key.
- * If the check condition is satisfied, then apply to set value.
- *
- * @param tableName the table name.
- * @param hashKey the hash key to check and set.
- * @param checkSortKey the sort key to check.
- * @param checkType the check type.
- * @param checkOperand the check operand.
- * @param setSortKey the sort key to set value if check condition is satisfied.
- * @param setValue the value to set if check condition is satisfied.
- * @param options the check-and-set options.
- * @return CheckAndSetResult
- * @throws PException throws exception if any error occurs.
- */
- public PegasusTableInterface.CheckAndSetResult checkAndSet(String tableName, byte[] hashKey, byte[] checkSortKey,
- CheckType checkType, byte[] checkOperand,
- byte[] setSortKey, byte[] setValue,
- CheckAndSetOptions options) throws PException;
注:
- 参数:需传入TableName、HashKey、CheckSortKey、CheckType、CheckOperand、SetSortKey、SetValue、Options。
- checkSortKey、checkType、checkOperand:用于指定检查的条件。
- setSortKey、setValue:用于指定条件检查成功后要set的新值。
- options:其他选项,包括:
- setValueTTLSeconds:新值的TTL时间;0表示不设置TTL限制。
- returnCheckValue:是否需要返回CheckSortKey对应的value。
- 返回值:CheckAndSetResult,包括:
- setSucceed:是否set成功。
- checkValueReturned:是否返回了CheckSortKey对应的value。
- checkValueExist:CheckSortKey对应的value是否存在;该域只有在
checkValueReturned=true
时有意义。 - checkValue:CheckSortKey对应的value值;该域只有在
checkValueExist=true
时有意义。
- 异常:如果出现异常,譬如网络错误、超时错误、服务端错误等,会抛出 PException。另外以下情况也会抛出异常:
- 如果CheckType为
int compare
类型的操作,且CheckOperand或者CheckValue转换为int64时出错,譬如不是合法的数字或者超出int64范围。
- 如果CheckType为