From d434fc5e4e8d291478273d3c8e07141c4457f575 Mon Sep 17 00:00:00 2001 From: J-N-K Date: Tue, 2 Jan 2024 20:22:37 +0100 Subject: [PATCH] [inmemory] Add filterCritera ordering (#16185) Signed-off-by: Jan N. Klug --- .../internal/InMemoryPersistenceService.java | 9 ++++- .../internal/InMemoryPersistenceTests.java | 39 +++++++++++++++++-- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/bundles/org.openhab.persistence.inmemory/src/main/java/org/openhab/persistence/inmemory/internal/InMemoryPersistenceService.java b/bundles/org.openhab.persistence.inmemory/src/main/java/org/openhab/persistence/inmemory/internal/InMemoryPersistenceService.java index 8564a7b6e9b..6b138ccdb63 100644 --- a/bundles/org.openhab.persistence.inmemory/src/main/java/org/openhab/persistence/inmemory/internal/InMemoryPersistenceService.java +++ b/bundles/org.openhab.persistence.inmemory/src/main/java/org/openhab/persistence/inmemory/internal/InMemoryPersistenceService.java @@ -173,9 +173,14 @@ public class InMemoryPersistenceService implements ModifiablePersistenceService Lock lock = persistItem.lock(); lock.lock(); + + Comparator comparator = filter.getOrdering() == FilterCriteria.Ordering.ASCENDING + ? Comparator.comparing(PersistEntry::timestamp) + : Comparator.comparing(PersistEntry::timestamp).reversed(); + try { - return persistItem.database().stream().filter(e -> applies(e, filter)).map(e -> toHistoricItem(itemName, e)) - .toList(); + return persistItem.database().stream().filter(e -> applies(e, filter)).sorted(comparator) + .map(e -> toHistoricItem(itemName, e)).toList(); } finally { lock.unlock(); } diff --git a/bundles/org.openhab.persistence.inmemory/src/test/java/org/openhab/persistence/inmemory/internal/InMemoryPersistenceTests.java b/bundles/org.openhab.persistence.inmemory/src/test/java/org/openhab/persistence/inmemory/internal/InMemoryPersistenceTests.java index 750e7f9b0a2..7aa36c434ec 100644 --- a/bundles/org.openhab.persistence.inmemory/src/test/java/org/openhab/persistence/inmemory/internal/InMemoryPersistenceTests.java +++ b/bundles/org.openhab.persistence.inmemory/src/test/java/org/openhab/persistence/inmemory/internal/InMemoryPersistenceTests.java @@ -13,15 +13,14 @@ package org.openhab.persistence.inmemory.internal; import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.closeTo; -import static org.hamcrest.Matchers.empty; -import static org.hamcrest.Matchers.hasSize; -import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.*; import static org.mockito.Mockito.when; import java.time.ZoneId; import java.time.ZonedDateTime; +import java.util.ArrayList; import java.util.Comparator; +import java.util.List; import java.util.TreeSet; import org.eclipse.jdt.annotation.NonNullByDefault; @@ -144,6 +143,38 @@ public class InMemoryPersistenceTests { assertThat(storedStates, is(empty())); } + @Test + public void querySupportsAscendingOrdering() { + ZonedDateTime start = ZonedDateTime.of(2020, 12, 1, 12, 0, 0, 0, ZoneId.systemDefault()); + service.store(item, start, new DecimalType(1)); + service.store(item, start.plusHours(1), new DecimalType(2)); + service.store(item, start.plusHours(2), new DecimalType(3)); + + filterCriteria.setOrdering(FilterCriteria.Ordering.ASCENDING); + filterCriteria.setBeginDate(start); + + List resultSet = new ArrayList<>(); + service.query(filterCriteria).forEach(h -> resultSet.add(((DecimalType) h.getState()).intValue())); + + assertThat(resultSet, contains(1, 2, 3)); + } + + @Test + public void querySupportsDescendingOrdering() { + ZonedDateTime start = ZonedDateTime.of(2020, 12, 1, 12, 0, 0, 0, ZoneId.systemDefault()); + service.store(item, start, new DecimalType(1)); + service.store(item, start.plusHours(1), new DecimalType(2)); + service.store(item, start.plusHours(2), new DecimalType(3)); + + filterCriteria.setOrdering(FilterCriteria.Ordering.DESCENDING); + filterCriteria.setBeginDate(start); + + List resultSet = new ArrayList<>(); + service.query(filterCriteria).forEach(h -> resultSet.add(((DecimalType) h.getState()).intValue())); + + assertThat(resultSet, contains(3, 2, 1)); + } + @Test public void removeBetweenTimes() { State historicState1 = new StringType("value1");