Skip to content

Commit

Permalink
use item methods if last state update/change not in persistence
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Herwege <[email protected]>
  • Loading branch information
mherwege committed Nov 29, 2024
1 parent 660102e commit 41cb337
Showing 1 changed file with 29 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
* @author Mark Herwege - lastChange and nextChange methods
* @author Mark Herwege - handle persisted GroupItem with QuantityType
* @author Mark Herwege - add median methods
* @author Mark Herwege - use item lastChange and lastUpdate methods if not in peristence
*/
@Component(immediate = true)
@NonNullByDefault
Expand Down Expand Up @@ -331,6 +332,7 @@ private static void internalPersist(Item item, TimeSeries timeSeries, @Nullable

/**
* Query the last historic update time of a given <code>item</code>. The default persistence service is used.
* Note the {@link Item.getLastStateUpdate()} is generally preferred to get the last update time of an item.
*
* @param item the item for which the last historic update time is to be returned
* @return point in time of the last historic update to <code>item</code>, <code>null</code> if there are no
Expand All @@ -343,6 +345,7 @@ private static void internalPersist(Item item, TimeSeries timeSeries, @Nullable

/**
* Query for the last historic update time of a given <code>item</code>.
* Note the {@link Item.getLastStateUpdate()} is generally preferred to get the last update time of an item.
*
* @param item the item for which the last historic update time is to be returned
* @param serviceId the name of the {@link PersistenceService} to use
Expand Down Expand Up @@ -386,6 +389,7 @@ private static void internalPersist(Item item, TimeSeries timeSeries, @Nullable

/**
* Query the last historic change time of a given <code>item</code>. The default persistence service is used.
* Note the {@link Item.getLastStateChange()} is generally preferred to get the last state change time of an item.
*
* @param item the item for which the last historic change time is to be returned
* @return point in time of the last historic change to <code>item</code>, <code>null</code> if there are no
Expand All @@ -398,6 +402,7 @@ private static void internalPersist(Item item, TimeSeries timeSeries, @Nullable

/**
* Query for the last historic change time of a given <code>item</code>.
* Note the {@link Item.getLastStateChange()} is generally preferred to get the last state change time of an item.
*
* @param item the item for which the last historic change time is to be returned
* @param serviceId the name of the {@link PersistenceService} to use
Expand Down Expand Up @@ -461,26 +466,26 @@ private static void internalPersist(Item item, TimeSeries timeSeries, @Nullable
filter.setPageNumber(startPage);

Iterable<HistoricItem> items = qService.query(filter);
Iterator<HistoricItem> itemIterator = items.iterator();
State state = item.getState();
if (itemIterator.hasNext()) {
if (!skipEqual) {
HistoricItem historicItem = itemIterator.next();
if (!forward && !historicItem.getState().equals(state)) {
// Last persisted state value different from current state value, so it must have updated since
// last persist. We do not know when.
return null;
}
return historicItem.getTimestamp();
} else {
HistoricItem historicItem = itemIterator.next();
int itemCount = 1;
if (!historicItem.getState().equals(state)) {
// Persisted state value different from current state value, so it must have changed, but we do
// not know when when looking backward.
return forward ? historicItem.getTimestamp() : null;
}
while (items != null) {
while (items != null) {
Iterator<HistoricItem> itemIterator = items.iterator();
int itemCount = 0;
State state = item.getState();
if (itemIterator.hasNext()) {
if (!skipEqual) {
HistoricItem historicItem = itemIterator.next();
if (!forward && !historicItem.getState().equals(state)) {
// Last persisted state value different from current state value, so it must have updated
// since last persist. We do not know when from persistence, so get it from the item.
return item.getLastStateUpdate();
}
return historicItem.getTimestamp();
} else {
HistoricItem historicItem = itemIterator.next();
if (!historicItem.getState().equals(state)) {
// Persisted state value different from current state value, so it must have changed, but we
// do not know when looking backward in persistence. Get it from the item.
return forward ? historicItem.getTimestamp() : item.getLastStateChange();
}
while (historicItem.getState().equals(state) && itemIterator.hasNext()) {
HistoricItem nextHistoricItem = itemIterator.next();
itemCount++;
Expand All @@ -492,7 +497,6 @@ private static void internalPersist(Item item, TimeSeries timeSeries, @Nullable
if (itemCount == filter.getPageSize()) {
filter.setPageNumber(++startPage);
items = qService.query(filter);
itemCount = 0;
} else {
items = null;
}
Expand All @@ -508,6 +512,7 @@ private static void internalPersist(Item item, TimeSeries timeSeries, @Nullable

/**
* Returns the previous state of a given <code>item</code>.
* Note the {@link Item.getLastState()} is generally preferred to get the previous state of an item.
*
* @param item the item to get the previous state value for
* @return the previous state or <code>null</code> if no previous state could be found, or if the default
Expand All @@ -519,6 +524,7 @@ private static void internalPersist(Item item, TimeSeries timeSeries, @Nullable

/**
* Returns the previous state of a given <code>item</code>.
* Note the {@link Item.getLastState()} is generally preferred to get the previous state of an item.
*
* @param item the item to get the previous state value for
* @param skipEqual if true, skips equal state values and searches the first state not equal the current state
Expand All @@ -532,6 +538,7 @@ private static void internalPersist(Item item, TimeSeries timeSeries, @Nullable
/**
* Returns the previous state of a given <code>item</code>.
* The {@link PersistenceService} identified by the <code>serviceId</code> is used.
* Note the {@link Item.getLastState()} is generally preferred to get the previous state of an item.
*
* @param item the item to get the previous state value for
* @param serviceId the name of the {@link PersistenceService} to use
Expand All @@ -545,6 +552,7 @@ private static void internalPersist(Item item, TimeSeries timeSeries, @Nullable
/**
* Returns the previous state of a given <code>item</code>.
* The {@link PersistenceService} identified by the <code>serviceId</code> is used.
* Note the {@link Item.getLastState()} is generally preferred to get the previous state of an item.
*
* @param item the item to get the previous state value for
* @param skipEqual if <code>true</code>, skips equal state values and searches the first state not equal the
Expand Down

0 comments on commit 41cb337

Please sign in to comment.