Repository: tinkerpop Updated Branches: refs/heads/tp33 cfac56909 -> ad256743b
Minor refacoring of cache access in TraversalStrategies CTR Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/1d9e6dc6 Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/1d9e6dc6 Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/1d9e6dc6 Branch: refs/heads/tp33 Commit: 1d9e6dc6d30c5c7d56e4007527365793eb1f223e Parents: 0d6f8fc Author: Stephen Mallette <sp...@genoprime.com> Authored: Wed Apr 25 09:18:08 2018 -0400 Committer: Stephen Mallette <sp...@genoprime.com> Committed: Wed Apr 25 09:18:08 2018 -0400 ---------------------------------------------------------------------- .../process/traversal/TraversalStrategies.java | 39 ++++++++++---------- 1 file changed, 20 insertions(+), 19 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/1d9e6dc6/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/TraversalStrategies.java ---------------------------------------------------------------------- diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/TraversalStrategies.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/TraversalStrategies.java index e84737c..37cd1a6 100644 --- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/TraversalStrategies.java +++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/TraversalStrategies.java @@ -203,7 +203,7 @@ public interface TraversalStrategies extends Serializable, Cloneable { * Keeps track of {@link GraphComputer} and/or {@link Graph} classes that have been initialized to the * classloader so that they do not have to be reflected again. */ - private static Set<Class> LOADED = ConcurrentHashMap.newKeySet(); + private static Map<Class, Boolean> LOADED = new ConcurrentHashMap<>(); private static final Map<Class<? extends Graph>, TraversalStrategies> GRAPH_CACHE = new HashMap<>(); private static final Map<Class<? extends GraphComputer>, TraversalStrategies> GRAPH_COMPUTER_CACHE = new HashMap<>(); @@ -249,26 +249,27 @@ public interface TraversalStrategies extends Serializable, Cloneable { } public static TraversalStrategies getStrategies(final Class graphOrGraphComputerClass) { - try { - // be sure to load the class so that its static{} traversal strategy registration component is loaded. - // this is more important for GraphComputer classes as they are typically not instantiated prior to - // strategy usage like Graph classes. - if (!LOADED.contains(graphOrGraphComputerClass)) { - final String graphComputerClassName = null != graphOrGraphComputerClass.getDeclaringClass() ? - graphOrGraphComputerClass.getCanonicalName().replace("." + graphOrGraphComputerClass.getSimpleName(), "$" + graphOrGraphComputerClass.getSimpleName()) : - graphOrGraphComputerClass.getCanonicalName(); + // be sure to load the class so that its static{} traversal strategy registration component is loaded. + // this is more important for GraphComputer classes as they are typically not instantiated prior to + // strategy usage like Graph classes. + LOADED.computeIfAbsent(graphOrGraphComputerClass, unused -> { + final String graphComputerClassName = null != graphOrGraphComputerClass.getDeclaringClass() ? + graphOrGraphComputerClass.getCanonicalName().replace("." + graphOrGraphComputerClass.getSimpleName(), "$" + graphOrGraphComputerClass.getSimpleName()) : + graphOrGraphComputerClass.getCanonicalName(); + + try { Class.forName(graphComputerClassName); - - // keep track of stuff we already loaded once - stuff in this if/statement isn't cheap and this - // method gets called a lot, basically every time a new traversal gets spun up (that includes - // child traversals. perhaps it is possible to just check the cache keys for this information, but - // it's not clear if this method will be called with something not in the cache and if it is and - // it results in error, then we'd probably not want to deal with this block again anyway - LOADED.add(graphOrGraphComputerClass); + } catch (ClassNotFoundException e) { + throw new IllegalStateException(e.getMessage(), e); } - } catch (final ClassNotFoundException e) { - throw new IllegalStateException(e.getMessage(), e); - } + + // keep track of stuff we already loaded once - stuff in this if/statement isn't cheap and this + // method gets called a lot, basically every time a new traversal gets spun up (that includes + // child traversals. perhaps it is possible to just check the cache keys for this information, but + // it's not clear if this method will be called with something not in the cache and if it is and + // it results in error, then we'd probably not want to deal with this block again anyway + return true; + }); if (GRAPH_CACHE.containsKey(graphOrGraphComputerClass)) { return GRAPH_CACHE.get(graphOrGraphComputerClass);