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