Search in sources :

Example 1 with Put

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

the class StorageMultipleClusteringKeyScanIntegrationTestBase method prepareRecords.

private List<ClusteringKey> prepareRecords(DataType firstClusteringKeyType, Order firstClusteringOrder, DataType secondClusteringKeyType, Order secondClusteringOrder) throws ExecutionException {
    RANDOM.setSeed(seed);
    List<ClusteringKey> ret = new ArrayList<>();
    List<Put> puts = new ArrayList<>();
    if (firstClusteringKeyType == DataType.BOOLEAN) {
        TestUtils.booleanValues(FIRST_CLUSTERING_KEY).forEach(firstClusteringKeyValue -> prepareRecords(firstClusteringKeyType, firstClusteringOrder, firstClusteringKeyValue, secondClusteringKeyType, secondClusteringOrder, puts, ret));
    } else {
        Set<Value<?>> valueSet = new HashSet<>();
        // Add min and max first clustering key values
        Arrays.asList(getMinValue(FIRST_CLUSTERING_KEY, firstClusteringKeyType), getMaxValue(FIRST_CLUSTERING_KEY, firstClusteringKeyType)).forEach(firstClusteringKeyValue -> {
            valueSet.add(firstClusteringKeyValue);
            prepareRecords(firstClusteringKeyType, firstClusteringOrder, firstClusteringKeyValue, secondClusteringKeyType, secondClusteringOrder, puts, ret);
        });
        IntStream.range(0, FIRST_CLUSTERING_KEY_NUM - 2).forEach(i -> {
            Value<?> firstClusteringKeyValue;
            while (true) {
                firstClusteringKeyValue = getFirstClusteringKeyValue(firstClusteringKeyType);
                // reject duplication
                if (!valueSet.contains(firstClusteringKeyValue)) {
                    valueSet.add(firstClusteringKeyValue);
                    break;
                }
            }
            prepareRecords(firstClusteringKeyType, firstClusteringOrder, firstClusteringKeyValue, secondClusteringKeyType, secondClusteringOrder, puts, ret);
        });
    }
    try {
        List<Put> buffer = new ArrayList<>();
        for (Put put : puts) {
            buffer.add(put);
            if (buffer.size() == 20) {
                storage.mutate(buffer);
                buffer.clear();
            }
        }
        if (!buffer.isEmpty()) {
            storage.mutate(buffer);
        }
    } catch (ExecutionException e) {
        throw new ExecutionException("put data to database failed", e);
    }
    ret.sort(getClusteringKeyComparator(firstClusteringOrder, secondClusteringOrder));
    return ret;
}
Also used : ArrayList(java.util.ArrayList) Value(com.scalar.db.io.Value) ExecutionException(com.scalar.db.exception.storage.ExecutionException) Put(com.scalar.db.api.Put) HashSet(java.util.HashSet)

Example 2 with Put

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

the class StorageIntegrationTestBase method scan_ScanWithLimitGiven_ShouldReturnGivenNumberOfResults.

@Test
public void scan_ScanWithLimitGiven_ShouldReturnGivenNumberOfResults() throws IOException, ExecutionException {
    // setup
    List<Put> puts = preparePuts();
    storage.mutate(Arrays.asList(puts.get(0), puts.get(1), puts.get(2)));
    Scan scan = new Scan(new Key(COL_NAME1, 0)).withOrdering(new Scan.Ordering(COL_NAME4, Scan.Ordering.Order.DESC)).withLimit(1);
    // exercise
    List<Result> actual = scanAll(scan);
    // verify
    assertThat(actual.size()).isEqualTo(1);
    assertThat(actual.get(0).getValue(COL_NAME4)).isEqualTo(Optional.of(new IntValue(COL_NAME4, 2)));
}
Also used : Ordering(com.scalar.db.api.Scan.Ordering) Scan(com.scalar.db.api.Scan) IntValue(com.scalar.db.io.IntValue) Put(com.scalar.db.api.Put) Key(com.scalar.db.io.Key) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Example 3 with Put

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

the class StorageIntegrationTestBase method mutate_PutAndDeleteGiven_ShouldUpdateAndDeleteRecordsProperly.

@Test
public void mutate_PutAndDeleteGiven_ShouldUpdateAndDeleteRecordsProperly() throws ExecutionException, IOException {
    // Arrange
    populateRecords();
    List<Put> puts = preparePuts();
    puts.get(1).withValue(COL_NAME3, Integer.MAX_VALUE);
    puts.get(2).withValue(COL_NAME3, Integer.MIN_VALUE);
    int pKey = 0;
    int cKey = 0;
    Delete delete = prepareDelete(pKey, cKey);
    Scan scan = new Scan(new Key(COL_NAME1, pKey));
    // Act
    assertThatCode(() -> storage.mutate(Arrays.asList(delete, puts.get(1), puts.get(2)))).doesNotThrowAnyException();
    // Assert
    List<Result> results = scanAll(scan);
    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_NAME3).isPresent()).isTrue();
    assertThat(results.get(0).getValue(COL_NAME3).get().getAsInt()).isEqualTo(Integer.MAX_VALUE);
    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_NAME3).isPresent()).isTrue();
    assertThat(results.get(1).getValue(COL_NAME3).get().getAsInt()).isEqualTo(Integer.MIN_VALUE);
}
Also used : Delete(com.scalar.db.api.Delete) Scan(com.scalar.db.api.Scan) Put(com.scalar.db.api.Put) Key(com.scalar.db.io.Key) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Example 4 with Put

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

the class StorageIntegrationTestBase method put_MultiplePutWithDifferentPartitionsGiven_ShouldThrowIllegalArgumentException.

@Test
public void put_MultiplePutWithDifferentPartitionsGiven_ShouldThrowIllegalArgumentException() throws IOException, ExecutionException {
    // Arrange
    List<Put> puts = preparePuts();
    // Act
    assertThatThrownBy(() -> storage.put(Arrays.asList(puts.get(0), puts.get(3), puts.get(6)))).isInstanceOf(IllegalArgumentException.class);
    // Assert
    List<Result> results;
    results = scanAll(new Scan(new Key(COL_NAME1, 0)));
    assertThat(results.size()).isEqualTo(0);
    results = scanAll(new Scan(new Key(COL_NAME1, 3)));
    assertThat(results.size()).isEqualTo(0);
    results = scanAll(new Scan(new Key(COL_NAME1, 6)));
    assertThat(results.size()).isEqualTo(0);
}
Also used : Scan(com.scalar.db.api.Scan) Put(com.scalar.db.api.Put) Key(com.scalar.db.io.Key) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Example 5 with Put

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

the class StorageIntegrationTestBase method put_PutWithoutValuesGiven_ShouldStoreProperly.

@Test
public void put_PutWithoutValuesGiven_ShouldStoreProperly() throws ExecutionException {
    // Arrange
    Key partitionKey = new Key(COL_NAME1, 0);
    Key clusteringKey = new Key(COL_NAME4, 0);
    // Act
    assertThatCode(() -> storage.put(new Put(partitionKey, clusteringKey))).doesNotThrowAnyException();
    // Assert
    Optional<Result> result = storage.get(new Get(partitionKey, clusteringKey));
    assertThat(result).isPresent();
}
Also used : Get(com.scalar.db.api.Get) Key(com.scalar.db.io.Key) Put(com.scalar.db.api.Put) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Aggregations

Put (com.scalar.db.api.Put)374 Key (com.scalar.db.io.Key)216 Test (org.junit.jupiter.api.Test)209 Result (com.scalar.db.api.Result)108 Get (com.scalar.db.api.Get)93 Test (org.junit.Test)67 Delete (com.scalar.db.api.Delete)64 IntValue (com.scalar.db.io.IntValue)63 TextValue (com.scalar.db.io.TextValue)48 Scan (com.scalar.db.api.Scan)44 Value (com.scalar.db.io.Value)37 Assertions.catchThrowable (org.assertj.core.api.Assertions.catchThrowable)35 BooleanValue (com.scalar.db.io.BooleanValue)33 ConditionalExpression (com.scalar.db.api.ConditionalExpression)30 PutIfNotExists (com.scalar.db.api.PutIfNotExists)29 PutIf (com.scalar.db.api.PutIf)28 DoubleValue (com.scalar.db.io.DoubleValue)26 GrpcTransaction (com.scalar.db.transaction.rpc.GrpcTransaction)19 ExecutionException (com.scalar.db.exception.storage.ExecutionException)17 Mutation (com.scalar.db.api.Mutation)16