Search in sources :

Example 41 with SchemaDescriptor

use of org.apache.ignite.internal.schema.SchemaDescriptor in project ignite-3 by apache.

the class TupleMarshallerVarlenOnlyBenchmark method init.

/**
 * Setup.
 */
@Setup
public void init() {
    final long seed = System.currentTimeMillis();
    final boolean useString = "string".equals(type);
    rnd = new Random(seed);
    schema = new SchemaDescriptor(42, new Column[] { new Column("key", INT64, false, (Supplier<Object> & Serializable) () -> 0L) }, IntStream.range(0, fieldsCount).boxed().map(i -> new Column("col" + i, useString ? STRING : BYTES, nullable)).toArray(Column[]::new));
    marshaller = new TupleMarshallerImpl(new SchemaRegistryImpl(v -> null, () -> INITIAL_SCHEMA_VERSION) {

        @Override
        public SchemaDescriptor schema() {
            return schema;
        }

        @Override
        public SchemaDescriptor schema(int ver) {
            return schema;
        }

        @Override
        public int lastSchemaVersion() {
            return schema.version();
        }
    });
    if (useString) {
        final byte[] data = new byte[dataSize / fieldsCount];
        for (int i = 0; i < data.length; i++) {
            data[i] = (byte) (rnd.nextInt() & 0x7F);
        }
        // Latin1 string.
        val = new String(data, StandardCharsets.ISO_8859_1);
    } else {
        rnd.nextBytes((byte[]) (val = new byte[dataSize / fieldsCount]));
    }
}
Also used : IntStream(java.util.stream.IntStream) TupleMarshallerImpl(org.apache.ignite.internal.schema.marshaller.TupleMarshallerImpl) BenchmarkMode(org.openjdk.jmh.annotations.BenchmarkMode) Measurement(org.openjdk.jmh.annotations.Measurement) Blackhole(org.openjdk.jmh.infra.Blackhole) SchemaRegistryImpl(org.apache.ignite.internal.schema.registry.SchemaRegistryImpl) Random(java.util.Random) Scope(org.openjdk.jmh.annotations.Scope) Supplier(java.util.function.Supplier) Warmup(org.openjdk.jmh.annotations.Warmup) INT64(org.apache.ignite.internal.schema.NativeTypes.INT64) Row(org.apache.ignite.internal.schema.row.Row) OutputTimeUnit(org.openjdk.jmh.annotations.OutputTimeUnit) INITIAL_SCHEMA_VERSION(org.apache.ignite.internal.schema.registry.SchemaRegistryImpl.INITIAL_SCHEMA_VERSION) Runner(org.openjdk.jmh.runner.Runner) RunnerException(org.openjdk.jmh.runner.RunnerException) SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) Setup(org.openjdk.jmh.annotations.Setup) Columns(org.apache.ignite.internal.schema.Columns) Mode(org.openjdk.jmh.annotations.Mode) TupleMarshaller(org.apache.ignite.internal.schema.marshaller.TupleMarshaller) TupleMarshallerException(org.apache.ignite.internal.schema.marshaller.TupleMarshallerException) Param(org.openjdk.jmh.annotations.Param) State(org.openjdk.jmh.annotations.State) StandardCharsets(java.nio.charset.StandardCharsets) Benchmark(org.openjdk.jmh.annotations.Benchmark) Serializable(java.io.Serializable) TimeUnit(java.util.concurrent.TimeUnit) OptionsBuilder(org.openjdk.jmh.runner.options.OptionsBuilder) BYTES(org.apache.ignite.internal.schema.NativeTypes.BYTES) STRING(org.apache.ignite.internal.schema.NativeTypes.STRING) Column(org.apache.ignite.internal.schema.Column) Fork(org.openjdk.jmh.annotations.Fork) Tuple(org.apache.ignite.table.Tuple) SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) Serializable(java.io.Serializable) SchemaRegistryImpl(org.apache.ignite.internal.schema.registry.SchemaRegistryImpl) Random(java.util.Random) Column(org.apache.ignite.internal.schema.Column) Supplier(java.util.function.Supplier) TupleMarshallerImpl(org.apache.ignite.internal.schema.marshaller.TupleMarshallerImpl) Setup(org.openjdk.jmh.annotations.Setup)

Example 42 with SchemaDescriptor

use of org.apache.ignite.internal.schema.SchemaDescriptor in project ignite-3 by apache.

the class Example method useCase8.

/**
 * Use case 8: Here we show how to use mapper to represent the same data in different ways.
 */
@Disabled
@ParameterizedTest
@MethodSource("tableFactory")
public void useCase8(Table t) {
    new SchemaDescriptor(1, new Column[] { new Column("colId", NativeTypes.INT64, false) }, new Column[] { new Column("colData", NativeTypes.BYTES, true) });
    // Arbitrary user type.
    class UserObject {

        double salary;
    }
    // Domain class, which fields mapped to table columns.
    class Employee {

        UserObject fieldData;
    }
    // Domain class, which fields mapped to table columns.
    class Employee2 {

        byte[] fieldData;
    }
    Mapper.builder(Employee.class).map("fieldData.salary", "colSalary").build();
    // Actually, any bi-directional converter can be here instead.
    // Marshaller is a special case of "UserObject <--> byte[]" converter, just for example.
    // here, create some marshaller for UserObject.class.
    TypeConverter<UserObject, byte[]> marsh = null;
    // One-column only supported first-citizen types.
    Mapper.of(Long.class);
    Mapper.of(byte[].class);
    // Automatically maps object fields to columns with same names.
    Mapper.of(UserObject.class);
    // LongMapper -> long - is it possible?
    // Shortcut (supported one-column key and value).
    Mapper.of(Long.class, "colId");
    // Shortcut (key and value represented by byte array)
    // Does one-column record make sense ??? either one-column table ???
    Mapper.of(UserObject.class, "colData");
    // Keys, Values, and Records
    Mapper.builder(Employee.class).map("fieldData", "colData").map("fieldData2", "colData1").build();
    Mapper.builder(Employee.class).map("fieldData", "colData").build();
    Mapper.builder(Employee.class).map("fieldData", "colData", "fieldData2", "colData1").build();
    // Shortcuts (supported keys and values and records).
    Mapper.of(Employee.class, "fieldData", "colData");
    Mapper.of(Employee.class, "fieldData", "colData", "fieldData1", "colData1");
    // Shortcut (supported one-column key and value) with additional transformation.
    Mapper.of(UserObject.class, "data", marsh);
    // (supported one-column key and value and records) with additional transformation.
    Mapper.builder(Employee.class).convert("colData", marsh).map("fieldData", "colData").build();
    // OR another way to do the same
    Mapper.builder(Employee.class).map("fieldData", "colData", marsh).build();
    // Next views shows different approaches to map user objects to columns.
    KeyValueView<Long, Employee> v1 = t.keyValueView(Mapper.of(Long.class), Mapper.of(Employee.class, "fieldData", "colData"));
    KeyValueView<Long, Employee2> v2 = t.keyValueView(Mapper.of(Long.class), Mapper.builder(Employee2.class).convert("colData", marsh).map("fieldData", "colData").build());
    KeyValueView<Long, Employee2> v3 = t.keyValueView(Mapper.of(Long.class, "colId"), Mapper.builder(Employee2.class).map("fieldData", "colData", marsh).build());
    KeyValueView<Long, UserObject> v4 = t.keyValueView(Mapper.of(Long.class, "colId"), Mapper.of(UserObject.class, "colData", marsh));
    KeyValueView<Long, byte[]> v5 = t.keyValueView(Mapper.of(Long.class, "colId"), Mapper.of(byte[].class, "colData"));
    // The values in next operations are equivalent, and lead to the same row value part content.
    v1.put(null, 1L, new Employee());
    v2.put(null, 2L, new Employee2());
    v3.put(null, 3L, new Employee2());
    v4.put(null, 4L, new UserObject());
    v5.put(null, 5L, new byte[] {/* serialized UserObject bytes */
    });
    // Shortcut with classes for simple use-case
    KeyValueView<Long, String> v6 = t.keyValueView(Long.class, String.class);
    // Shortcut with classes for widely used case
    KeyValueView<Long, UserObject> v7 = t.keyValueView(Long.class, // obj.salary -> colSalary
    UserObject.class);
    // do the same as
    KeyValueView<Long, UserObject> v8 = t.keyValueView(Mapper.of(Long.class), // obj.salary -> colSalary
    Mapper.builder(UserObject.class).automap().build());
    KeyValueView<Long, UserObject> v9 = t.keyValueView(Mapper.of(Long.class), // UserObject -> byte[] -> colData
    Mapper.of(UserObject.class, "colData", marsh));
    // Get operations return the same result for all keys for each of row.
    // for 1 in 1..5
    // v1.get(iL) == v1.get(1L);
    // ============================  GET  ===============================================
    new SchemaDescriptor(1, new Column[] { new Column("colId", NativeTypes.INT64, false) }, new Column[] { new Column("colData", NativeTypes.BYTES, true), new Column("colSalary", NativeTypes.BYTES, true) });
    // indistinguishable absent value and null column
    UserObject obj = v4.get(null, 1L);
    // Optional way
    // Optional<UserObject> optionalObj = v4.get(1L); // abuse of Optional type
    // NullableValue way
    NullableValue<UserObject> nullableValue = v4.getNullable(null, 1L);
    // what if user uses this syntax for nullable column?
    UserObject userObject = v4.get(null, 1L);
    // 1. Exception always
    // 2. Exception if column value is null (use getNullable)
    // Get or default
    String str = v6.getOrDefault(null, 1L, "default");
    // ============================  PUT  ===============================================
    v4.put(null, 1L, null);
    v4.remove(null, 1L, null);
}
Also used : SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) Column(org.apache.ignite.internal.schema.Column) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource) Disabled(org.junit.jupiter.api.Disabled)

Example 43 with SchemaDescriptor

use of org.apache.ignite.internal.schema.SchemaDescriptor in project ignite-3 by apache.

the class KeyValueBinaryViewOperationsTest method getAndPut.

@Test
public void getAndPut() {
    SchemaDescriptor schema = schemaDescriptor();
    KeyValueView<Tuple, Tuple> tbl = createTable(schema).keyValueView();
    final Tuple key = Tuple.create().set("id", 1L);
    final Tuple val = Tuple.create().set("val", 11L);
    final Tuple val2 = Tuple.create().set("val", 22L);
    final Tuple val3 = Tuple.create().set("val", 33L);
    assertNull(tbl.get(null, key));
    // Insert new tuple.
    assertNull(tbl.getAndPut(null, key, val));
    assertEqualsValues(schema, val, tbl.get(null, key));
    assertEqualsValues(schema, val, tbl.get(null, Tuple.create().set("id", 1L)));
    assertEqualsValues(schema, val, tbl.getAndPut(null, key, val2));
    assertEqualsValues(schema, val2, tbl.getAndPut(null, key, Tuple.create().set("val", 33L)));
    assertEqualsValues(schema, val3, tbl.get(null, key));
    assertNull(tbl.get(null, Tuple.create().set("id", 2L)));
}
Also used : SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) Tuple(org.apache.ignite.table.Tuple) Test(org.junit.jupiter.api.Test)

Example 44 with SchemaDescriptor

use of org.apache.ignite.internal.schema.SchemaDescriptor in project ignite-3 by apache.

the class KeyValueBinaryViewOperationsTest method removeExact.

@Test
public void removeExact() {
    SchemaDescriptor schema = schemaDescriptor();
    final KeyValueView<Tuple, Tuple> tbl = createTable(schema).keyValueView();
    final Tuple key = Tuple.create().set("id", 1L);
    final Tuple key2 = Tuple.create().set("id", 2L);
    final Tuple val = Tuple.create().set("val", 11L);
    final Tuple val2 = Tuple.create().set("val", 22L);
    // Put KV pair.
    tbl.put(null, key, val);
    assertEqualsValues(schema, val, tbl.get(null, key));
    // Fails to delete KV pair with unexpected value.
    assertFalse(tbl.remove(null, key, val2));
    assertEqualsValues(schema, val, tbl.get(null, key));
    // Delete KV pair with expected value.
    assertTrue(tbl.remove(null, key, val));
    assertNull(tbl.get(null, key));
    // Once again.
    assertFalse(tbl.remove(null, key, val));
    assertNull(tbl.get(null, key));
    // Try to remove non-existed key.
    assertThrows(Exception.class, () -> tbl.remove(null, key, null));
    assertNull(tbl.get(null, key));
    // Put KV pair.
    tbl.put(null, key, val2);
    assertEqualsValues(schema, val2, tbl.get(null, key));
    // Check null value ignored.
    assertThrows(Exception.class, () -> tbl.remove(null, key, null));
    assertEqualsValues(schema, val2, tbl.get(null, key));
    // Delete KV pair with expected value.
    assertTrue(tbl.remove(null, key, val2));
    assertNull(tbl.get(null, key));
    assertFalse(tbl.remove(null, key2, val2));
    assertNull(tbl.get(null, key2));
}
Also used : SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) Tuple(org.apache.ignite.table.Tuple) Test(org.junit.jupiter.api.Test)

Example 45 with SchemaDescriptor

use of org.apache.ignite.internal.schema.SchemaDescriptor in project ignite-3 by apache.

the class KeyValueBinaryViewOperationsTest method getAll.

@Test
public void getAll() {
    SchemaDescriptor schema = schemaDescriptor();
    KeyValueView<Tuple, Tuple> tbl = createTable(schema).keyValueView();
    Tuple key1 = Tuple.create().set("id", 1L);
    Tuple key2 = Tuple.create().set("id", 2L);
    Tuple key3 = Tuple.create().set("id", 3L);
    tbl.putAll(null, Map.of(key1, Tuple.create().set("val", 11L), key3, Tuple.create().set("val", 33L)));
    Map<Tuple, Tuple> res = tbl.getAll(null, List.of(key1, key2, key3));
    assertEquals(2, res.size());
    assertEquals(Tuple.create().set("val", 11L), res.get(key1));
    assertEquals(Tuple.create().set("val", 33L), res.get(key3));
    assertNull(res.get(key2));
}
Also used : SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) Tuple(org.apache.ignite.table.Tuple) Test(org.junit.jupiter.api.Test)

Aggregations

SchemaDescriptor (org.apache.ignite.internal.schema.SchemaDescriptor)105 Column (org.apache.ignite.internal.schema.Column)78 Test (org.junit.jupiter.api.Test)48 Tuple (org.apache.ignite.table.Tuple)35 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)34 Row (org.apache.ignite.internal.schema.row.Row)32 MethodSource (org.junit.jupiter.params.provider.MethodSource)30 BinaryRow (org.apache.ignite.internal.schema.BinaryRow)22 TestObjectWithAllTypes (org.apache.ignite.internal.schema.testobjects.TestObjectWithAllTypes)11 DummySchemaManagerImpl (org.apache.ignite.internal.table.impl.DummySchemaManagerImpl)11 TupleMarshaller (org.apache.ignite.internal.schema.marshaller.TupleMarshaller)10 TupleMarshallerImpl (org.apache.ignite.internal.schema.marshaller.TupleMarshallerImpl)10 List (java.util.List)9 ArrayList (java.util.ArrayList)8 NotNull (org.jetbrains.annotations.NotNull)8 UUID (java.util.UUID)7 Collectors (java.util.stream.Collectors)7 TableImpl (org.apache.ignite.internal.table.TableImpl)7 Random (java.util.Random)6 Map (java.util.Map)5