use of com.scalar.db.api.Delete in project scalardb by scalar-labs.
the class CrudHandlerTest method scan_CalledAfterDeleteUnderRealSnapshot_ShouldReturnResultsWithoutDeletedRecord.
@Test
public void scan_CalledAfterDeleteUnderRealSnapshot_ShouldReturnResultsWithoutDeletedRecord() throws ExecutionException, CrudException {
// Arrange
Scan scan = prepareScan();
result = prepareResult(TransactionState.COMMITTED);
ImmutableMap<String, Column<?>> columns = ImmutableMap.<String, Column<?>>builder().put(ANY_NAME_1, TextColumn.of(ANY_NAME_1, ANY_TEXT_1)).put(ANY_NAME_2, TextColumn.of(ANY_NAME_2, ANY_TEXT_3)).put(Attribute.ID, ScalarDbUtils.toColumn(Attribute.toIdValue(ANY_ID_2))).put(Attribute.STATE, ScalarDbUtils.toColumn(Attribute.toStateValue(TransactionState.COMMITTED))).put(Attribute.VERSION, ScalarDbUtils.toColumn(Attribute.toVersionValue(2))).put(Attribute.BEFORE_ID, ScalarDbUtils.toColumn(Attribute.toBeforeIdValue(ANY_ID_1))).put(Attribute.BEFORE_STATE, ScalarDbUtils.toColumn(Attribute.toBeforeStateValue(TransactionState.COMMITTED))).put(Attribute.BEFORE_VERSION, ScalarDbUtils.toColumn(Attribute.toBeforeVersionValue(1))).build();
Result result2 = new ResultImpl(columns, TABLE_METADATA);
Map<Snapshot.Key, Optional<TransactionResult>> readSet = new HashMap<>();
Map<Snapshot.Key, Delete> deleteSet = new HashMap<>();
snapshot = new Snapshot(ANY_TX_ID, Isolation.SNAPSHOT, null, tableMetadataManager, parallelExecutor, readSet, new HashMap<>(), new HashMap<>(), deleteSet);
handler = new CrudHandler(storage, snapshot, tableMetadataManager);
when(scanner.iterator()).thenReturn(Arrays.asList(result, result2).iterator());
when(storage.scan(scan)).thenReturn(scanner);
Delete delete = new Delete(new Key(ANY_NAME_1, ANY_TEXT_1), new Key(ANY_NAME_2, ANY_TEXT_3)).forNamespace(ANY_NAMESPACE_NAME).forTable(ANY_TABLE_NAME);
// Act
handler.delete(delete);
List<Result> results = handler.scan(scan);
// Assert
assertThat(results.size()).isEqualTo(1);
assertThat(results.get(0)).isEqualTo(new FilteredResult(result, Collections.emptyList(), TABLE_METADATA));
// check the delete set
assertThat(deleteSet.size()).isEqualTo(1);
assertThat(deleteSet).containsKey(new Snapshot.Key(delete));
// check if the scanned data is inserted correctly in the read set
assertThat(readSet.size()).isEqualTo(2);
Snapshot.Key key1 = new Snapshot.Key(scan, result);
assertThat(readSet.get(key1).isPresent()).isTrue();
assertThat(readSet.get(key1).get()).isEqualTo(new TransactionResult(result));
Snapshot.Key key2 = new Snapshot.Key(scan, result2);
assertThat(readSet.get(key2).isPresent()).isTrue();
assertThat(readSet.get(key2).get()).isEqualTo(new TransactionResult(result2));
}
use of com.scalar.db.api.Delete in project scalardb by scalar-labs.
the class PrepareMutationComposerTest method prepareDelete.
private Delete prepareDelete() {
Key partitionKey = new Key(ANY_NAME_1, ANY_TEXT_1);
Key clusteringKey = new Key(ANY_NAME_2, ANY_TEXT_2);
return new Delete(partitionKey, clusteringKey).forNamespace(ANY_NAMESPACE_NAME).forTable(ANY_TABLE_NAME);
}
use of com.scalar.db.api.Delete in project scalardb by scalar-labs.
the class PrepareMutationComposerTest method delete_DeleteAndNullResultGiven_ShouldComposePutWithPutIfNotExistsCondition.
@Test
public void delete_DeleteAndNullResultGiven_ShouldComposePutWithPutIfNotExistsCondition() {
// Arrange
Delete delete = prepareDelete();
// Act
composer.add(delete, null);
// Assert
Put actual = (Put) mutations.get(0);
Put expected = new Put(delete.getPartitionKey(), delete.getClusteringKey().orElse(null)).forNamespace(delete.forNamespace().get()).forTable(delete.forTable().get());
expected.withConsistency(Consistency.LINEARIZABLE);
expected.withCondition(new PutIfNotExists());
expected.withValue(Attribute.toPreparedAtValue(ANY_TIME_5));
expected.withValue(Attribute.toIdValue(ANY_ID_3));
expected.withValue(Attribute.toStateValue(TransactionState.DELETED));
expected.withValue(Attribute.toVersionValue(1));
assertThat(actual).isEqualTo(expected);
}
use of com.scalar.db.api.Delete in project scalardb by scalar-labs.
the class PrepareMutationComposerTest method delete_DeleteAndResultGiven_ShouldComposePutWithPutIfCondition.
@Test
public void delete_DeleteAndResultGiven_ShouldComposePutWithPutIfCondition() {
// Arrange
Delete delete = prepareDelete();
TransactionResult result = prepareResult();
// Act
composer.add(delete, result);
// Assert
Put actual = (Put) mutations.get(0);
Put expected = new Put(delete.getPartitionKey(), delete.getClusteringKey().orElse(null)).forNamespace(delete.forNamespace().get()).forTable(delete.forTable().get());
expected.withConsistency(Consistency.LINEARIZABLE);
expected.withCondition(new PutIf(new ConditionalExpression(VERSION, toVersionValue(2), Operator.EQ), new ConditionalExpression(ID, toIdValue(ANY_ID_2), Operator.EQ)));
expected.withValue(Attribute.toPreparedAtValue(ANY_TIME_5));
expected.withValue(Attribute.toIdValue(ANY_ID_3));
expected.withValue(Attribute.toStateValue(TransactionState.DELETED));
expected.withValue(Attribute.toVersionValue(3));
expected.withValue(Attribute.toBeforePreparedAtValue(ANY_TIME_3));
expected.withValue(Attribute.toBeforeCommittedAtValue(ANY_TIME_4));
expected.withValue(Attribute.toBeforeIdValue(ANY_ID_2));
expected.withValue(Attribute.toBeforeStateValue(TransactionState.COMMITTED));
expected.withValue(Attribute.toBeforeVersionValue(2));
expected.withValue(Attribute.BEFORE_PREFIX + ANY_NAME_3, ANY_INT_2);
assertThat(actual).isEqualTo(expected);
}
use of com.scalar.db.api.Delete in project scalardb by scalar-labs.
the class RollbackMutationComposerTest method add_ScanAndPreparedResultByThisGivenAndBeforeResultNotGiven_ShouldComposeDelete.
@Test
public void add_ScanAndPreparedResultByThisGivenAndBeforeResultNotGiven_ShouldComposeDelete() throws ExecutionException {
// Arrange
composer = new RollbackMutationComposer(ANY_ID_2, storage, tableMetadataManager, mutations);
TransactionResult result = prepareInitialResult(ANY_ID_2, TransactionState.PREPARED);
Scan scan = prepareScan();
// Act
composer.add(scan, result);
// Assert
Delete actual = (Delete) mutations.get(0);
Delete expected = new Delete(scan.getPartitionKey(), result.getClusteringKey().orElse(null)).forNamespace(scan.forNamespace().get()).forTable(scan.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);
}
Aggregations