Search in sources :

Example 26 with ConditionalExpression

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

the class RollbackMutationComposerTest method add_GetAndPreparedResultGivenAndBeforeResultNotGiven_ShouldComposeDelete.

@Test
public void add_GetAndPreparedResultGivenAndBeforeResultNotGiven_ShouldComposeDelete() throws ExecutionException {
    // Arrange
    composer = new RollbackMutationComposer(ANY_ID_2, storage, tableMetadataManager, mutations);
    TransactionResult result = prepareInitialResult(ANY_ID_2, TransactionState.PREPARED);
    Get get = prepareGet();
    // Act
    composer.add(get, result);
    // Assert
    Delete actual = (Delete) mutations.get(0);
    Delete expected = new Delete(get.getPartitionKey(), get.getClusteringKey().orElse(null)).forNamespace(get.forNamespace().get()).forTable(get.forTable().get());
    expected.withConsistency(Consistency.LINEARIZABLE);
    expected.withCondition(new DeleteIf(new ConditionalExpression(ID, toIdValue(ANY_ID_2), Operator.EQ), new ConditionalExpression(STATE, toStateValue(TransactionState.PREPARED), Operator.EQ)));
    assertThat(actual).isEqualTo(expected);
}
Also used : Delete(com.scalar.db.api.Delete) Get(com.scalar.db.api.Get) ConditionalExpression(com.scalar.db.api.ConditionalExpression) DeleteIf(com.scalar.db.api.DeleteIf) Test(org.junit.jupiter.api.Test)

Example 27 with ConditionalExpression

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

the class DeleteQuery method bind.

@Override
public void bind(PreparedStatement preparedStatement) throws SQLException {
    PreparedStatementBinder binder = new PreparedStatementBinder(preparedStatement, tableMetadata, rdbEngine);
    for (Column<?> column : partitionKey.getColumns()) {
        column.accept(binder);
        binder.throwSQLExceptionIfOccurred();
    }
    if (clusteringKey.isPresent()) {
        for (Column<?> column : clusteringKey.get().getColumns()) {
            column.accept(binder);
            binder.throwSQLExceptionIfOccurred();
        }
    }
    for (ConditionalExpression condition : otherConditions) {
        // no need to bind for 'is null' and 'is not null' conditions
        if (condition.getOperator() != Operator.IS_NULL && condition.getOperator() != Operator.IS_NOT_NULL) {
            condition.getColumn().accept(binder);
            binder.throwSQLExceptionIfOccurred();
        }
    }
}
Also used : ConditionalExpression(com.scalar.db.api.ConditionalExpression)

Example 28 with ConditionalExpression

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

the class PrepareMutationComposer method add.

private void add(Delete base, TransactionResult result) {
    Put put = new Put(base.getPartitionKey(), getClusteringKey(base, result).orElse(null)).forNamespace(base.forNamespace().get()).forTable(base.forTable().get()).withConsistency(Consistency.LINEARIZABLE);
    List<Value<?>> values = new ArrayList<>();
    values.add(Attribute.toIdValue(id));
    values.add(Attribute.toStateValue(TransactionState.DELETED));
    values.add(Attribute.toPreparedAtValue(current));
    if (result != null) {
        values.addAll(createBeforeValues(base, result));
        int version = result.getVersion();
        values.add(Attribute.toVersionValue(version + 1));
        // check if the record is not interrupted by other conflicting transactions
        put.withCondition(new PutIf(new ConditionalExpression(VERSION, toVersionValue(version), Operator.EQ), new ConditionalExpression(ID, toIdValue(result.getId()), Operator.EQ)));
    } else {
        put.withValue(Attribute.toVersionValue(1));
        // check if the record is not created by other conflicting transactions
        put.withCondition(new PutIfNotExists());
    }
    put.withValues(values);
    mutations.add(put);
}
Also used : PutIfNotExists(com.scalar.db.api.PutIfNotExists) PutIf(com.scalar.db.api.PutIf) ConditionalExpression(com.scalar.db.api.ConditionalExpression) Value(com.scalar.db.io.Value) Attribute.toIdValue(com.scalar.db.transaction.consensuscommit.Attribute.toIdValue) Attribute.toVersionValue(com.scalar.db.transaction.consensuscommit.Attribute.toVersionValue) ArrayList(java.util.ArrayList) Put(com.scalar.db.api.Put)

Example 29 with ConditionalExpression

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

the class PrepareMutationComposer method add.

private void add(Put base, TransactionResult result) {
    Put put = new Put(base.getPartitionKey(), getClusteringKey(base, result).orElse(null)).forNamespace(base.forNamespace().get()).forTable(base.forTable().get()).withConsistency(Consistency.LINEARIZABLE);
    put.withValue(Attribute.toIdValue(id));
    put.withValue(Attribute.toStateValue(TransactionState.PREPARED));
    put.withValue(Attribute.toPreparedAtValue(current));
    base.getColumns().values().forEach(put::withValue);
    if (result != null) {
        // overwrite existing record
        put.withValues(createBeforeValues(base, result));
        int version = result.getVersion();
        put.withValue(Attribute.toVersionValue(version + 1));
        // check if the record is not interrupted by other conflicting transactions
        put.withCondition(new PutIf(new ConditionalExpression(VERSION, toVersionValue(version), Operator.EQ), new ConditionalExpression(ID, toIdValue(result.getId()), Operator.EQ)));
    } else {
        // initial record
        put.withValue(Attribute.toVersionValue(1));
        // check if the record is not created by other conflicting transactions
        put.withCondition(new PutIfNotExists());
    }
    mutations.add(put);
}
Also used : PutIfNotExists(com.scalar.db.api.PutIfNotExists) PutIf(com.scalar.db.api.PutIf) ConditionalExpression(com.scalar.db.api.ConditionalExpression) Put(com.scalar.db.api.Put)

Example 30 with ConditionalExpression

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

the class RollbackMutationComposer method composePut.

private Put composePut(Operation base, TransactionResult result) throws ExecutionException {
    assert result.getState().equals(TransactionState.PREPARED) || result.getState().equals(TransactionState.DELETED);
    TransactionalTableMetadata metadata = tableMetadataManager.getTransactionalTableMetadata(base);
    LinkedHashSet<String> beforeImageColumnNames = metadata.getBeforeImageColumnNames();
    Map<String, Value<?>> map = new HashMap<>();
    result.getValues().forEach((k, v) -> {
        if (beforeImageColumnNames.contains(k)) {
            String key = k.substring(Attribute.BEFORE_PREFIX.length());
            map.put(key, v.copyWith(key));
        }
    });
    return new Put(getPartitionKey(base, result), getClusteringKey(base, result).orElse(null)).forNamespace(base.forNamespace().get()).forTable(base.forTable().get()).withCondition(new PutIf(new ConditionalExpression(ID, toIdValue(id), Operator.EQ), new ConditionalExpression(STATE, toStateValue(result.getState()), Operator.EQ))).withConsistency(Consistency.LINEARIZABLE).withValues(map.values());
}
Also used : PutIf(com.scalar.db.api.PutIf) HashMap(java.util.HashMap) ConditionalExpression(com.scalar.db.api.ConditionalExpression) TextValue(com.scalar.db.io.TextValue) Value(com.scalar.db.io.Value) Attribute.toIdValue(com.scalar.db.transaction.consensuscommit.Attribute.toIdValue) Attribute.toStateValue(com.scalar.db.transaction.consensuscommit.Attribute.toStateValue) Put(com.scalar.db.api.Put)

Aggregations

ConditionalExpression (com.scalar.db.api.ConditionalExpression)55 Test (org.junit.jupiter.api.Test)35 PutIf (com.scalar.db.api.PutIf)30 Put (com.scalar.db.api.Put)27 TextValue (com.scalar.db.io.TextValue)22 DeleteIf (com.scalar.db.api.DeleteIf)18 Key (com.scalar.db.io.Key)18 Delete (com.scalar.db.api.Delete)14 Get (com.scalar.db.api.Get)13 IntValue (com.scalar.db.io.IntValue)13 Test (org.junit.Test)12 Result (com.scalar.db.api.Result)10 Scan (com.scalar.db.api.Scan)7 HashMap (java.util.HashMap)6 PutIfNotExists (com.scalar.db.api.PutIfNotExists)4 PreparedStatement (java.sql.PreparedStatement)4 MutationCondition (com.scalar.db.api.MutationCondition)3 Value (com.scalar.db.io.Value)3 DeleteIfExists (com.scalar.db.api.DeleteIfExists)2 Attribute.toIdValue (com.scalar.db.transaction.consensuscommit.Attribute.toIdValue)2