]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Add Collector.isEnabled and implement support in CollectorImpl
authorTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Mon, 8 Nov 2021 11:04:24 +0000 (13:04 +0200)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Mon, 8 Nov 2021 11:04:56 +0000 (13:04 +0200)
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

bundles/org.simantics.history/src/org/simantics/history/Collector.java
bundles/org.simantics.history/src/org/simantics/history/impl/CollectorImpl.java
bundles/org.simantics.history/src/org/simantics/history/util/subscription/SubscriptionItem.java

index 234c832f52dc640e8097067b9876830d71dcaa91..9dbce9bc1671aa949a87f981aa1e2c00a5d5ff3f 100644 (file)
@@ -215,4 +215,15 @@ public interface Collector {
         */
        void setState(Bean newState);
 
+       /**
+        * Allows history collector writers to optimize their invocations of the collector.
+        * 
+        * @return <code>true</code> if the this collector is enabled for collection or
+        *         <code>false</code> otherwise.
+        * @since 1.50.0, 1.35.3, 1.48.0.1
+        */
+       default boolean isEnabled() {
+               return true;
+       }
+
 }
index e956b37117fcc365cbb85cf0e2b339752d814bcc..c202f733ca6af88c284351de3cfadea3eee9899d 100644 (file)
@@ -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)
                {
index 2bb078c2120c3e3e6954674fe29702206d6e5734..fb0613ab63f25e558f754db25d8ab31767bae6e4 100644 (file)
@@ -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.