Search in sources :

Example 51 with Put

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

the class DistributedTransactionServiceWithConsensusCommitWithExtraReadIntegrationTest method commit_WriteSkewOnExistingRecordsWithSerializableWithExtraRead_OneShouldCommitTheOtherShouldThrowCommitConflictException.

private void commit_WriteSkewOnExistingRecordsWithSerializableWithExtraRead_OneShouldCommitTheOtherShouldThrowCommitConflictException(String table1, String table2) throws TransactionException {
    // Arrange
    List<Put> puts = Arrays.asList(preparePut(0, 0, table1).withValue(BALANCE, 1), preparePut(0, 1, table2).withValue(BALANCE, 1));
    GrpcTransaction transaction = manager.start();
    transaction.put(puts);
    transaction.commit();
    // Act
    GrpcTransaction transaction1 = manager.start();
    GrpcTransaction transaction2 = manager.start();
    Get get1_1 = prepareGet(0, 1, table2);
    Optional<Result> result1 = transaction1.get(get1_1);
    assertThat(result1).isPresent();
    int current1 = getBalance(result1.get());
    Get get1_2 = prepareGet(0, 0, table1);
    transaction1.get(get1_2);
    Get get2_1 = prepareGet(0, 0, table1);
    Optional<Result> result2 = transaction2.get(get2_1);
    assertThat(result2).isPresent();
    int current2 = getBalance(result2.get());
    Get get2_2 = prepareGet(0, 1, table2);
    transaction2.get(get2_2);
    Put put1 = preparePut(0, 0, table1).withValue(BALANCE, current1 + 1);
    transaction1.put(put1);
    Put put2 = preparePut(0, 1, table2).withValue(BALANCE, current2 + 1);
    transaction2.put(put2);
    transaction1.commit();
    Throwable thrown = catchThrowable(transaction2::commit);
    // Assert
    transaction = manager.start();
    result1 = transaction.get(get1_1);
    result2 = transaction.get(get2_1);
    transaction.commit();
    assertThat(result1).isPresent();
    assertThat(getBalance(result1.get())).isEqualTo(1);
    assertThat(result2).isPresent();
    assertThat(getBalance(result2.get())).isEqualTo(2);
    assertThat(thrown).isInstanceOf(CommitConflictException.class);
}
Also used : DistributedTransactionServiceWithConsensusCommitIntegrationTest.prepareGet(com.scalar.db.server.DistributedTransactionServiceWithConsensusCommitIntegrationTest.prepareGet) Get(com.scalar.db.api.Get) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) DistributedTransactionServiceWithConsensusCommitIntegrationTest.preparePut(com.scalar.db.server.DistributedTransactionServiceWithConsensusCommitIntegrationTest.preparePut) Put(com.scalar.db.api.Put) GrpcTransaction(com.scalar.db.transaction.rpc.GrpcTransaction) Result(com.scalar.db.api.Result)

Example 52 with Put

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

the class DistributedTransactionServiceWithConsensusCommitIntegrationTest method putAndCommit_PutGivenForNonExisting_ShouldCreateRecord.

@Test
public void putAndCommit_PutGivenForNonExisting_ShouldCreateRecord() throws TransactionException {
    // Arrange
    int expected = INITIAL_BALANCE;
    Put put = preparePut(0, 0, TABLE_1).withValue(BALANCE, expected);
    GrpcTransaction transaction = manager.start();
    // Act
    transaction.put(put);
    transaction.commit();
    // Assert
    Get get = prepareGet(0, 0, TABLE_1);
    GrpcTransaction another = manager.start();
    Optional<Result> result = another.get(get);
    another.commit();
    assertThat(result).isPresent();
    assertThat(getBalance(result.get())).isEqualTo(expected);
}
Also used : Get(com.scalar.db.api.Get) Put(com.scalar.db.api.Put) GrpcTransaction(com.scalar.db.transaction.rpc.GrpcTransaction) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Example 53 with Put

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

the class DistributedStorageServiceTest method mutate_StorageThrowsIllegalArgumentException_ShouldThrowInvalidArgumentError.

@Test
public void mutate_StorageThrowsIllegalArgumentException_ShouldThrowInvalidArgumentError() throws ExecutionException {
    // Arrange
    Key partitionKey = new Key("col1", 1);
    MutateRequest request = MutateRequest.newBuilder().addMutation(ProtoUtils.toMutation(new Put(partitionKey))).build();
    @SuppressWarnings("unchecked") StreamObserver<Empty> responseObserver = mock(StreamObserver.class);
    doThrow(IllegalArgumentException.class).when(storage).mutate(anyList());
    // Act
    storageService.mutate(request, responseObserver);
    // Assert
    verify(responseObserver).onError(exceptionCaptor.capture());
    assertThat(exceptionCaptor.getValue().getStatus().getCode()).isEqualTo(Status.Code.INVALID_ARGUMENT);
}
Also used : Empty(com.google.protobuf.Empty) MutateRequest(com.scalar.db.rpc.MutateRequest) Key(com.scalar.db.io.Key) Put(com.scalar.db.api.Put) Test(org.junit.jupiter.api.Test)

Example 54 with Put

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

the class DistributedStorageServiceTest method mutate_IsCalledWithMixedPutAndDelete_StorageShouldBeCalledProperly.

@Test
public void mutate_IsCalledWithMixedPutAndDelete_StorageShouldBeCalledProperly() throws ExecutionException {
    // Arrange
    Key partitionKey = new Key("col1", 1);
    MutateRequest request = MutateRequest.newBuilder().addAllMutation(Arrays.asList(ProtoUtils.toMutation(new Put(partitionKey)), ProtoUtils.toMutation(new Delete(partitionKey)))).build();
    @SuppressWarnings("unchecked") StreamObserver<Empty> responseObserver = mock(StreamObserver.class);
    // Act
    storageService.mutate(request, responseObserver);
    // Assert
    verify(storage).mutate(anyList());
    verify(responseObserver).onNext(any());
    verify(responseObserver).onCompleted();
}
Also used : Delete(com.scalar.db.api.Delete) Empty(com.google.protobuf.Empty) MutateRequest(com.scalar.db.rpc.MutateRequest) Key(com.scalar.db.io.Key) Put(com.scalar.db.api.Put) Test(org.junit.jupiter.api.Test)

Example 55 with Put

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

the class DistributedStorageServiceTest method mutate_IsCalledWithSinglePut_StorageShouldBeCalledProperly.

@Test
public void mutate_IsCalledWithSinglePut_StorageShouldBeCalledProperly() throws ExecutionException {
    // Arrange
    Key partitionKey = new Key("col1", 1);
    MutateRequest request = MutateRequest.newBuilder().addMutation(ProtoUtils.toMutation(new Put(partitionKey))).build();
    @SuppressWarnings("unchecked") StreamObserver<Empty> responseObserver = mock(StreamObserver.class);
    // Act
    storageService.mutate(request, responseObserver);
    // Assert
    verify(storage).mutate(anyList());
    verify(responseObserver).onNext(any());
    verify(responseObserver).onCompleted();
}
Also used : Empty(com.google.protobuf.Empty) MutateRequest(com.scalar.db.rpc.MutateRequest) Key(com.scalar.db.io.Key) Put(com.scalar.db.api.Put) Test(org.junit.jupiter.api.Test)

Aggregations

Put (com.scalar.db.api.Put)374 Key (com.scalar.db.io.Key)216 Test (org.junit.jupiter.api.Test)209 Result (com.scalar.db.api.Result)108 Get (com.scalar.db.api.Get)93 Test (org.junit.Test)67 Delete (com.scalar.db.api.Delete)64 IntValue (com.scalar.db.io.IntValue)63 TextValue (com.scalar.db.io.TextValue)48 Scan (com.scalar.db.api.Scan)44 Value (com.scalar.db.io.Value)37 Assertions.catchThrowable (org.assertj.core.api.Assertions.catchThrowable)35 BooleanValue (com.scalar.db.io.BooleanValue)33 ConditionalExpression (com.scalar.db.api.ConditionalExpression)30 PutIfNotExists (com.scalar.db.api.PutIfNotExists)29 PutIf (com.scalar.db.api.PutIf)28 DoubleValue (com.scalar.db.io.DoubleValue)26 GrpcTransaction (com.scalar.db.transaction.rpc.GrpcTransaction)19 ExecutionException (com.scalar.db.exception.storage.ExecutionException)17 Mutation (com.scalar.db.api.Mutation)16