Search in sources :

Example 1 with Mutation

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

the class ScalarDbUtilsTest method copyAndSetTargetToIfNot_MutationsGiven_ShouldReturnDifferentInstance.

@Test
public void copyAndSetTargetToIfNot_MutationsGiven_ShouldReturnDifferentInstance() {
    // Arrange
    Put put = new Put(new Key("c1", "v1"));
    Delete delete = new Delete(new Key("c1", "v1"));
    List<Mutation> mutations = Arrays.asList(put, delete);
    // Act
    List<Mutation> actual = ScalarDbUtils.copyAndSetTargetToIfNot(mutations, NAMESPACE, TABLE);
    // Assert
    assertThat(actual == mutations).isFalse();
    assertThat(actual.get(0) == put).isFalse();
    assertThat(actual.get(1) == delete).isFalse();
    assertThat(put.forNamespace()).isNotPresent();
    assertThat(put.forTable()).isNotPresent();
    assertThat(delete.forNamespace()).isNotPresent();
    assertThat(delete.forTable()).isNotPresent();
    assertThat(actual.get(0).forNamespace()).isEqualTo(NAMESPACE);
    assertThat(actual.get(0).forTable()).isEqualTo(TABLE);
    assertThat(actual.get(1).forNamespace()).isEqualTo(NAMESPACE);
    assertThat(actual.get(1).forTable()).isEqualTo(TABLE);
}
Also used : Delete(com.scalar.db.api.Delete) Mutation(com.scalar.db.api.Mutation) Put(com.scalar.db.api.Put) Key(com.scalar.db.io.Key) Test(org.junit.jupiter.api.Test)

Example 2 with Mutation

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

the class DistributedStorageService method mutate.

@Override
public void mutate(MutateRequest request, StreamObserver<Empty> responseObserver) {
    execute(() -> {
        List<Mutation> mutations;
        if (request.getMutationCount() > 0) {
            TableMetadata metadata = tableMetadataManager.getTableMetadata(request.getMutationList().get(0).getNamespace(), request.getMutationList().get(0).getTable());
            if (metadata == null) {
                throw new IllegalArgumentException("the specified table is not found");
            }
            mutations = new ArrayList<>(request.getMutationCount());
            for (com.scalar.db.rpc.Mutation mutation : request.getMutationList()) {
                mutations.add(ProtoUtils.toMutation(mutation, metadata));
            }
        } else {
            mutations = Collections.emptyList();
        }
        storage.mutate(mutations);
        responseObserver.onNext(Empty.getDefaultInstance());
        responseObserver.onCompleted();
    }, responseObserver, "mutate");
}
Also used : TableMetadata(com.scalar.db.api.TableMetadata) DistributedStorageGrpc(com.scalar.db.rpc.DistributedStorageGrpc) Mutation(com.scalar.db.api.Mutation)

Example 3 with Mutation

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

the class GrpcStorageTest method mutate_StubThrowInternalError_ShouldThrowExecutionException.

@Test
public void mutate_StubThrowInternalError_ShouldThrowExecutionException() {
    // Arrange
    Key partitionKey = new Key("col1", 1);
    List<Mutation> mutations = Arrays.asList(new Put(partitionKey), new Delete(partitionKey));
    when(blockingStub.mutate(any())).thenThrow(Status.INTERNAL.asRuntimeException());
    // Act
    assertThatThrownBy(() -> storage.mutate(mutations)).isInstanceOf(ExecutionException.class);
}
Also used : Delete(com.scalar.db.api.Delete) Mutation(com.scalar.db.api.Mutation) Key(com.scalar.db.io.Key) Put(com.scalar.db.api.Put) Test(org.junit.jupiter.api.Test)

Example 4 with Mutation

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

the class GrpcStorageTest method mutate_isCalledWithProperArguments_StubShouldBeCalledProperly.

@Test
public void mutate_isCalledWithProperArguments_StubShouldBeCalledProperly() throws ExecutionException {
    // Arrange
    Key partitionKey = new Key("col1", 1);
    List<Mutation> mutations = Arrays.asList(new Put(partitionKey), new Delete(partitionKey));
    // Act
    storage.mutate(mutations);
    // Assert
    verify(blockingStub).mutate(any());
}
Also used : Delete(com.scalar.db.api.Delete) Mutation(com.scalar.db.api.Mutation) Key(com.scalar.db.io.Key) Put(com.scalar.db.api.Put) Test(org.junit.jupiter.api.Test)

Example 5 with Mutation

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

the class GrpcStorageTest method mutate_StubThrowInvalidArgumentError_ShouldThrowIllegalArgumentException.

@Test
public void mutate_StubThrowInvalidArgumentError_ShouldThrowIllegalArgumentException() {
    // Arrange
    Key partitionKey = new Key("col1", 1);
    List<Mutation> mutations = Arrays.asList(new Put(partitionKey), new Delete(partitionKey));
    when(blockingStub.mutate(any())).thenThrow(Status.INVALID_ARGUMENT.asRuntimeException());
    // Act Assert
    assertThatThrownBy(() -> storage.mutate(mutations)).isInstanceOf(IllegalArgumentException.class);
}
Also used : Delete(com.scalar.db.api.Delete) Mutation(com.scalar.db.api.Mutation) Key(com.scalar.db.io.Key) Put(com.scalar.db.api.Put) Test(org.junit.jupiter.api.Test)

Aggregations

Mutation (com.scalar.db.api.Mutation)24 Put (com.scalar.db.api.Put)16 Delete (com.scalar.db.api.Delete)14 Key (com.scalar.db.io.Key)7 Nonnull (javax.annotation.Nonnull)7 Test (org.junit.jupiter.api.Test)5 TableMetadata (com.scalar.db.api.TableMetadata)4 ConditionalExpression (com.scalar.db.api.ConditionalExpression)2 ExecutionException (com.scalar.db.exception.storage.ExecutionException)2 NoMutationException (com.scalar.db.exception.storage.NoMutationException)2 RetriableExecutionException (com.scalar.db.exception.storage.RetriableExecutionException)2 HashMap (java.util.HashMap)2 BatchStatement (com.datastax.driver.core.BatchStatement)1 ResultSet (com.datastax.driver.core.ResultSet)1 WriteTimeoutException (com.datastax.driver.core.exceptions.WriteTimeoutException)1 MutationCondition (com.scalar.db.api.MutationCondition)1 PutIf (com.scalar.db.api.PutIf)1 PutIfExists (com.scalar.db.api.PutIfExists)1 PutIfNotExists (com.scalar.db.api.PutIfNotExists)1 Value (com.scalar.db.io.Value)1