Search in sources :

Example 31 with Tuple

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

the class Example method useCase4.

/**
 * Use case 4: Conditional serialization. The table has structure is [ [id int, orgId int] // key [owner varchar, type int,
 * conditionalDetails byte[]] // value ]
 */
@Disabled
@ParameterizedTest
@MethodSource("tableFactory")
public void useCase4(Table t) {
    class OrderKey {

        final int id;

        final int orgId;

        OrderKey(int id, int orgId) {
            this.id = id;
            this.orgId = orgId;
        }
    }
    class OrderValue {

        String owner;

        // Discriminator value.
        int type;

        /* BillingDetails */
        Object billingDetails;
    }
    class CreditCard {

        /* extends BillingDetails */
        long cardNumber;

        int expYear;

        int expMonth;
    }
    class BankAccount {

        /* extends BillingDetails */
        long account;

        String bankName;
    }
    KeyValueView<OrderKey, OrderValue> orderKvView = t.keyValueView(Mapper.of(OrderKey.class, "key"), Mapper.builder(OrderValue.class).build());
    OrderValue ov = orderKvView.get(null, new OrderKey(1, 1));
    // Same with direct Row access and BinaryObject wrapper.
    Tuple res = t.recordView().get(null, Tuple.create().set("id", 1).set("orgId", 1));
    byte[] objData = res.value("billingDetails");
    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("billingDetails");
    // Same with RecordAPI.
    class OrderRecord {

        final int id;

        final int orgId;

        String owner;

        int type;

        BinaryObject billingDetails;

        OrderRecord(int id, int orgId) {
            this.id = id;
            this.orgId = orgId;
        }
    }
    final RecordView<OrderRecord> orderRecView = t.recordView(OrderRecord.class);
    OrderRecord orderRecord = orderRecView.get(null, new OrderRecord(1, 1));
    binObj = orderRecord.billingDetails;
    // Manual deserialization is possible as well.
    Object billingDetails = orderRecord.type == 0 ? BinaryObjects.deserialize(binObj, CreditCard.class) : BinaryObjects.deserialize(binObj, BankAccount.class);
}
Also used : BinaryObject(org.apache.ignite.binary.BinaryObject) 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 32 with Tuple

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

the class Example method useCase6.

/**
 * Use case 6: a simple one. The table has the structure [ [id long] // key [name varchar, lastName varchar, decimal salary, int
 * department] // value ] We show how to use the raw TableRow and a mapped class.
 */
@Disabled
@ParameterizedTest
@MethodSource("tableFactory")
public void useCase6(Table t) {
    // Search row will allow nulls even in non-null columns.
    Tuple res = t.recordView().get(null, Tuple.create().set("id", 1));
    String name = res.value("name");
    String lastName = res.value("latName");
    BigDecimal salary = res.value("salary");
    Integer department = res.value("department");
    // We may have primitive-returning methods if needed.
    int departmentPrimitive = res.intValue("department");
    // Note that schema itself already defined which fields are key field.
    class Employee {

        String name;

        String lastName;

        BigDecimal salary;

        int department;
    }
    class Key {

        long id;
    }
    KeyValueView<Long, Employee> employeeView = t.keyValueView(Long.class, Employee.class);
    Employee e = employeeView.get(null, 1L);
}
Also used : Tuple(org.apache.ignite.table.Tuple) BigDecimal(java.math.BigDecimal) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource) Disabled(org.junit.jupiter.api.Disabled)

Example 33 with Tuple

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

the class Example method useCase1.

/**
 * Use case 1: a simple one. The table has the structure [ [id int, orgId int] // key [name varchar, lastName varchar, decimal salary,
 * int department] // value ] We show how to use the raw TableRow and a mapped class.
 */
@Disabled
@ParameterizedTest
@MethodSource("tableFactory")
public void useCase1(Table t) {
    // Search row will allow nulls even in non-null columns.
    Tuple res = t.recordView().get(null, Tuple.create().set("id", 1).set("orgId", 1));
    String name = res.value("name");
    String lastName = res.value("latName");
    BigDecimal salary = res.value("salary");
    Integer department = res.value("department");
    // We may have primitive-returning methods if needed.
    int departmentPrimitive = res.intValue("department");
    // Note that schema itself already defined which fields are key field.
    class Employee {

        final int id;

        final int orgId;

        String name;

        String lastName;

        BigDecimal salary;

        int department;

        Employee(int id, int orgId) {
            this.id = id;
            this.orgId = orgId;
        }
    }
    RecordView<Employee> employeeView = t.recordView(Employee.class);
    Employee e = employeeView.get(null, new Employee(1, 1));
    // As described in the IEP-54, we can have a truncated mapping.
    class TruncatedEmployee {

        final int id;

        final int orgId;

        String name;

        String lastName;

        TruncatedEmployee(int id, int orgId) {
            this.id = id;
            this.orgId = orgId;
        }
    }
    RecordView<TruncatedEmployee> truncatedEmployeeView = t.recordView(TruncatedEmployee.class);
    // salary and department will not be sent over the network during this call.
    TruncatedEmployee te = truncatedEmployeeView.get(null, new TruncatedEmployee(1, 1));
}
Also used : Tuple(org.apache.ignite.table.Tuple) BigDecimal(java.math.BigDecimal) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource) Disabled(org.junit.jupiter.api.Disabled)

Example 34 with Tuple

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

the class InteropOperationsTest method validateTuple.

/**
 * Test specified tuple.
 *
 * @param id Expected tuple id.
 * @param t Tuple to test.
 * @param nulls If {@code true} - nullable fields will be filled.
 */
private void validateTuple(int id, Tuple t, boolean nulls) {
    long actualId = t.longValue("id");
    assertEquals(id, actualId);
    Tuple expected = createTuple(id, nulls);
    expected.set("id", (long) id);
    for (Column col : SCHEMA.valueColumns().columns()) {
        if (!nulls && col.nullable()) {
            continue;
        }
        String colName = col.name();
        NativeType type = col.type();
        if (NativeTypes.INT8.equals(type)) {
            assertEquals(expected.byteValue(colName), t.byteValue(colName));
        } else if (NativeTypes.INT16.equals(type)) {
            assertEquals(expected.shortValue(colName), t.shortValue(colName));
        } else if (NativeTypes.INT32.equals(type)) {
            assertEquals(expected.intValue(colName), t.intValue(colName));
        } else if (NativeTypes.INT64.equals(type)) {
            assertEquals(expected.longValue(colName), t.longValue(colName));
        } else if (NativeTypes.FLOAT.equals(type)) {
            assertEquals(expected.floatValue(colName), t.floatValue(colName));
        } else if (NativeTypes.DOUBLE.equals(type)) {
            assertEquals(expected.doubleValue(colName), t.doubleValue(colName));
        } else if (NativeTypes.BYTES.equals(type)) {
            assertArrayEquals((byte[]) expected.value(colName), (byte[]) t.value(colName));
        } else if (NativeTypes.STRING.equals(type)) {
            assertEquals(expected.stringValue(colName), t.stringValue(colName));
        } else if (NativeTypes.UUID.equals(type)) {
            assertEquals(expected.uuidValue(colName), t.uuidValue(colName));
        } else if (NativeTypes.DATE.equals(type)) {
            assertEquals(expected.dateValue(colName), t.dateValue(colName));
        } else if (NativeTypes.time().equals(type)) {
            assertEquals(expected.timeValue(colName), t.timeValue(colName));
        } else if (NativeTypes.datetime().equals(type)) {
            assertEquals(expected.datetimeValue(colName), t.datetimeValue(colName));
        } else if (NativeTypes.timestamp().equals(type)) {
            assertEquals(expected.timestampValue(colName), expected.timestampValue(colName));
        } else if (NativeTypes.numberOf(2).equals(type)) {
            assertEquals((BigInteger) expected.value(colName), t.value(colName));
        } else if (NativeTypes.decimalOf(5, 2).equals(type)) {
            assertEquals((BigDecimal) expected.value(colName), t.value(colName));
        } else if (NativeTypes.bitmaskOf(8).equals(type)) {
            assertEquals(expected.bitmaskValue(colName), t.bitmaskValue(colName));
        } else {
            fail("Unable to validate value of type " + type);
        }
    }
    assertTrue(!nulls ^ expected.equals(t), "nulls = " + nulls + ", id = " + id);
}
Also used : Column(org.apache.ignite.internal.schema.Column) NativeType(org.apache.ignite.internal.schema.NativeType) BigInteger(java.math.BigInteger) Tuple(org.apache.ignite.table.Tuple)

Example 35 with Tuple

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

the class InteropOperationsTest method createTuple.

/**
 * Create tuple with specified id and nulls fields filled.
 *
 * @param id Id.
 * @param nulls If {@code true} - nullable fields will be filled.
 * @return Tuple with all requested fields.
 */
private Tuple createTuple(int id, boolean nulls) {
    Tuple res = Tuple.create();
    for (Column col : SCHEMA.valueColumns().columns()) {
        if (!nulls && col.nullable()) {
            continue;
        }
        String colName = col.name();
        NativeType type = col.type();
        if (NativeTypes.INT8.equals(type)) {
            res.set(colName, (byte) id);
        } else if (NativeTypes.INT16.equals(type)) {
            res.set(colName, (short) id);
        } else if (NativeTypes.INT32.equals(type)) {
            res.set(colName, id);
        } else if (NativeTypes.INT64.equals(type)) {
            res.set(colName, (long) id);
        } else if (NativeTypes.FLOAT.equals(type)) {
            res.set(colName, (float) id);
        } else if (NativeTypes.DOUBLE.equals(type)) {
            res.set(colName, (double) id);
        } else if (NativeTypes.BYTES.equals(type)) {
            res.set(colName, String.valueOf(id).getBytes(StandardCharsets.UTF_8));
        } else if (NativeTypes.STRING.equals(type)) {
            res.set(colName, String.valueOf(id));
        } else if (NativeTypes.UUID.equals(type)) {
            res.set(colName, new UUID(0L, (long) id));
        } else if (NativeTypes.DATE.equals(type)) {
            res.set(colName, LocalDate.ofYearDay(2021, id));
        } else if (NativeTypes.time().equals(type)) {
            res.set(colName, LocalTime.ofSecondOfDay(id));
        } else if (NativeTypes.datetime().equals(type)) {
            res.set(colName, LocalDateTime.ofEpochSecond(id, 0, ZoneOffset.UTC));
        } else if (NativeTypes.timestamp().equals(type)) {
            res.set(colName, Instant.ofEpochSecond(id));
        } else if (NativeTypes.numberOf(2).equals(type)) {
            res.set(colName, BigInteger.valueOf(id));
        } else if (NativeTypes.decimalOf(5, 2).equals(type)) {
            res.set(colName, BigDecimal.valueOf(id * 100).movePointLeft(2));
        } else if (NativeTypes.bitmaskOf(8).equals(type)) {
            BitSet bitSet = new BitSet();
            bitSet.set(id);
            res.set(colName, bitSet);
        } else {
            fail("Unable to fullfill value of type " + type);
        }
    }
    return res;
}
Also used : Column(org.apache.ignite.internal.schema.Column) BitSet(java.util.BitSet) NativeType(org.apache.ignite.internal.schema.NativeType) UUID(java.util.UUID) Tuple(org.apache.ignite.table.Tuple)

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