Search in sources :

Example 41 with BinaryRow

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

the class KeyValueViewImpl method marshal.

/**
 * Marshal key-value pairs.
 *
 * @param pairs Key-value map.
 * @return Binary rows.
 */
@NotNull
public List<BinaryRow> marshal(@NotNull Map<K, V> pairs) {
    final KvMarshaller<K, V> marsh = marshaller(schemaReg.lastSchemaVersion());
    List<BinaryRow> rows = new ArrayList<>(pairs.size());
    try {
        for (Map.Entry<K, V> pair : pairs.entrySet()) {
            final BinaryRow row = marsh.marshal(Objects.requireNonNull(pair.getKey()), pair.getValue());
            rows.add(row);
        }
    } catch (MarshallerException e) {
        throw new IgniteException(e);
    }
    return rows;
}
Also used : MarshallerException(org.apache.ignite.internal.schema.marshaller.MarshallerException) IgniteException(org.apache.ignite.lang.IgniteException) ArrayList(java.util.ArrayList) BinaryRow(org.apache.ignite.internal.schema.BinaryRow) HashMap(java.util.HashMap) Map(java.util.Map) NotNull(org.jetbrains.annotations.NotNull)

Example 42 with BinaryRow

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

the class KeyValueViewImpl method unmarshalValue.

/**
 * Unmarshal value object from given binary row.
 *
 * @param binaryRow Binary row.
 * @return Value object.
 */
private V unmarshalValue(BinaryRow binaryRow) {
    if (binaryRow == null || !binaryRow.hasValue()) {
        return null;
    }
    Row row = schemaReg.resolve(binaryRow);
    KvMarshaller<K, V> marshaller = marshaller(row.schemaVersion());
    try {
        return marshaller.unmarshalValue(row);
    } catch (MarshallerException e) {
        throw new IgniteException(e);
    }
}
Also used : MarshallerException(org.apache.ignite.internal.schema.marshaller.MarshallerException) IgniteException(org.apache.ignite.lang.IgniteException) BinaryRow(org.apache.ignite.internal.schema.BinaryRow) Row(org.apache.ignite.internal.schema.row.Row)

Example 43 with BinaryRow

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

the class TxAbstractTest method testScan.

@Test
public void testScan() throws InterruptedException {
    accounts.recordView().upsertAll(null, List.of(makeValue(1, 100.), makeValue(2, 200.)));
    Flow.Publisher<BinaryRow> pub = ((TableImpl) accounts).internalTable().scan(0, null);
    List<Tuple> rows = new ArrayList<>();
    CountDownLatch l = new CountDownLatch(1);
    pub.subscribe(new Flow.Subscriber<BinaryRow>() {

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

        @Override
        public void onNext(BinaryRow item) {
            Row row = ((TableImpl) accounts).schemaView().resolve(item);
            rows.add(TableRow.tuple(row));
        }

        @Override
        public void onError(Throwable throwable) {
        // No-op.
        }

        @Override
        public void onComplete() {
            l.countDown();
        }
    });
    assertTrue(l.await(5_000, TimeUnit.MILLISECONDS));
    Map<Long, Tuple> map = new HashMap<>();
    for (Tuple row : rows) {
        map.put(row.longValue("accountNumber"), row);
    }
    assertEquals(100., map.get(1L).doubleValue("balance"));
    assertEquals(200., map.get(2L).doubleValue("balance"));
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) BinaryRow(org.apache.ignite.internal.schema.BinaryRow) CountDownLatch(java.util.concurrent.CountDownLatch) Flow(java.util.concurrent.Flow) Row(org.apache.ignite.internal.schema.row.Row) BinaryRow(org.apache.ignite.internal.schema.BinaryRow) Tuple(org.apache.ignite.table.Tuple) Test(org.junit.jupiter.api.Test) IgniteAbstractTest(org.apache.ignite.internal.testframework.IgniteAbstractTest)

Example 44 with BinaryRow

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

the class ItInternalTableScanTest method testExceptionRowScan.

/**
 * Checks that exception from storage cursor creation properly propagates to subscriber.
 */
@Test
public void testExceptionRowScan() throws Exception {
    // The latch that allows to await Subscriber.onError() before asserting test invariants.
    CountDownLatch gotExceptionLatch = new CountDownLatch(1);
    AtomicReference<Throwable> gotException = new AtomicReference<>();
    when(mockStorage.scan(any())).thenThrow(new StorageException("Some storage exception"));
    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);
            gotExceptionLatch.countDown();
        }

        @Override
        public void onComplete() {
            fail("Should never get here.");
        }
    });
    gotExceptionLatch.await();
    assertEquals(gotException.get().getCause().getClass(), StorageException.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) StorageException(org.apache.ignite.internal.storage.StorageException) Test(org.junit.jupiter.api.Test)

Example 45 with BinaryRow

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

the class KeyValueBinaryViewImpl method getAllAsync.

/**
 * {@inheritDoc}
 */
@Override
@NotNull
public CompletableFuture<Map<Tuple, Tuple>> getAllAsync(@Nullable Transaction tx, @NotNull Collection<Tuple> keys) {
    Objects.requireNonNull(keys);
    List<BinaryRow> keyRows = new ArrayList<>(keys.size());
    for (Tuple keyRec : keys) {
        final Row keyRow = marshal(keyRec, null);
        keyRows.add(keyRow);
    }
    return tbl.getAll(keyRows, (InternalTransaction) tx).thenApply(ts -> ts.stream().filter(Objects::nonNull).filter(BinaryRow::hasValue).map(this::wrap).collect(Collectors.toMap(TableRow::keyTuple, TableRow::valueTuple)));
}
Also used : ArrayList(java.util.ArrayList) Objects(java.util.Objects) BinaryRow(org.apache.ignite.internal.schema.BinaryRow) BinaryRow(org.apache.ignite.internal.schema.BinaryRow) Row(org.apache.ignite.internal.schema.row.Row) InternalTransaction(org.apache.ignite.internal.tx.InternalTransaction) Tuple(org.apache.ignite.table.Tuple) NotNull(org.jetbrains.annotations.NotNull)

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