From: Sami Salonen Date: Sun, 28 Apr 2024 18:52:16 +0000 (+0300) Subject: [dynamodb] Optimize consumed read capacity (#16693) X-Git-Url: https://git.basschouten.com/?a=commitdiff_plain;h=dee61a7651227fcf2fb6f3f9ba967fc58671a6ce;p=openhab-addons.git [dynamodb] Optimize consumed read capacity (#16693) We optimize consumed read capacity Signed-off-by: Sami Salonen --- diff --git a/bundles/org.openhab.persistence.dynamodb/README.md b/bundles/org.openhab.persistence.dynamodb/README.md index aac172ee88..db54fe0fb6 100644 --- a/bundles/org.openhab.persistence.dynamodb/README.md +++ b/bundles/org.openhab.persistence.dynamodb/README.md @@ -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`). + diff --git a/bundles/org.openhab.persistence.dynamodb/src/main/java/org/openhab/persistence/dynamodb/internal/DynamoDBQueryUtils.java b/bundles/org.openhab.persistence.dynamodb/src/main/java/org/openhab/persistence/dynamodb/internal/DynamoDBQueryUtils.java index ce92d1e8be..4d0e59390f 100644 --- a/bundles/org.openhab.persistence.dynamodb/src/main/java/org/openhab/persistence/dynamodb/internal/DynamoDBQueryUtils.java +++ b/bundles/org.openhab.persistence.dynamodb/src/main/java/org/openhab/persistence/dynamodb/internal/DynamoDBQueryUtils.java @@ -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> dtoClass, FilterCriteria filter, UnitProvider unitProvider) {