use of com.scalar.db.api.Scan in project scalardb by scalar-labs.
the class AdminIntegrationTestBase method truncateTable_ShouldTruncateProperly.
@Test
public void truncateTable_ShouldTruncateProperly() throws ExecutionException, IOException {
// Arrange
Key partitionKey = new Key(COL_NAME2, "aaa", COL_NAME1, 1);
Key clusteringKey = new Key(COL_NAME4, 2, COL_NAME3, "bbb");
storage.put(new Put(partitionKey, clusteringKey).withValue(COL_NAME5, 3).withValue(COL_NAME6, "ccc").withValue(COL_NAME7, 4L).withValue(COL_NAME8, 1.0f).withValue(COL_NAME9, 1.0d).withValue(COL_NAME10, true).withValue(COL_NAME11, "ddd".getBytes(StandardCharsets.UTF_8)).forNamespace(namespace1).forTable(TABLE1));
// Act
admin.truncateTable(namespace1, TABLE1);
// Assert
Scanner scanner = storage.scan(new Scan(partitionKey).forNamespace(namespace1).forTable(TABLE1));
assertThat(scanner.all()).isEmpty();
scanner.close();
}
use of com.scalar.db.api.Scan in project scalardb by scalar-labs.
the class SelectStatementHandler method executeQueryWithIndex.
private List<Map<String, AttributeValue>> executeQueryWithIndex(Selection selection, TableMetadata tableMetadata) {
DynamoOperation dynamoOperation = new DynamoOperation(selection, tableMetadata);
Value<?> keyValue = selection.getPartitionKey().get().get(0);
String column = keyValue.getName();
String indexTable = dynamoOperation.getGlobalIndexName(column);
QueryRequest.Builder builder = QueryRequest.builder().tableName(dynamoOperation.getTableName()).indexName(indexTable);
String expressionColumnName = DynamoOperation.COLUMN_NAME_ALIAS + "0";
String condition = expressionColumnName + " = " + DynamoOperation.VALUE_ALIAS + "0";
ValueBinder binder = new ValueBinder(DynamoOperation.VALUE_ALIAS);
keyValue.accept(binder);
Map<String, AttributeValue> bindMap = binder.build();
builder.keyConditionExpression(condition).expressionAttributeValues(bindMap).expressionAttributeNames(ImmutableMap.of(expressionColumnName, column));
if (!selection.getProjections().isEmpty()) {
projectionExpression(builder, selection);
}
if (selection instanceof Scan) {
Scan scan = (Scan) selection;
if (scan.getLimit() > 0) {
builder.limit(scan.getLimit());
}
}
QueryResponse queryResponse = client.query(builder.build());
return new ArrayList<>(queryResponse.items());
}
use of com.scalar.db.api.Scan in project scalardb by scalar-labs.
the class JdbcServiceTest method whenScanExecuted_shouldCallQueryBuilder.
@Test
@SuppressFBWarnings("OBL_UNSATISFIED_OBLIGATION")
public void whenScanExecuted_shouldCallQueryBuilder() throws Exception {
// Arrange
when(queryBuilder.select(any())).thenReturn(selectQueryBuilder);
when(selectQueryBuilder.from(any(), any(), any())).thenReturn(selectQueryBuilder);
when(selectQueryBuilder.where(any(), any(), anyBoolean(), any(), anyBoolean())).thenReturn(selectQueryBuilder);
when(selectQueryBuilder.orderBy(any())).thenReturn(selectQueryBuilder);
when(selectQueryBuilder.limit(anyInt())).thenReturn(selectQueryBuilder);
when(selectQueryBuilder.build()).thenReturn(selectQuery);
when(connection.prepareStatement(any())).thenReturn(preparedStatement);
when(preparedStatement.executeQuery()).thenReturn(resultSet);
when(resultSet.next()).thenReturn(false);
// Act
Scan scan = new Scan(new Key("p1", "val")).forNamespace(NAMESPACE).forTable(TABLE);
jdbcService.scan(scan, connection);
// Assert
verify(operationChecker).check(any(Scan.class));
verify(queryBuilder).select(any());
}
use of com.scalar.db.api.Scan in project scalardb by scalar-labs.
the class StorageWithReservedKeywordIntegrationTestBase method delete_WithReservedKeywordAndDeleteWithPartitionKeyAndClusteringKeyGiven_ShouldDeleteSingleRecordProperly.
@Test
public void delete_WithReservedKeywordAndDeleteWithPartitionKeyAndClusteringKeyGiven_ShouldDeleteSingleRecordProperly() throws IOException, ExecutionException {
// Arrange
populateRecords();
int pKey = 0;
int cKey = 0;
Key partitionKey = new Key(columnName1, pKey);
// Act
Delete delete = prepareDelete(pKey, cKey);
assertThatCode(() -> storage.delete(delete)).doesNotThrowAnyException();
// Assert
List<Result> results = scanAll(new Scan(partitionKey));
assertThat(results.size()).isEqualTo(2);
assertThat(results.get(0).getValue(columnName1).isPresent()).isTrue();
assertThat(results.get(0).getValue(columnName1).get().getAsInt()).isEqualTo(0);
assertThat(results.get(0).getValue(columnName4).isPresent()).isTrue();
assertThat(results.get(0).getValue(columnName4).get().getAsInt()).isEqualTo(cKey + 1);
assertThat(results.get(1).getValue(columnName1).isPresent()).isTrue();
assertThat(results.get(1).getValue(columnName1).get().getAsInt()).isEqualTo(0);
assertThat(results.get(1).getValue(columnName4).isPresent()).isTrue();
assertThat(results.get(1).getValue(columnName4).get().getAsInt()).isEqualTo(cKey + 2);
}
use of com.scalar.db.api.Scan in project scalardb by scalar-labs.
the class StorageWithReservedKeywordIntegrationTestBase method delete_WithReservedKeywordAndMultipleDeleteWithDifferentConditionsGiven_ShouldDeleteProperly.
@Test
public void delete_WithReservedKeywordAndMultipleDeleteWithDifferentConditionsGiven_ShouldDeleteProperly() throws IOException, ExecutionException {
// Arrange
List<Put> puts = preparePuts();
List<Delete> deletes = prepareDeletes();
storage.mutate(Arrays.asList(puts.get(0), puts.get(1), puts.get(2)));
deletes.get(0).withCondition(new DeleteIfExists());
deletes.get(1).withCondition(new DeleteIf(new ConditionalExpression(columnName2, new TextValue("1"), ConditionalExpression.Operator.EQ)));
// Act
assertThatCode(() -> storage.delete(Arrays.asList(deletes.get(0), deletes.get(1), deletes.get(2)))).doesNotThrowAnyException();
// Assert
List<Result> results = scanAll(new Scan(new Key(columnName1, 0)));
assertThat(results.size()).isEqualTo(0);
}
Aggregations