Search in sources :

Example 36 with TextValue

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

the class PutTest method withValues_ProperValueGiven_ShouldReturnWhatsSet.

@Test
public void withValues_ProperValueGiven_ShouldReturnWhatsSet() {
    // Arrange
    Put put = preparePut();
    TextValue value1 = new TextValue(ANY_NAME_1, ANY_TEXT_1);
    TextValue value2 = new TextValue(ANY_NAME_2, ANY_TEXT_2);
    // Act
    put.withValues(Arrays.asList(value1, value2));
    // Assert
    assertThat(put.getValues()).isEqualTo(ImmutableMap.of(value1.getName(), value1, value2.getName(), value2));
    assertThat(put.getColumns()).isEqualTo(ImmutableMap.of(value1.getName(), ScalarDbUtils.toColumn(value1), value2.getName(), ScalarDbUtils.toColumn(value2)));
    assertThat(put.getContainedColumnNames()).isEqualTo(ImmutableSet.of(ANY_NAME_1, ANY_NAME_2));
    assertThat(put.containsColumn(ANY_NAME_1)).isTrue();
    assertThat(put.isNullValue(ANY_NAME_1)).isFalse();
    assertThat(put.getTextValue(ANY_NAME_1)).isEqualTo(ANY_TEXT_1);
    assertThat(put.getValueAsObject(ANY_NAME_1)).isEqualTo(ANY_TEXT_1);
    assertThat(put.containsColumn(ANY_NAME_2)).isTrue();
    assertThat(put.isNullValue(ANY_NAME_2)).isFalse();
    assertThat(put.getTextValue(ANY_NAME_2)).isEqualTo(ANY_TEXT_2);
    assertThat(put.getValueAsObject(ANY_NAME_2)).isEqualTo(ANY_TEXT_2);
}
Also used : TextValue(com.scalar.db.io.TextValue) Test(org.junit.jupiter.api.Test)

Example 37 with TextValue

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

the class ResultImplTest method getClusteringKey_RequiredValuesGiven_ShouldReturnClusteringKey.

@Test
public void getClusteringKey_RequiredValuesGiven_ShouldReturnClusteringKey() {
    // Arrange
    ResultImpl result = new ResultImpl(columns, TABLE_METADATA);
    // Act
    Optional<Key> key = result.getClusteringKey();
    // Assert
    assertThat(key.isPresent()).isTrue();
    assertThat(key.get().get().size()).isEqualTo(1);
    assertThat(key.get().get().get(0)).isEqualTo(new TextValue(ANY_NAME_2, ANY_TEXT_2));
}
Also used : TextValue(com.scalar.db.io.TextValue) Key(com.scalar.db.io.Key) Test(org.junit.jupiter.api.Test)

Example 38 with TextValue

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

the class ConditionalExpressionTest method constructor_ProperArgsGiven_ShouldConstructProperly.

@Test
public void constructor_ProperArgsGiven_ShouldConstructProperly() {
    // Arrange
    // Act
    ConditionalExpression expression1 = new ConditionalExpression("col1", new TextValue("aaa"), Operator.EQ);
    ConditionalExpression expression2 = new ConditionalExpression("col2", true, Operator.EQ);
    ConditionalExpression expression3 = new ConditionalExpression("col3", 123, Operator.NE);
    ConditionalExpression expression4 = new ConditionalExpression("col4", 456L, Operator.GT);
    ConditionalExpression expression5 = new ConditionalExpression("col5", 1.23F, Operator.GTE);
    ConditionalExpression expression6 = new ConditionalExpression("col6", 4.56D, Operator.LT);
    ConditionalExpression expression7 = new ConditionalExpression("col7", "text", Operator.LTE);
    ConditionalExpression expression8 = new ConditionalExpression("col8", "blob".getBytes(StandardCharsets.UTF_8), Operator.EQ);
    ConditionalExpression expression9 = new ConditionalExpression("col9", ByteBuffer.wrap("blob2".getBytes(StandardCharsets.UTF_8)), Operator.NE);
    // Assert
    assertThat(expression1.getName()).isEqualTo("col1");
    assertThat(expression1.getValue()).isEqualTo(new TextValue("aaa"));
    assertThat(expression1.getTextValue()).isEqualTo("aaa");
    assertThat(expression1.getValueAsObject()).isEqualTo("aaa");
    assertThat(expression1.getOperator()).isEqualTo(Operator.EQ);
    assertThat(expression2.getName()).isEqualTo("col2");
    assertThat(expression2.getValue()).isEqualTo(new BooleanValue(true));
    assertThat(expression2.getBooleanValue()).isTrue();
    assertThat(expression2.getValueAsObject()).isEqualTo(true);
    assertThat(expression2.getOperator()).isEqualTo(Operator.EQ);
    assertThat(expression3.getName()).isEqualTo("col3");
    assertThat(expression3.getValue()).isEqualTo(new IntValue(123));
    assertThat(expression3.getIntValue()).isEqualTo(123);
    assertThat(expression3.getValueAsObject()).isEqualTo(123);
    assertThat(expression3.getOperator()).isEqualTo(Operator.NE);
    assertThat(expression4.getName()).isEqualTo("col4");
    assertThat(expression4.getValue()).isEqualTo(new BigIntValue(456L));
    assertThat(expression4.getBigIntValue()).isEqualTo(456L);
    assertThat(expression4.getValueAsObject()).isEqualTo(456L);
    assertThat(expression4.getOperator()).isEqualTo(Operator.GT);
    assertThat(expression5.getName()).isEqualTo("col5");
    assertThat(expression5.getValue()).isEqualTo(new FloatValue(1.23F));
    assertThat(expression5.getFloatValue()).isEqualTo(1.23F);
    assertThat(expression5.getValueAsObject()).isEqualTo(1.23F);
    assertThat(expression5.getOperator()).isEqualTo(Operator.GTE);
    assertThat(expression6.getName()).isEqualTo("col6");
    assertThat(expression6.getValue()).isEqualTo(new DoubleValue(4.56D));
    assertThat(expression6.getDoubleValue()).isEqualTo(4.56D);
    assertThat(expression6.getValueAsObject()).isEqualTo(4.56D);
    assertThat(expression6.getOperator()).isEqualTo(Operator.LT);
    assertThat(expression7.getName()).isEqualTo("col7");
    assertThat(expression7.getValue()).isEqualTo(new TextValue("text"));
    assertThat(expression7.getTextValue()).isEqualTo("text");
    assertThat(expression7.getValueAsObject()).isEqualTo("text");
    assertThat(expression7.getOperator()).isEqualTo(Operator.LTE);
    assertThat(expression8.getName()).isEqualTo("col8");
    assertThat(expression8.getValue()).isEqualTo(new BlobValue("blob".getBytes(StandardCharsets.UTF_8)));
    assertThat(expression8.getBlobValue()).isEqualTo(ByteBuffer.wrap("blob".getBytes(StandardCharsets.UTF_8)));
    assertThat(expression8.getBlobValueAsByteBuffer()).isEqualTo(ByteBuffer.wrap("blob".getBytes(StandardCharsets.UTF_8)));
    assertThat(expression8.getBlobValueAsBytes()).isEqualTo("blob".getBytes(StandardCharsets.UTF_8));
    assertThat(expression8.getValueAsObject()).isEqualTo(ByteBuffer.wrap("blob".getBytes(StandardCharsets.UTF_8)));
    assertThat(expression8.getOperator()).isEqualTo(Operator.EQ);
    assertThat(expression9.getName()).isEqualTo("col9");
    assertThat(expression9.getValue()).isEqualTo(new BlobValue("blob2".getBytes(StandardCharsets.UTF_8)));
    assertThat(expression9.getBlobValue()).isEqualTo(ByteBuffer.wrap("blob2".getBytes(StandardCharsets.UTF_8)));
    assertThat(expression9.getBlobValueAsByteBuffer()).isEqualTo(ByteBuffer.wrap("blob2".getBytes(StandardCharsets.UTF_8)));
    assertThat(expression9.getBlobValueAsBytes()).isEqualTo("blob2".getBytes(StandardCharsets.UTF_8));
    assertThat(expression9.getValueAsObject()).isEqualTo(ByteBuffer.wrap("blob2".getBytes(StandardCharsets.UTF_8)));
    assertThat(expression9.getOperator()).isEqualTo(Operator.NE);
}
Also used : DoubleValue(com.scalar.db.io.DoubleValue) TextValue(com.scalar.db.io.TextValue) BooleanValue(com.scalar.db.io.BooleanValue) FloatValue(com.scalar.db.io.FloatValue) IntValue(com.scalar.db.io.IntValue) BigIntValue(com.scalar.db.io.BigIntValue) BigIntValue(com.scalar.db.io.BigIntValue) BlobValue(com.scalar.db.io.BlobValue) Test(org.junit.jupiter.api.Test)

Example 39 with TextValue

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

the class RollbackMutationComposer method add.

/**
 * rollback in either prepare phase in commit or lazy recovery phase in read
 */
@Override
public void add(Operation base, TransactionResult result) throws ExecutionException {
    TransactionResult latest;
    if (result == null || !result.getId().equals(id)) {
        // rollback from snapshot
        latest = getLatestResult(base, result).orElse(null);
        if (latest == null) {
            LOGGER.debug("the record was not prepared or has already rollback deleted");
            return;
        }
        if (!latest.getId().equals(id)) {
            LOGGER.debug("the record is not prepared (yet) by this transaction or has already rolled back");
            return;
        }
    } else {
        latest = result;
    }
    TextValue beforeId = (TextValue) latest.getValue(Attribute.BEFORE_ID).get();
    if (beforeId.get().isPresent()) {
        mutations.add(composePut(base, latest));
    } else {
        // no record to rollback, so it should be deleted
        mutations.add(composeDelete(base, latest));
    }
}
Also used : TextValue(com.scalar.db.io.TextValue)

Example 40 with TextValue

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

the class DistributedStorageIntegrationTestBase method put_MultiplePutWithDifferentConditionsGiven_ShouldStoreProperly.

@Test
public void put_MultiplePutWithDifferentConditionsGiven_ShouldStoreProperly() throws IOException, ExecutionException {
    // Arrange
    List<Put> puts = preparePuts();
    storage.put(puts.get(1));
    puts.get(0).withCondition(new PutIfNotExists());
    puts.get(1).withCondition(new PutIf(new ConditionalExpression(COL_NAME2, new TextValue("1"), ConditionalExpression.Operator.EQ)));
    // Act
    assertThatCode(() -> storage.put(Arrays.asList(puts.get(0), puts.get(1)))).doesNotThrowAnyException();
    // Assert
    List<Result> results = scanAll(new Scan(new Key(COL_NAME1, 0)));
    assertThat(results.size()).isEqualTo(2);
    assertThat(results.get(0).getValue(COL_NAME1).isPresent()).isTrue();
    assertThat(results.get(0).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
    assertThat(results.get(0).getValue(COL_NAME4).isPresent()).isTrue();
    assertThat(results.get(0).getValue(COL_NAME4).get().getAsInt()).isEqualTo(0);
    assertThat(results.get(1).getValue(COL_NAME1).isPresent()).isTrue();
    assertThat(results.get(1).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
    assertThat(results.get(1).getValue(COL_NAME4).isPresent()).isTrue();
    assertThat(results.get(1).getValue(COL_NAME4).get().getAsInt()).isEqualTo(1);
}
Also used : TextValue(com.scalar.db.io.TextValue) Key(com.scalar.db.io.Key) ExpectedResult(com.scalar.db.util.TestUtils.ExpectedResult) 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