Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[inmemory] Add filterCritera ordering #16185

Merged
merged 1 commit into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,14 @@ public Iterable<HistoricItem> query(FilterCriteria filter) {

Lock lock = persistItem.lock();
lock.lock();

Comparator<PersistEntry> 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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -144,6 +143,38 @@ public void queryUnknownItemReturnsEmptyList() {
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<Integer> 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<Integer> 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");
Expand Down