Search in sources :

Example 66 with Put

use of com.scalar.db.api.Put 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 67 with Put

use of com.scalar.db.api.Put 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 68 with Put

use of com.scalar.db.api.Put in project scalardb by scalar-labs.

the class InsertStatementHandlerTest method preparePutWithClusteringKey.

private Put preparePutWithClusteringKey() {
    Key partitionKey = new Key(ANY_NAME_1, ANY_TEXT_1);
    Key clusteringKey = new Key(ANY_NAME_2, ANY_TEXT_2);
    return new Put(partitionKey, clusteringKey).withValue(ANY_NAME_3, ANY_INT_1).forNamespace(ANY_NAMESPACE_NAME).forTable(ANY_TABLE_NAME);
}
Also used : Key(com.scalar.db.io.Key) Put(com.scalar.db.api.Put)

Example 69 with Put

use of com.scalar.db.api.Put in project scalardb by scalar-labs.

the class InsertStatementHandlerTest method preparePutWithReservedKeywords.

private Put preparePutWithReservedKeywords() {
    Key partitionKey = new Key("from", ANY_TEXT_1);
    Key clusteringKey = new Key("to", ANY_TEXT_2);
    return new Put(partitionKey, clusteringKey).withValue("one", ANY_INT_1).forNamespace("keyspace").forTable("table");
}
Also used : Key(com.scalar.db.io.Key) Put(com.scalar.db.api.Put)

Example 70 with Put

use of com.scalar.db.api.Put in project scalardb by scalar-labs.

the class UpdateStatementHandlerTest method preparePutWithReservedKeywords.

private Put preparePutWithReservedKeywords() {
    Key partitionKey = new Key("from", ANY_TEXT_1);
    Key clusteringKey = new Key("to", ANY_TEXT_2);
    return new Put(partitionKey, clusteringKey).withValue("one", ANY_INT_1).forNamespace("keyspace").forTable("table");
}
Also used : Key(com.scalar.db.io.Key) Put(com.scalar.db.api.Put)

Aggregations

Put (com.scalar.db.api.Put)374 Key (com.scalar.db.io.Key)216 Test (org.junit.jupiter.api.Test)209 Result (com.scalar.db.api.Result)108 Get (com.scalar.db.api.Get)93 Test (org.junit.Test)67 Delete (com.scalar.db.api.Delete)64 IntValue (com.scalar.db.io.IntValue)63 TextValue (com.scalar.db.io.TextValue)48 Scan (com.scalar.db.api.Scan)44 Value (com.scalar.db.io.Value)37 Assertions.catchThrowable (org.assertj.core.api.Assertions.catchThrowable)35 BooleanValue (com.scalar.db.io.BooleanValue)33 ConditionalExpression (com.scalar.db.api.ConditionalExpression)30 PutIfNotExists (com.scalar.db.api.PutIfNotExists)29 PutIf (com.scalar.db.api.PutIf)28 DoubleValue (com.scalar.db.io.DoubleValue)26 GrpcTransaction (com.scalar.db.transaction.rpc.GrpcTransaction)19 ExecutionException (com.scalar.db.exception.storage.ExecutionException)17 Mutation (com.scalar.db.api.Mutation)16