Skip to content

Commit

Permalink
default query with alias replacement
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Herwege <mark.herwege@telenet.be>
  • Loading branch information
mherwege committed Jan 6, 2025
1 parent 03c5d13 commit b7d0862
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,20 @@ public enum Ordering {
/** Filter result to only contain entries that evaluate to true with the given operator and state */
private @Nullable State state;

public FilterCriteria() {
}

public FilterCriteria(FilterCriteria filter) {
this.itemName = filter.itemName;
this.beginDate = filter.beginDate;
this.endDate = filter.endDate;
this.pageNumber = filter.pageNumber;
this.pageSize = filter.pageSize;
this.operator = filter.operator;
this.ordering = filter.ordering;
this.state = filter.state;
}

public @Nullable String getItemName() {
return itemName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,22 @@ public interface ModifiablePersistenceService extends QueryablePersistenceServic
* Removes data associated with an item from a persistence service.
* If all data is removed for the specified item, the persistence service should free any resources associated with
* the item (e.g. remove any tables or delete files from the storage).
* Persistence services supporting aliases should override the default implementation from this interface that
* ignores aliases when querying.
* Persistence services supporting aliases, and relying on lookups in the item registry, should override the default
* implementation from this interface.
*
* @param filter the filter to apply to the data removal. ItemName can not be null.
* @param alias for item name in database
* @return true if the query executed successfully
* @throws IllegalArgumentException if item name is null.
*/
default boolean remove(FilterCriteria filter, @Nullable String alias) throws IllegalArgumentException {
// Default implementation ignores alias
// Default implementation changes the filter to have the alias as itemName.
// This gives correct results as long as the persistence service does not rely on a lookup in the item registry
// (in which case the item will not be found).
if (alias != null) {
FilterCriteria aliasFilter = new FilterCriteria(filter).setItemName(alias);
return remove(aliasFilter);
}
return remove(filter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@
*/
package org.openhab.core.persistence;

import java.time.ZonedDateTime;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.types.State;

/**
* A queryable persistence service which can be used to store and retrieve
Expand All @@ -39,15 +43,39 @@ public interface QueryablePersistenceService extends PersistenceService {

/**
* Queries the {@link PersistenceService} for historic data with a given {@link FilterCriteria}.
* If the persistence service implementing this interface supports aliases, the default implementation should be
* overriden to query the database with the aliased name.
* If the persistence service implementing this interface supports aliases and relies on item registry lookups, the
* default implementation should be overriden to query the database with the aliased name.
*
* @param filter the filter to apply to the query
* @param alias for item name in database
* @return a time series of items
*/
default Iterable<HistoricItem> query(FilterCriteria filter, @Nullable String alias) {
// Default implementation ignores alias
// Default implementation changes the filter to have the alias as itemName and sets it back in the returned
// result.
// This gives correct results as long as the persistence service does not rely on a lookup in the item registry
// (in which case the item will not be found).
String itemName = filter.getItemName();
if (itemName != null && alias != null) {
FilterCriteria aliasFilter = new FilterCriteria(filter).setItemName(alias);
return StreamSupport.stream(query(aliasFilter).spliterator(), false).map(hi -> new HistoricItem() {

@Override
public ZonedDateTime getTimestamp() {
return hi.getTimestamp();
}

@Override
public State getState() {
return hi.getState();
}

@Override
public String getName() {
return itemName;
}
}).collect(Collectors.toList());
}
return query(filter);
}

Expand Down

0 comments on commit b7d0862

Please sign in to comment.