Search in sources :

Example 6 with Mutation

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

the class BatchHandler method execute.

private ResultSet execute(List<? extends Mutation> mutations) {
    BatchStatement batch = new BatchStatement();
    BatchComposer composer = new BatchComposer(batch, handlers);
    boolean conditional = false;
    for (Mutation mutation : mutations) {
        if (mutation.getCondition().isPresent()) {
            conditional = true;
        }
        // appropriate statement handler is selected here
        mutation.accept(composer);
    }
    if (conditional) {
        setConsistencyForConditionalMutation(batch);
    }
    return session.execute(batch);
}
Also used : BatchStatement(com.datastax.driver.core.BatchStatement) Mutation(com.scalar.db.api.Mutation)

Example 7 with Mutation

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

the class DynamoMutation method getCondition.

@Nonnull
public String getCondition() {
    ConditionExpressionBuilder builder = new ConditionExpressionBuilder(CONDITION_COLUMN_NAME_ALIAS, CONDITION_VALUE_ALIAS);
    Mutation mutation = (Mutation) getOperation();
    mutation.getCondition().ifPresent(c -> c.accept(builder));
    return builder.build();
}
Also used : Mutation(com.scalar.db.api.Mutation) Nonnull(javax.annotation.Nonnull)

Example 8 with Mutation

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

the class CosmosMutation method getMutationType.

@Nonnull
public MutationType getMutationType() {
    Mutation mutation = (Mutation) getOperation();
    if (!mutation.getCondition().isPresent()) {
        if (mutation instanceof Put) {
            return MutationType.PUT;
        } else {
            return MutationType.DELETE_IF;
        }
    }
    MutationCondition condition = mutation.getCondition().get();
    if (condition instanceof PutIfNotExists) {
        return MutationType.PUT_IF_NOT_EXISTS;
    } else if (condition instanceof PutIfExists || condition instanceof PutIf) {
        return MutationType.PUT_IF;
    } else {
        return MutationType.DELETE_IF;
    }
}
Also used : PutIfNotExists(com.scalar.db.api.PutIfNotExists) PutIf(com.scalar.db.api.PutIf) MutationCondition(com.scalar.db.api.MutationCondition) Mutation(com.scalar.db.api.Mutation) Put(com.scalar.db.api.Put) PutIfExists(com.scalar.db.api.PutIfExists) Nonnull(javax.annotation.Nonnull)

Example 9 with Mutation

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

the class CosmosMutation method makeConditionalQuery.

@Nonnull
public String makeConditionalQuery() {
    Mutation mutation = (Mutation) getOperation();
    SelectConditionStep<org.jooq.Record> select;
    if (isPrimaryKeySpecified()) {
        String id = getId();
        select = DSL.using(SQLDialect.DEFAULT).selectFrom("Record r").where(DSL.field("r.id").eq(id));
    } else {
        String concatenatedPartitionKey = getConcatenatedPartitionKey();
        select = DSL.using(SQLDialect.DEFAULT).selectFrom("Record r").where(DSL.field("r.concatenatedPartitionKey").eq(concatenatedPartitionKey));
        mutation.getClusteringKey().ifPresent(k -> {
            ValueBinder binder = new ValueBinder();
            k.getColumns().forEach(c -> {
                Field<Object> field = DSL.field("r.clusteringKey" + quoteKeyword(c.getName()));
                binder.set(s -> select.and(field.equal(s)));
                c.accept(binder);
            });
        });
    }
    ConditionalQueryBuilder builder = new ConditionalQueryBuilder(select);
    mutation.getCondition().ifPresent(c -> c.accept(builder));
    return builder.getQuery();
}
Also used : Mutation(com.scalar.db.api.Mutation) Nonnull(javax.annotation.Nonnull)

Example 10 with Mutation

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

the class MutateStatementHandler method handle.

/**
 * Executes the specified {@link Mutation} {@link Operation}
 *
 * @param operation {@link Mutation} operation
 * @return a {@code ResultSet}
 * @throws RetriableExecutionException if the execution failed, but it can be retriable
 * @throws ReadRepairableExecutionException if the execution partially failed, which can be
 *     repaired by a following read
 */
@Override
@Nonnull
public ResultSet handle(Operation operation) throws ExecutionException {
    try {
        ResultSet results = handleInternal(operation);
        Mutation mutation = (Mutation) operation;
        if (mutation.getCondition().isPresent() && !results.one().getBool(0)) {
            throw new NoMutationException("no mutation was applied.");
        }
        return results;
    } catch (WriteTimeoutException e) {
        LOGGER.warn("write timeout happened during mutate operation.", e);
        if (e.getWriteType() == WriteType.CAS) {
            // retry needs to be done if applications need to do the operation exactly
            throw new RetriableExecutionException("paxos phase in CAS operation failed.", e);
        } else if (e.getWriteType() == WriteType.SIMPLE) {
            Mutation mutation = (Mutation) operation;
            if (mutation.getCondition().isPresent()) {
                // learn phase needs to be repaired (by re-reading)
                throw new ReadRepairableExecutionException("learn phase in CAS operation failed.", e);
            } else {
                // retry needs to be done if applications need to do the operation exactly
                throw new RetriableExecutionException("simple write operation failed.", e);
            }
        } else {
            throw new ExecutionException("something wrong because it is neither CAS nor SIMPLE", e);
        }
    } catch (RuntimeException e) {
        LOGGER.warn(e.getMessage(), e);
        throw new RetriableExecutionException(e.getMessage(), e);
    }
}
Also used : WriteTimeoutException(com.datastax.driver.core.exceptions.WriteTimeoutException) ResultSet(com.datastax.driver.core.ResultSet) Mutation(com.scalar.db.api.Mutation) RetriableExecutionException(com.scalar.db.exception.storage.RetriableExecutionException) ExecutionException(com.scalar.db.exception.storage.ExecutionException) RetriableExecutionException(com.scalar.db.exception.storage.RetriableExecutionException) NoMutationException(com.scalar.db.exception.storage.NoMutationException) Nonnull(javax.annotation.Nonnull)

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