Search in sources :

Example 1 with SchemaAware

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);
}
Also used : SchemaAware(org.apache.ignite.internal.schema.SchemaAware) SchemaMismatchException(org.apache.ignite.internal.schema.SchemaMismatchException) HashMap(java.util.HashMap) Column(org.apache.ignite.internal.schema.Column) Columns(org.apache.ignite.internal.schema.Columns) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with SchemaAware

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());
}
Also used : SchemaAware(org.apache.ignite.internal.schema.SchemaAware) TupleMarshaller(org.apache.ignite.internal.schema.marshaller.TupleMarshaller) ByteBufferRow(org.apache.ignite.internal.schema.ByteBufferRow) TupleMarshallerImpl(org.apache.ignite.internal.schema.marshaller.TupleMarshallerImpl) Row(org.apache.ignite.internal.schema.row.Row) ByteBufferRow(org.apache.ignite.internal.schema.ByteBufferRow) DummySchemaManagerImpl(org.apache.ignite.internal.table.impl.DummySchemaManagerImpl) Tuple(org.apache.ignite.table.Tuple) Test(org.junit.jupiter.api.Test)

Example 3 with SchemaAware

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());
}
Also used : SchemaAware(org.apache.ignite.internal.schema.SchemaAware) TupleMarshaller(org.apache.ignite.internal.schema.marshaller.TupleMarshaller) ByteBufferRow(org.apache.ignite.internal.schema.ByteBufferRow) TupleMarshallerImpl(org.apache.ignite.internal.schema.marshaller.TupleMarshallerImpl) Row(org.apache.ignite.internal.schema.row.Row) ByteBufferRow(org.apache.ignite.internal.schema.ByteBufferRow) DummySchemaManagerImpl(org.apache.ignite.internal.table.impl.DummySchemaManagerImpl) Tuple(org.apache.ignite.table.Tuple) Test(org.junit.jupiter.api.Test)

Example 4 with SchemaAware

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"));
}
Also used : SchemaAware(org.apache.ignite.internal.schema.SchemaAware) TupleMarshaller(org.apache.ignite.internal.schema.marshaller.TupleMarshaller) ByteBufferRow(org.apache.ignite.internal.schema.ByteBufferRow) TupleMarshallerImpl(org.apache.ignite.internal.schema.marshaller.TupleMarshallerImpl) Row(org.apache.ignite.internal.schema.row.Row) ByteBufferRow(org.apache.ignite.internal.schema.ByteBufferRow) DummySchemaManagerImpl(org.apache.ignite.internal.table.impl.DummySchemaManagerImpl) Tuple(org.apache.ignite.table.Tuple) Test(org.junit.jupiter.api.Test)

Example 5 with SchemaAware

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);
    }
}
Also used : SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) SchemaAware(org.apache.ignite.internal.schema.SchemaAware) Tuple(org.apache.ignite.table.Tuple)

Aggregations

SchemaAware (org.apache.ignite.internal.schema.SchemaAware)6 Tuple (org.apache.ignite.table.Tuple)5 ByteBufferRow (org.apache.ignite.internal.schema.ByteBufferRow)3 TupleMarshaller (org.apache.ignite.internal.schema.marshaller.TupleMarshaller)3 TupleMarshallerImpl (org.apache.ignite.internal.schema.marshaller.TupleMarshallerImpl)3 Row (org.apache.ignite.internal.schema.row.Row)3 DummySchemaManagerImpl (org.apache.ignite.internal.table.impl.DummySchemaManagerImpl)3 Test (org.junit.jupiter.api.Test)3 SchemaDescriptor (org.apache.ignite.internal.schema.SchemaDescriptor)2 HashMap (java.util.HashMap)1 Column (org.apache.ignite.internal.schema.Column)1 Columns (org.apache.ignite.internal.schema.Columns)1 SchemaMismatchException (org.apache.ignite.internal.schema.SchemaMismatchException)1 NotNull (org.jetbrains.annotations.NotNull)1