use of org.apache.ignite.internal.schema.row.RowAssembler in project ignite-3 by apache.
the class UpgradingRowAdapterTest method serializeValuesToRow.
/**
* Validates row values after serialization-then-deserialization.
*
* @param schema Row schema.
* @param vals Row values.
* @return Row bytes.
*/
private byte[] serializeValuesToRow(SchemaDescriptor schema, List<Object> vals) {
assertEquals(schema.keyColumns().length() + schema.valueColumns().length(), vals.size());
int nonNullVarLenKeyCols = 0;
int nonNullVarLenValCols = 0;
int nonNullVarLenKeySize = 0;
int nonNullVarLenValSize = 0;
for (int i = 0; i < vals.size(); i++) {
NativeTypeSpec type = schema.column(i).type().spec();
if (vals.get(i) != null && !type.fixedLength()) {
if (type == NativeTypeSpec.BYTES) {
byte[] val = (byte[]) vals.get(i);
if (schema.isKeyColumn(i)) {
nonNullVarLenKeyCols++;
nonNullVarLenKeySize += val.length;
} else {
nonNullVarLenValCols++;
nonNullVarLenValSize += val.length;
}
} else if (type == NativeTypeSpec.STRING) {
if (schema.isKeyColumn(i)) {
nonNullVarLenKeyCols++;
nonNullVarLenKeySize += RowAssembler.utf8EncodedLength((CharSequence) vals.get(i));
} else {
nonNullVarLenValCols++;
nonNullVarLenValSize += RowAssembler.utf8EncodedLength((CharSequence) vals.get(i));
}
} else if (type == NativeTypeSpec.NUMBER) {
if (schema.isKeyColumn(i)) {
nonNullVarLenKeyCols++;
nonNullVarLenKeySize += RowAssembler.sizeInBytes((BigInteger) vals.get(i));
} else {
nonNullVarLenValCols++;
nonNullVarLenValSize += RowAssembler.sizeInBytes((BigInteger) vals.get(i));
}
} else if (type == NativeTypeSpec.DECIMAL) {
if (schema.isKeyColumn(i)) {
nonNullVarLenKeyCols++;
nonNullVarLenKeySize += RowAssembler.sizeInBytes((BigDecimal) vals.get(i));
} else {
nonNullVarLenValCols++;
nonNullVarLenValSize += RowAssembler.sizeInBytes((BigDecimal) vals.get(i));
}
} else {
throw new IllegalStateException("Unsupported variable-length type: " + type);
}
}
}
RowAssembler asm = new RowAssembler(schema, nonNullVarLenKeySize, nonNullVarLenKeyCols, nonNullVarLenValSize, nonNullVarLenValCols);
for (int i = 0; i < vals.size(); i++) {
if (vals.get(i) == null) {
asm.appendNull();
} else {
NativeType type = schema.column(i).type();
switch(type.spec()) {
case INT8:
asm.appendByte((Byte) vals.get(i));
break;
case INT16:
asm.appendShort((Short) vals.get(i));
break;
case INT32:
asm.appendInt((Integer) vals.get(i));
break;
case INT64:
asm.appendLong((Long) vals.get(i));
break;
case FLOAT:
asm.appendFloat((Float) vals.get(i));
break;
case DOUBLE:
asm.appendDouble((Double) vals.get(i));
break;
case UUID:
asm.appendUuid((java.util.UUID) vals.get(i));
break;
case STRING:
asm.appendString((String) vals.get(i));
break;
case NUMBER:
asm.appendNumber((BigInteger) vals.get(i));
break;
case DECIMAL:
asm.appendDecimal((BigDecimal) vals.get(i));
break;
case BYTES:
asm.appendBytes((byte[]) vals.get(i));
break;
case BITMASK:
asm.appendBitmask((BitSet) vals.get(i));
break;
case DATE:
asm.appendDate((LocalDate) vals.get(i));
break;
case TIME:
asm.appendTime((LocalTime) vals.get(i));
break;
case DATETIME:
asm.appendDateTime((LocalDateTime) vals.get(i));
break;
case TIMESTAMP:
asm.appendTimestamp((Instant) vals.get(i));
break;
default:
throw new IllegalStateException("Unsupported test type: " + type);
}
}
}
return asm.toBytes();
}
use of org.apache.ignite.internal.schema.row.RowAssembler in project ignite-3 by apache.
the class ColumnBindingTest method wrongIdentityBinding.
/**
* Wrong identity binding.
*/
@Test
public void wrongIdentityBinding() {
// Incompatible types.
final ColumnBinding binding = ColumnBinding.createIdentityBinding(new Column("val", UUID, true).copy(0), java.util.UUID.class, null);
assertThrows(MarshallerException.class, () -> binding.value("Some string"));
final Pair<RowAssembler, Row> mocks = createMocks();
assertThrows(MarshallerException.class, () -> binding.write(mocks.getFirst(), "Other string"));
// Implicit serialization is not supported yet.
assertThrows(IllegalArgumentException.class, () -> ColumnBinding.createIdentityBinding(new Column("val", BYTES, true).copy(0), TestSimpleObject.class, null));
}
use of org.apache.ignite.internal.schema.row.RowAssembler in project ignite-3 by apache.
the class ColumnBindingTest method fieldsBindingWithConverter.
/**
* Fields binding with converter.
*
* @throws Exception If failed.
*/
@Test
public void fieldsBindingWithConverter() throws Exception {
Column[] cols = new Column[] { new Column("data", BYTES, true) };
final Pair<RowAssembler, Row> mocks = createMocks();
final RowAssembler rowAssembler = mocks.getFirst();
final Row row = mocks.getSecond();
TestObjectWrapper obj = new TestObjectWrapper();
obj.data = TestSimpleObject.randomObject(rnd);
for (int i = 0; i < cols.length; i++) {
ColumnBinding.createFieldBinding(cols[i].copy(i), TestObjectWrapper.class, cols[i].name(), new SerializingConverter()).write(rowAssembler, obj);
}
TestObjectWrapper restoredObj = new TestObjectWrapper();
for (int i = 0; i < cols.length; i++) {
ColumnBinding.createFieldBinding(cols[i].copy(i), TestObjectWrapper.class, cols[i].name(), new SerializingConverter()).read(row, restoredObj);
}
assertEquals(obj, restoredObj);
}
use of org.apache.ignite.internal.schema.row.RowAssembler in project ignite-3 by apache.
the class RowTest method checkValues.
/**
* Validates row values after serialization-then-deserialization.
*
* @param schema Row schema.
* @param vals Row values.
*/
private void checkValues(SchemaDescriptor schema, Object... vals) {
assertEquals(schema.keyColumns().length() + schema.valueColumns().length(), vals.length);
int nonNullVarLenKeyCols = 0;
int nonNullVarLenValCols = 0;
int nonNullVarLenKeySize = 0;
int nonNullVarLenValSize = 0;
for (int i = 0; i < vals.length; i++) {
NativeTypeSpec type = schema.column(i).type().spec();
if (vals[i] != null && !type.fixedLength()) {
if (type == NativeTypeSpec.BYTES) {
byte[] val = (byte[]) vals[i];
if (schema.isKeyColumn(i)) {
nonNullVarLenKeyCols++;
nonNullVarLenKeySize += val.length;
} else {
nonNullVarLenValCols++;
nonNullVarLenValSize += val.length;
}
} else if (type == NativeTypeSpec.STRING) {
if (schema.isKeyColumn(i)) {
nonNullVarLenKeyCols++;
nonNullVarLenKeySize += RowAssembler.utf8EncodedLength((CharSequence) vals[i]);
} else {
nonNullVarLenValCols++;
nonNullVarLenValSize += RowAssembler.utf8EncodedLength((CharSequence) vals[i]);
}
} else if (type == NativeTypeSpec.NUMBER) {
if (schema.isKeyColumn(i)) {
nonNullVarLenKeyCols++;
nonNullVarLenKeySize += RowAssembler.sizeInBytes((BigInteger) vals[i]);
} else {
nonNullVarLenValCols++;
nonNullVarLenValSize += RowAssembler.sizeInBytes((BigInteger) vals[i]);
}
} else if (type == NativeTypeSpec.DECIMAL) {
if (schema.isKeyColumn(i)) {
nonNullVarLenKeyCols++;
nonNullVarLenKeySize += RowAssembler.sizeInBytes((BigDecimal) vals[i]);
} else {
nonNullVarLenValCols++;
nonNullVarLenValSize += RowAssembler.sizeInBytes((BigDecimal) vals[i]);
}
} else {
throw new IllegalStateException("Unsupported variable-length type: " + type);
}
}
}
RowAssembler asm = new RowAssembler(schema, nonNullVarLenKeySize, nonNullVarLenKeyCols, nonNullVarLenValSize, nonNullVarLenValCols);
for (int i = 0; i < vals.length; i++) {
if (vals[i] == null) {
asm.appendNull();
} else {
NativeType type = schema.column(i).type();
switch(type.spec()) {
case INT8:
asm.appendByte((Byte) vals[i]);
break;
case INT16:
asm.appendShort((Short) vals[i]);
break;
case INT32:
asm.appendInt((Integer) vals[i]);
break;
case INT64:
asm.appendLong((Long) vals[i]);
break;
case FLOAT:
asm.appendFloat((Float) vals[i]);
break;
case DOUBLE:
asm.appendDouble((Double) vals[i]);
break;
case UUID:
asm.appendUuid((java.util.UUID) vals[i]);
break;
case STRING:
asm.appendString((String) vals[i]);
break;
case NUMBER:
asm.appendNumber((BigInteger) vals[i]);
break;
case DECIMAL:
asm.appendDecimal((BigDecimal) vals[i]);
break;
case BYTES:
asm.appendBytes((byte[]) vals[i]);
break;
case BITMASK:
asm.appendBitmask((BitSet) vals[i]);
break;
case DATE:
asm.appendDate((LocalDate) vals[i]);
break;
case TIME:
asm.appendTime((LocalTime) vals[i]);
break;
case DATETIME:
asm.appendDateTime((LocalDateTime) vals[i]);
break;
case TIMESTAMP:
asm.appendTimestamp((Instant) vals[i]);
break;
default:
throw new IllegalStateException("Unsupported test type: " + type);
}
}
}
byte[] data = asm.toBytes();
Row row = new Row(schema, new ByteBufferRow(data));
for (int i = 0; i < vals.length; i++) {
Column col = schema.column(i);
NativeTypeSpec type = col.type().spec();
if (type == NativeTypeSpec.BYTES) {
assertArrayEquals((byte[]) vals[i], (byte[]) NativeTypeSpec.BYTES.objectValue(row, i), "Failed for column: " + col);
} else {
assertEquals(vals[i], type.objectValue(row, i), "Failed for column: " + col);
}
}
}
use of org.apache.ignite.internal.schema.row.RowAssembler in project ignite-3 by apache.
the class RowAssemblerAdvancedSchemaTest method varlenNullableColumns.
/**
* Validate row layout for schema of fix-len nullable key and fix-len nullable value.
*/
@Test
public void varlenNullableColumns() {
SchemaDescriptor schema = new SchemaDescriptor(42, new Column[] { new Column("keyCol", INT8, false) }, new Column[] { new Column("valCol1", STRING, true), new Column("valCol2", BYTES, true), new Column("valCol3", STRING, true) });
// Last col null
assertRowBytesEquals(new byte[] { 42, 0, 125, 64, 31, -9, 6, 0, 0, 0, 0, 22, 20, 0, 0, 0, 1, 4, 1, 2, 33, -77, -26, -120, -111, -26, -124, -101, 74, 97, 118, 97 }, new RowAssembler(schema, 0, 2).appendByte((byte) 22).appendBytes(new byte[] { 33, -77 }).appendString("我愛Java").appendNull().toBytes());
// Fist col null.
assertRowBytesEquals(new byte[] { 42, 0, 125, 64, 31, -9, 6, 0, 0, 0, 0, 22, 23, 0, 0, 0, 1, 1, 1, 10, -26, -120, -111, -26, -124, -101, 74, 97, 118, 97, 97, 115, 99, 105, 105 }, new RowAssembler(schema, 0, 2).appendByte((byte) 22).appendNull().appendString("我愛Java").appendString("ascii").toBytes());
// Middle col null.
assertRowBytesEquals(new byte[] { 42, 0, 125, 64, 31, -9, 6, 0, 0, 0, 0, 22, 15, 0, 0, 0, 1, 2, 1, 2, 33, -77, 97, 115, 99, 105, 105 }, new RowAssembler(schema, 0, 2).appendByte((byte) 22).appendBytes(new byte[] { 33, -77 }).appendNull().appendString("ascii").toBytes());
// All null.
assertRowBytesEquals(new byte[] { 42, 0, 125, 64, 31, -9, 6, 0, 0, 0, 0, 22, 6, 0, 0, 0, 0, 7 }, new RowAssembler(schema, 0, 0).appendByte((byte) 22).appendNull().appendNull().appendNull().toBytes());
// No value.
assertRowBytesEquals(new byte[] { 0, 0, 125, 64, 31, -9, 6, 0, 0, 0, 0, 22 }, new RowAssembler(schema, 0, 0).appendByte((byte) 22).toBytes());
}
Aggregations