private byte[] fieldToBytes(Field<?> field, FieldType.Type type) throws InvalidFieldException { if (field.getValue() == null) { return null; } switch (type) { case INTEGER: return Bytes.toBytes((Integer) field.getValue()); case LONG: return Bytes.toBytes((Long) field.getValue()); case FLOAT: return Bytes.toBytes((Float) field.getValue()); case DOUBLE: return Bytes.toBytes((Double) field.getValue()); case STRING: return Bytes.toBytes((String) field.getValue()); case BYTES: return (byte[]) field.getValue(); default: throw new InvalidFieldException(schema.getTableId(), field.getName()); } }
@Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } Field field = (Field) o; if (!Objects.equals(fieldType, field.fieldType)) { return false; } if (fieldType.getType() == FieldType.Type.BYTES) { return Bytes.equals((byte[]) value, (byte[]) field.getValue()); } return Objects.equals(value, field.value); }
private void addKey(MDSKey.Builder key, Field<?> field, FieldType.Type type) throws InvalidFieldException { if (field.getValue() == null) { throw new InvalidFieldException(schema.getTableId(), field.getName(), "is a primary key and value is null"); } switch (type) { case INTEGER: key.add((Integer) field.getValue()); return; case LONG: key.add((Long) field.getValue()); return; case STRING: key.add((String) field.getValue()); return; case BYTES: key.add((byte[]) field.getValue()); return; default: throw new InvalidFieldException(schema.getTableId(), field.getName()); } }
private void setField(PreparedStatement statement, Field field, int parameterIndex) throws SQLException, InvalidFieldException { fieldValidator.validateField(field); Object value = field.getValue(); FieldType.Type type = tableSchema.getType(field.getName()); if (type == null) {
if (oldValue.getValue() != null) { 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"); } }
Optional<StructuredRow> rowOptional = table.read(keys); Assert.assertTrue(rowOptional.isPresent()); Assert.assertEquals(longField.getValue(), rowOptional.get().getLong(LONG_COL)); }); Assert.assertTrue(rowOptional.isPresent()); Assert.assertEquals(((Long) longField.getValue()) + increment, (long) rowOptional.get().getLong(LONG_COL)); });