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