]> git.basschouten.com Git - openhab-addons.git/commitdiff
[dynamodb] Optimize consumed read capacity (#16693)
authorSami Salonen <ssalonen@gmail.com>
Sun, 28 Apr 2024 18:52:16 +0000 (21:52 +0300)
committerGitHub <noreply@github.com>
Sun, 28 Apr 2024 18:52:16 +0000 (20:52 +0200)
We optimize consumed read capacity

Signed-off-by: Sami Salonen <ssalonen@gmail.com>
bundles/org.openhab.persistence.dynamodb/README.md
bundles/org.openhab.persistence.dynamodb/src/main/java/org/openhab/persistence/dynamodb/internal/DynamoDBQueryUtils.java

index aac172ee88498749e87b626afc8eae8f20965bab..db54fe0fb6c3181da0cf529eb6358bdf90db6f2f 100644 (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`).
+
index ce92d1e8befbde3d50787aab5ec6402abd4eb4ec..4d0e59390f4b43e3f287f7b66913ae66dac4adb4 100644 (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) {