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);
}
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);
}
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));
}
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);
}
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();
}
Aggregations