@Override public boolean compareAndSwap(Collection<Field<?>> keys, Field<?> oldValue, Field<?> newValue) { LOG.trace("Table {}: CompareAndSwap with keys {}, oldValue {}, newValue {}", schema.getTableId(), keys, oldValue, newValue); fieldValidator.validateField(oldValue); if (oldValue.getFieldType() != newValue.getFieldType()) { throw new IllegalArgumentException( String.format("Field types of oldValue (%s) and newValue (%s) are not the same", oldValue.getFieldType(), newValue.getFieldType())); } if (!oldValue.getName().equals(newValue.getName())) { throw new IllegalArgumentException( String.format("Trying to compare and swap different fields. Old Value = %s, New Value = %s", oldValue, newValue)); } if (schema.isPrimaryKeyColumn(oldValue.getName())) { throw new IllegalArgumentException("Cannot use compare and swap on a primary key field"); } return table.compareAndSwap(convertKeyToBytes(keys, false), Bytes.toBytes(oldValue.getName()), fieldToBytes(oldValue, oldValue.getFieldType()), fieldToBytes(newValue, newValue.getFieldType())); }
fieldValidator.validatePrimaryKeys(keys, false); fieldValidator.validateField(oldValue); if (oldValue.getFieldType() != newValue.getFieldType()) { throw new IllegalArgumentException( String.format("Field types of oldValue (%s) and newValue (%s) are not the same", oldValue.getFieldType(), newValue.getFieldType())); Field<?> dbValue = createField(oldValue.getName(), oldValue.getFieldType(), colValue); if (!oldValue.equals(dbValue)) { return false;
/** * Validate if the given field matches the schema. The given field is invalid if: it is not present in the given * schema, its type is different than the given schema, or if it is a primary key but the given value is null. * * @param field the field to validate * @throws InvalidFieldException if the field does not pass the validation */ public void validateField(Field<?> field) throws InvalidFieldException { String fieldName = field.getName(); FieldType.Type expected = tableSchema.getType(fieldName); FieldType.Type actual = field.getFieldType(); if (expected == null) { throw new InvalidFieldException(tableSchema.getTableId(), fieldName); } if (!expected.equals(actual)) { throw new InvalidFieldException(tableSchema.getTableId(), fieldName, expected, actual); } if (tableSchema.isPrimaryKeyColumn(fieldName) && field.getValue() == null) { throw new InvalidFieldException(tableSchema.getTableId(), fieldName, "is a primary key but the value is null"); } }