Update structure and value cache when refreshing variable 04/2404/4
authorJussi Koskela <jussi.koskela@semantum.fi>
Wed, 31 Oct 2018 14:03:38 +0000 (16:03 +0200)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Tue, 20 Nov 2018 09:10:39 +0000 (09:10 +0000)
Previous implementation only updated value cache contents after the
previous cached results expired, which could cause unacceptable delays
in new NodeManager query results. Also, if there were no listeners for
the NodeManager query results, results wouldn't get updated anymore.

gitlab #173

Change-Id: I162fbddea7a5b312c7dceb1448d6ef1cc1f07b73

bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/NodeCache.java
bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/NodeSupport.java
bundles/org.simantics.simulator.toolkit.db/src/org/simantics/simulator/toolkit/db/StandardVariableNodeManager.java

index 309efe31e21efa9f3ffb02ef9db223c924d64e83..5ed9678e9ec41aa4d75f6375b09362a1844f83a1 100644 (file)
@@ -116,6 +116,24 @@ public class NodeCache<Node,Value> {
                scheduleExpiration(node, System.nanoTime() + defaultExpirationTimeInNs);
        }
 
+       public synchronized void remove(Node node) {
+               if (disposed)
+                       return;
+               Long expTime = exp.get(node);
+               if (expTime != null) {
+                       expirationTimes.remove(expTime);
+               }
+               map.remove(node);
+       }
+
+       public synchronized void clear() {
+               if (disposed)
+                       return;
+               expirationTimes.clear();
+               exp.clear();
+               map.clear();
+       }
+
        public synchronized void dispose() {
                disposed = true;
                expirationTimes.clear();
index 79bc4eb5ee2926e52c6cce934c80382651ee2357..3ac88ae6769eb24bea768ac66077a502864dced2 100644 (file)
@@ -5,6 +5,8 @@ import java.util.concurrent.TimeUnit;
 import org.simantics.databoard.binding.mutable.Variant;
 import org.simantics.db.layer0.variable.Variables.NodeStructure;
 import org.simantics.simulator.variable.NodeManager;
+import org.simantics.simulator.variable.exceptions.NoValueException;
+import org.simantics.simulator.variable.exceptions.NodeManagerException;
 
 /**
  * @author Antti Villberg
@@ -55,6 +57,18 @@ public class NodeSupport<Node> {
                                && manager.equals(other.manager);
        }
 
+       public void refreshCache(Node node) throws NodeManagerException {
+               VariableNode<Node> variableNode = new VariableNode<>(this, node);
+               NodeStructure ns = NodeStructureRequest.get(variableNode);
+               structureCache.put(node, ns);
+               try {
+                       Variant value = manager.getValue(node);
+                       valueCache.put(node, value);
+               } catch (NoValueException e) {
+                       valueCache.remove(node);
+               };
+       }
+
        public void dispose() {
                valueCache.dispose();
                structureCache.dispose();
index a08d733d1374f78c0831e4071524a4b212d005a5..aefa672d46e33ecd3aa675c7f626a960247feedc 100644 (file)
@@ -16,6 +16,9 @@ import org.simantics.db.layer0.variable.NodeSupport;
 import org.simantics.simulator.toolkit.StandardNodeManager;
 import org.simantics.simulator.toolkit.StandardNodeManagerSupport;
 import org.simantics.simulator.toolkit.StandardRealm;
+import org.simantics.simulator.variable.exceptions.NodeManagerException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Adds support for Layer0 Variable interface in StandardNodeManager
@@ -24,6 +27,7 @@ import org.simantics.simulator.toolkit.StandardRealm;
  */
 public class StandardVariableNodeManager<Node, Engine extends StandardNodeManagerSupport<Node>> extends StandardNodeManager<Node,Engine> {
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(StandardVariableNodeManager.class); 
     protected NodeSupport<Node> support;
 
     public StandardVariableNodeManager(StandardRealm<Node, Engine> realm, Node root) {
@@ -33,9 +37,13 @@ public class StandardVariableNodeManager<Node, Engine extends StandardNodeManage
 
     @Override
     public void refreshVariable(Node node) {
+        try {
+            support.refreshCache(node);
+        } catch (NodeManagerException e) {
+            LOGGER.error("Failed to refresh variable", e);
+            throw new RuntimeException(e);
+        }
         super.refreshVariable(node);
-        support.valueCache.clearExpired();
-        support.structureCache.clearExpired();
     }
 
 }
\ No newline at end of file