From 0df790c68706013e5c8740dc48793243d9362cd2 Mon Sep 17 00:00:00 2001 From: Joan Pujol Date: Sat, 15 Jan 2022 16:39:19 +0100 Subject: [PATCH] [influxdb] Fix previous item state performance issues (#11993) Fixes #11878 Signed-off-by: Joan Pujol --- ...Influx2FilterCriteriaQueryCreatorImpl.java | 30 +++++++++++++------ ...luxFilterCriteriaQueryCreatorImplTest.java | 12 ++++++++ 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/bundles/org.openhab.persistence.influxdb/src/main/java/org/openhab/persistence/influxdb/internal/influx2/Influx2FilterCriteriaQueryCreatorImpl.java b/bundles/org.openhab.persistence.influxdb/src/main/java/org/openhab/persistence/influxdb/internal/influx2/Influx2FilterCriteriaQueryCreatorImpl.java index 9bb62af45a1..606f77ebe26 100644 --- a/bundles/org.openhab.persistence.influxdb/src/main/java/org/openhab/persistence/influxdb/internal/influx2/Influx2FilterCriteriaQueryCreatorImpl.java +++ b/bundles/org.openhab.persistence.influxdb/src/main/java/org/openhab/persistence/influxdb/internal/influx2/Influx2FilterCriteriaQueryCreatorImpl.java @@ -87,19 +87,31 @@ public class Influx2FilterCriteriaQueryCreatorImpl implements FilterCriteriaQuer flux = flux.filter(restrictions); } - if (criteria.getOrdering() != null) { - boolean desc = criteria.getOrdering() == FilterCriteria.Ordering.DESCENDING; - flux = flux.sort().withDesc(desc).withColumns(new String[] { COLUMN_TIME_NAME_V2 }); - } - - if (criteria.getPageSize() != Integer.MAX_VALUE) { - flux = flux.limit(criteria.getPageSize()).withPropertyValue("offset", - criteria.getPageNumber() * criteria.getPageSize()); - } + flux = applyOrderingAndPageSize(criteria, flux); return flux.toString(); } + private Flux applyOrderingAndPageSize(FilterCriteria criteria, Flux flux) { + var lastOptimization = criteria.getOrdering() == FilterCriteria.Ordering.DESCENDING + && criteria.getPageSize() == 1; + + if (lastOptimization) { + flux = flux.last(); + } else { + if (criteria.getOrdering() != null) { + boolean desc = criteria.getOrdering() == FilterCriteria.Ordering.DESCENDING; + flux = flux.sort().withDesc(desc).withColumns(new String[] { COLUMN_TIME_NAME_V2 }); + } + + if (criteria.getPageSize() != Integer.MAX_VALUE) { + flux = flux.limit(criteria.getPageSize()).withPropertyValue("offset", + criteria.getPageNumber() * criteria.getPageSize()); + } + } + return flux; + } + private String calculateMeasurementName(String itemName) { String name = itemName; diff --git a/bundles/org.openhab.persistence.influxdb/src/test/java/org/openhab/persistence/influxdb/internal/InfluxFilterCriteriaQueryCreatorImplTest.java b/bundles/org.openhab.persistence.influxdb/src/test/java/org/openhab/persistence/influxdb/internal/InfluxFilterCriteriaQueryCreatorImplTest.java index 5a59591c910..7ae4f46ce35 100644 --- a/bundles/org.openhab.persistence.influxdb/src/test/java/org/openhab/persistence/influxdb/internal/InfluxFilterCriteriaQueryCreatorImplTest.java +++ b/bundles/org.openhab.persistence.influxdb/src/test/java/org/openhab/persistence/influxdb/internal/InfluxFilterCriteriaQueryCreatorImplTest.java @@ -168,6 +168,18 @@ public class InfluxFilterCriteriaQueryCreatorImplTest { + "|> sort(desc:false, columns:[\"_time\"])")); } + @Test + public void testPreviousState() { + FilterCriteria criteria = createBaseCriteria(); + criteria.setOrdering(FilterCriteria.Ordering.DESCENDING); + criteria.setPageSize(1); + String queryV2 = instanceV2.createQuery(criteria, RETENTION_POLICY); + assertThat(queryV2, + equalTo("from(bucket:\"origin\")\n\t" + "|> range(start:-100y)\n\t" + + "|> filter(fn: (r) => r[\"_measurement\"] == \"sampleItem\")\n\t" + + "|> keep(columns:[\"_measurement\", \"_time\", \"_value\"])\n\t" + "|> last()")); + } + private FilterCriteria createBaseCriteria() { return createBaseCriteria(ITEM_NAME); }