Search in sources :

Example 66 with Delete

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));
}
Also used : Delete(com.scalar.db.api.Delete) Optional(java.util.Optional) HashMap(java.util.HashMap) ResultImpl(com.scalar.db.common.ResultImpl) Result(com.scalar.db.api.Result) TextColumn(com.scalar.db.io.TextColumn) Column(com.scalar.db.io.Column) Scan(com.scalar.db.api.Scan) Key(com.scalar.db.io.Key) Test(org.junit.jupiter.api.Test)

Example 67 with Delete

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);
}
Also used : Delete(com.scalar.db.api.Delete) Key(com.scalar.db.io.Key)

Example 68 with Delete

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);
}
Also used : Delete(com.scalar.db.api.Delete) PutIfNotExists(com.scalar.db.api.PutIfNotExists) Put(com.scalar.db.api.Put) Test(org.junit.jupiter.api.Test)

Example 69 with Delete

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);
}
Also used : Delete(com.scalar.db.api.Delete) PutIf(com.scalar.db.api.PutIf) ConditionalExpression(com.scalar.db.api.ConditionalExpression) Put(com.scalar.db.api.Put) Test(org.junit.jupiter.api.Test)

Example 70 with Delete

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);
}
Also used : Delete(com.scalar.db.api.Delete) ConditionalExpression(com.scalar.db.api.ConditionalExpression) Scan(com.scalar.db.api.Scan) DeleteIf(com.scalar.db.api.DeleteIf) Test(org.junit.jupiter.api.Test)

Aggregations

Delete (com.scalar.db.api.Delete)174 Key (com.scalar.db.io.Key)112 Test (org.junit.jupiter.api.Test)111 Put (com.scalar.db.api.Put)59 Get (com.scalar.db.api.Get)29 Result (com.scalar.db.api.Result)29 Test (org.junit.Test)25 DeleteIfExists (com.scalar.db.api.DeleteIfExists)24 ConditionalExpression (com.scalar.db.api.ConditionalExpression)16 DeleteIf (com.scalar.db.api.DeleteIf)15 Mutation (com.scalar.db.api.Mutation)14 MutationCondition (com.scalar.db.api.MutationCondition)11 Scan (com.scalar.db.api.Scan)11 PutIfNotExists (com.scalar.db.api.PutIfNotExists)10 TextValue (com.scalar.db.io.TextValue)10 ExecutionException (com.scalar.db.exception.storage.ExecutionException)8 CosmosStoredProcedureRequestOptions (com.azure.cosmos.models.CosmosStoredProcedureRequestOptions)6 GrpcTransaction (com.scalar.db.transaction.rpc.GrpcTransaction)6 DeleteItemRequest (software.amazon.awssdk.services.dynamodb.model.DeleteItemRequest)6 PartitionKey (com.azure.cosmos.models.PartitionKey)5