Search in sources :

Example 71 with Get

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);
}
Also used : Get(com.scalar.db.api.Get) Key(com.scalar.db.io.Key) Put(com.scalar.db.api.Put) Result(com.scalar.db.api.Result)

Example 72 with Get

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;
    }
}
Also used : DistributedTransaction(com.scalar.db.api.DistributedTransaction) Get(com.scalar.db.api.Get) Key(com.scalar.db.io.Key) Put(com.scalar.db.api.Put) TransactionException(com.scalar.db.exception.transaction.TransactionException) IOException(java.io.IOException) Result(com.scalar.db.api.Result)

Example 73 with Get

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);
}
Also used : Get(com.scalar.db.api.Get) Key(com.scalar.db.io.Key) Test(org.junit.jupiter.api.Test)

Example 74 with Get

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);
}
Also used : Get(com.scalar.db.api.Get) Key(com.scalar.db.io.Key) Test(org.junit.jupiter.api.Test)

Example 75 with Get

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();
}
Also used : Get(com.scalar.db.api.Get) Key(com.scalar.db.io.Key) Test(org.junit.jupiter.api.Test)

Aggregations

Get (com.scalar.db.api.Get)286 Test (org.junit.jupiter.api.Test)130 Result (com.scalar.db.api.Result)129 Key (com.scalar.db.io.Key)126 Test (org.junit.Test)88 Put (com.scalar.db.api.Put)86 GrpcTransaction (com.scalar.db.transaction.rpc.GrpcTransaction)32 Delete (com.scalar.db.api.Delete)28 IntValue (com.scalar.db.io.IntValue)28 Assertions.catchThrowable (org.assertj.core.api.Assertions.catchThrowable)21 TextValue (com.scalar.db.io.TextValue)16 ConditionalExpression (com.scalar.db.api.ConditionalExpression)13 PartitionKey (com.azure.cosmos.models.PartitionKey)12 TableMetadata (com.scalar.db.api.TableMetadata)10 BooleanValue (com.scalar.db.io.BooleanValue)10 PutIf (com.scalar.db.api.PutIf)8 BigIntValue (com.scalar.db.io.BigIntValue)7 HashMap (java.util.HashMap)7 BlobValue (com.scalar.db.io.BlobValue)6 DistributedTransactionServiceWithConsensusCommitIntegrationTest.prepareGet (com.scalar.db.server.DistributedTransactionServiceWithConsensusCommitIntegrationTest.prepareGet)6