Skip to content

Commit 29e5341

Browse files
committed
simplify and document
Signed-off-by: Mark Herwege <mark.herwege@telenet.be>
1 parent 763b26b commit 29e5341

2 files changed

Lines changed: 24 additions & 11 deletions

File tree

bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/PersistenceManager.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,16 @@
2424
*/
2525
@NonNullByDefault
2626
public interface PersistenceManager {
27+
28+
/**
29+
* External code that updates persisted data, that may have an impact on the persistence logic (restoring item
30+
* states, forecast logic), should call this method to inform the {@link PersistenceManager} about a potential
31+
* impact. The {@link PersistenceManager} will query the service again to get the necessary information. This all
32+
* happens in the calling thread and may therefore take some time. If this undesired, this call should be performed
33+
* asynchronously.
34+
*
35+
* @param persistenceService the persistence service
36+
* @param item the item for which persisted data has been updated
37+
*/
2738
void handleExternalPersistenceDataChange(PersistenceService persistenceService, Item item);
2839
}

bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/internal/PersistenceManagerImpl.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -467,11 +467,8 @@ public void handleExternalPersistenceDataChange(PersistenceService persistenceSe
467467
container.getMatchingConfigurations(FORECAST)))
468468
.distinct().anyMatch(itemConf -> appliesToItem(itemConf, item)))
469469
.forEach(container -> {
470-
container.scheduleNextPersistedForecastForItem(item.getName());
471-
PersistedItem persistedItem = container.getPersistedItem(item);
472-
if (persistedItem != null) {
473-
container.restoreItemStateFromPersistenceUpdate(item, persistedItem);
474-
}
470+
container.restoreItemStateFromPersistenceUpdate(item);
471+
container.scheduleNextPersistedForecastForItem(item);
475472
});
476473
}
477474

@@ -589,7 +586,7 @@ public void addItem(Item item) {
589586
restoreItemStateOnStartup(item);
590587
}
591588
if (getMatchingConfigurations(FORECAST).anyMatch(configuration -> appliesToItem(configuration, item))) {
592-
scheduleNextPersistedForecastForItem(item.getName());
589+
scheduleNextPersistedForecastForItem(item);
593590
}
594591
}
595592
}
@@ -627,13 +624,12 @@ public void scheduleNextForecastForItem(Item item, Instant time, State state) {
627624
logger.trace("Scheduled forecasted value for {} at {}", item.getName(), time);
628625
}
629626

630-
public void scheduleNextPersistedForecastForItem(String itemName) {
631-
Item item = itemRegistry.get(itemName);
627+
public void scheduleNextPersistedForecastForItem(Item item) {
632628
if (item instanceof GenericItem) {
633629
String alias = getAlias(item);
634630
QueryablePersistenceService queryService = (QueryablePersistenceService) persistenceService;
635-
FilterCriteria filter = new FilterCriteria().setItemName(itemName).setBeginDate(ZonedDateTime.now())
636-
.setOrdering(ASCENDING);
631+
FilterCriteria filter = new FilterCriteria().setItemName(item.getName())
632+
.setBeginDate(ZonedDateTime.now()).setOrdering(ASCENDING);
637633
Iterator<HistoricItem> result = safeCaller.create(queryService, QueryablePersistenceService.class)
638634
.onTimeout(() -> logger.warn("Querying persistence service '{}' takes more than {}ms.",
639635
queryService.getId(), SafeCaller.DEFAULT_TIMEOUT))
@@ -707,7 +703,13 @@ public String getName() {
707703
}
708704
};
709705
restoreItemStateFromPersistenceUpdate(item, persistedItem);
710-
scheduleNextPersistedForecastForItem(item.getName());
706+
}
707+
708+
public void restoreItemStateFromPersistenceUpdate(Item item) {
709+
PersistedItem persistedItem = getPersistedItem(item);
710+
if (persistedItem != null) {
711+
restoreItemStateFromPersistenceUpdate(item, persistedItem);
712+
}
711713
}
712714

713715
private void restoreItemStateFromPersistenceUpdate(Item item, PersistedItem persistedItem) {

0 commit comments

Comments
 (0)