[dynamodb] Optimize consumed read capacity (#16693)

We optimize consumed read capacity

Signed-off-by: Sami Salonen <ssalonen@gmail.com>
This commit is contained in:
Sami Salonen 2024-04-28 21:52:16 +03:00 committed by GitHub
parent c9c75124aa
commit dee61a7651
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 0 deletions

View File

@ -262,4 +262,9 @@ Eclipse instructions
-DDYNAMODBTEST_REGION=REGION-ID
-DDYNAMODBTEST_ACCESS=ACCESS-KEY
-DDYNAMODBTEST_SECRET=SECRET
--add-opens=java.base/java.lang=ALL-UNNAMED
```
The `--add-opens` parameter is necessary also with the local temporary DynamoDB server, otherwise the mockito will fail at runtime with (`java.base does not "opens java.lang" to unnamed module`).

View File

@ -62,6 +62,7 @@ public class DynamoDBQueryUtils {
}
addFilterbyItemAndTimeFilter(queryBuilder, expectedTableSchema, itemName, filter);
addStateFilter(queryBuilder, expectedTableSchema, item, dtoClass, filter, unitProvider);
addLimit(queryBuilder, filter);
addProjection(dtoClass, expectedTableSchema, queryBuilder);
return queryBuilder.build();
}
@ -94,6 +95,27 @@ public class DynamoDBQueryUtils {
}
}
/**
* Add optimization to limit amount of data queried from DynamoDB
*
* DynamoDB allows to limit the amount of items read by the query ("Limit" parameter) - additional items are
* paginated in the raw DynamoDB responses. We can use this to optimize the read capacity.
*
* DynamoDB FilterExpression is applied after the query finishes but before results are returned. The query
* still consumes the same read capacity. It is also to note here that the query might return less than "Limit"
* items, and the results are paginated. Since the final openHAB pagination is done in the persistence service, the
* pagination of the DynamoDB remains as a hidden implementation detail.
*
* @param queryBuilder builder for DynamoDB query
* @param filter openHAB filter
*/
private static void addLimit(QueryEnhancedRequest.Builder queryBuilder, final FilterCriteria filter) {
boolean pageSizeSpecified = filter.getPageSize() != Integer.MAX_VALUE;
if (pageSizeSpecified) {
queryBuilder.limit(filter.getPageSize());
}
}
private static void addStateFilter(QueryEnhancedRequest.Builder queryBuilder,
ExpectedTableSchema expectedTableSchema, Item item, Class<? extends DynamoDBItem<?>> dtoClass,
FilterCriteria filter, UnitProvider unitProvider) {