Search in sources :

Example 11 with BinaryRow

use of org.apache.ignite.internal.schema.BinaryRow 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 12 with BinaryRow

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

the class KvMarshallerTest method classWithPrivateConstructor.

@ParameterizedTest
@MethodSource("marshallerFactoryProvider")
public void classWithPrivateConstructor(MarshallerFactory factory) throws MarshallerException {
    Column[] cols = new Column[] { new Column("primLongCol".toUpperCase(), INT64, false), new Column("primIntCol".toUpperCase(), INT32, false) };
    SchemaDescriptor schema = new SchemaDescriptor(1, cols, cols);
    KvMarshaller<TestObjectWithPrivateConstructor, TestObjectWithPrivateConstructor> marshaller = factory.create(schema, TestObjectWithPrivateConstructor.class, TestObjectWithPrivateConstructor.class);
    final TestObjectWithPrivateConstructor key = TestObjectWithPrivateConstructor.randomObject(rnd);
    final TestObjectWithPrivateConstructor val = TestObjectWithPrivateConstructor.randomObject(rnd);
    BinaryRow row = marshaller.marshal(key, val);
    Object key1 = marshaller.unmarshalKey(new Row(schema, row));
    Object val1 = marshaller.unmarshalValue(new Row(schema, row));
    assertTrue(key.getClass().isInstance(key1));
    assertTrue(val.getClass().isInstance(val1));
    assertEquals(key, key);
    assertEquals(val, val1);
}
Also used : SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) Column(org.apache.ignite.internal.schema.Column) BinaryRow(org.apache.ignite.internal.schema.BinaryRow) Row(org.apache.ignite.internal.schema.row.Row) BinaryRow(org.apache.ignite.internal.schema.BinaryRow) TestObjectWithPrivateConstructor(org.apache.ignite.internal.schema.testobjects.TestObjectWithPrivateConstructor) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 13 with BinaryRow

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

the class ItInternalTableScanTest method requestNtest.

/**
 * Checks whether publisher provides all existing data and then completes if requested by reqAmount rows at a time.
 *
 * @param submittedItems Items to be pushed by publisher.
 * @param reqAmount      Amount of rows to request at a time.
 * @throws Exception If Any.
 */
private void requestNtest(List<DataRow> submittedItems, int reqAmount) throws Exception {
    AtomicInteger cursorTouchCnt = new AtomicInteger(0);
    List<BinaryRow> retrievedItems = Collections.synchronizedList(new ArrayList<>());
    when(mockStorage.scan(any())).thenAnswer(invocation -> {
        var cursor = mock(Cursor.class);
        when(cursor.hasNext()).thenAnswer(hnInvocation -> cursorTouchCnt.get() < submittedItems.size());
        when(cursor.next()).thenAnswer(ninvocation -> submittedItems.get(cursorTouchCnt.getAndIncrement()));
        return cursor;
    });
    // The latch that allows to await Subscriber.onError() before asserting test invariants.
    CountDownLatch subscriberAllDataAwaitLatch = new CountDownLatch(1);
    internalTbl.scan(0, null).subscribe(new Subscriber<>() {

        private Subscription subscription;

        @Override
        public void onSubscribe(Subscription subscription) {
            this.subscription = subscription;
            subscription.request(reqAmount);
        }

        @Override
        public void onNext(BinaryRow item) {
            retrievedItems.add(item);
            if (retrievedItems.size() % reqAmount == 0) {
                subscription.request(reqAmount);
            }
        }

        @Override
        public void onError(Throwable throwable) {
            fail("onError call is not expected.");
        }

        @Override
        public void onComplete() {
            subscriberAllDataAwaitLatch.countDown();
        }
    });
    subscriberAllDataAwaitLatch.await();
    assertEquals(submittedItems.size(), retrievedItems.size());
    List<byte[]> expItems = submittedItems.stream().map(DataRow::valueBytes).collect(Collectors.toList());
    List<byte[]> gotItems = retrievedItems.stream().map(BinaryRow::bytes).collect(Collectors.toList());
    for (int i = 0; i < expItems.size(); i++) {
        assertTrue(Arrays.equals(expItems.get(i), gotItems.get(i)));
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BinaryRow(org.apache.ignite.internal.schema.BinaryRow) CountDownLatch(java.util.concurrent.CountDownLatch) Subscription(java.util.concurrent.Flow.Subscription)

Example 14 with BinaryRow

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

the class ItInternalTableScanTest method testSecondSubscriptionFiresIllegalStateException.

/**
 * Checks that in case of second subscription {@link IllegalStateException} will be fires to onError.
 *
 * @throws Exception If any.
 */
@Test
public void testSecondSubscriptionFiresIllegalStateException() throws Exception {
    Flow.Publisher<BinaryRow> scan = internalTbl.scan(0, null);
    scan.subscribe(new Subscriber<>() {

        @Override
        public void onSubscribe(Subscription subscription) {
        }

        @Override
        public void onNext(BinaryRow item) {
        }

        @Override
        public void onError(Throwable throwable) {
        }

        @Override
        public void onComplete() {
        }
    });
    // The latch that allows to await Subscriber.onError() before asserting test invariants.
    CountDownLatch gotExceptionLatch = new CountDownLatch(1);
    AtomicReference<Throwable> gotException = new AtomicReference<>();
    scan.subscribe(new Subscriber<>() {

        @Override
        public void onSubscribe(Subscription subscription) {
        }

        @Override
        public void onNext(BinaryRow item) {
        }

        @Override
        public void onError(Throwable throwable) {
            gotException.set(throwable);
            gotExceptionLatch.countDown();
        }

        @Override
        public void onComplete() {
        }
    });
    gotExceptionLatch.await();
    assertEquals(gotException.get().getClass(), IllegalStateException.class);
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) BinaryRow(org.apache.ignite.internal.schema.BinaryRow) Subscription(java.util.concurrent.Flow.Subscription) CountDownLatch(java.util.concurrent.CountDownLatch) Flow(java.util.concurrent.Flow) Test(org.junit.jupiter.api.Test)

Example 15 with BinaryRow

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

the class ItInternalTableScanTest method testExceptionRowScanCursorHasNext.

/**
 * Checks that exception from storage cursors has next properly propagates to subscriber.
 */
@Test
public void testExceptionRowScanCursorHasNext() throws Exception {
    // The latch that allows to await Subscriber.onComplete() before asserting test invariants
    // and avoids the race between closing the cursor and stopping the node.
    CountDownLatch subscriberFinishedLatch = new CountDownLatch(2);
    AtomicReference<Throwable> gotException = new AtomicReference<>();
    when(mockStorage.scan(any())).thenAnswer(invocation -> {
        var cursor = mock(Cursor.class);
        when(cursor.hasNext()).thenAnswer(hnInvocation -> true);
        when(cursor.next()).thenAnswer(hnInvocation -> {
            throw new NoSuchElementException("test");
        });
        doAnswer(invocationClose -> {
            subscriberFinishedLatch.countDown();
            return null;
        }).when(cursor).close();
        return cursor;
    });
    internalTbl.scan(0, null).subscribe(new Subscriber<>() {

        @Override
        public void onSubscribe(Subscription subscription) {
            subscription.request(1);
        }

        @Override
        public void onNext(BinaryRow item) {
            fail("Should never get here.");
        }

        @Override
        public void onError(Throwable throwable) {
            gotException.set(throwable);
            subscriberFinishedLatch.countDown();
        }

        @Override
        public void onComplete() {
            fail("Should never get here.");
        }
    });
    subscriberFinishedLatch.await();
    assertEquals(gotException.get().getCause().getClass(), NoSuchElementException.class);
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) BinaryRow(org.apache.ignite.internal.schema.BinaryRow) CountDownLatch(java.util.concurrent.CountDownLatch) Subscription(java.util.concurrent.Flow.Subscription) NoSuchElementException(java.util.NoSuchElementException) Test(org.junit.jupiter.api.Test)

Aggregations

BinaryRow (org.apache.ignite.internal.schema.BinaryRow)57 Row (org.apache.ignite.internal.schema.row.Row)34 SchemaDescriptor (org.apache.ignite.internal.schema.SchemaDescriptor)22 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)19 MethodSource (org.junit.jupiter.params.provider.MethodSource)19 Column (org.apache.ignite.internal.schema.Column)18 ArrayList (java.util.ArrayList)13 NotNull (org.jetbrains.annotations.NotNull)11 IgniteException (org.apache.ignite.lang.IgniteException)10 MarshallerException (org.apache.ignite.internal.schema.marshaller.MarshallerException)9 CountDownLatch (java.util.concurrent.CountDownLatch)6 Subscription (java.util.concurrent.Flow.Subscription)6 Test (org.junit.jupiter.api.Test)5 Map (java.util.Map)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 TestObjectWithAllTypes (org.apache.ignite.internal.schema.testobjects.TestObjectWithAllTypes)4 HashMap (java.util.HashMap)3 UUID (java.util.UUID)3 Flow (java.util.concurrent.Flow)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3