use of com.scalar.db.api.Mutation in project scalardb by scalar-labs.
the class JdbcDatabase method mutate.
@Override
public void mutate(List<? extends Mutation> mutations) throws ExecutionException {
if (mutations.size() == 1) {
Mutation mutation = mutations.get(0);
if (mutation instanceof Put) {
put((Put) mutation);
} else if (mutation instanceof Delete) {
delete((Delete) mutation);
}
return;
}
mutations = copyAndSetTargetToIfNot(mutations);
Connection connection = null;
try {
connection = dataSource.getConnection();
connection.setAutoCommit(false);
} catch (SQLException e) {
close(connection);
throw new ExecutionException("mutate operation failed", e);
}
try {
if (!jdbcService.mutate(mutations, connection)) {
try {
connection.rollback();
} catch (SQLException e) {
throw new ExecutionException("failed to rollback", e);
}
throw new NoMutationException("no mutation was applied");
} else {
connection.commit();
}
} catch (SQLException e) {
try {
connection.rollback();
} catch (SQLException sqlException) {
throw new ExecutionException("failed to rollback", sqlException);
}
if (JdbcUtils.isConflictError(e, rdbEngine)) {
// conflicts can happen. Throw RetriableExecutionException in that case.
throw new RetriableExecutionException("conflict happened in a mutate operation", e);
}
throw new ExecutionException("mutate operation failed", e);
} finally {
close(connection);
}
}
use of com.scalar.db.api.Mutation in project scalardb by scalar-labs.
the class JdbcService method mutate.
public boolean mutate(List<? extends Mutation> mutations, Connection connection) throws SQLException, ExecutionException {
checkArgument(mutations.size() != 0);
operationChecker.check(mutations);
for (Mutation mutation : mutations) {
if (mutation instanceof Put) {
if (!put((Put) mutation, connection)) {
return false;
}
} else {
if (!delete((Delete) mutation, connection)) {
return false;
}
}
}
return true;
}
use of com.scalar.db.api.Mutation in project scalardb by scalar-labs.
the class DeleteStatementHandler method execute.
@Override
protected List<Record> execute(Operation operation) throws CosmosException, ExecutionException {
Mutation mutation = (Mutation) operation;
TableMetadata tableMetadata = metadataManager.getTableMetadata(mutation);
if (mutation.getCondition().isPresent()) {
executeStoredProcedure(mutation, tableMetadata);
} else {
execute(mutation, tableMetadata);
}
return Collections.emptyList();
}
use of com.scalar.db.api.Mutation in project scalardb by scalar-labs.
the class ProtoUtils method toMutation.
public static Mutation toMutation(com.scalar.db.rpc.Mutation mutation, TableMetadata metadata) {
Key partitionKey = toKey(mutation.getPartitionKey(), metadata);
Key clusteringKey;
if (mutation.hasClusteringKey()) {
clusteringKey = toKey(mutation.getClusteringKey(), metadata);
} else {
clusteringKey = null;
}
Mutation ret;
if (mutation.getType() == com.scalar.db.rpc.Mutation.Type.PUT) {
Put put = new Put(partitionKey, clusteringKey);
mutation.getValueList().forEach(v -> put.withValue(toColumn(v.getName(), v, metadata)));
ret = put;
} else {
ret = new Delete(partitionKey, clusteringKey);
}
if (!mutation.getNamespace().isEmpty()) {
ret.forNamespace(mutation.getNamespace());
}
if (!mutation.getTable().isEmpty()) {
ret.forTable(mutation.getTable());
}
ret.withConsistency(toConsistency(mutation.getConsistency()));
if (mutation.hasCondition()) {
ret.withCondition(toCondition(mutation.getCondition(), metadata));
}
return ret;
}
use of com.scalar.db.api.Mutation in project scalardb by scalar-labs.
the class Dynamo method mutate.
@Override
public void mutate(List<? extends Mutation> mutations) throws ExecutionException {
checkArgument(mutations.size() != 0);
if (mutations.size() == 1) {
Mutation mutation = mutations.get(0);
if (mutation instanceof Put) {
put((Put) mutation);
} else if (mutation instanceof Delete) {
delete((Delete) mutation);
}
return;
}
mutations = copyAndSetTargetToIfNot(mutations);
operationChecker.check(mutations);
for (Mutation mutation : mutations) {
operationChecker.check(mutation);
}
batchHandler.handle(mutations);
}
Aggregations