use of com.scalar.db.exception.transaction.TransactionException in project scalardb by scalar-labs.
the class DistributedTransactionServiceWithJdbcTransactionIntegrationTest method populateRecords.
private void populateRecords() throws TransactionException {
GrpcTransaction transaction = manager.start();
IntStream.range(0, NUM_ACCOUNTS).forEach(i -> IntStream.range(0, NUM_TYPES).forEach(j -> {
Key partitionKey = new Key(ACCOUNT_ID, i);
Key clusteringKey = new Key(ACCOUNT_TYPE, j);
Put put = new Put(partitionKey, clusteringKey).forNamespace(NAMESPACE).forTable(TABLE).withValue(BALANCE, INITIAL_BALANCE);
try {
transaction.put(put);
} catch (CrudException e) {
throw new RuntimeException(e);
}
}));
transaction.commit();
}
use of com.scalar.db.exception.transaction.TransactionException in project scalardb by scalar-labs.
the class ElectronicMoneyWithTransaction method charge.
@Override
public void charge(String id, int amount) throws TransactionException {
// Start a transaction
DistributedTransaction tx = manager.start();
try {
// Retrieve the current balance for id
Get get = new Get(new Key(ID, id));
Optional<Result> result = tx.get(get);
// Calculate the balance
int balance = amount;
if (result.isPresent()) {
int current = result.get().getValue(BALANCE).get().getAsInt();
balance += current;
}
// Update the balance
Put put = new Put(new Key(ID, id)).withValue(BALANCE, balance);
tx.put(put);
// Commit the transaction (records are automatically recovered in case of failure)
tx.commit();
} catch (Exception e) {
tx.abort();
throw e;
}
}
use of com.scalar.db.exception.transaction.TransactionException in project scalardb by scalar-labs.
the class DistributedTransactionIntegrationTestBase method scan_ScanAllWithProjectionsGiven_ShouldRetrieveSpecifiedValues.
@Test
public void scan_ScanAllWithProjectionsGiven_ShouldRetrieveSpecifiedValues() throws TransactionException {
// Arrange
populateRecords();
DistributedTransaction transaction = manager.start();
ScanAll scanAll = prepareScanAll().withProjection(ACCOUNT_TYPE).withProjection(BALANCE);
// Act
List<Result> results = transaction.scan(scanAll);
transaction.commit();
// Assert
List<ExpectedResult> expectedResults = new ArrayList<>();
IntStream.range(0, NUM_ACCOUNTS).forEach(i -> IntStream.range(0, NUM_TYPES).forEach(j -> {
ExpectedResultBuilder erBuilder = new ExpectedResultBuilder().clusteringKey(Key.ofInt(ACCOUNT_TYPE, j)).nonKeyColumns(ImmutableList.of(IntColumn.of(BALANCE, INITIAL_BALANCE)));
expectedResults.add(erBuilder.build());
}));
assertResultsContainsExactlyInAnyOrder(results, expectedResults);
}
use of com.scalar.db.exception.transaction.TransactionException in project scalardb by scalar-labs.
the class TwoPhaseCommitTransactionIntegrationTestBase method scan_ScanAllWithProjectionsGiven_ShouldRetrieveSpecifiedValues.
@Test
public void scan_ScanAllWithProjectionsGiven_ShouldRetrieveSpecifiedValues() throws TransactionException {
// Arrange
populateRecords();
TwoPhaseCommitTransaction transaction = manager.start();
ScanAll scanAll = prepareScanAll().withProjection(ACCOUNT_TYPE).withProjection(BALANCE);
// Act
List<Result> results = transaction.scan(scanAll);
transaction.prepare();
transaction.validate();
transaction.commit();
// Assert
List<ExpectedResult> expectedResults = new ArrayList<>();
IntStream.range(0, NUM_ACCOUNTS).forEach(i -> IntStream.range(0, NUM_TYPES).forEach(j -> {
ExpectedResultBuilder erBuilder = new ExpectedResultBuilder().clusteringKey(Key.ofInt(ACCOUNT_TYPE, j)).nonKeyColumns(ImmutableList.of(IntColumn.of(BALANCE, INITIAL_BALANCE)));
expectedResults.add(erBuilder.build());
}));
assertResultsContainsExactlyInAnyOrder(results, expectedResults);
results.forEach(result -> {
assertThat(result.contains(ACCOUNT_ID)).isFalse();
assertThat(result.contains(SOME_COLUMN)).isFalse();
});
}
use of com.scalar.db.exception.transaction.TransactionException in project scalardb by scalar-labs.
the class TwoPhaseCommitTransactionIntegrationTestBase method populateRecords.
private void populateRecords() throws TransactionException {
TwoPhaseCommitTransaction transaction = manager.start();
IntStream.range(0, NUM_ACCOUNTS).forEach(i -> IntStream.range(0, NUM_TYPES).forEach(j -> {
Key partitionKey = new Key(ACCOUNT_ID, i);
Key clusteringKey = new Key(ACCOUNT_TYPE, j);
Put put = new Put(partitionKey, clusteringKey).forNamespace(namespace).forTable(TABLE).withIntValue(BALANCE, INITIAL_BALANCE).withIntValue(SOME_COLUMN, i * j);
try {
transaction.put(put);
} catch (CrudException e) {
throw new RuntimeException(e);
}
}));
transaction.prepare();
transaction.validate();
transaction.commit();
}
Aggregations