use of com.scalar.db.transaction.rpc.GrpcTransaction in project scalardb by scalar-labs.
the class DistributedTransactionServiceWithConsensusCommitIntegrationTest method prepareTransfer.
private GrpcTransaction prepareTransfer(int fromId, String fromTable, int toId, String toTable, int amount) throws TransactionException {
boolean differentTables = !toTable.equals(fromTable);
GrpcTransaction transaction = manager.start();
List<Get> fromGets = prepareGets(fromTable);
List<Get> toGets = differentTables ? prepareGets(toTable) : fromGets;
Optional<Result> fromResult = transaction.get(fromGets.get(fromId));
assertThat(fromResult).isPresent();
IntValue fromBalance = new IntValue(BALANCE, getBalance(fromResult.get()) - amount);
Optional<Result> toResult = transaction.get(toGets.get(toId));
assertThat(toResult).isPresent();
IntValue toBalance = new IntValue(BALANCE, getBalance(toResult.get()) + amount);
List<Put> fromPuts = preparePuts(fromTable);
List<Put> toPuts = differentTables ? preparePuts(toTable) : fromPuts;
fromPuts.get(fromId).withValue(fromBalance);
toPuts.get(toId).withValue(toBalance);
transaction.put(fromPuts.get(fromId));
transaction.put(toPuts.get(toId));
return transaction;
}
use of com.scalar.db.transaction.rpc.GrpcTransaction in project scalardb by scalar-labs.
the class DistributedTransactionServiceWithConsensusCommitIntegrationTest method putAndCommit_PutGivenForExistingAfterRead_ShouldUpdateRecord.
@Test
public void putAndCommit_PutGivenForExistingAfterRead_ShouldUpdateRecord() throws TransactionException {
// Arrange
populateRecords(TABLE_1);
Get get = prepareGet(0, 0, TABLE_1);
GrpcTransaction transaction = manager.start();
// Act
Optional<Result> result = transaction.get(get);
assertThat(result).isPresent();
int expected = getBalance(result.get()) + 100;
Put put = preparePut(0, 0, TABLE_1).withValue(BALANCE, expected);
transaction.put(put);
transaction.commit();
// Assert
GrpcTransaction another = manager.start();
Optional<Result> actual = another.get(get);
assertThat(actual).isPresent();
another.commit();
assertThat(getBalance(actual.get())).isEqualTo(expected);
}
use of com.scalar.db.transaction.rpc.GrpcTransaction in project scalardb by scalar-labs.
the class DistributedTransactionServiceWithConsensusCommitIntegrationTest method commit_ConflictingPutAndDeleteGivenForExisting_ShouldCommitPutAndAbortDelete.
private void commit_ConflictingPutAndDeleteGivenForExisting_ShouldCommitPutAndAbortDelete(String table1, String table2) throws TransactionException {
// Arrange
boolean differentTables = !table1.equals(table2);
int amount = 200;
int from = 0;
int to = NUM_TYPES;
int anotherFrom = to;
int anotherTo = NUM_TYPES * 2;
populateRecords(table1);
if (differentTables) {
populateRecords(table2);
}
// Act
GrpcTransaction transaction = manager.start();
List<Get> gets1 = prepareGets(table1);
List<Delete> deletes1 = prepareDeletes(table1);
List<Get> gets2 = differentTables ? prepareGets(table2) : gets1;
List<Delete> deletes2 = differentTables ? prepareDeletes(table2) : deletes1;
transaction.get(gets1.get(from));
transaction.delete(deletes1.get(from));
transaction.get(gets2.get(to));
transaction.delete(deletes2.get(to));
assertThatCode(() -> prepareTransfer(anotherFrom, table2, anotherTo, table1, amount).commit()).doesNotThrowAnyException();
assertThatThrownBy(transaction::commit).isInstanceOf(CommitException.class);
// Assert
GrpcTransaction another = manager.start();
Optional<Result> fromResult = another.get(gets1.get(from));
assertThat(fromResult).isPresent();
assertThat(getBalance(fromResult.get())).isEqualTo(INITIAL_BALANCE);
Optional<Result> toResult = another.get(gets2.get(to));
assertThat(toResult).isPresent();
assertThat(getBalance(toResult.get())).isEqualTo(INITIAL_BALANCE - amount);
Optional<Result> anotherToResult = another.get(gets1.get(anotherTo));
assertThat(anotherToResult).isPresent();
assertThat(getBalance(anotherToResult.get())).isEqualTo(INITIAL_BALANCE + amount);
another.commit();
}
use of com.scalar.db.transaction.rpc.GrpcTransaction in project scalardb by scalar-labs.
the class DistributedTransactionServiceWithConsensusCommitIntegrationTest method get_DeleteCalledBefore_ShouldReturnEmpty.
@Test
public void get_DeleteCalledBefore_ShouldReturnEmpty() throws TransactionException {
// Arrange
GrpcTransaction transaction = manager.start();
transaction.put(preparePut(0, 0, TABLE_1).withValue(BALANCE, 1));
transaction.commit();
// Act
GrpcTransaction transaction1 = manager.start();
Get get = prepareGet(0, 0, TABLE_1);
Optional<Result> resultBefore = transaction1.get(get);
transaction1.delete(prepareDelete(0, 0, TABLE_1));
Optional<Result> resultAfter = transaction1.get(get);
assertThatCode(transaction1::commit).doesNotThrowAnyException();
// Assert
assertThat(resultBefore.isPresent()).isTrue();
assertThat(resultAfter.isPresent()).isFalse();
}
use of com.scalar.db.transaction.rpc.GrpcTransaction in project scalardb by scalar-labs.
the class DistributedTransactionServiceWithConsensusCommitIntegrationTest method putAndCommit_GetsAndPutsGiven_ShouldCommitProperly.
private void putAndCommit_GetsAndPutsGiven_ShouldCommitProperly(String fromTable, String toTable) throws TransactionException {
// Arrange
boolean differentTables = !fromTable.equals(toTable);
populateRecords(fromTable);
if (differentTables) {
populateRecords(toTable);
}
List<Get> fromGets = prepareGets(fromTable);
List<Get> toGets = differentTables ? prepareGets(toTable) : fromGets;
int amount = 100;
int fromBalance = INITIAL_BALANCE - amount;
int toBalance = INITIAL_BALANCE + amount;
int from = 0;
int to = NUM_TYPES;
// Act
prepareTransfer(from, fromTable, to, toTable, amount).commit();
// Assert
GrpcTransaction another = manager.start();
Optional<Result> fromResult = another.get(fromGets.get(from));
assertThat(fromResult).isPresent();
assertThat(getBalance(fromResult.get())).isEqualTo(fromBalance);
Optional<Result> toResult = another.get(toGets.get(to));
assertThat(toResult).isPresent();
assertThat(getBalance(toResult.get())).isEqualTo(toBalance);
another.commit();
}
Aggregations