Search in sources :

Example 11 with Mutation

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);
    }
}
Also used : Delete(com.scalar.db.api.Delete) SQLException(java.sql.SQLException) Connection(java.sql.Connection) Mutation(com.scalar.db.api.Mutation) ExecutionException(com.scalar.db.exception.storage.ExecutionException) RetriableExecutionException(com.scalar.db.exception.storage.RetriableExecutionException) RetriableExecutionException(com.scalar.db.exception.storage.RetriableExecutionException) NoMutationException(com.scalar.db.exception.storage.NoMutationException) Put(com.scalar.db.api.Put)

Example 12 with Mutation

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;
}
Also used : Delete(com.scalar.db.api.Delete) Mutation(com.scalar.db.api.Mutation) Put(com.scalar.db.api.Put)

Example 13 with Mutation

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();
}
Also used : TableMetadata(com.scalar.db.api.TableMetadata) Mutation(com.scalar.db.api.Mutation)

Example 14 with Mutation

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

Example 15 with Mutation

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);
}
Also used : Delete(com.scalar.db.api.Delete) Mutation(com.scalar.db.api.Mutation) Put(com.scalar.db.api.Put)

Aggregations

Mutation (com.scalar.db.api.Mutation)24 Put (com.scalar.db.api.Put)16 Delete (com.scalar.db.api.Delete)14 Key (com.scalar.db.io.Key)7 Nonnull (javax.annotation.Nonnull)7 Test (org.junit.jupiter.api.Test)5 TableMetadata (com.scalar.db.api.TableMetadata)4 ConditionalExpression (com.scalar.db.api.ConditionalExpression)2 ExecutionException (com.scalar.db.exception.storage.ExecutionException)2 NoMutationException (com.scalar.db.exception.storage.NoMutationException)2 RetriableExecutionException (com.scalar.db.exception.storage.RetriableExecutionException)2 HashMap (java.util.HashMap)2 BatchStatement (com.datastax.driver.core.BatchStatement)1 ResultSet (com.datastax.driver.core.ResultSet)1 WriteTimeoutException (com.datastax.driver.core.exceptions.WriteTimeoutException)1 MutationCondition (com.scalar.db.api.MutationCondition)1 PutIf (com.scalar.db.api.PutIf)1 PutIfExists (com.scalar.db.api.PutIfExists)1 PutIfNotExists (com.scalar.db.api.PutIfNotExists)1 Value (com.scalar.db.io.Value)1