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;
}
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));
}
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));
}
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();
}
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);
}
Aggregations