use of org.apache.ignite.internal.schema.SchemaAware in project ignite-3 by apache.
the class TupleMarshallerImpl method toInternalTuple.
/**
* Analyze tuple and wrap into internal tuple.
*
* @param schema Schema.
* @param tuple Key or value tuple.
* @param keyFlag If {@code true} marshal key columns, otherwise marshall value columns.
* @return Internal tuple.
* @throws SchemaMismatchException If tuple doesn't match the schema.
*/
@NotNull
private InternalTuple toInternalTuple(SchemaDescriptor schema, Tuple tuple, boolean keyFlag) throws SchemaMismatchException {
if (tuple == null) {
return InternalTuple.NO_VALUE;
}
Columns columns = keyFlag ? schema.keyColumns() : schema.valueColumns();
int nonNullVarlen = 0;
int nonNullVarlenSize = 0;
int knownColumns = 0;
Map<String, Object> defaults = new HashMap<>();
if (tuple instanceof SchemaAware && Objects.equals(((SchemaAware) tuple).schema(), schema)) {
for (int i = 0, len = columns.length(); i < len; i++) {
final Column col = columns.column(i);
Object val = tuple.valueOrDefault(col.name(), POISON_OBJECT);
assert val != POISON_OBJECT;
if (val == null || columns.firstVarlengthColumn() < i) {
continue;
}
nonNullVarlenSize += getValueSize(val, col.type());
nonNullVarlen++;
}
} else {
for (int i = 0, len = columns.length(); i < len; i++) {
final Column col = columns.column(i);
Object val = tuple.valueOrDefault(col.name(), POISON_OBJECT);
if (val == POISON_OBJECT) {
if (keyFlag) {
throw new SchemaMismatchException("Missed key column: " + col.name());
}
val = col.defaultValue();
defaults.put(col.name(), val);
} else {
knownColumns++;
}
col.validate(val);
if (val == null || columns.isFixedSize(i)) {
continue;
}
nonNullVarlenSize += getValueSize(val, col.type());
nonNullVarlen++;
}
}
return new InternalTuple(tuple, nonNullVarlen, nonNullVarlenSize, defaults, knownColumns);
}
use of org.apache.ignite.internal.schema.SchemaAware in project ignite-3 by apache.
the class MutableRowTupleAdapterTest method testRowTupleSchemaAwareness.
@Test
public void testRowTupleSchemaAwareness() throws TupleMarshallerException {
TupleMarshaller marshaller = new TupleMarshallerImpl(new DummySchemaManagerImpl(schema));
Row row = new Row(schema, new ByteBufferRow(marshaller.marshal(Tuple.create().set("id", 1L).set("name", "Shirt")).bytes()));
Tuple tuple = TableRow.tuple(row);
Tuple key = TableRow.keyTuple(row);
final Tuple val = TableRow.valueTuple(row);
assertTrue(tuple instanceof SchemaAware);
assertNotNull(((SchemaAware) tuple).schema());
assertNotNull(((SchemaAware) key).schema());
assertNotNull(((SchemaAware) val).schema());
tuple.set("name", "noname");
assertNull(((SchemaAware) tuple).schema());
assertNotNull(((SchemaAware) key).schema());
assertNotNull(((SchemaAware) val).schema());
}
use of org.apache.ignite.internal.schema.SchemaAware in project ignite-3 by apache.
the class MutableRowTupleAdapterTest method testKeyValueTupleSchemaAwareness.
@Test
public void testKeyValueTupleSchemaAwareness() throws TupleMarshallerException {
TupleMarshaller marshaller = new TupleMarshallerImpl(new DummySchemaManagerImpl(schema));
Row row = new Row(schema, new ByteBufferRow(marshaller.marshal(Tuple.create().set("id", 1L).set("name", "Shirt")).bytes()));
Tuple tuple = TableRow.tuple(row);
Tuple key = TableRow.keyTuple(row);
final Tuple val = TableRow.valueTuple(row);
assertTrue(tuple instanceof SchemaAware);
key.set("foo", "bar");
assertNotNull(((SchemaAware) tuple).schema());
assertNull(((SchemaAware) key).schema());
assertNotNull(((SchemaAware) val).schema());
val.set("id", 1L);
assertNotNull(((SchemaAware) tuple).schema());
assertNull(((SchemaAware) key).schema());
assertNull(((SchemaAware) val).schema());
}
use of org.apache.ignite.internal.schema.SchemaAware in project ignite-3 by apache.
the class MutableRowTupleAdapterTest method testKeyValueTupleMutability.
@Test
public void testKeyValueTupleMutability() throws TupleMarshallerException {
TupleMarshaller marshaller = new TupleMarshallerImpl(new DummySchemaManagerImpl(schema));
Row row = new Row(schema, new ByteBufferRow(marshaller.marshal(Tuple.create().set("id", 1L).set("name", "Shirt")).bytes()));
Tuple tuple = TableRow.tuple(row);
Tuple key = TableRow.keyTuple(row);
final Tuple val = TableRow.valueTuple(row);
assertTrue(tuple instanceof SchemaAware);
key.set("id", 3L);
assertEquals(3L, (Long) key.value("id"));
assertEquals(1L, (Long) tuple.value("id"));
val.set("name", "noname");
assertEquals("noname", val.value("name"));
assertEquals("Shirt", tuple.value("name"));
val.set("foo", "bar");
assertEquals("bar", val.value("foo"));
assertThrows(IllegalArgumentException.class, () -> key.value("foo"));
assertThrows(IllegalArgumentException.class, () -> tuple.value("foo"));
}
use of org.apache.ignite.internal.schema.SchemaAware in project ignite-3 by apache.
the class ClientTableCommon method writeTuplesNullable.
/**
* Writes multiple tuples with null flags.
*
* @param packer Packer.
* @param tuples Tuples.
* @param part Which part of tuple to write.
* @param schemaRegistry The registry.
* @param skipHeader Whether to skip the tuple header.
* @throws IgniteException on failed serialization.
*/
public static void writeTuplesNullable(ClientMessagePacker packer, Collection<Tuple> tuples, TuplePart part, SchemaRegistry schemaRegistry, boolean skipHeader) {
if (tuples == null || tuples.isEmpty()) {
packer.packNil();
return;
}
SchemaDescriptor schema = schemaRegistry.schema();
packer.packInt(schema.version());
packer.packInt(tuples.size());
for (Tuple tuple : tuples) {
if (tuple == null) {
packer.packBoolean(false);
continue;
}
assert schema.version() == ((SchemaAware) tuple).schema().version();
packer.packBoolean(true);
writeTuple(packer, tuple, schema, skipHeader, part);
}
}
Aggregations