Search in sources :

Example 31 with ScanAll

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

the class Snapshot method isWriteSetOverlappedWith.

private boolean isWriteSetOverlappedWith(Scan scan) {
    for (Map.Entry<Key, Put> entry : writeSet.entrySet()) {
        Put put = entry.getValue();
        if (scan instanceof ScanAll && put.forNamespace().equals(scan.forNamespace()) && put.forTable().equals(scan.forTable())) {
            return true;
        }
        if (!put.forNamespace().equals(scan.forNamespace()) || !put.forTable().equals(scan.forTable()) || !put.getPartitionKey().equals(scan.getPartitionKey())) {
            continue;
        }
        // If partition keys match and a primary key does not have a clustering key
        if (!put.getClusteringKey().isPresent()) {
            return true;
        }
        com.scalar.db.io.Key writtenKey = put.getClusteringKey().get();
        boolean isStartGiven = scan.getStartClusteringKey().isPresent();
        boolean isEndGiven = scan.getEndClusteringKey().isPresent();
        // If no range is specified, which means it scans the whole partition space
        if (!isStartGiven && !isEndGiven) {
            return true;
        }
        if (isStartGiven && isEndGiven) {
            com.scalar.db.io.Key startKey = scan.getStartClusteringKey().get();
            com.scalar.db.io.Key endKey = scan.getEndClusteringKey().get();
            // If startKey <= writtenKey <= endKey
            if ((scan.getStartInclusive() && writtenKey.equals(startKey)) || (writtenKey.compareTo(startKey) > 0 && writtenKey.compareTo(endKey) < 0) || (scan.getEndInclusive() && writtenKey.equals(endKey))) {
                return true;
            }
        }
        if (isStartGiven && !isEndGiven) {
            com.scalar.db.io.Key startKey = scan.getStartClusteringKey().get();
            // If startKey <= writtenKey
            if ((scan.getStartInclusive() && startKey.equals(writtenKey)) || writtenKey.compareTo(startKey) > 0) {
                return true;
            }
        }
        if (!isStartGiven) {
            com.scalar.db.io.Key endKey = scan.getEndClusteringKey().get();
            // If writtenKey <= endKey
            if ((scan.getEndInclusive() && writtenKey.equals(endKey)) || writtenKey.compareTo(endKey) < 0) {
                return true;
            }
        }
    }
    return false;
}
Also used : ScanAll(com.scalar.db.api.ScanAll) HashMap(java.util.HashMap) Map(java.util.Map) Put(com.scalar.db.api.Put)

Example 32 with ScanAll

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

the class SelectStatementHandlerTest method handle_ScanAllOperationWithPartitionKeyAndColumnProjected_ShouldProjectOnlyGivenColumns.

@Test
public void handle_ScanAllOperationWithPartitionKeyAndColumnProjected_ShouldProjectOnlyGivenColumns() {
    // Arrange
    when(container.queryItems(anyString(), any(CosmosQueryRequestOptions.class), eq(Record.class))).thenReturn(responseIterable);
    Record expected = new Record();
    when(responseIterable.iterator()).thenReturn(Collections.singletonList(expected).iterator());
    ScanAll scanAll = prepareScanAll().withProjections(Arrays.asList(ANY_NAME_1, ANY_NAME_4));
    // Act Assert
    assertThatCode(() -> handler.handle(scanAll)).doesNotThrowAnyException();
    // Assert
    String expectedQuery = "select r.id, " + "r.concatenatedPartitionKey, " + "{\"name1\":r.partitionKey[\"name1\"]} as partitionKey, " + "{\"name4\":r.values[\"name4\"]} as values " + "from Record r";
    verify(container).queryItems(eq(expectedQuery), any(CosmosQueryRequestOptions.class), eq(Record.class));
}
Also used : CosmosQueryRequestOptions(com.azure.cosmos.models.CosmosQueryRequestOptions) ScanAll(com.scalar.db.api.ScanAll) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test)

Example 33 with ScanAll

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

the class SelectStatementHandlerTest method handle_ScanAllOperationWithProjectionsOnMultiplePartitionAndClusteringKeys_ShouldCallQueryItemsWithProperQuery.

@Test
public void handle_ScanAllOperationWithProjectionsOnMultiplePartitionAndClusteringKeys_ShouldCallQueryItemsWithProperQuery() {
    // Arrange
    when(metadata.getPartitionKeyNames()).thenReturn(new LinkedHashSet<>(Arrays.asList(ANY_NAME_1, ANY_NAME_2)));
    when(metadata.getClusteringKeyNames()).thenReturn(new LinkedHashSet<>(Arrays.asList(ANY_NAME_3, ANY_NAME_4)));
    when(metadata.getClusteringOrder(ANY_NAME_3)).thenReturn(Order.ASC);
    when(metadata.getClusteringOrder(ANY_NAME_4)).thenReturn(Order.DESC);
    when(container.queryItems(anyString(), any(CosmosQueryRequestOptions.class), eq(Record.class))).thenReturn(responseIterable);
    Record expected = new Record();
    when(responseIterable.iterator()).thenReturn(Collections.singletonList(expected).iterator());
    ScanAll scanAll = new ScanAll().withProjections(Arrays.asList(ANY_NAME_1, ANY_NAME_2, ANY_NAME_3, ANY_NAME_4)).forNamespace(ANY_NAMESPACE_NAME).forTable(ANY_TABLE_NAME);
    String query = "select r.id, " + "r.concatenatedPartitionKey, " + "{\"name1\":r.partitionKey[\"name1\"],\"name2\":r.partitionKey[\"name2\"]} as partitionKey, " + "{\"name3\":r.clusteringKey[\"name3\"],\"name4\":r.clusteringKey[\"name4\"]} as clusteringKey " + "from Record r";
    // Act Assert
    assertThatCode(() -> handler.handle(scanAll)).doesNotThrowAnyException();
    // Assert
    verify(container).queryItems(eq(query), any(CosmosQueryRequestOptions.class), eq(Record.class));
}
Also used : CosmosQueryRequestOptions(com.azure.cosmos.models.CosmosQueryRequestOptions) ScanAll(com.scalar.db.api.ScanAll) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test)

Example 34 with ScanAll

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

the class OperationCheckerTest method whenCheckingScanAllOperationWithAllValidArguments_shouldNotThrowAnyException.

@Test
public void whenCheckingScanAllOperationWithAllValidArguments_shouldNotThrowAnyException() {
    // Arrange
    List<String> projections = Arrays.asList(COL1, COL2, COL3);
    int limit = 10;
    ScanAll scanAll = new ScanAll().withProjections(projections).withLimit(limit).forNamespace(NAMESPACE).forTable(TABLE_NAME);
    // Act Assert
    assertThatCode(() -> operationChecker.check(scanAll)).doesNotThrowAnyException();
}
Also used : ScanAll(com.scalar.db.api.ScanAll) Test(org.junit.jupiter.api.Test)

Example 35 with ScanAll

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

the class SelectStatementHandlerTest method prepare_ScanAllOperationWithLimit_ShouldPrepareProperQuery.

@Test
public void prepare_ScanAllOperationWithLimit_ShouldPrepareProperQuery() {
    // Arrange
    String expected = Joiner.on(" ").skipNulls().join(new String[] { "SELECT * FROM", ANY_NAMESPACE_NAME + "." + ANY_TABLE_NAME, "LIMIT", ANY_LIMIT + ";" });
    configureBehavior(expected);
    ScanAll scanAll = prepareScanAll();
    scanAll.withLimit(ANY_LIMIT);
    // Act
    handler.prepare(scanAll);
    // Assert
    verify(session).prepare(expected);
}
Also used : ScanAll(com.scalar.db.api.ScanAll) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test)

Aggregations

ScanAll (com.scalar.db.api.ScanAll)52 Test (org.junit.jupiter.api.Test)43 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)10 Result (com.scalar.db.api.Result)9 CosmosQueryRequestOptions (com.azure.cosmos.models.CosmosQueryRequestOptions)8 Put (com.scalar.db.api.Put)4 HashMap (java.util.HashMap)4 Scan (com.scalar.db.api.Scan)3 TableMetadata (com.scalar.db.api.TableMetadata)3 Key (com.scalar.db.io.Key)3 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)3 Assertions.catchThrowable (org.assertj.core.api.Assertions.catchThrowable)3 ScanRequest (software.amazon.awssdk.services.dynamodb.model.ScanRequest)3 SelectQuery (com.scalar.db.storage.jdbc.query.SelectQuery)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 List (java.util.List)2 Nonnull (javax.annotation.Nonnull)2 ResultSet (com.datastax.driver.core.ResultSet)1 Select (com.datastax.driver.core.querybuilder.Select)1