Search in sources :

Example 86 with Delete

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

the class MultiStorageIntegrationTest method whenMutateDataToTable3_ShouldExecuteForDefaultStorage.

@Test
public void whenMutateDataToTable3_ShouldExecuteForDefaultStorage() throws ExecutionException {
    // Arrange
    String namespace = NAMESPACE1;
    String table = TABLE3;
    Key partitionKey = new Key(COL_NAME1, 1);
    Key clusteringKey1 = new Key(COL_NAME4, 1);
    Key clusteringKey2 = new Key(COL_NAME4, 2);
    Put put = new Put(partitionKey, clusteringKey1).withValue(COL_NAME3, 3).forNamespace(namespace).forTable(table);
    storage1.put(put);
    storage2.put(put);
    // Act
    multiStorage.mutate(Arrays.asList(new Put(partitionKey, clusteringKey2).withValue(COL_NAME3, 3).forNamespace(namespace).forTable(table), new Delete(partitionKey, clusteringKey1).forNamespace(namespace).forTable(table)));
    // Assert
    Get get1 = new Get(partitionKey, clusteringKey1).forNamespace(namespace).forTable(table);
    Get get2 = new Get(partitionKey, clusteringKey2).forNamespace(namespace).forTable(table);
    Optional<Result> result = multiStorage.get(get1);
    assertThat(result.isPresent()).isFalse();
    result = multiStorage.get(get2);
    assertThat(result.isPresent()).isTrue();
    assertThat(getCol1Value(result.get())).isEqualTo(1);
    assertThat(getCol3Value(result.get())).isEqualTo(3);
    assertThat(getCol4Value(result.get())).isEqualTo(2);
    result = storage1.get(get1);
    assertThat(result.isPresent()).isFalse();
    result = storage1.get(get2);
    assertThat(result.isPresent()).isTrue();
    assertThat(getCol1Value(result.get())).isEqualTo(1);
    assertThat(getCol3Value(result.get())).isEqualTo(3);
    assertThat(getCol4Value(result.get())).isEqualTo(2);
    result = storage2.get(get1);
    assertThat(result.isPresent()).isTrue();
    assertThat(getCol1Value(result.get())).isEqualTo(1);
    assertThat(getCol3Value(result.get())).isEqualTo(3);
    assertThat(getCol4Value(result.get())).isEqualTo(1);
    result = storage2.get(get2);
    assertThat(result.isPresent()).isFalse();
}
Also used : Delete(com.scalar.db.api.Delete) Get(com.scalar.db.api.Get) Key(com.scalar.db.io.Key) Put(com.scalar.db.api.Put) Result(com.scalar.db.api.Result) Test(org.junit.jupiter.api.Test)

Example 87 with Delete

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

the class MultiStorageIntegrationTest method whenDeleteDataFromTable3_DataShouldBeDeletedFromDefaultStorage.

@Test
public void whenDeleteDataFromTable3_DataShouldBeDeletedFromDefaultStorage() throws ExecutionException {
    // Arrange
    String namespace = NAMESPACE1;
    String table = TABLE3;
    Key partitionKey = new Key(COL_NAME1, 1);
    Key clusteringKey = new Key(COL_NAME4, 4);
    Put put = new Put(partitionKey, clusteringKey).withValue(COL_NAME3, 3).forNamespace(namespace).forTable(table);
    storage1.put(put);
    storage2.put(put);
    // Act
    multiStorage.delete(new Delete(partitionKey, clusteringKey).forNamespace(namespace).forTable(table));
    // Assert
    Get get = new Get(partitionKey, clusteringKey).forNamespace(namespace).forTable(table);
    Optional<Result> result = multiStorage.get(get);
    assertThat(result.isPresent()).isFalse();
    result = storage1.get(get);
    assertThat(result.isPresent()).isFalse();
    result = storage2.get(get);
    assertThat(result.isPresent()).isTrue();
    assertThat(getCol1Value(result.get())).isEqualTo(1);
    assertThat(getCol3Value(result.get())).isEqualTo(3);
    assertThat(getCol4Value(result.get())).isEqualTo(4);
}
Also used : Delete(com.scalar.db.api.Delete) Get(com.scalar.db.api.Get) Key(com.scalar.db.io.Key) Put(com.scalar.db.api.Put) Result(com.scalar.db.api.Result) Test(org.junit.jupiter.api.Test)

Example 88 with Delete

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

the class ConsensusCommitSpecificIntegrationTestBase method commit_DeleteGivenWithoutRead_ShouldNotThrowAnyExceptions.

@Test
public void commit_DeleteGivenWithoutRead_ShouldNotThrowAnyExceptions() {
    // Arrange
    Delete delete = prepareDelete(0, 0, namespace1, TABLE_1);
    ConsensusCommit transaction = manager.start();
    // Act Assert
    transaction.delete(delete);
    assertThatCode(transaction::commit).doesNotThrowAnyException();
}
Also used : Delete(com.scalar.db.api.Delete) Test(org.junit.jupiter.api.Test)

Example 89 with Delete

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

the class TwoPhaseConsensusCommitSpecificIntegrationTestBase method prepareDelete.

private Delete prepareDelete(int id, int type, String table) {
    Key partitionKey = new Key(ACCOUNT_ID, id);
    Key clusteringKey = new Key(ACCOUNT_TYPE, type);
    return new Delete(partitionKey, clusteringKey).forNamespace(namespace).forTable(table).withConsistency(Consistency.LINEARIZABLE);
}
Also used : Delete(com.scalar.db.api.Delete) Key(com.scalar.db.io.Key)

Example 90 with Delete

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

the class JdbcDatabase method mutate.

@Override
public void mutate(List<? extends Mutation> mutations) throws ExecutionException {
    if (mutations.size() == 1) {
        Mutation mutation = mutations.get(0);
        if (mutation instanceof Put) {
            put((Put) mutation);
        } else if (mutation instanceof Delete) {
            delete((Delete) mutation);
        }
        return;
    }
    mutations = copyAndSetTargetToIfNot(mutations);
    Connection connection = null;
    try {
        connection = dataSource.getConnection();
        connection.setAutoCommit(false);
    } catch (SQLException e) {
        close(connection);
        throw new ExecutionException("mutate operation failed", e);
    }
    try {
        if (!jdbcService.mutate(mutations, connection)) {
            try {
                connection.rollback();
            } catch (SQLException e) {
                throw new ExecutionException("failed to rollback", e);
            }
            throw new NoMutationException("no mutation was applied");
        } else {
            connection.commit();
        }
    } catch (SQLException e) {
        try {
            connection.rollback();
        } catch (SQLException sqlException) {
            throw new ExecutionException("failed to rollback", sqlException);
        }
        if (JdbcUtils.isConflictError(e, rdbEngine)) {
            // conflicts can happen. Throw RetriableExecutionException in that case.
            throw new RetriableExecutionException("conflict happened in a mutate operation", e);
        }
        throw new ExecutionException("mutate operation failed", e);
    } finally {
        close(connection);
    }
}
Also used : Delete(com.scalar.db.api.Delete) SQLException(java.sql.SQLException) Connection(java.sql.Connection) Mutation(com.scalar.db.api.Mutation) ExecutionException(com.scalar.db.exception.storage.ExecutionException) RetriableExecutionException(com.scalar.db.exception.storage.RetriableExecutionException) RetriableExecutionException(com.scalar.db.exception.storage.RetriableExecutionException) NoMutationException(com.scalar.db.exception.storage.NoMutationException) Put(com.scalar.db.api.Put)

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