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);
}
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;
}
}
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);
}
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");
}
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");
}
Aggregations