Skip to content

Commit

Permalink
Add a cache for NodeLookup
Browse files Browse the repository at this point in the history
This reintroduces a previously introduced cache with an improvement
to add every node seen during lookup instead only the nodes found.
  • Loading branch information
Christian Buchegger committed Sep 27, 2018
1 parent 12ab916 commit 3226172
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ public VNode remove(VNode n) {
// ((FlowModel)n).clear();
// }
VNode result = nodes.remove(n.getId());
nodeLookup.invalidateCache();
observableNodes.remove(n);

// removeNodeSkin(n);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,9 @@ public interface NodeLookup extends Lookup<VNode> {
* such connector exists
*/
public Connector getConnectorById(String id);

/**
* Invalidates the Lookup cache;
*/
default void invalidateCache() {};
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
*/
package eu.mihosoft.vrl.workflow;

import java.util.HashMap;
import java.util.Objects;

/**
Expand All @@ -46,7 +47,7 @@
public class NodeLookupImpl implements NodeLookup {

private final VFlowModel root;
// private final Map<String, VNode> cache = new HashMap<>();
private HashMap<String, VNode> cache = new HashMap<String, VNode>();

public NodeLookupImpl(VFlowModel root) {
this.root = root;
Expand Down Expand Up @@ -75,18 +76,21 @@ public Connector getConnectorById(String globalId) {

@Override
public VNode getById(String globalId) {
VNode result = cache.get(globalId);

// VNode result = cache.get(globalId);

// if (result!=null)return result;

VNode result = getNodeByGlobalId(root, globalId);

// cache.put(globalId, result);

if (result == null) {
result = getNodeByGlobalId(root, globalId);
cache.put(globalId, result);
}
return result;
}

@Override
public void invalidateCache() {
System.out.println("Cache invalidated");
cache.clear();
}

private VNode getNodeByGlobalId(VFlowModel parent, String id) {

if (Objects.equals(parent.getId(),id)) {
Expand All @@ -97,7 +101,7 @@ private VNode getNodeByGlobalId(VFlowModel parent, String id) {
if (n.getId().equals(id)) {
return n;
}

cache.put(n.getId(), n);
if (n instanceof FlowModel) {
VNode result = getNodeByGlobalId((VFlowModel) n, id);
if (result != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ public ObservableList<VNode> getNodes() {

@Override
public VNode remove(VNode n) {
getNodeLookup().invalidateCache();
return getModel().remove(n);
}

Expand Down

0 comments on commit 3226172

Please sign in to comment.