use of com.scalar.db.api.Get in project scalardb by scalar-labs.
the class ElectronicMoneyWithStorage method charge.
@Override
public void charge(String id, int amount) throws ExecutionException {
// Retrieve the current balance for id
Get get = new Get(new Key(ID, id));
Optional<Result> result = storage.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);
storage.put(put);
}
use of com.scalar.db.api.Get 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.api.Get in project scalardb by scalar-labs.
the class OperationCheckerTest method whenCheckingOperationWithWrongTable_shouldThrowIllegalArgumentException.
@Test
public void whenCheckingOperationWithWrongTable_shouldThrowIllegalArgumentException() throws ExecutionException {
// Arrange
Key partitionKey = new Key(PKEY1, 1, PKEY2, "val1");
Key clusteringKey = new Key(CKEY1, 2, CKEY2, "val2");
List<String> projections = Arrays.asList(COL1, COL2, COL3);
Get get = new Get(partitionKey, clusteringKey).withProjections(projections).forNamespace(NAMESPACE).forTable(TABLE_NAME);
// Returning null means table not found
when(metadataManager.getTableMetadata(any())).thenReturn(null);
// Act Assert
assertThatThrownBy(() -> operationChecker.check(get)).isInstanceOf(IllegalArgumentException.class);
}
use of com.scalar.db.api.Get in project scalardb by scalar-labs.
the class OperationCheckerTest method whenCheckingGetOperationWithInvalidPartitionKey_shouldThrowIllegalArgumentException.
@Test
public void whenCheckingGetOperationWithInvalidPartitionKey_shouldThrowIllegalArgumentException() {
// Arrange
Key partitionKey = new Key(PKEY1, 1, "p3", "val1");
Key clusteringKey = new Key(CKEY1, 2, CKEY2, "val2");
List<String> projections = Arrays.asList(COL1, COL2, COL3);
Get get = new Get(partitionKey, clusteringKey).withProjections(projections).forNamespace(NAMESPACE).forTable(TABLE_NAME);
// Act Assert
assertThatThrownBy(() -> operationChecker.check(get)).isInstanceOf(IllegalArgumentException.class);
}
use of com.scalar.db.api.Get in project scalardb by scalar-labs.
the class JdbcDatabaseTest method whenGetOperationExecuted_shouldCallJdbcService.
@Test
public void whenGetOperationExecuted_shouldCallJdbcService() throws Exception {
// Arrange
// Act
Get get = new Get(new Key("p1", "val")).forNamespace(NAMESPACE).forTable(TABLE);
jdbcDatabase.get(get);
// Assert
verify(jdbcService).get(any(), any());
verify(connection).close();
}
Aggregations