Search in sources :

Example 76 with Tuple

use of org.apache.ignite.table.Tuple in project ignite-3 by apache.

the class TupleMarshallerVarlenOnlyBenchmark method measureTupleBuildAndMarshallerCost.

/**
 * Measure tuple build then marshall.
 *
 * @param bh Black hole.
 */
@Benchmark
public void measureTupleBuildAndMarshallerCost(Blackhole bh) throws TupleMarshallerException {
    final Columns cols = schema.valueColumns();
    final Tuple valBld = Tuple.create(cols.length());
    for (int i = 0; i < cols.length(); i++) {
        valBld.set(cols.column(i).name(), val);
    }
    Tuple keyTuple = Tuple.create(1).set("key", rnd.nextLong());
    final Row row = marshaller.marshal(keyTuple, valBld);
    bh.consume(row);
}
Also used : Columns(org.apache.ignite.internal.schema.Columns) Row(org.apache.ignite.internal.schema.row.Row) Tuple(org.apache.ignite.table.Tuple) Benchmark(org.openjdk.jmh.annotations.Benchmark)

Example 77 with Tuple

use of org.apache.ignite.table.Tuple in project ignite-3 by apache.

the class Example method useCase5.

/**
 * Use case 5: using byte[] and binary objects in columns. The table has structure [ [id int, orgId int] // key [originalObject byte[],
 * upgradedObject byte[], int department] // value ] Where {@code originalObject} is some value that was originally put to the column,
 * {@code upgradedObject} is a version 2 of the object, and department is an extracted field.
 */
@Disabled
@ParameterizedTest
@MethodSource("tableFactory")
public void useCase5(Table t) {
    Tuple res = t.recordView().get(null, Tuple.create().set("id", 1).set("orgId", 1));
    byte[] objData = res.value("originalObject");
    BinaryObject binObj = BinaryObjects.wrap(objData);
    // Work with the binary object as in Ignite 2.x
    // Additionally, we may have a shortcut similar to primitive methods.
    binObj = res.binaryObjectValue("upgradedObject");
    // Plain byte[] and BinaryObject fields in a class are straightforward.
    class Record {

        final int id;

        final int orgId;

        byte[] originalObject;

        Tuple upgradedObject;

        int department;

        Record(int id, int orgId) {
            this.id = id;
            this.orgId = orgId;
        }
    }
    RecordView<Record> recordView = t.recordView(Record.class);
    // Similarly work with the binary objects.
    Record rec = recordView.get(null, new Record(1, 1));
    // Now assume that we have some POJO classes to deserialize the binary objects.
    class JavaPerson {

        String name;

        String lastName;
    }
    class JavaPersonV2 extends JavaPerson {

        int department;
    }
    // We can have a compound record deserializing the whole tuple automatically.
    class JavaPersonRecord {

        JavaPerson originalObject;

        JavaPersonV2 upgradedObject;

        int department;
    }
    RecordView<JavaPersonRecord> personRecordView = t.recordView(JavaPersonRecord.class);
    // Or we can have an arbitrary record with custom class selection.
    class TruncatedRecord {

        JavaPerson upgradedObject;

        int department;
    }
    // Custom serializer.
    TypeConverter<JavaPerson, byte[]> serializer = new TypeConverter<>() {

        @Override
        public byte[] toColumnType(JavaPerson obj) throws Exception {
            return BinaryObjects.serialize(obj).bytes();
        }

        @Override
        public JavaPerson toObjectType(byte[] data) throws Exception {
            return BinaryObjects.deserialize(BinaryObjects.wrap(data), JavaPerson.class);
        }
    };
    RecordView<TruncatedRecord> truncatedView = t.recordView(Mapper.builder(TruncatedRecord.class).convert("upgradedObject", serializer).map("updradedObject", "updradedObject").build());
    // Or we can have a custom conditional type selection.
    RecordView<TruncatedRecord> truncatedView2 = t.recordView(Mapper.builder(TruncatedRecord.class).build());
}
Also used : TypeConverter(org.apache.ignite.table.mapper.TypeConverter) BinaryObject(org.apache.ignite.binary.BinaryObject) Tuple(org.apache.ignite.table.Tuple) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource) Disabled(org.junit.jupiter.api.Disabled)

Example 78 with Tuple

use of org.apache.ignite.table.Tuple in project ignite-3 by apache.

the class InteropOperationsTest method writeRecordBinary.

/**
 * Write through record binary  view.
 *
 * @param id Id to write.
 * @param nulls If {@code true} - nullable fields will be filled, if {@code false} - otherwise.
 */
private void writeRecordBinary(int id, boolean nulls) {
    Tuple t1 = createTuple(id, nulls);
    t1.set("id", (long) id);
    assertTrue(R_BIN_VIEW.insert(null, t1));
}
Also used : Tuple(org.apache.ignite.table.Tuple)

Example 79 with Tuple

use of org.apache.ignite.table.Tuple in project ignite-3 by apache.

the class InteropOperationsTest method writeKeyValueBinary.

/**
 * Write through binary view.
 *
 * @param id Id to write.
 * @param nulls If {@code true} - nullable fields will be filled, if {@code false} - otherwise.
 */
private void writeKeyValueBinary(int id, boolean nulls) {
    Tuple k = Tuple.create().set("id", (long) id);
    Tuple v = createTuple(id, nulls);
    KV_BIN_VIEW.put(null, k, v);
}
Also used : Tuple(org.apache.ignite.table.Tuple)

Example 80 with Tuple

use of org.apache.ignite.table.Tuple in project ignite-3 by apache.

the class MutableRowTupleAdapterTest method getTuple.

private Tuple getTuple() {
    try {
        Tuple original = Tuple.create().set("id", 3L).set("name", "Shirt");
        TupleMarshaller marshaller = new TupleMarshallerImpl(new DummySchemaManagerImpl(schema));
        return TableRow.tuple(new Row(schema, new ByteBufferRow(marshaller.marshal(original).bytes())));
    } catch (TupleMarshallerException e) {
        return fail();
    }
}
Also used : TupleMarshallerException(org.apache.ignite.internal.schema.marshaller.TupleMarshallerException) 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) Tuple(org.apache.ignite.table.Tuple) DummySchemaManagerImpl(org.apache.ignite.internal.table.impl.DummySchemaManagerImpl)

Aggregations

Tuple (org.apache.ignite.table.Tuple)130 Test (org.junit.jupiter.api.Test)101 SchemaDescriptor (org.apache.ignite.internal.schema.SchemaDescriptor)33 Table (org.apache.ignite.table.Table)27 Column (org.apache.ignite.internal.schema.Column)25 Row (org.apache.ignite.internal.schema.row.Row)21 IgniteAbstractTest (org.apache.ignite.internal.testframework.IgniteAbstractTest)19 Ignite (org.apache.ignite.Ignite)17 TupleMarshaller (org.apache.ignite.internal.schema.marshaller.TupleMarshaller)17 TupleMarshallerImpl (org.apache.ignite.internal.schema.marshaller.TupleMarshallerImpl)17 DummySchemaManagerImpl (org.apache.ignite.internal.table.impl.DummySchemaManagerImpl)17 InternalTransaction (org.apache.ignite.internal.tx.InternalTransaction)15 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)11 ByteBufferRow (org.apache.ignite.internal.schema.ByteBufferRow)10 TableDefinition (org.apache.ignite.schema.definition.TableDefinition)9 ArrayList (java.util.ArrayList)8 Disabled (org.junit.jupiter.api.Disabled)7 MethodSource (org.junit.jupiter.params.provider.MethodSource)7 BigDecimal (java.math.BigDecimal)6 Transaction (org.apache.ignite.tx.Transaction)6