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)));
}
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);
}
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)));
}
}
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);
}
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);
}
Aggregations