Search in sources :

Example 1 with InternalTransaction

use of org.apache.ignite.internal.tx.InternalTransaction in project ignite-3 by apache.

the class KeyValueBinaryViewImpl method removeAllAsync.

/**
 * {@inheritDoc}
 */
@Override
@NotNull
public CompletableFuture<Collection<Tuple>> removeAllAsync(@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.deleteAll(keyRows, (InternalTransaction) tx).thenApply(t -> t.stream().filter(Objects::nonNull).map(this::wrap).map(TableRow::valueTuple).collect(Collectors.toList()));
}
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)

Example 2 with InternalTransaction

use of org.apache.ignite.internal.tx.InternalTransaction in project ignite-3 by apache.

the class RecordBinaryViewImpl method getAsync.

/**
 * {@inheritDoc}
 */
@Override
@NotNull
public CompletableFuture<Tuple> getAsync(@Nullable Transaction tx, @NotNull Tuple keyRec) {
    Objects.requireNonNull(keyRec);
    // Convert to portable format to pass TX/storage layer.
    final Row keyRow = marshal(keyRec, true);
    return tbl.get(keyRow, (InternalTransaction) tx).thenApply(this::wrap);
}
Also used : BinaryRow(org.apache.ignite.internal.schema.BinaryRow) Row(org.apache.ignite.internal.schema.row.Row) InternalTransaction(org.apache.ignite.internal.tx.InternalTransaction) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with InternalTransaction

use of org.apache.ignite.internal.tx.InternalTransaction in project ignite-3 by apache.

the class TxAbstractTest method testGetAllConflict.

/**
 * Tests if a transaction is rolled back if one of the batch keys can't be locked.
 */
@Test
public void testGetAllConflict() throws Exception {
    accounts.recordView().upsertAll(null, List.of(makeValue(1, 100.), makeValue(2, 200.)));
    InternalTransaction tx = (InternalTransaction) igniteTransactions.begin();
    InternalTransaction tx2 = (InternalTransaction) igniteTransactions.begin();
    RecordView<Tuple> txAcc = accounts.recordView();
    RecordView<Tuple> txAcc2 = accounts.recordView();
    txAcc2.upsert(tx2, makeValue(1, 300.));
    txAcc.upsert(tx, makeValue(2, 400.));
    Exception err = assertThrows(Exception.class, () -> txAcc.getAll(tx, List.of(makeKey(2), makeKey(1))));
    assertTrue(err.getMessage().contains("Failed to acquire a lock"), err.getMessage());
    validateBalance(txAcc2.getAll(tx2, List.of(makeKey(2), makeKey(1))), 200., 300.);
    validateBalance(txAcc2.getAll(tx2, List.of(makeKey(1), makeKey(2))), 300., 200.);
    assertTrue(IgniteTestUtils.waitForCondition(() -> TxState.ABORTED == tx.state(), 5_000), tx.state().toString());
    tx2.commit();
    validateBalance(accounts.recordView().getAll(null, List.of(makeKey(2), makeKey(1))), 200., 300.);
}
Also used : InternalTransaction(org.apache.ignite.internal.tx.InternalTransaction) Tuple(org.apache.ignite.table.Tuple) TransactionException(org.apache.ignite.tx.TransactionException) IgniteException(org.apache.ignite.lang.IgniteException) Test(org.junit.jupiter.api.Test) IgniteAbstractTest(org.apache.ignite.internal.testframework.IgniteAbstractTest)

Example 4 with InternalTransaction

use of org.apache.ignite.internal.tx.InternalTransaction in project ignite-3 by apache.

the class TxAbstractTest method testCommit.

@Test
public void testCommit() throws TransactionException {
    InternalTransaction tx = (InternalTransaction) igniteTransactions.begin();
    Tuple key = makeKey(1);
    var table = accounts.recordView();
    table.upsert(tx, makeValue(1, 100.));
    assertEquals(100., table.get(tx, key).doubleValue("balance"));
    table.upsert(tx, makeValue(1, 200.));
    assertEquals(200., table.get(tx, key).doubleValue("balance"));
    tx.commit();
    assertEquals(200., accounts.recordView().get(null, key).doubleValue("balance"));
    assertEquals(COMMITED, txManager(accounts).state(tx.timestamp()));
}
Also used : InternalTransaction(org.apache.ignite.internal.tx.InternalTransaction) Tuple(org.apache.ignite.table.Tuple) Test(org.junit.jupiter.api.Test) IgniteAbstractTest(org.apache.ignite.internal.testframework.IgniteAbstractTest)

Example 5 with InternalTransaction

use of org.apache.ignite.internal.tx.InternalTransaction in project ignite-3 by apache.

the class TxAbstractTest method testAbort.

@Test
public void testAbort() throws TransactionException {
    InternalTransaction tx = (InternalTransaction) igniteTransactions.begin();
    Tuple key = makeKey(1);
    var table = accounts.recordView();
    table.upsert(tx, makeValue(1, 100.));
    assertEquals(100., table.get(tx, key).doubleValue("balance"));
    table.upsert(tx, makeValue(1, 200.));
    assertEquals(200., table.get(tx, key).doubleValue("balance"));
    tx.rollback();
    assertNull(accounts.recordView().get(null, key));
    assertEquals(ABORTED, txManager(accounts).state(tx.timestamp()));
}
Also used : InternalTransaction(org.apache.ignite.internal.tx.InternalTransaction) Tuple(org.apache.ignite.table.Tuple) Test(org.junit.jupiter.api.Test) IgniteAbstractTest(org.apache.ignite.internal.testframework.IgniteAbstractTest)

Aggregations

InternalTransaction (org.apache.ignite.internal.tx.InternalTransaction)24 IgniteAbstractTest (org.apache.ignite.internal.testframework.IgniteAbstractTest)14 Test (org.junit.jupiter.api.Test)14 Tuple (org.apache.ignite.table.Tuple)12 BinaryRow (org.apache.ignite.internal.schema.BinaryRow)8 NotNull (org.jetbrains.annotations.NotNull)8 Row (org.apache.ignite.internal.schema.row.Row)6 ArrayList (java.util.ArrayList)4 IgniteException (org.apache.ignite.lang.IgniteException)4 TransactionException (org.apache.ignite.tx.TransactionException)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 Int2ObjectMap (it.unimi.dsi.fastutil.ints.Int2ObjectMap)2 Int2ObjectOpenHashMap (it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap)2 Collection (java.util.Collection)2 Comparator (java.util.Comparator)2 List (java.util.List)2 Map (java.util.Map)2 UUID (java.util.UUID)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 CompletableFuture.completedFuture (java.util.concurrent.CompletableFuture.completedFuture)2