Search in sources :

Example 1 with PutIf

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

the class StorageIntegrationTestBase method put_PutWithIfGivenWhenNoSuchRecord_ShouldThrowNoMutationException.

@Test
public void put_PutWithIfGivenWhenNoSuchRecord_ShouldThrowNoMutationException() 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 PutIf(new ConditionalExpression(COL_NAME3, new IntValue(pKey + cKey + 1), ConditionalExpression.Operator.EQ)));
    puts.get(0).withValue(COL_NAME3, Integer.MAX_VALUE);
    assertThatThrownBy(() -> storage.put(puts.get(0))).isInstanceOf(NoMutationException.class);
    // 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, pKey + cKey)));
}
Also used : PutIf(com.scalar.db.api.PutIf) Get(com.scalar.db.api.Get) ConditionalExpression(com.scalar.db.api.ConditionalExpression) IntValue(com.scalar.db.io.IntValue) Put(com.scalar.db.api.Put) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Example 2 with PutIf

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

the class StorageWithReservedKeywordIntegrationTestBase method put_PutWithReservedKeywordAndIfGivenWhenSuchRecordExists_ShouldUpdateRecord.

@Test
public void put_PutWithReservedKeywordAndIfGivenWhenSuchRecordExists_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 PutIf(new ConditionalExpression(columnName3, new IntValue(pKey + cKey), ConditionalExpression.Operator.EQ)));
    puts.get(0).withValue(columnName3, 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(columnName1)).isEqualTo(Optional.of(new IntValue(columnName1, pKey)));
    assertThat(result.getValue(columnName4)).isEqualTo(Optional.of(new IntValue(columnName4, cKey)));
    assertThat(result.getValue(columnName3)).isEqualTo(Optional.of(new IntValue(columnName3, Integer.MAX_VALUE)));
}
Also used : PutIf(com.scalar.db.api.PutIf) Get(com.scalar.db.api.Get) ConditionalExpression(com.scalar.db.api.ConditionalExpression) IntValue(com.scalar.db.io.IntValue) Put(com.scalar.db.api.Put) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Example 3 with PutIf

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

the class UpdateStatementHandlerTest method setConsistency_PutOperationWithIfGiven_ShouldPrepareWithQuorumAndSerial.

@Test
public void setConsistency_PutOperationWithIfGiven_ShouldPrepareWithQuorumAndSerial() {
    // Arrange
    configureBehavior(null);
    put = preparePutWithClusteringKey();
    put.withCondition(new PutIf(new ConditionalExpression(ANY_NAME_4, new IntValue(ANY_INT_2), Operator.EQ))).withConsistency(Consistency.EVENTUAL);
    // Act
    handler.setConsistency(bound, put);
    // Assert
    verify(bound).setConsistencyLevel(ConsistencyLevel.QUORUM);
    verify(bound).setSerialConsistencyLevel(ConsistencyLevel.SERIAL);
}
Also used : PutIf(com.scalar.db.api.PutIf) ConditionalExpression(com.scalar.db.api.ConditionalExpression) IntValue(com.scalar.db.io.IntValue) Test(org.junit.jupiter.api.Test)

Example 4 with PutIf

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

the class UpdateStatementHandlerTest method prepare_PutOperationWithIfGiven_ShouldPrepareProperQuery.

@Test
public void prepare_PutOperationWithIfGiven_ShouldPrepareProperQuery() {
    // Arrange
    String expected = Joiner.on(" ").skipNulls().join(new String[] { "UPDATE", ANY_NAMESPACE_NAME + "." + ANY_TABLE_NAME, "SET", ANY_NAME_3 + "=?", "WHERE", ANY_NAME_1 + "=?", "AND", ANY_NAME_2 + "=?", "IF", ANY_NAME_4 + "=?", "AND", ANY_NAME_4 + "!=?", "AND", ANY_NAME_4 + ">?", "AND", ANY_NAME_4 + ">=?", "AND", ANY_NAME_4 + "<?", "AND", ANY_NAME_4 + "<=?;" });
    configureBehavior(expected);
    put = preparePutWithClusteringKey();
    put.withCondition(new PutIf(new ConditionalExpression(ANY_NAME_4, new IntValue(ANY_INT_2), Operator.EQ), new ConditionalExpression(ANY_NAME_4, new IntValue(ANY_INT_2), Operator.NE), new ConditionalExpression(ANY_NAME_4, new IntValue(ANY_INT_2), Operator.GT), new ConditionalExpression(ANY_NAME_4, new IntValue(ANY_INT_2), Operator.GTE), new ConditionalExpression(ANY_NAME_4, new IntValue(ANY_INT_2), Operator.LT), new ConditionalExpression(ANY_NAME_4, new IntValue(ANY_INT_2), Operator.LTE)));
    // Act
    handler.prepare(put);
    // Assert
    verify(session).prepare(expected);
}
Also used : PutIf(com.scalar.db.api.PutIf) ConditionalExpression(com.scalar.db.api.ConditionalExpression) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) IntValue(com.scalar.db.io.IntValue) Test(org.junit.jupiter.api.Test)

Example 5 with PutIf

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

the class OperationCheckerTest method whenCheckingPutOperationWithInvalidPutIfCondition_shouldThrowIllegalArgumentException.

@Test
public void whenCheckingPutOperationWithInvalidPutIfCondition_shouldThrowIllegalArgumentException() {
    // Arrange
    Key partitionKey = new Key(PKEY1, 1, PKEY2, "val1");
    Key clusteringKey = new Key(CKEY1, 2, CKEY2, "val1");
    List<Value<?>> values = Arrays.asList(new IntValue(COL1, 1), new DoubleValue(COL2, 0.1), new BooleanValue(COL3, true));
    MutationCondition condition = new PutIf(new ConditionalExpression(COL1, new TextValue("1"), ConditionalExpression.Operator.EQ));
    Put put = new Put(partitionKey, clusteringKey).withValues(values).withCondition(condition).forNamespace(NAMESPACE).forTable(TABLE_NAME);
    // Act Assert
    assertThatThrownBy(() -> operationChecker.check(put)).isInstanceOf(IllegalArgumentException.class);
}
Also used : PutIf(com.scalar.db.api.PutIf) DoubleValue(com.scalar.db.io.DoubleValue) MutationCondition(com.scalar.db.api.MutationCondition) TextValue(com.scalar.db.io.TextValue) BooleanValue(com.scalar.db.io.BooleanValue) ConditionalExpression(com.scalar.db.api.ConditionalExpression) IntValue(com.scalar.db.io.IntValue) DoubleValue(com.scalar.db.io.DoubleValue) TextValue(com.scalar.db.io.TextValue) Value(com.scalar.db.io.Value) BooleanValue(com.scalar.db.io.BooleanValue) IntValue(com.scalar.db.io.IntValue) Key(com.scalar.db.io.Key) Put(com.scalar.db.api.Put) Test(org.junit.jupiter.api.Test)

Aggregations

PutIf (com.scalar.db.api.PutIf)34 ConditionalExpression (com.scalar.db.api.ConditionalExpression)30 Put (com.scalar.db.api.Put)26 Test (org.junit.jupiter.api.Test)25 Get (com.scalar.db.api.Get)8 IntValue (com.scalar.db.io.IntValue)8 TextValue (com.scalar.db.io.TextValue)7 Key (com.scalar.db.io.Key)6 PutIfNotExists (com.scalar.db.api.PutIfNotExists)5 Result (com.scalar.db.api.Result)5 Test (org.junit.Test)5 Scan (com.scalar.db.api.Scan)4 MutationCondition (com.scalar.db.api.MutationCondition)3 Value (com.scalar.db.io.Value)3 HashMap (java.util.HashMap)3 Delete (com.scalar.db.api.Delete)2 Attribute.toIdValue (com.scalar.db.transaction.consensuscommit.Attribute.toIdValue)2 Mutation (com.scalar.db.api.Mutation)1 PutIfExists (com.scalar.db.api.PutIfExists)1 BooleanValue (com.scalar.db.io.BooleanValue)1