Search in sources :

Example 21 with PutIfNotExists

use of com.scalar.db.api.PutIfNotExists 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 22 with PutIfNotExists

use of com.scalar.db.api.PutIfNotExists 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 23 with PutIfNotExists

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

the class BatchHandlerTest method handle_CorrectHandlerAndAtLeastOneConditionalPutGiven_ShouldSetConsistencyProperly.

@Test
public void handle_CorrectHandlerAndAtLeastOneConditionalPutGiven_ShouldSetConsistencyProperly() {
    // Arrange
    configureBehavior();
    mutations = prepareNonConditionalPuts();
    mutations.get(1).withCondition(new PutIfNotExists());
    when(session.execute(any(Statement.class))).thenReturn(results);
    when(results.wasApplied()).thenReturn(true);
    spy = prepareSpiedBatchHandler();
    // Act Assert
    assertThatCode(() -> spy.handle(mutations)).doesNotThrowAnyException();
    // Assert
    verify(spy).setConsistencyForConditionalMutation(any(BatchStatement.class));
}
Also used : PutIfNotExists(com.scalar.db.api.PutIfNotExists) PreparedStatement(com.datastax.driver.core.PreparedStatement) BoundStatement(com.datastax.driver.core.BoundStatement) BatchStatement(com.datastax.driver.core.BatchStatement) Statement(com.datastax.driver.core.Statement) BatchStatement(com.datastax.driver.core.BatchStatement) Test(org.junit.jupiter.api.Test)

Example 24 with PutIfNotExists

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

the class PrepareMutationComposer method add.

// This prepares a record that was read but didn't exist to avoid anti-dependency for the record.
// This is only called when Serializable with Extra-write strategy is enabled.
private void add(Get base) {
    Put put = new Put(base.getPartitionKey(), getClusteringKey(base, null).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));
    values.add(Attribute.toVersionValue(1));
    // check if the record is not interrupted by other conflicting transactions
    put.withCondition(new PutIfNotExists());
    put.withValues(values);
    mutations.add(put);
}
Also used : PutIfNotExists(com.scalar.db.api.PutIfNotExists) 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 25 with PutIfNotExists

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

the class JdbcDatabaseTest method whenMutateOperationWithConditionExecutedAndJdbcServiceReturnsFalse_shouldThrowNoMutationException.

@Test
public void whenMutateOperationWithConditionExecutedAndJdbcServiceReturnsFalse_shouldThrowNoMutationException() throws Exception {
    // Arrange
    when(jdbcService.mutate(any(), any())).thenReturn(false);
    // Act Assert
    assertThatThrownBy(() -> {
        Put put = new Put(new Key("p1", "val1")).withValue("v1", "val2").withCondition(new PutIfNotExists()).forNamespace(NAMESPACE).forTable(TABLE);
        Delete delete = new Delete(new Key("p1", "val1")).withCondition(new DeleteIfExists()).forNamespace(NAMESPACE).forTable(TABLE);
        jdbcDatabase.mutate(Arrays.asList(put, delete));
    }).isInstanceOf(NoMutationException.class);
    verify(connection).close();
}
Also used : PutIfNotExists(com.scalar.db.api.PutIfNotExists) Delete(com.scalar.db.api.Delete) Put(com.scalar.db.api.Put) Key(com.scalar.db.io.Key) DeleteIfExists(com.scalar.db.api.DeleteIfExists) Test(org.junit.jupiter.api.Test)

Aggregations

PutIfNotExists (com.scalar.db.api.PutIfNotExists)37 Put (com.scalar.db.api.Put)27 Test (org.junit.jupiter.api.Test)26 Key (com.scalar.db.io.Key)15 Delete (com.scalar.db.api.Delete)8 Test (org.junit.Test)7 DeleteIfExists (com.scalar.db.api.DeleteIfExists)6 MutationCondition (com.scalar.db.api.MutationCondition)6 Result (com.scalar.db.api.Result)6 TextValue (com.scalar.db.io.TextValue)6 PutIf (com.scalar.db.api.PutIf)5 Scan (com.scalar.db.api.Scan)5 IntValue (com.scalar.db.io.IntValue)5 Value (com.scalar.db.io.Value)5 ConditionalExpression (com.scalar.db.api.ConditionalExpression)4 BooleanValue (com.scalar.db.io.BooleanValue)4 CosmosStoredProcedureRequestOptions (com.azure.cosmos.models.CosmosStoredProcedureRequestOptions)3 DoubleValue (com.scalar.db.io.DoubleValue)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 TransactWriteItemsRequest (software.amazon.awssdk.services.dynamodb.model.TransactWriteItemsRequest)3