From ad0aa149a7de3e3ce3034a59ad7761d03d697501 Mon Sep 17 00:00:00 2001 From: Tuukka Lehtonen Date: Mon, 8 Nov 2021 13:04:24 +0200 Subject: [PATCH] Add Collector.isEnabled and implement support in CollectorImpl Also add default constructor for SubscriptionItem that uses cached BINDING instead of retrieving it from Bindings for every instance. CollectorImpl methods invoked by datasource adapters are now short-circuited based on the enabledness of the collector. gitlab #771 --- .../src/org/simantics/history/Collector.java | 11 +++++++ .../simantics/history/impl/CollectorImpl.java | 31 +++++++++++++++++-- .../util/subscription/SubscriptionItem.java | 8 +++++ 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/bundles/org.simantics.history/src/org/simantics/history/Collector.java b/bundles/org.simantics.history/src/org/simantics/history/Collector.java index 234c832f5..9dbce9bc1 100644 --- a/bundles/org.simantics.history/src/org/simantics/history/Collector.java +++ b/bundles/org.simantics.history/src/org/simantics/history/Collector.java @@ -215,4 +215,15 @@ public interface Collector { */ void setState(Bean newState); + /** + * Allows history collector writers to optimize their invocations of the collector. + * + * @return true if the this collector is enabled for collection or + * false otherwise. + * @since 1.50.0, 1.35.3, 1.48.0.1 + */ + default boolean isEnabled() { + return true; + } + } diff --git a/bundles/org.simantics.history/src/org/simantics/history/impl/CollectorImpl.java b/bundles/org.simantics.history/src/org/simantics/history/impl/CollectorImpl.java index e956b3711..c202f733c 100644 --- a/bundles/org.simantics.history/src/org/simantics/history/impl/CollectorImpl.java +++ b/bundles/org.simantics.history/src/org/simantics/history/impl/CollectorImpl.java @@ -87,6 +87,15 @@ public class CollectorImpl implements Collector { /** True when all items are open */ boolean itemsOpen = true; + + /** True when history collection will write data to disk */ + boolean enabled = true; + + /** + * Transient info only set in {@link #beginStep(NumberBinding, Object)} based on + * {@link #enabled} + */ + private transient boolean stepEnabled = true; public FlushPolicy flushPolicy = FlushPolicy.FlushOnEveryStep; @@ -122,7 +131,16 @@ public class CollectorImpl implements Collector { this.history = history; this.state = (CollectorState) previousState.clone(); } - + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + @Override + public boolean isEnabled() { + return enabled; + } + @Override public synchronized void addItem(Bean item) throws HistoryException { try { @@ -494,13 +512,20 @@ public class CollectorImpl implements Collector { @Override public void beginStep(NumberBinding binding, Object time) throws HistoryException { - openAllItems(); + stepEnabled = enabled; updateDt(binding, time); state.time.setValue(binding, time); + if (enabled) { + openAllItems(); + } } @Override public void setValue(String id, Binding binding, Object value) throws HistoryException { + // Short-circuit calls when collection not enabled + if (!stepEnabled) + return; + VariableState vs = state.values.get( id ); if (vs == null) { vs = new VariableState(); @@ -554,6 +579,8 @@ public class CollectorImpl implements Collector { @Override public void endStep() throws HistoryException { + if (!stepEnabled) + return; // Write values to streams for (ActiveStream i : items) { diff --git a/bundles/org.simantics.history/src/org/simantics/history/util/subscription/SubscriptionItem.java b/bundles/org.simantics.history/src/org/simantics/history/util/subscription/SubscriptionItem.java index 2bb078c21..fb0613ab6 100644 --- a/bundles/org.simantics.history/src/org/simantics/history/util/subscription/SubscriptionItem.java +++ b/bundles/org.simantics.history/src/org/simantics/history/util/subscription/SubscriptionItem.java @@ -14,7 +14,9 @@ package org.simantics.history.util.subscription; import java.util.Collection; import java.util.Iterator; +import org.simantics.databoard.Bindings; import org.simantics.databoard.annotations.Identifier; +import org.simantics.databoard.binding.Binding; import org.simantics.databoard.type.Datatype; import org.simantics.databoard.util.Bean; @@ -31,6 +33,12 @@ import org.simantics.databoard.util.Bean; */ public class SubscriptionItem extends Bean { + private static final Binding BINDING = Bindings.getBindingUnchecked(SubscriptionItem.class); + + public SubscriptionItem() { + super(BINDING); + } + /** * Create HistoryItem descriptions for collecting one variable with multiple * sampling formats. -- 2.47.1