[influxdb] Fix previous item state performance issues (#11993)

Fixes #11878

Signed-off-by: Joan Pujol <joanpujol@gmail.com>
This commit is contained in:
Joan Pujol 2022-01-15 16:39:19 +01:00 committed by GitHub
parent bb2e4c7c65
commit 0df790c687
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 9 deletions

View File

@ -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;

View File

@ -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);
}