Search in sources :

Example 36 with BinaryRow

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

the class InternalTableImpl method enlistInTx.

/**
 * Enlists multiple rows into a transaction.
 *
 * @param keyRows Rows.
 * @param tx The transaction.
 * @param op Command factory.
 * @param reducer The reducer.
 * @param <R> Reducer's input.
 * @param <T> Reducer's output.
 * @return The future.
 */
private <R, T> CompletableFuture<T> enlistInTx(Collection<BinaryRow> keyRows, InternalTransaction tx, BiFunction<Collection<BinaryRow>, InternalTransaction, Command> op, Function<CompletableFuture<R>[], CompletableFuture<T>> reducer) {
    final boolean implicit = tx == null;
    final InternalTransaction tx0 = implicit ? txManager.begin() : tx;
    Int2ObjectOpenHashMap<List<BinaryRow>> keyRowsByPartition = mapRowsToPartitions(keyRows);
    CompletableFuture<R>[] futures = new CompletableFuture[keyRowsByPartition.size()];
    int batchNum = 0;
    for (Int2ObjectOpenHashMap.Entry<List<BinaryRow>> partToRows : keyRowsByPartition.int2ObjectEntrySet()) {
        CompletableFuture<RaftGroupService> fut = enlist(partToRows.getIntKey(), tx0);
        futures[batchNum++] = fut.thenCompose(svc -> svc.run(op.apply(partToRows.getValue(), tx0)));
    }
    CompletableFuture<T> fut = reducer.apply(futures);
    return postEnlist(fut, implicit, tx0);
}
Also used : DeleteCommand(org.apache.ignite.internal.table.distributed.command.DeleteCommand) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Subscription(java.util.concurrent.Flow.Subscription) BiFunction(java.util.function.BiFunction) InsertCommand(org.apache.ignite.internal.table.distributed.command.InsertCommand) DeleteAllCommand(org.apache.ignite.internal.table.distributed.command.DeleteAllCommand) DeleteExactAllCommand(org.apache.ignite.internal.table.distributed.command.DeleteExactAllCommand) IgniteLogger(org.apache.ignite.lang.IgniteLogger) GetAndDeleteCommand(org.apache.ignite.internal.table.distributed.command.GetAndDeleteCommand) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) Map(java.util.Map) ScanRetrieveBatchCommand(org.apache.ignite.internal.table.distributed.command.scan.ScanRetrieveBatchCommand) GetAllCommand(org.apache.ignite.internal.table.distributed.command.GetAllCommand) Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) ScanCloseCommand(org.apache.ignite.internal.table.distributed.command.scan.ScanCloseCommand) Collection(java.util.Collection) UUID(java.util.UUID) ReplaceCommand(org.apache.ignite.internal.table.distributed.command.ReplaceCommand) Collectors(java.util.stream.Collectors) Command(org.apache.ignite.raft.client.Command) TxManager(org.apache.ignite.internal.tx.TxManager) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) IgniteStringFormatter(org.apache.ignite.lang.IgniteStringFormatter) NotNull(org.jetbrains.annotations.NotNull) GetAndReplaceCommand(org.apache.ignite.internal.table.distributed.command.GetAndReplaceCommand) ScanInitCommand(org.apache.ignite.internal.table.distributed.command.scan.ScanInitCommand) BinaryRow(org.apache.ignite.internal.schema.BinaryRow) InternalTable(org.apache.ignite.internal.table.InternalTable) UpsertCommand(org.apache.ignite.internal.table.distributed.command.UpsertCommand) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) ArrayList(java.util.ArrayList) IgniteUuidGenerator(org.apache.ignite.lang.IgniteUuidGenerator) Publisher(java.util.concurrent.Flow.Publisher) GetAndUpsertCommand(org.apache.ignite.internal.table.distributed.command.GetAndUpsertCommand) DeleteExactCommand(org.apache.ignite.internal.table.distributed.command.DeleteExactCommand) InternalTransaction(org.apache.ignite.internal.tx.InternalTransaction) ReplaceIfExistCommand(org.apache.ignite.internal.table.distributed.command.ReplaceIfExistCommand) MultiRowsResponse(org.apache.ignite.internal.table.distributed.command.response.MultiRowsResponse) TableStorage(org.apache.ignite.internal.storage.engine.TableStorage) NetworkAddress(org.apache.ignite.network.NetworkAddress) TestOnly(org.jetbrains.annotations.TestOnly) Peer(org.apache.ignite.raft.client.Peer) Subscriber(java.util.concurrent.Flow.Subscriber) GetCommand(org.apache.ignite.internal.table.distributed.command.GetCommand) Int2ObjectMap(it.unimi.dsi.fastutil.ints.Int2ObjectMap) UpsertAllCommand(org.apache.ignite.internal.table.distributed.command.UpsertAllCommand) InsertAllCommand(org.apache.ignite.internal.table.distributed.command.InsertAllCommand) Comparator(java.util.Comparator) SingleRowResponse(org.apache.ignite.internal.table.distributed.command.response.SingleRowResponse) IgniteUuid(org.apache.ignite.lang.IgniteUuid) Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) InternalTransaction(org.apache.ignite.internal.tx.InternalTransaction) CompletableFuture(java.util.concurrent.CompletableFuture) List(java.util.List) ArrayList(java.util.ArrayList)

Example 37 with BinaryRow

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

the class InternalTableImpl method collectMultiRowsResponses.

/**
 * TODO asch keep the same order as for keys Collects multirow responses from multiple futures into a single collection IGNITE-16004.
 *
 * @param futs Futures.
 * @return Row collection.
 */
private CompletableFuture<Collection<BinaryRow>> collectMultiRowsResponses(CompletableFuture<?>[] futs) {
    return CompletableFuture.allOf(futs).thenApply(response -> {
        List<BinaryRow> list = new ArrayList<>(futs.length);
        for (CompletableFuture<?> future : futs) {
            MultiRowsResponse ret = (MultiRowsResponse) future.join();
            List<BinaryRow> values = ret.getValues();
            if (values != null) {
                list.addAll(values);
            }
        }
        return list;
    });
}
Also used : MultiRowsResponse(org.apache.ignite.internal.table.distributed.command.response.MultiRowsResponse) ArrayList(java.util.ArrayList) BinaryRow(org.apache.ignite.internal.schema.BinaryRow)

Example 38 with BinaryRow

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

the class VersionedRowStore method resolve.

/**
 * Resolves a multi-versioned value depending on a viewer's timestamp.
 *
 * @param val        The value.
 * @param timestamp  The timestamp
 * @return New and old rows pair.
 * @see #versionedRow
 */
private Pair<BinaryRow, BinaryRow> resolve(Value val, Timestamp timestamp) {
    if (val.timestamp == null) {
        // New or after reset.
        assert val.oldRow == null : val;
        return new Pair<>(val.newRow, null);
    }
    // Checks "inTx" condition. Will be false if this is a first transactional op.
    if (val.timestamp.equals(timestamp)) {
        return new Pair<>(val.newRow, val.oldRow);
    }
    TxState state = txManager.state(val.timestamp);
    BinaryRow cur;
    if (state == TxState.ABORTED) {
        // Was aborted and had written a temp value.
        cur = val.oldRow;
    } else {
        cur = val.newRow;
    }
    return new Pair<>(cur, cur);
}
Also used : TxState(org.apache.ignite.internal.tx.TxState) BinaryRow(org.apache.ignite.internal.schema.BinaryRow) Pair(org.apache.ignite.internal.util.Pair)

Example 39 with BinaryRow

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

the class VersionedRowStore method getAndUpsert.

/**
 * Upserts a row and returns previous value.
 *
 * @param row The row.
 * @param ts The timestamp.
 * @return Previous row.
 */
@Nullable
public BinaryRow getAndUpsert(@NotNull BinaryRow row, Timestamp ts) {
    assert row != null;
    BinaryRow oldRow = get(row, ts);
    upsert(row, ts);
    return oldRow != null ? oldRow : null;
}
Also used : BinaryRow(org.apache.ignite.internal.schema.BinaryRow) Nullable(org.jetbrains.annotations.Nullable)

Example 40 with BinaryRow

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

the class KeyValueViewImpl method unmarshalKeys.

/**
 * Marshal keys.
 *
 * @param rows Binary rows.
 * @return Keys.
 */
@NotNull
public Collection<K> unmarshalKeys(Collection<BinaryRow> rows) {
    if (rows.isEmpty()) {
        return Collections.emptyList();
    }
    final KvMarshaller<K, V> marsh = marshaller(schemaReg.lastSchemaVersion());
    List<K> keys = new ArrayList<>(rows.size());
    try {
        for (Row row : schemaReg.resolve(rows)) {
            if (row != null) {
                keys.add(marsh.unmarshalKey(row));
            }
        }
        return keys;
    } catch (MarshallerException e) {
        throw new IgniteException(e);
    }
}
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) Row(org.apache.ignite.internal.schema.row.Row) 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