Search in sources :

Example 1 with Key

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

the class StorageMultipleClusteringKeyScanIntegrationTestBase method getScan.

private Scan getScan(DataType firstClusteringKeyType, Order firstClusteringOrder, DataType secondClusteringKeyType, Order secondClusteringOrder, @Nullable ClusteringKey startClusteringKey, @Nullable Boolean startInclusive, @Nullable ClusteringKey endClusteringKey, @Nullable Boolean endInclusive, OrderingType orderingType, int limit) {
    Scan scan = new Scan(getPartitionKey()).forNamespace(getNamespaceName(firstClusteringKeyType)).forTable(getTableName(firstClusteringKeyType, firstClusteringOrder, secondClusteringKeyType, secondClusteringOrder));
    if (startClusteringKey != null && startInclusive != null) {
        Key key;
        if (startClusteringKey.second != null) {
            key = new Key(startClusteringKey.first, startClusteringKey.second);
        } else {
            key = new Key(startClusteringKey.first);
        }
        scan.withStart(key, startInclusive);
    }
    if (endClusteringKey != null && endInclusive != null) {
        Key key;
        if (endClusteringKey.second != null) {
            key = new Key(endClusteringKey.first, endClusteringKey.second);
        } else {
            key = new Key(endClusteringKey.first);
        }
        scan.withEnd(key, endInclusive);
    }
    switch(orderingType) {
        case BOTH_SPECIFIED:
            scan.withOrdering(new Ordering(FIRST_CLUSTERING_KEY, firstClusteringOrder)).withOrdering(new Ordering(SECOND_CLUSTERING_KEY, secondClusteringOrder));
            break;
        case ONLY_FIRST_SPECIFIED:
            scan.withOrdering(new Ordering(FIRST_CLUSTERING_KEY, firstClusteringOrder));
            break;
        case BOTH_SPECIFIED_AND_REVERSED:
            scan.withOrdering(new Ordering(FIRST_CLUSTERING_KEY, TestUtils.reverseOrder(firstClusteringOrder))).withOrdering(new Ordering(SECOND_CLUSTERING_KEY, TestUtils.reverseOrder(secondClusteringOrder)));
            break;
        case ONLY_FIRST_SPECIFIED_AND_REVERSED:
            scan.withOrdering(new Ordering(FIRST_CLUSTERING_KEY, TestUtils.reverseOrder(firstClusteringOrder)));
            break;
        case NOTHING:
            break;
        default:
            throw new AssertionError();
    }
    if (limit > 0) {
        scan.withLimit(limit);
    }
    return scan;
}
Also used : Ordering(com.scalar.db.api.Scan.Ordering) Scan(com.scalar.db.api.Scan) Key(com.scalar.db.io.Key)

Example 2 with Key

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

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

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

the class StorageIntegrationTestBase method scannerIterator_ScanWithPartitionKeyGiven_ShouldRetrieveCorrectResults.

@Test
public void scannerIterator_ScanWithPartitionKeyGiven_ShouldRetrieveCorrectResults() throws ExecutionException, IOException {
    // Arrange
    populateRecords();
    int pKey = 0;
    // Act
    Scan scan = new Scan(new Key(COL_NAME1, pKey));
    List<Result> actual = new ArrayList<>();
    Scanner scanner = storage.scan(scan);
    scanner.forEach(actual::add);
    scanner.close();
    // Assert
    assertThat(actual.size()).isEqualTo(3);
    assertThat(actual.get(0).getValue(COL_NAME1).isPresent()).isTrue();
    assertThat(actual.get(0).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
    assertThat(actual.get(0).getValue(COL_NAME4).isPresent()).isTrue();
    assertThat(actual.get(0).getValue(COL_NAME4).get().getAsInt()).isEqualTo(0);
    assertThat(actual.get(1).getValue(COL_NAME1).isPresent()).isTrue();
    assertThat(actual.get(1).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
    assertThat(actual.get(1).getValue(COL_NAME4).isPresent()).isTrue();
    assertThat(actual.get(1).getValue(COL_NAME4).get().getAsInt()).isEqualTo(1);
    assertThat(actual.get(2).getValue(COL_NAME1).isPresent()).isTrue();
    assertThat(actual.get(2).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
    assertThat(actual.get(2).getValue(COL_NAME4).isPresent()).isTrue();
    assertThat(actual.get(2).getValue(COL_NAME4).get().getAsInt()).isEqualTo(2);
}
Also used : Scanner(com.scalar.db.api.Scanner) ArrayList(java.util.ArrayList) Scan(com.scalar.db.api.Scan) Key(com.scalar.db.io.Key) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Example 5 with Key

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

Aggregations

Key (com.scalar.db.io.Key)603 Test (org.junit.jupiter.api.Test)391 Put (com.scalar.db.api.Put)211 Scan (com.scalar.db.api.Scan)145 Get (com.scalar.db.api.Get)134 Delete (com.scalar.db.api.Delete)118 Result (com.scalar.db.api.Result)94 Test (org.junit.Test)90 TextValue (com.scalar.db.io.TextValue)88 IntValue (com.scalar.db.io.IntValue)81 BooleanValue (com.scalar.db.io.BooleanValue)51 HashMap (java.util.HashMap)42 ExpectedResult (com.scalar.db.util.TestUtils.ExpectedResult)40 ArrayList (java.util.ArrayList)39 Value (com.scalar.db.io.Value)38 DoubleValue (com.scalar.db.io.DoubleValue)35 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)31 QueryRequest (software.amazon.awssdk.services.dynamodb.model.QueryRequest)29 KeyBytesEncoder (com.scalar.db.storage.dynamo.bytes.KeyBytesEncoder)27 ByteBuffer (java.nio.ByteBuffer)27