use of com.scalar.db.io.IntValue in project scalardb by scalar-labs.
the class DistributedStorageIntegrationTestBase method put_PutWithNullValue_ShouldPutProperly.
@Test
public void put_PutWithNullValue_ShouldPutProperly() throws ExecutionException {
// Arrange
Put put = preparePuts().get(0);
storage.put(put);
put.withTextValue(COL_NAME2, null);
put.withBooleanValue(COL_NAME5, null);
// Act
storage.put(put);
// Assert
Optional<Result> actual = storage.get(prepareGet(0, 0));
assertThat(actual.isPresent()).isTrue();
Result result = actual.get();
assertThat(result.getValue(COL_NAME1)).isEqualTo(Optional.of(new IntValue(COL_NAME1, 0)));
assertThat(result.getValue(COL_NAME4)).isEqualTo(Optional.of(new IntValue(COL_NAME4, 0)));
assertThat(result.getValue(COL_NAME2)).isEqualTo(Optional.of(new TextValue(COL_NAME2, (String) null)));
assertThat(result.getValue(COL_NAME3)).isEqualTo(Optional.of(new IntValue(COL_NAME3, 0)));
assertThat(result.getValue(COL_NAME5)).isEqualTo(Optional.of(new BooleanValue(COL_NAME5, false)));
assertThat(result.getContainedColumnNames()).isEqualTo(new HashSet<>(Arrays.asList(COL_NAME1, COL_NAME2, COL_NAME3, COL_NAME4, COL_NAME5, COL_NAME6)));
assertThat(result.contains(COL_NAME1)).isTrue();
assertThat(result.isNull(COL_NAME1)).isFalse();
assertThat(result.getInt(COL_NAME1)).isEqualTo(0);
assertThat(result.getAsObject(COL_NAME1)).isEqualTo(0);
assertThat(result.contains(COL_NAME4)).isTrue();
assertThat(result.isNull(COL_NAME4)).isFalse();
assertThat(result.getInt(COL_NAME4)).isEqualTo(0);
assertThat(result.getAsObject(COL_NAME4)).isEqualTo(0);
assertThat(result.contains(COL_NAME2)).isTrue();
assertThat(result.isNull(COL_NAME2)).isTrue();
assertThat(result.getText(COL_NAME2)).isNull();
assertThat(result.getAsObject(COL_NAME2)).isNull();
assertThat(result.contains(COL_NAME3)).isTrue();
assertThat(result.isNull(COL_NAME3)).isFalse();
assertThat(result.getInt(COL_NAME3)).isEqualTo(0);
assertThat(result.getAsObject(COL_NAME3)).isEqualTo(0);
assertThat(result.contains(COL_NAME5)).isTrue();
assertThat(result.isNull(COL_NAME5)).isTrue();
assertThat(result.getBoolean(COL_NAME5)).isFalse();
assertThat(result.getAsObject(COL_NAME5)).isNull();
assertThat(result.contains(COL_NAME6)).isTrue();
assertThat(result.isNull(COL_NAME6)).isTrue();
assertThat(result.getBlob(COL_NAME6)).isNull();
assertThat(result.getAsObject(COL_NAME6)).isNull();
}
use of com.scalar.db.io.IntValue in project scalardb by scalar-labs.
the class DistributedStorageIntegrationTestBase method put_PutWithIfExistsGivenWhenSuchRecordExists_ShouldUpdateRecord.
@Test
public void put_PutWithIfExistsGivenWhenSuchRecordExists_ShouldUpdateRecord() throws ExecutionException {
// Arrange
int pKey = 0;
int cKey = 0;
List<Put> puts = preparePuts();
Get get = prepareGet(pKey, cKey);
// Act Assert
storage.put(puts.get(0));
puts.get(0).withCondition(new PutIfExists());
puts.get(0).withValue(COL_NAME3, Integer.MAX_VALUE);
assertThatCode(() -> storage.put(puts.get(0))).doesNotThrowAnyException();
// Assert
Optional<Result> actual = storage.get(get);
assertThat(actual.isPresent()).isTrue();
Result result = actual.get();
assertThat(result.getValue(COL_NAME1)).isEqualTo(Optional.of(new IntValue(COL_NAME1, pKey)));
assertThat(result.getValue(COL_NAME4)).isEqualTo(Optional.of(new IntValue(COL_NAME4, cKey)));
assertThat(result.getValue(COL_NAME3)).isEqualTo(Optional.of(new IntValue(COL_NAME3, Integer.MAX_VALUE)));
}
use of com.scalar.db.io.IntValue in project scalardb by scalar-labs.
the class DistributedStorageIntegrationTestBase method mutate_SinglePutGiven_ShouldStoreProperly.
@Test
public void mutate_SinglePutGiven_ShouldStoreProperly() throws ExecutionException {
// Arrange
int pKey = 0;
int cKey = 0;
List<Put> puts = preparePuts();
Key partitionKey = new Key(COL_NAME1, pKey);
Key clusteringKey = new Key(COL_NAME4, cKey);
Get get = new Get(partitionKey, clusteringKey);
// Act
storage.mutate(Collections.singletonList(puts.get(pKey * 2 + cKey)));
// Assert
Optional<Result> actual = storage.get(get);
assertThat(actual.isPresent()).isTrue();
assertThat(actual.get().getValue(COL_NAME1)).isEqualTo(Optional.of(new IntValue(COL_NAME1, pKey)));
assertThat(actual.get().getValue(COL_NAME2)).isEqualTo(Optional.of(new TextValue(COL_NAME2, Integer.toString(pKey + cKey))));
assertThat(actual.get().getValue(COL_NAME3)).isEqualTo(Optional.of(new IntValue(COL_NAME3, pKey + cKey)));
assertThat(actual.get().getValue(COL_NAME4)).isEqualTo(Optional.of(new IntValue(COL_NAME4, cKey)));
assertThat(actual.get().getValue(COL_NAME5)).isEqualTo(Optional.of(new BooleanValue(COL_NAME5, cKey % 2 == 0)));
}
use of com.scalar.db.io.IntValue in project scalardb by scalar-labs.
the class DistributedTransactionIntegrationTestBase method putAndCommit_GetsAndPutsGiven_ShouldCommitProperly.
@Test
public void putAndCommit_GetsAndPutsGiven_ShouldCommitProperly() throws TransactionException {
// Arrange
populateRecords();
List<Get> gets = prepareGets();
int amount = 100;
int fromId = 0;
int toId = NUM_TYPES;
// Act
DistributedTransaction transaction = manager.start();
Optional<Result> fromResult = transaction.get(gets.get(fromId));
assertThat(fromResult.isPresent()).isTrue();
IntValue fromBalance = new IntValue(BALANCE, getBalance(fromResult.get()) - amount);
Optional<Result> toResult = transaction.get(gets.get(toId));
assertThat(toResult.isPresent()).isTrue();
IntValue toBalance = new IntValue(BALANCE, getBalance(toResult.get()) + amount);
List<Put> puts = preparePuts();
puts.get(fromId).withValue(fromBalance);
puts.get(toId).withValue(toBalance);
transaction.put(puts.get(fromId));
transaction.put(puts.get(toId));
transaction.commit();
// Assert
DistributedTransaction another = manager.start();
fromResult = another.get(gets.get(fromId));
assertThat(fromResult.isPresent()).isTrue();
assertThat(getBalance(fromResult.get())).isEqualTo(INITIAL_BALANCE - amount);
toResult = another.get(gets.get(toId));
assertThat(toResult.isPresent()).isTrue();
assertThat(getBalance(toResult.get())).isEqualTo(INITIAL_BALANCE + amount);
another.commit();
}
use of com.scalar.db.io.IntValue in project scalardb by scalar-labs.
the class CoordinatorTest method getState_TransactionIdGiven_ShouldReturnState.
@Test
public void getState_TransactionIdGiven_ShouldReturnState() throws ExecutionException, CoordinatorException {
// Arrange
Result result = mock(Result.class);
when(result.getValue(Attribute.ID)).thenReturn(Optional.of(new TextValue(Attribute.ID, ANY_ID_1)));
when(result.getValue(Attribute.STATE)).thenReturn(Optional.of(new IntValue(Attribute.STATE, TransactionState.COMMITTED.get())));
when(result.getValue(Attribute.CREATED_AT)).thenReturn(Optional.of(new BigIntValue(Attribute.CREATED_AT, ANY_TIME_1)));
when(storage.get(any(Get.class))).thenReturn(Optional.of(result));
// Act
Optional<Coordinator.State> state = coordinator.getState(ANY_ID_1);
// Assert
assertThat(state.get().getId()).isEqualTo(ANY_ID_1);
Assertions.assertThat(state.get().getState()).isEqualTo(TransactionState.COMMITTED);
assertThat(state.get().getCreatedAt()).isEqualTo(ANY_TIME_1);
}
Aggregations