Search in sources :

Example 56 with TextValue

use of com.scalar.db.io.TextValue in project scalardb by scalar-labs.

the class CoordinatorTest method createPutWith_StateGiven_ShouldCreateWithCorrectValues.

@Test
public void createPutWith_StateGiven_ShouldCreateWithCorrectValues() throws ExecutionException {
    // Arrange
    long current = System.currentTimeMillis();
    Coordinator.State state = new Coordinator.State(ANY_ID_1, TransactionState.COMMITTED, current);
    doNothing().when(storage).put(any(Put.class));
    // Act
    Put put = coordinator.createPutWith(state);
    // Assert
    assertThat(put.getPartitionKey().get().get(0)).isEqualTo(new TextValue(Attribute.ID, ANY_ID_1));
    assertThat(put.getColumns().get(Attribute.STATE)).isEqualTo(ScalarDbUtils.toColumn(Attribute.toStateValue(TransactionState.COMMITTED)));
    assertThat(put.getColumns().get(Attribute.CREATED_AT)).isEqualTo(ScalarDbUtils.toColumn(Attribute.toCreatedAtValue(current)));
    assertThat(put.getConsistency()).isEqualTo(Consistency.LINEARIZABLE);
    assertThat(put.getCondition().get()).isExactlyInstanceOf(PutIfNotExists.class);
    assertThat(put.forNamespace().get()).isEqualTo(Coordinator.NAMESPACE);
    assertThat(put.forTable().get()).isEqualTo(Coordinator.TABLE);
}
Also used : TransactionState(com.scalar.db.api.TransactionState) TextValue(com.scalar.db.io.TextValue) Put(com.scalar.db.api.Put) Test(org.junit.jupiter.api.Test)

Example 57 with TextValue

use of com.scalar.db.io.TextValue 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);
}
Also used : TextValue(com.scalar.db.io.TextValue) TransactionState(com.scalar.db.api.TransactionState) Get(com.scalar.db.api.Get) IntValue(com.scalar.db.io.IntValue) BigIntValue(com.scalar.db.io.BigIntValue) Result(com.scalar.db.api.Result) BigIntValue(com.scalar.db.io.BigIntValue) Test(org.junit.jupiter.api.Test)

Example 58 with TextValue

use of com.scalar.db.io.TextValue in project scalardb by scalar-labs.

the class CoordinatorTest method getState_WithCoordinatorNamespaceChanged_ShouldGetWithChangedNamespace.

@Test
public void getState_WithCoordinatorNamespaceChanged_ShouldGetWithChangedNamespace() throws ExecutionException, CoordinatorException {
    // Arrange
    when(config.getCoordinatorNamespace()).thenReturn(Optional.of("changed_coordinator"));
    coordinator = new Coordinator(storage, config);
    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
    ArgumentCaptor<Get> captor = ArgumentCaptor.forClass(Get.class);
    verify(storage).get(captor.capture());
    assertThat(captor.getValue().forNamespace().get()).isEqualTo("changed_coordinator");
    assertThat(captor.getValue().forTable().get()).isEqualTo(Coordinator.TABLE);
    assertThat(state.get().getId()).isEqualTo(ANY_ID_1);
    Assertions.assertThat(state.get().getState()).isEqualTo(TransactionState.COMMITTED);
    assertThat(state.get().getCreatedAt()).isEqualTo(ANY_TIME_1);
}
Also used : TextValue(com.scalar.db.io.TextValue) TransactionState(com.scalar.db.api.TransactionState) Get(com.scalar.db.api.Get) IntValue(com.scalar.db.io.IntValue) BigIntValue(com.scalar.db.io.BigIntValue) Result(com.scalar.db.api.Result) BigIntValue(com.scalar.db.io.BigIntValue) Test(org.junit.jupiter.api.Test)

Example 59 with TextValue

use of com.scalar.db.io.TextValue in project scalardb by scalar-labs.

the class MergedResultTest method getValue_ResultAndPutGiven_ShouldReturnMergedValue.

@Test
public void getValue_ResultAndPutGiven_ShouldReturnMergedValue() {
    // Arrange
    Put put = new Put(new Key(ANY_NAME_1, ANY_TEXT_1), new Key(ANY_NAME_2, ANY_TEXT_2)).withValue(ANY_NAME_3, ANY_INT_3);
    MergedResult mergedResult = new MergedResult(Optional.of(result), put, TABLE_METADATA);
    // Act Assert
    assertThat(mergedResult.getValue(ANY_NAME_1)).isEqualTo(Optional.of(new TextValue(ANY_NAME_1, ANY_TEXT_1)));
    assertThat(mergedResult.getValue(ANY_NAME_2)).isEqualTo(Optional.of(new TextValue(ANY_NAME_2, ANY_TEXT_2)));
    assertThat(mergedResult.getValue(ANY_NAME_3)).isEqualTo(Optional.of(new IntValue(ANY_NAME_3, ANY_INT_3)));
    assertThat(mergedResult.getValue(ANY_NAME_4)).isEqualTo(Optional.of(new TextValue(ANY_NAME_4, ANY_TEXT_3)));
    assertThat(mergedResult.getValue(Attribute.ID)).isEqualTo(Optional.of(Attribute.toIdValue(ANY_ID_2)));
    assertThat(mergedResult.getValue(Attribute.PREPARED_AT)).isEqualTo(Optional.of(Attribute.toPreparedAtValue(ANY_TIME_3)));
    assertThat(mergedResult.getValue(Attribute.COMMITTED_AT)).isEqualTo(Optional.of(Attribute.toCommittedAtValue(ANY_TIME_4)));
    assertThat(mergedResult.getValue(Attribute.STATE)).isEqualTo(Optional.of(Attribute.toStateValue(TransactionState.COMMITTED)));
    assertThat(mergedResult.getValue(Attribute.VERSION)).isEqualTo(Optional.of(Attribute.toVersionValue(ANY_VERSION_2)));
    assertThat(mergedResult.getValue(Attribute.BEFORE_PREFIX + ANY_NAME_3)).isEqualTo(Optional.of(new IntValue(Attribute.BEFORE_PREFIX + ANY_NAME_3, ANY_INT_1)));
    assertThat(mergedResult.getValue(Attribute.BEFORE_ID)).isEqualTo(Optional.of(Attribute.toBeforeIdValue(ANY_ID_1)));
    assertThat(mergedResult.getValue(Attribute.BEFORE_PREPARED_AT)).isEqualTo(Optional.of(Attribute.toBeforePreparedAtValue(ANY_TIME_1)));
    assertThat(mergedResult.getValue(Attribute.BEFORE_COMMITTED_AT)).isEqualTo(Optional.of(Attribute.toBeforeCommittedAtValue(ANY_TIME_2)));
    assertThat(mergedResult.getValue(Attribute.BEFORE_STATE)).isEqualTo(Optional.of(Attribute.toBeforeStateValue(TransactionState.COMMITTED)));
    assertThat(mergedResult.getValue(Attribute.BEFORE_VERSION)).isEqualTo(Optional.of(Attribute.toBeforeVersionValue(ANY_VERSION_1)));
    assertThat(mergedResult.getContainedColumnNames()).isEqualTo(new HashSet<>(Arrays.asList(ANY_NAME_1, ANY_NAME_2, ANY_NAME_3, ANY_NAME_4, Attribute.ID, Attribute.PREPARED_AT, Attribute.COMMITTED_AT, Attribute.STATE, Attribute.VERSION, Attribute.BEFORE_PREFIX + ANY_NAME_3, Attribute.BEFORE_PREFIX + ANY_NAME_4, Attribute.BEFORE_ID, Attribute.BEFORE_PREPARED_AT, Attribute.BEFORE_COMMITTED_AT, Attribute.BEFORE_STATE, Attribute.BEFORE_VERSION)));
    assertThat(mergedResult.contains(ANY_NAME_1)).isTrue();
    assertThat(mergedResult.isNull(ANY_NAME_1)).isFalse();
    assertThat(mergedResult.getText(ANY_NAME_1)).isEqualTo(ANY_TEXT_1);
    assertThat(mergedResult.getAsObject(ANY_NAME_1)).isEqualTo(ANY_TEXT_1);
    assertThat(mergedResult.contains(ANY_NAME_2)).isTrue();
    assertThat(mergedResult.isNull(ANY_NAME_2)).isFalse();
    assertThat(mergedResult.getText(ANY_NAME_2)).isEqualTo(ANY_TEXT_2);
    assertThat(mergedResult.getAsObject(ANY_NAME_2)).isEqualTo(ANY_TEXT_2);
    assertThat(mergedResult.contains(ANY_NAME_3)).isTrue();
    assertThat(mergedResult.isNull(ANY_NAME_3)).isFalse();
    assertThat(mergedResult.getInt(ANY_NAME_3)).isEqualTo(ANY_INT_3);
    assertThat(mergedResult.getAsObject(ANY_NAME_3)).isEqualTo(ANY_INT_3);
    assertThat(mergedResult.contains(ANY_NAME_4)).isTrue();
    assertThat(mergedResult.isNull(ANY_NAME_4)).isFalse();
    assertThat(mergedResult.getText(ANY_NAME_4)).isEqualTo(ANY_TEXT_3);
    assertThat(mergedResult.getAsObject(ANY_NAME_4)).isEqualTo(ANY_TEXT_3);
    assertThat(mergedResult.contains(Attribute.ID)).isTrue();
    assertThat(mergedResult.isNull(Attribute.ID)).isFalse();
    assertThat(mergedResult.getText(Attribute.ID)).isEqualTo(ANY_ID_2);
    assertThat(mergedResult.getAsObject(Attribute.ID)).isEqualTo(ANY_ID_2);
    assertThat(mergedResult.contains(Attribute.PREPARED_AT)).isTrue();
    assertThat(mergedResult.isNull(Attribute.PREPARED_AT)).isFalse();
    assertThat(mergedResult.getBigInt(Attribute.PREPARED_AT)).isEqualTo(ANY_TIME_3);
    assertThat(mergedResult.getAsObject(Attribute.PREPARED_AT)).isEqualTo(ANY_TIME_3);
    assertThat(mergedResult.contains(Attribute.COMMITTED_AT)).isTrue();
    assertThat(mergedResult.isNull(Attribute.COMMITTED_AT)).isFalse();
    assertThat(mergedResult.getBigInt(Attribute.COMMITTED_AT)).isEqualTo(ANY_TIME_4);
    assertThat(mergedResult.getAsObject(Attribute.COMMITTED_AT)).isEqualTo(ANY_TIME_4);
    assertThat(mergedResult.contains(Attribute.STATE)).isTrue();
    assertThat(mergedResult.isNull(Attribute.STATE)).isFalse();
    assertThat(mergedResult.getInt(Attribute.STATE)).isEqualTo(TransactionState.COMMITTED.get());
    assertThat(mergedResult.getAsObject(Attribute.STATE)).isEqualTo(TransactionState.COMMITTED.get());
    assertThat(mergedResult.contains(Attribute.VERSION)).isTrue();
    assertThat(mergedResult.isNull(Attribute.VERSION)).isFalse();
    assertThat(mergedResult.getInt(Attribute.VERSION)).isEqualTo(ANY_VERSION_2);
    assertThat(mergedResult.getAsObject(Attribute.VERSION)).isEqualTo(ANY_VERSION_2);
    assertThat(mergedResult.contains(Attribute.BEFORE_PREFIX + ANY_NAME_3)).isTrue();
    assertThat(mergedResult.isNull(Attribute.BEFORE_PREFIX + ANY_NAME_3)).isFalse();
    assertThat(mergedResult.getInt(Attribute.BEFORE_PREFIX + ANY_NAME_3)).isEqualTo(ANY_INT_1);
    assertThat(mergedResult.getAsObject(Attribute.BEFORE_PREFIX + ANY_NAME_3)).isEqualTo(ANY_INT_1);
    assertThat(mergedResult.contains(Attribute.BEFORE_PREFIX + ANY_NAME_4)).isTrue();
    assertThat(mergedResult.isNull(Attribute.BEFORE_PREFIX + ANY_NAME_4)).isFalse();
    assertThat(mergedResult.getText(Attribute.BEFORE_PREFIX + ANY_NAME_4)).isEqualTo(ANY_TEXT_4);
    assertThat(mergedResult.getAsObject(Attribute.BEFORE_PREFIX + ANY_NAME_4)).isEqualTo(ANY_TEXT_4);
    assertThat(mergedResult.contains(Attribute.BEFORE_ID)).isTrue();
    assertThat(mergedResult.isNull(Attribute.BEFORE_ID)).isFalse();
    assertThat(mergedResult.getText(Attribute.BEFORE_ID)).isEqualTo(ANY_ID_1);
    assertThat(mergedResult.getAsObject(Attribute.BEFORE_ID)).isEqualTo(ANY_ID_1);
    assertThat(mergedResult.contains(Attribute.BEFORE_PREPARED_AT)).isTrue();
    assertThat(mergedResult.isNull(Attribute.BEFORE_PREPARED_AT)).isFalse();
    assertThat(mergedResult.getBigInt(Attribute.BEFORE_PREPARED_AT)).isEqualTo(ANY_TIME_1);
    assertThat(mergedResult.getAsObject(Attribute.BEFORE_PREPARED_AT)).isEqualTo(ANY_TIME_1);
    assertThat(mergedResult.contains(Attribute.BEFORE_COMMITTED_AT)).isTrue();
    assertThat(mergedResult.isNull(Attribute.BEFORE_COMMITTED_AT)).isFalse();
    assertThat(mergedResult.getBigInt(Attribute.BEFORE_COMMITTED_AT)).isEqualTo(ANY_TIME_2);
    assertThat(mergedResult.getAsObject(Attribute.BEFORE_COMMITTED_AT)).isEqualTo(ANY_TIME_2);
    assertThat(mergedResult.contains(Attribute.BEFORE_STATE)).isTrue();
    assertThat(mergedResult.isNull(Attribute.BEFORE_STATE)).isFalse();
    assertThat(mergedResult.getInt(Attribute.BEFORE_STATE)).isEqualTo(TransactionState.COMMITTED.get());
    assertThat(mergedResult.getAsObject(Attribute.BEFORE_STATE)).isEqualTo(TransactionState.COMMITTED.get());
    assertThat(mergedResult.contains(Attribute.BEFORE_VERSION)).isTrue();
    assertThat(mergedResult.isNull(Attribute.BEFORE_VERSION)).isFalse();
    assertThat(mergedResult.getInt(Attribute.BEFORE_VERSION)).isEqualTo(ANY_VERSION_1);
    assertThat(mergedResult.getAsObject(Attribute.BEFORE_VERSION)).isEqualTo(ANY_VERSION_1);
}
Also used : TextValue(com.scalar.db.io.TextValue) IntValue(com.scalar.db.io.IntValue) Put(com.scalar.db.api.Put) Key(com.scalar.db.io.Key) Test(org.junit.jupiter.api.Test)

Example 60 with TextValue

use of com.scalar.db.io.TextValue in project scalardb by scalar-labs.

the class MergedResultTest method getValues_OnlyPutGiven_ShouldReturnMergedValues.

@Test
public void getValues_OnlyPutGiven_ShouldReturnMergedValues() {
    // Arrange
    Put put = new Put(new Key(ANY_NAME_1, ANY_TEXT_1), new Key(ANY_NAME_2, ANY_TEXT_2)).withValue(ANY_NAME_3, ANY_INT_3);
    MergedResult mergedResult = new MergedResult(Optional.empty(), put, TABLE_METADATA);
    // Act
    Map<String, Value<?>> values = mergedResult.getValues();
    // Assert
    assertThat(values.get(ANY_NAME_1)).isEqualTo(new TextValue(ANY_NAME_1, ANY_TEXT_1));
    assertThat(values.get(ANY_NAME_2)).isEqualTo(new TextValue(ANY_NAME_2, ANY_TEXT_2));
    assertThat(values.get(ANY_NAME_3)).isEqualTo(new IntValue(ANY_NAME_3, ANY_INT_3));
    assertThat(values.get(ANY_NAME_4)).isEqualTo(new TextValue(ANY_NAME_4, (String) null));
    assertThat(values.get(Attribute.ID)).isEqualTo(Attribute.toIdValue(null));
    assertThat(values.get(Attribute.PREPARED_AT)).isEqualTo(Attribute.toPreparedAtValue(0L));
    assertThat(values.get(Attribute.COMMITTED_AT)).isEqualTo(Attribute.toCommittedAtValue(0L));
    assertThat(values.get(Attribute.STATE)).isEqualTo(new IntValue(Attribute.STATE, 0));
    assertThat(values.get(Attribute.VERSION)).isEqualTo(Attribute.toVersionValue(0));
    assertThat(values.get(Attribute.BEFORE_PREFIX + ANY_NAME_3)).isEqualTo(new IntValue(Attribute.BEFORE_PREFIX + ANY_NAME_3, 0));
    assertThat(values.get(Attribute.BEFORE_ID)).isEqualTo(Attribute.toBeforeIdValue(null));
    assertThat(values.get(Attribute.BEFORE_PREPARED_AT)).isEqualTo(Attribute.toBeforePreparedAtValue(0L));
    assertThat(values.get(Attribute.BEFORE_COMMITTED_AT)).isEqualTo(Attribute.toBeforeCommittedAtValue(0L));
    assertThat(values.get(Attribute.BEFORE_STATE)).isEqualTo(new IntValue(Attribute.BEFORE_STATE, 0));
    assertThat(values.get(Attribute.BEFORE_VERSION)).isEqualTo(Attribute.toBeforeVersionValue(0));
}
Also used : TextValue(com.scalar.db.io.TextValue) IntValue(com.scalar.db.io.IntValue) TextValue(com.scalar.db.io.TextValue) Value(com.scalar.db.io.Value) IntValue(com.scalar.db.io.IntValue) Put(com.scalar.db.api.Put) Key(com.scalar.db.io.Key) Test(org.junit.jupiter.api.Test)

Aggregations

TextValue (com.scalar.db.io.TextValue)89 Key (com.scalar.db.io.Key)60 IntValue (com.scalar.db.io.IntValue)51 Test (org.junit.jupiter.api.Test)47 Test (org.junit.Test)38 BooleanValue (com.scalar.db.io.BooleanValue)36 Put (com.scalar.db.api.Put)27 BigIntValue (com.scalar.db.io.BigIntValue)27 DoubleValue (com.scalar.db.io.DoubleValue)26 BlobValue (com.scalar.db.io.BlobValue)25 FloatValue (com.scalar.db.io.FloatValue)24 Result (com.scalar.db.api.Result)23 ConditionalExpression (com.scalar.db.api.ConditionalExpression)21 Get (com.scalar.db.api.Get)17 Value (com.scalar.db.io.Value)15 DeleteIf (com.scalar.db.api.DeleteIf)11 Delete (com.scalar.db.api.Delete)8 ExpectedResult (com.scalar.db.util.TestUtils.ExpectedResult)8 PutIf (com.scalar.db.api.PutIf)6 PreparedStatement (java.sql.PreparedStatement)6