Skip to content

Commit

Permalink
[dynamodb] Optimize consumed read capacity (openhab#16693)
Browse files Browse the repository at this point in the history
We optimize consumed read capacity

Signed-off-by: Sami Salonen <ssalonen@gmail.com>
Signed-off-by: Patrik Gfeller <patrik.gfeller@proton.me>
  • Loading branch information
ssalonen authored and pgfeller committed Sep 29, 2024
1 parent 408ef5d commit 824af54
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions bundles/org.openhab.persistence.dynamodb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`).

Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public static QueryEnhancedRequest createQueryExpression(Class<? extends DynamoD
}
addFilterbyItemAndTimeFilter(queryBuilder, expectedTableSchema, itemName, filter);
addStateFilter(queryBuilder, expectedTableSchema, item, dtoClass, filter, unitProvider);
addLimit(queryBuilder, filter);
addProjection(dtoClass, expectedTableSchema, queryBuilder);
return queryBuilder.build();
}
Expand Down Expand Up @@ -94,6 +95,27 @@ private static void addProjection(Class<? extends DynamoDBItem<?>> dtoClass,
}
}

/**
* 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) {
Expand Down

0 comments on commit 824af54

Please sign in to comment.