Search in sources :

Example 1 with AsmMarshallerGenerator

use of org.apache.ignite.internal.schema.marshaller.asm.AsmMarshallerGenerator in project ignite-3 by apache.

the class KvMarshallerTest method columnNameMapping.

@ParameterizedTest
@MethodSource("marshallerFactoryProvider")
public void columnNameMapping(MarshallerFactory factory) throws MarshallerException {
    Assumptions.assumeFalse(factory instanceof AsmMarshallerGenerator, "Generated marshaller doesn't support column mapping, yet.");
    SchemaDescriptor schema = new SchemaDescriptor(1, new Column[] { new Column("key".toUpperCase(), INT64, false) }, new Column[] { new Column("col1".toUpperCase(), INT64, false), new Column("col2".toUpperCase(), INT64, true), new Column("col3".toUpperCase(), STRING, false) });
    Mapper<TestKeyObject> keyMapper = Mapper.builder(TestKeyObject.class).map("id", "key").build();
    Mapper<TestObject> valMapper = Mapper.builder(TestObject.class).map("longCol", "col1").map("stringCol", "col3").build();
    KvMarshaller<TestKeyObject, TestObject> marshaller = factory.create(schema, keyMapper, valMapper);
    final TestKeyObject key = TestKeyObject.randomObject(rnd);
    final TestObject val = TestObject.randomObject(rnd);
    BinaryRow row = marshaller.marshal(key, val);
    Object restoredVal = marshaller.unmarshalValue(new Row(schema, row));
    Object restoredKey = marshaller.unmarshalKey(new Row(schema, row));
    assertTrue(key.getClass().isInstance(restoredKey));
    assertTrue(val.getClass().isInstance(restoredVal));
    val.longCol2 = null;
    assertEquals(key, restoredKey);
    assertEquals(val, restoredVal);
}
Also used : SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) Column(org.apache.ignite.internal.schema.Column) AsmMarshallerGenerator(org.apache.ignite.internal.schema.marshaller.asm.AsmMarshallerGenerator) BinaryRow(org.apache.ignite.internal.schema.BinaryRow) Row(org.apache.ignite.internal.schema.row.Row) BinaryRow(org.apache.ignite.internal.schema.BinaryRow) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 2 with AsmMarshallerGenerator

use of org.apache.ignite.internal.schema.marshaller.asm.AsmMarshallerGenerator in project ignite-3 by apache.

the class KvMarshallerTest method pojoMapping.

@ParameterizedTest
@MethodSource("marshallerFactoryProvider")
public void pojoMapping(MarshallerFactory factory) throws MarshallerException, IOException {
    Assumptions.assumeFalse(factory instanceof AsmMarshallerGenerator, "Generated marshaller doesn't support column mapping.");
    final SchemaDescriptor schema = new SchemaDescriptor(1, new Column[] { new Column("key", INT64, false) }, new Column[] { new Column("val", BYTES, true) });
    final TestPojo pojo = new TestPojo(42);
    final byte[] serializedPojo = serializeObject(pojo);
    final KvMarshaller<Long, TestPojo> marshaller1 = factory.create(schema, Mapper.of(Long.class, "\"key\""), Mapper.of(TestPojo.class, "\"val\"", new SerializingConverter<>()));
    final KvMarshaller<Long, byte[]> marshaller2 = factory.create(schema, Mapper.of(Long.class, "\"key\""), Mapper.of(byte[].class, "\"val\""));
    final KvMarshaller<Long, TestPojoWrapper> marshaller3 = factory.create(schema, Mapper.of(Long.class, "\"key\""), Mapper.builder(TestPojoWrapper.class).map("pojoField", "\"val\"", new SerializingConverter<>()).build());
    final KvMarshaller<Long, TestPojoWrapper> marshaller4 = factory.create(schema, Mapper.of(Long.class, "\"key\""), Mapper.builder(TestPojoWrapper.class).map("rawField", "\"val\"").build());
    BinaryRow row = marshaller1.marshal(1L, pojo);
    BinaryRow row2 = marshaller2.marshal(1L, serializedPojo);
    BinaryRow row3 = marshaller3.marshal(1L, new TestPojoWrapper(pojo));
    BinaryRow row4 = marshaller4.marshal(1L, new TestPojoWrapper(serializedPojo));
    // Verify all rows are equivalent.
    assertArrayEquals(row.bytes(), row2.bytes());
    assertArrayEquals(row.bytes(), row3.bytes());
    assertArrayEquals(row.bytes(), row4.bytes());
    // Check key.
    assertEquals(1L, marshaller1.unmarshalKey(new Row(schema, row)));
    assertEquals(1L, marshaller2.unmarshalKey(new Row(schema, row)));
    assertEquals(1L, marshaller3.unmarshalKey(new Row(schema, row)));
    assertEquals(1L, marshaller4.unmarshalKey(new Row(schema, row)));
    // Check values.
    assertEquals(pojo, marshaller1.unmarshalValue(new Row(schema, row)));
    assertArrayEquals(serializedPojo, marshaller2.unmarshalValue(new Row(schema, row)));
    assertEquals(new TestPojoWrapper(pojo), marshaller3.unmarshalValue(new Row(schema, row)));
    assertEquals(new TestPojoWrapper(serializedPojo), marshaller4.unmarshalValue(new Row(schema, row)));
}
Also used : SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) BinaryRow(org.apache.ignite.internal.schema.BinaryRow) Column(org.apache.ignite.internal.schema.Column) AsmMarshallerGenerator(org.apache.ignite.internal.schema.marshaller.asm.AsmMarshallerGenerator) SerializingConverter(org.apache.ignite.internal.schema.marshaller.reflection.SerializingConverter) Row(org.apache.ignite.internal.schema.row.Row) BinaryRow(org.apache.ignite.internal.schema.BinaryRow) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 3 with AsmMarshallerGenerator

use of org.apache.ignite.internal.schema.marshaller.asm.AsmMarshallerGenerator in project ignite-3 by apache.

the class KvMarshallerTest method narrowType.

@ParameterizedTest
@MethodSource("marshallerFactoryProvider")
public void narrowType(MarshallerFactory factory) throws MarshallerException {
    Assumptions.assumeFalse(factory instanceof AsmMarshallerGenerator, "Generated marshaller doesn't support truncated values, yet.");
    Column[] cols = new Column[] { new Column("primitiveIntCol".toUpperCase(), INT32, false), new Column("primitiveLongCol".toUpperCase(), INT64, false), new Column("primitiveFloatCol".toUpperCase(), FLOAT, false), new Column("primitiveDoubleCol".toUpperCase(), DOUBLE, false), new Column("stringCol".toUpperCase(), STRING, false), new Column("uuidCol".toUpperCase(), UUID, false) };
    SchemaDescriptor schema = new SchemaDescriptor(1, cols, columnsAllTypes(true));
    KvMarshaller<TestTruncatedObject, TestTruncatedObject> marshaller = factory.create(schema, TestTruncatedObject.class, TestTruncatedObject.class);
    final TestTruncatedObject key = TestTruncatedObject.randomObject(rnd);
    final TestTruncatedObject val = TestTruncatedObject.randomObject(rnd);
    BinaryRow row = marshaller.marshal(key, val);
    Object restoredVal = marshaller.unmarshalValue(new Row(schema, row));
    Object restoredKey = marshaller.unmarshalKey(new Row(schema, row));
    assertTrue(key.getClass().isInstance(restoredKey));
    assertTrue(val.getClass().isInstance(restoredVal));
    assertEquals(key, restoredKey);
    assertEquals(val, restoredVal);
}
Also used : SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) Column(org.apache.ignite.internal.schema.Column) AsmMarshallerGenerator(org.apache.ignite.internal.schema.marshaller.asm.AsmMarshallerGenerator) BinaryRow(org.apache.ignite.internal.schema.BinaryRow) Row(org.apache.ignite.internal.schema.row.Row) BinaryRow(org.apache.ignite.internal.schema.BinaryRow) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 4 with AsmMarshallerGenerator

use of org.apache.ignite.internal.schema.marshaller.asm.AsmMarshallerGenerator in project ignite-3 by apache.

the class SerializerBenchmarkTest method init.

/**
 * Initialize.
 */
@Setup
public void init() {
    long seed = System.currentTimeMillis();
    rnd = new Random(seed);
    final Class<?> valClass;
    Thread.currentThread().setContextClassLoader(new DynamicClassLoader(AsmMarshallerGenerator.getClassLoader()));
    if (fieldsCount == 0) {
        valClass = Long.class;
        objectFactory = (Factory<Object>) rnd::nextLong;
    } else {
        valClass = createGeneratedObjectClass(fieldsCount, long.class);
        objectFactory = new ObjectFactory<>(valClass);
    }
    schema = new SchemaDescriptor(1, new Column[] { new Column("key", INT64, true) }, mapFieldsToColumns(valClass));
    KvMarshaller<?, ?> marshaller = ("Java".equals(serializerName)) ? new ReflectionMarshallerFactory().create(schema, Long.class, valClass) : new AsmMarshallerGenerator().create(schema, Long.class, valClass);
    this.marshaller = (KvMarshaller<Object, Object>) marshaller;
}
Also used : DynamicClassLoader(com.facebook.presto.bytecode.DynamicClassLoader) SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) Random(java.util.Random) Column(org.apache.ignite.internal.schema.Column) ReflectionMarshallerFactory(org.apache.ignite.internal.schema.marshaller.reflection.ReflectionMarshallerFactory) AsmMarshallerGenerator(org.apache.ignite.internal.schema.marshaller.asm.AsmMarshallerGenerator) Setup(org.openjdk.jmh.annotations.Setup)

Aggregations

Column (org.apache.ignite.internal.schema.Column)4 SchemaDescriptor (org.apache.ignite.internal.schema.SchemaDescriptor)4 AsmMarshallerGenerator (org.apache.ignite.internal.schema.marshaller.asm.AsmMarshallerGenerator)4 BinaryRow (org.apache.ignite.internal.schema.BinaryRow)3 Row (org.apache.ignite.internal.schema.row.Row)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 MethodSource (org.junit.jupiter.params.provider.MethodSource)3 DynamicClassLoader (com.facebook.presto.bytecode.DynamicClassLoader)1 Random (java.util.Random)1 ReflectionMarshallerFactory (org.apache.ignite.internal.schema.marshaller.reflection.ReflectionMarshallerFactory)1 SerializingConverter (org.apache.ignite.internal.schema.marshaller.reflection.SerializingConverter)1 Setup (org.openjdk.jmh.annotations.Setup)1