use of org.apache.ignite.table.Tuple in project ignite-3 by apache.
the class TxAbstractTest method testTwoTables.
@Test
public void testTwoTables() throws TransactionException {
customers.recordView().upsert(null, makeValue(1, "test"));
accounts.recordView().upsert(null, makeValue(1, 100.));
assertEquals("test", customers.recordView().get(null, makeKey(1)).stringValue("name"));
assertEquals(100., accounts.recordView().get(null, makeKey(1)).doubleValue("balance"));
InternalTransaction tx = (InternalTransaction) igniteTransactions.begin();
InternalTransaction tx2 = (InternalTransaction) igniteTransactions.begin();
var txCust = customers.recordView();
var txAcc = accounts.recordView();
txCust.upsert(tx, makeValue(1, "test2"));
txAcc.upsert(tx, makeValue(1, 200.));
Tuple txValCust = txCust.get(tx, makeKey(1));
assertEquals("test2", txValCust.stringValue("name"));
txValCust.set("accountNumber", 2L);
txValCust.set("name", "test3");
Tuple txValAcc = txAcc.get(tx, makeKey(1));
assertEquals(200., txValAcc.doubleValue("balance"));
txValAcc.set("accountNumber", 2L);
txValAcc.set("balance", 300.);
tx.commit();
tx2.commit();
assertEquals("test2", customers.recordView().get(null, makeKey(1)).stringValue("name"));
assertEquals(200., accounts.recordView().get(null, makeKey(1)).doubleValue("balance"));
assertTrue(lockManager(accounts).isEmpty());
}
use of org.apache.ignite.table.Tuple in project ignite-3 by apache.
the class TxAbstractTest method doTestComplex.
/**
* Checks operation over tuple record view. The scenario was moved from ITDistributedTableTest.
*
* @param view Record view.
* @param tx Transaction or {@code null} for implicit one.
*/
private void doTestComplex(RecordView<Tuple> view, Transaction tx) {
final int keysCnt = 10;
long start = System.nanoTime();
for (long i = 0; i < keysCnt; i++) {
view.insert(tx, makeValue(i, i + 2.));
}
long dur = (long) ((System.nanoTime() - start) / 1000 / 1000.);
log.info("Inserted={}, time={}ms avg={} tps={}", keysCnt, dur, dur / keysCnt, 1000 / (dur / (float) keysCnt));
for (long i = 0; i < keysCnt; i++) {
Tuple entry = view.get(tx, makeKey(i));
assertEquals(i + 2., entry.doubleValue("balance"));
}
for (int i = 0; i < keysCnt; i++) {
view.upsert(tx, makeValue(i, i + 5.));
Tuple entry = view.get(tx, makeKey(i));
assertEquals(i + 5., entry.doubleValue("balance"));
}
HashSet<Tuple> keys = new HashSet<>();
for (long i = 0; i < keysCnt; i++) {
keys.add(makeKey(i));
}
Collection<Tuple> entries = view.getAll(tx, keys);
assertEquals(keysCnt, entries.size());
for (long i = 0; i < keysCnt; i++) {
boolean res = view.replace(tx, makeValue(i, i + 5.), makeValue(i, i + 2.));
assertTrue(res, "Failed to replace for idx=" + i);
}
for (long i = 0; i < keysCnt; i++) {
boolean res = view.delete(tx, makeKey(i));
assertTrue(res);
Tuple entry = view.get(tx, makeKey(i));
assertNull(entry);
}
ArrayList<Tuple> batch = new ArrayList<>(keysCnt);
for (long i = 0; i < keysCnt; i++) {
batch.add(makeValue(i, i + 2.));
}
view.upsertAll(tx, batch);
for (long i = 0; i < keysCnt; i++) {
Tuple entry = view.get(tx, makeKey(i));
assertEquals(i + 2., entry.doubleValue("balance"));
}
view.deleteAll(tx, keys);
for (Tuple key : keys) {
Tuple entry = view.get(tx, key);
assertNull(entry);
}
}
use of org.apache.ignite.table.Tuple in project ignite-3 by apache.
the class NumericTypesSerializerTest method testStringDecimalSpecialCase.
@Test
public void testStringDecimalSpecialCase() throws TupleMarshallerException {
schema = new SchemaDescriptor(42, new Column[] { new Column("key", NativeTypes.INT64, false) }, new Column[] { new Column("decimalCol", NativeTypes.decimalOf(1, 0), false) });
// representation of "0000" value.
final Tuple tup = createTuple().set("key", rnd.nextLong()).set("decimalCol", new BigDecimal("0E+3"));
TupleMarshaller marshaller = new TupleMarshallerImpl(new DummySchemaManagerImpl(schema));
final Row row = marshaller.marshal(tup);
assertEquals(row.decimalValue(1), BigDecimal.ZERO);
}
use of org.apache.ignite.table.Tuple in project ignite-3 by apache.
the class NumericTypesSerializerTest method testPrecisionRestrictionsForNumbers.
@Test
public void testPrecisionRestrictionsForNumbers() {
schema = new SchemaDescriptor(42, new Column[] { new Column("key", NativeTypes.INT64, false) }, new Column[] { new Column("number1", NativeTypes.numberOf(5), false) });
TupleMarshaller marshaller = new TupleMarshallerImpl(new DummySchemaManagerImpl(schema));
final Tuple badTup = createTuple().set("key", rnd.nextLong());
assertThrows(TupleMarshallerException.class, () -> marshaller.marshal(badTup.set("number1", BigInteger.valueOf(999991L))), "Column's type mismatch");
assertThrows(TupleMarshallerException.class, () -> marshaller.marshal(badTup.set("number1", new BigInteger("111111"))), "Column's type mismatch");
assertThrows(TupleMarshallerException.class, () -> marshaller.marshal(badTup.set("number1", BigInteger.valueOf(-999991L))), "Column's type mismatch");
assertThrows(TupleMarshallerException.class, () -> marshaller.marshal(badTup.set("number1", new BigInteger("-111111"))), "Column's type mismatch");
}
use of org.apache.ignite.table.Tuple in project ignite-3 by apache.
the class NumericTypesSerializerTest method testSameBinaryRepresentation.
/**
* Test.
*/
@ParameterizedTest
@MethodSource("sameDecimals")
public void testSameBinaryRepresentation(Pair<BigInteger, BigInteger> pair) throws Exception {
schema = new SchemaDescriptor(42, new Column[] { new Column("key", NativeTypes.INT64, false) }, new Column[] { new Column("decimalCol", NativeTypes.decimalOf(19, 3), false) });
TupleMarshaller marshaller = new TupleMarshallerImpl(new DummySchemaManagerImpl(schema));
long randomKey = rnd.nextLong();
final Tuple firstTup = createTuple().set("key", randomKey).set("decimalCol", pair.getFirst());
final Tuple secondTup = createTuple().set("key", randomKey).set("decimalCol", pair.getSecond());
final Row firstRow = marshaller.marshal(firstTup);
final Row secondRow = marshaller.marshal(secondTup);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
firstRow.writeTo(stream);
byte[] firstRowInBytes = stream.toByteArray();
stream.reset();
secondRow.writeTo(stream);
byte[] secondRowInBytes = stream.toByteArray();
assertArrayEquals(firstRowInBytes, secondRowInBytes);
}
Aggregations