Search in sources :

Example 51 with IntValue

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

the class ConsensusCommitSpecificIntegrationTestBase method putAndCommit_GetsAndPutsGiven_ShouldCommitProperly.

private void putAndCommit_GetsAndPutsGiven_ShouldCommitProperly(String fromNamespace, String fromTable, String toNamespace, String toTable) throws CommitException, UnknownTransactionStatusException, CrudException {
    // Arrange
    boolean differentTables = !fromNamespace.equals(toNamespace) || !fromTable.equals(toTable);
    populateRecords(fromNamespace, fromTable);
    if (differentTables) {
        populateRecords(toNamespace, toTable);
    }
    List<Get> fromGets = prepareGets(fromNamespace, fromTable);
    List<Get> toGets = differentTables ? prepareGets(toNamespace, toTable) : fromGets;
    int amount = 100;
    IntValue fromBalance = new IntValue(BALANCE, INITIAL_BALANCE - amount);
    IntValue toBalance = new IntValue(BALANCE, INITIAL_BALANCE + amount);
    int from = 0;
    int to = NUM_TYPES;
    // Act
    prepareTransfer(from, fromNamespace, fromTable, to, toNamespace, toTable, amount).commit();
    // Assert
    ConsensusCommit another = manager.start();
    Optional<Result> fromResult = another.get(fromGets.get(from));
    assertThat(fromResult).isPresent();
    assertThat(fromResult.get().getValue(BALANCE)).isEqualTo(Optional.of(fromBalance));
    Optional<Result> toResult = another.get(toGets.get(to));
    assertThat(toResult).isPresent();
    assertThat(toResult.get().getValue(BALANCE)).isEqualTo(Optional.of(toBalance));
}
Also used : Get(com.scalar.db.api.Get) IntValue(com.scalar.db.io.IntValue) Result(com.scalar.db.api.Result)

Example 52 with IntValue

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

the class TwoPhaseConsensusCommitSpecificIntegrationTestBase method populatePreparedRecordAndCoordinatorStateRecord.

private void populatePreparedRecordAndCoordinatorStateRecord(String table, TransactionState recordState, long preparedAt, TransactionState coordinatorState) throws ExecutionException, CoordinatorException {
    Key partitionKey = new Key(new IntValue(ACCOUNT_ID, 0));
    Key clusteringKey = new Key(new IntValue(ACCOUNT_TYPE, 0));
    Put put = new Put(partitionKey, clusteringKey).forNamespace(namespace).forTable(table).withValue(new IntValue(BALANCE, INITIAL_BALANCE)).withValue(Attribute.toIdValue(ANY_ID_2)).withValue(Attribute.toStateValue(recordState)).withValue(Attribute.toVersionValue(2)).withValue(Attribute.toPreparedAtValue(preparedAt)).withValue(Attribute.toBeforeIdValue(ANY_ID_1)).withValue(Attribute.toBeforeStateValue(TransactionState.COMMITTED)).withValue(Attribute.toBeforeVersionValue(1)).withValue(Attribute.toBeforePreparedAtValue(1)).withValue(Attribute.toBeforeCommittedAtValue(1));
    storage.put(put);
    if (coordinatorState == null) {
        return;
    }
    State state = new State(ANY_ID_2, coordinatorState);
    coordinator.putState(state);
}
Also used : State(com.scalar.db.transaction.consensuscommit.Coordinator.State) TransactionState(com.scalar.db.api.TransactionState) IntValue(com.scalar.db.io.IntValue) Key(com.scalar.db.io.Key) Put(com.scalar.db.api.Put)

Example 53 with IntValue

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

the class TwoPhaseCommitTransactionIntegrationTestBase method putAndCommit_GetsAndPutsGiven_ShouldCommitProperly.

@Test
public void putAndCommit_GetsAndPutsGiven_ShouldCommitProperly() throws TransactionException {
    // Arrange
    populateRecords();
    int amount = 100;
    int fromId = 0;
    int toId = NUM_TYPES;
    // Act
    TwoPhaseCommitTransaction transaction = manager.start();
    List<Get> gets = prepareGets();
    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.prepare();
    transaction.validate();
    transaction.commit();
    // Assert
    TwoPhaseCommitTransaction 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.prepare();
    another.validate();
    another.commit();
}
Also used : IntValue(com.scalar.db.io.IntValue) ExpectedResult(com.scalar.db.util.TestUtils.ExpectedResult) Test(org.junit.jupiter.api.Test)

Example 54 with IntValue

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

the class PutTest method withXXXValue_ProperValuesGiven_ShouldReturnWhatsSet.

@Test
public void withXXXValue_ProperValuesGiven_ShouldReturnWhatsSet() {
    // Arrange
    Put put = preparePut();
    // Act
    put.withBooleanValue("val1", true).withIntValue("val2", 5678).withBigIntValue("val3", 1234L).withFloatValue("val4", 4.56f).withDoubleValue("val5", 1.23).withTextValue("val6", "string_value").withBlobValue("val7", "blob_value".getBytes(StandardCharsets.UTF_8)).withBlobValue("val8", ByteBuffer.wrap("blob_value2".getBytes(StandardCharsets.UTF_8))).withBooleanValue("val9", null).withIntValue("val10", null).withBigIntValue("val11", null).withFloatValue("val12", null).withDoubleValue("val13", null).withTextValue("val14", null).withBlobValue("val15", (ByteBuffer) null).withBlobValue("val16", (byte[]) null);
    // Assert
    Map<String, Value<?>> values = put.getValues();
    assertThat(values.size()).isEqualTo(16);
    assertThat(values.get("val1")).isEqualTo(new BooleanValue("val1", true));
    assertThat(values.get("val2")).isEqualTo(new IntValue("val2", 5678));
    assertThat(values.get("val3")).isEqualTo(new BigIntValue("val3", 1234L));
    assertThat(values.get("val4")).isEqualTo(new FloatValue("val4", 4.56f));
    assertThat(values.get("val5")).isEqualTo(new DoubleValue("val5", 1.23));
    assertThat(values.get("val6")).isEqualTo(new TextValue("val6", "string_value"));
    assertThat(values.get("val7")).isEqualTo(new BlobValue("val7", "blob_value".getBytes(StandardCharsets.UTF_8)));
    assertThat(values.get("val8")).isEqualTo(new BlobValue("val8", "blob_value2".getBytes(StandardCharsets.UTF_8)));
    assertThat(values).containsKey("val9");
    assertThat(values.get("val9")).isNull();
    assertThat(values).containsKey("val10");
    assertThat(values.get("val10")).isNull();
    assertThat(values).containsKey("val11");
    assertThat(values.get("val11")).isNull();
    assertThat(values).containsKey("val12");
    assertThat(values.get("val12")).isNull();
    assertThat(values).containsKey("val13");
    assertThat(values.get("val13")).isNull();
    assertThat(values).containsKey("val14");
    assertThat(values.get("val14")).isNull();
    assertThat(values).containsKey("val15");
    assertThat(values.get("val15")).isNull();
    assertThat(values).containsKey("val16");
    assertThat(values.get("val16")).isNull();
    Map<String, Column<?>> columns = put.getColumns();
    assertThat(columns.size()).isEqualTo(16);
    assertThat(columns.get("val1")).isEqualTo(BooleanColumn.of("val1", true));
    assertThat(columns.get("val2")).isEqualTo(IntColumn.of("val2", 5678));
    assertThat(columns.get("val3")).isEqualTo(BigIntColumn.of("val3", 1234L));
    assertThat(columns.get("val4")).isEqualTo(FloatColumn.of("val4", 4.56f));
    assertThat(columns.get("val5")).isEqualTo(DoubleColumn.of("val5", 1.23));
    assertThat(columns.get("val6")).isEqualTo(TextColumn.of("val6", "string_value"));
    assertThat(columns.get("val7")).isEqualTo(BlobColumn.of("val7", "blob_value".getBytes(StandardCharsets.UTF_8)));
    assertThat(columns.get("val8")).isEqualTo(BlobColumn.of("val8", "blob_value2".getBytes(StandardCharsets.UTF_8)));
    assertThat(columns.get("val9")).isEqualTo(BooleanColumn.ofNull("val9"));
    assertThat(columns.get("val10")).isEqualTo(IntColumn.ofNull("val10"));
    assertThat(columns.get("val11")).isEqualTo(BigIntColumn.ofNull("val11"));
    assertThat(columns.get("val12")).isEqualTo(FloatColumn.ofNull("val12"));
    assertThat(columns.get("val13")).isEqualTo(DoubleColumn.ofNull("val13"));
    assertThat(columns.get("val14")).isEqualTo(TextColumn.ofNull("val14"));
    assertThat(columns.get("val15")).isEqualTo(BlobColumn.ofNull("val15"));
    assertThat(columns.get("val16")).isEqualTo(BlobColumn.ofNull("val16"));
    assertThat(put.getContainedColumnNames()).isEqualTo(ImmutableSet.of("val1", "val2", "val3", "val4", "val5", "val6", "val7", "val8", "val9", "val10", "val11", "val12", "val13", "val14", "val15", "val16"));
    assertThat(put.containsColumn("val1")).isTrue();
    assertThat(put.isNullValue("val1")).isFalse();
    assertThat(put.getBooleanValue("val1")).isTrue();
    assertThat(put.getValueAsObject("val1")).isEqualTo(true);
    assertThat(put.containsColumn("val2")).isTrue();
    assertThat(put.isNullValue("val2")).isFalse();
    assertThat(put.getIntValue("val2")).isEqualTo(5678);
    assertThat(put.getValueAsObject("val2")).isEqualTo(5678);
    assertThat(put.containsColumn("val3")).isTrue();
    assertThat(put.isNullValue("val3")).isFalse();
    assertThat(put.getBigIntValue("val3")).isEqualTo(1234L);
    assertThat(put.getValueAsObject("val3")).isEqualTo(1234L);
    assertThat(put.containsColumn("val4")).isTrue();
    assertThat(put.isNullValue("val4")).isFalse();
    assertThat(put.getFloatValue("val4")).isEqualTo(4.56f);
    assertThat(put.getValueAsObject("val4")).isEqualTo(4.56f);
    assertThat(put.containsColumn("val5")).isTrue();
    assertThat(put.isNullValue("val5")).isFalse();
    assertThat(put.getDoubleValue("val5")).isEqualTo(1.23);
    assertThat(put.getValueAsObject("val5")).isEqualTo(1.23);
    assertThat(put.containsColumn("val6")).isTrue();
    assertThat(put.isNullValue("val6")).isFalse();
    assertThat(put.getTextValue("val6")).isEqualTo("string_value");
    assertThat(put.getValueAsObject("val6")).isEqualTo("string_value");
    assertThat(put.containsColumn("val7")).isTrue();
    assertThat(put.isNullValue("val7")).isFalse();
    assertThat(put.getBlobValue("val7")).isEqualTo(ByteBuffer.wrap("blob_value".getBytes(StandardCharsets.UTF_8)));
    assertThat(put.getBlobValueAsByteBuffer("val7")).isEqualTo(ByteBuffer.wrap("blob_value".getBytes(StandardCharsets.UTF_8)));
    assertThat(put.getBlobValueAsBytes("val7")).isEqualTo("blob_value".getBytes(StandardCharsets.UTF_8));
    assertThat(put.getValueAsObject("val7")).isEqualTo(ByteBuffer.wrap("blob_value".getBytes(StandardCharsets.UTF_8)));
    assertThat(put.containsColumn("val8")).isTrue();
    assertThat(put.isNullValue("val8")).isFalse();
    assertThat(put.getBlobValue("val8")).isEqualTo(ByteBuffer.wrap("blob_value2".getBytes(StandardCharsets.UTF_8)));
    assertThat(put.getBlobValueAsByteBuffer("val8")).isEqualTo(ByteBuffer.wrap("blob_value2".getBytes(StandardCharsets.UTF_8)));
    assertThat(put.getBlobValueAsBytes("val8")).isEqualTo("blob_value2".getBytes(StandardCharsets.UTF_8));
    assertThat(put.getValueAsObject("val8")).isEqualTo(ByteBuffer.wrap("blob_value2".getBytes(StandardCharsets.UTF_8)));
    assertThat(put.containsColumn("val9")).isTrue();
    assertThat(put.isNullValue("val9")).isTrue();
    assertThat(put.getBooleanValue("val9")).isFalse();
    assertThat(put.getValueAsObject("val9")).isNull();
    assertThat(put.containsColumn("val10")).isTrue();
    assertThat(put.isNullValue("val10")).isTrue();
    assertThat(put.getIntValue("val10")).isEqualTo(0);
    assertThat(put.getValueAsObject("val10")).isNull();
    assertThat(put.containsColumn("val11")).isTrue();
    assertThat(put.isNullValue("val11")).isTrue();
    assertThat(put.getBigIntValue("val11")).isEqualTo(0L);
    assertThat(put.getValueAsObject("val11")).isNull();
    assertThat(put.containsColumn("val12")).isTrue();
    assertThat(put.isNullValue("val12")).isTrue();
    assertThat(put.getFloatValue("val12")).isEqualTo(0.0F);
    assertThat(put.getValueAsObject("val12")).isNull();
    assertThat(put.containsColumn("val13")).isTrue();
    assertThat(put.isNullValue("val13")).isTrue();
    assertThat(put.getDoubleValue("val13")).isEqualTo(0.0);
    assertThat(put.getValueAsObject("val13")).isNull();
    assertThat(put.containsColumn("val14")).isTrue();
    assertThat(put.isNullValue("val14")).isTrue();
    assertThat(put.getTextValue("val14")).isNull();
    assertThat(put.getValueAsObject("val14")).isNull();
    assertThat(put.containsColumn("val15")).isTrue();
    assertThat(put.isNullValue("val15")).isTrue();
    assertThat(put.getBlobValue("val15")).isNull();
    assertThat(put.getValueAsObject("val15")).isNull();
    assertThat(put.containsColumn("val16")).isTrue();
    assertThat(put.isNullValue("val16")).isTrue();
    assertThat(put.getBlobValue("val16")).isNull();
    assertThat(put.getValueAsObject("val16")).isNull();
}
Also used : ByteBuffer(java.nio.ByteBuffer) BlobValue(com.scalar.db.io.BlobValue) DoubleValue(com.scalar.db.io.DoubleValue) IntColumn(com.scalar.db.io.IntColumn) BooleanColumn(com.scalar.db.io.BooleanColumn) TextColumn(com.scalar.db.io.TextColumn) DoubleColumn(com.scalar.db.io.DoubleColumn) BigIntColumn(com.scalar.db.io.BigIntColumn) FloatColumn(com.scalar.db.io.FloatColumn) Column(com.scalar.db.io.Column) BlobColumn(com.scalar.db.io.BlobColumn) TextValue(com.scalar.db.io.TextValue) BooleanValue(com.scalar.db.io.BooleanValue) IntValue(com.scalar.db.io.IntValue) DoubleValue(com.scalar.db.io.DoubleValue) TextValue(com.scalar.db.io.TextValue) Value(com.scalar.db.io.Value) FloatValue(com.scalar.db.io.FloatValue) BigIntValue(com.scalar.db.io.BigIntValue) BlobValue(com.scalar.db.io.BlobValue) 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) Test(org.junit.jupiter.api.Test)

Example 55 with IntValue

use of com.scalar.db.io.IntValue 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)

Aggregations

IntValue (com.scalar.db.io.IntValue)108 Test (org.junit.jupiter.api.Test)65 TextValue (com.scalar.db.io.TextValue)63 Key (com.scalar.db.io.Key)62 Put (com.scalar.db.api.Put)55 BooleanValue (com.scalar.db.io.BooleanValue)48 DoubleValue (com.scalar.db.io.DoubleValue)38 Result (com.scalar.db.api.Result)35 Test (org.junit.Test)33 Get (com.scalar.db.api.Get)29 Value (com.scalar.db.io.Value)26 BigIntValue (com.scalar.db.io.BigIntValue)23 BlobValue (com.scalar.db.io.BlobValue)20 FloatValue (com.scalar.db.io.FloatValue)19 ExpectedResult (com.scalar.db.util.TestUtils.ExpectedResult)15 ConditionalExpression (com.scalar.db.api.ConditionalExpression)13 MutationCondition (com.scalar.db.api.MutationCondition)12 PutIf (com.scalar.db.api.PutIf)8 TransactionState (com.scalar.db.api.TransactionState)8 DeleteIf (com.scalar.db.api.DeleteIf)6