You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by sp...@apache.org on 2017/04/17 13:30:14 UTC

[3/6] tinkerpop git commit: GraphManager support opening of specific graphs

GraphManager support opening of specific graphs

This changeset allows an implementor to open a specific graph. One may
use this concept to implement a dynamic graph cache.

Furthermore, to ensure that rebindings work as intended, i.e. the list
of graphs returned to the HttpGremlinEndpointHandler, or "open graphs",
must include the to-be-rebound-graph. This changeset includes a change
to return these rebound graphs specifically.

Similar story as above for the WebSockets class, StandardOpProcessor.

Similar story for sessions, SessionOpProcessor.

Furthermore, the serializers at server boot only need to be aware of the
graphs defined in the settings object, so the relevant change here is in
AbstractChannelizer.

Furthermore:

To encourage a GraphManager implementation whose modification of the
Map<String, Graph> object aligns more closely with accepted "Getter and
Setter" design patterns, we update the adding of graphs to the
GraphManager Map by calling the new `addGraph(String, Graph)` rather
than publicly editting it with a call to `getGraphs().put(String,
Graph)`.

Also added `addTraversalSource(String, TraversalSource) for same
reasons.

Also, updated BasicGraphManager according to the new specifications.


Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/67471d13
Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/67471d13
Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/67471d13

Branch: refs/heads/tp32
Commit: 67471d13b5fa05d19d51f117a95530a81011f792
Parents: 1f2cfa0
Author: dpitera <dp...@us.ibm.com>
Authored: Mon Nov 21 13:01:47 2016 -0500
Committer: dpitera <dp...@us.ibm.com>
Committed: Mon Mar 27 13:38:16 2017 -0400

----------------------------------------------------------------------
 .../gremlin/server/AbstractChannelizer.java     | 10 ++++++--
 .../tinkerpop/gremlin/server/GraphManager.java  | 27 ++++++++++++++++++++
 .../handler/HttpGremlinEndpointHandler.java     | 12 ++++-----
 .../server/op/session/SessionOpProcessor.java   | 12 ++++-----
 .../server/op/standard/StandardOpProcessor.java | 12 ++++-----
 .../gremlin/server/util/BasicGraphManager.java  | 16 ++++++++++++
 .../server/util/ServerGremlinExecutor.java      |  4 +--
 7 files changed, 71 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/67471d13/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/AbstractChannelizer.java
----------------------------------------------------------------------
diff --git a/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/AbstractChannelizer.java b/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/AbstractChannelizer.java
index d28fd4f..2a5ca55 100644
--- a/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/AbstractChannelizer.java
+++ b/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/AbstractChannelizer.java
@@ -33,6 +33,7 @@ import org.apache.tinkerpop.gremlin.server.auth.Authenticator;
 import org.apache.tinkerpop.gremlin.server.handler.IteratorHandler;
 import org.apache.tinkerpop.gremlin.server.handler.OpExecutorHandler;
 import org.apache.tinkerpop.gremlin.server.handler.OpSelectorHandler;
+import org.apache.tinkerpop.gremlin.structure.Graph;
 import io.netty.channel.ChannelInitializer;
 import io.netty.channel.ChannelPipeline;
 import io.netty.channel.socket.SocketChannel;
@@ -53,6 +54,7 @@ import java.util.Optional;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.stream.Stream;
+import java.util.Iterator;
 
 /**
  * A base implementation for the {@code Channelizer} which does a basic configuration of the pipeline, one that
@@ -179,8 +181,12 @@ public abstract class AbstractChannelizer extends ChannelInitializer<SocketChann
                 }
 
                 final MessageSerializer serializer = (MessageSerializer) clazz.newInstance();
+                Map<String, Graph> graphsDefinedAtStartup = new HashMap<String, Graph>();
+                for (String graphName : settings.graphs.keySet()) {
+                    graphsDefinedAtStartup.put(graphName, graphManager.getGraph(graphName));
+                }
                 if (config.config != null)
-                    serializer.configure(config.config, graphManager.getGraphs());
+                    serializer.configure(config.config, graphsDefinedAtStartup);
 
                 return Optional.ofNullable(serializer);
             } catch (ClassNotFoundException cnfe) {
@@ -254,4 +260,4 @@ public abstract class AbstractChannelizer extends ChannelInitializer<SocketChann
             return null;
         }
     }
-}
\ No newline at end of file
+}

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/67471d13/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/GraphManager.java
----------------------------------------------------------------------
diff --git a/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/GraphManager.java b/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/GraphManager.java
index e74eccb..473127e 100644
--- a/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/GraphManager.java
+++ b/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/GraphManager.java
@@ -33,6 +33,19 @@ public interface GraphManager {
      * @return a {@link Map} where the key is the name of the {@link Graph} and the value is the {@link Graph} itself
      */
     public Map<String, Graph> getGraphs();
+    
+    /**
+     * Get {@link Graph} instance whose name matches {@link gName}
+     *
+     * @return {@link Graph} if exists, else null 
+     */
+    public Graph getGraph(String gName);
+
+    /**
+     * Add {@link Graph} g with name {@link String} gName to 
+     * {@link Map<String, Graph>} returned by call to getGraphs()
+     */
+    public void addGraph(String gName, Graph g);
 
     /**
      * Get a list of the {@link TraversalSource} instances and their binding names
@@ -43,8 +56,22 @@ public interface GraphManager {
     public Map<String, TraversalSource> getTraversalSources();
 
     /**
+     * Get {@link TraversalSource} instance whose name matches {@link tsName}
+     *
+     * @return {@link TraversalSource} if exists, else null
+     */
+    
+    public TraversalSource getTraversalSource(String tsName);
+    /**
      * Get the {@link Graph} and {@link TraversalSource} list as a set of bindings.
      */
+    
+    /**
+     * Add {@link TraversalSource} ts with name {@link String} tsName to 
+     * {@link Map<String, TraversalSource>} returned by call to getTraversalSources()
+     */
+    public void addTraversalSource(String tsName, TraversalSource ts);
+    
     public Bindings getAsBindings();
 
     /**

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/67471d13/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/handler/HttpGremlinEndpointHandler.java
----------------------------------------------------------------------
diff --git a/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/handler/HttpGremlinEndpointHandler.java b/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/handler/HttpGremlinEndpointHandler.java
index 899d488..a179b52 100644
--- a/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/handler/HttpGremlinEndpointHandler.java
+++ b/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/handler/HttpGremlinEndpointHandler.java
@@ -290,16 +290,16 @@ public class HttpGremlinEndpointHandler extends ChannelInboundHandlerAdapter {
         if (!rebindingMap.isEmpty()) {
             for (Map.Entry<String, String> kv : rebindingMap.entrySet()) {
                 boolean found = false;
-                final Map<String, Graph> graphs = this.graphManager.getGraphs();
-                if (graphs.containsKey(kv.getValue())) {
-                    bindings.put(kv.getKey(), graphs.get(kv.getValue()));
+                final Graph g = this.graphManager.getGraph(kv.getValue());
+                if (null != g) {
+                    bindings.put(kv.getKey(), g);
                     found = true;
                 }
 
                 if (!found) {
-                    final Map<String, TraversalSource> traversalSources = this.graphManager.getTraversalSources();
-                    if (traversalSources.containsKey(kv.getValue())) {
-                        bindings.put(kv.getKey(), traversalSources.get(kv.getValue()));
+                    final TraversalSource ts = this.graphManager.getTraversalSource(kv.getValue());
+                    if (null != ts) {
+                        bindings.put(kv.getKey(), ts);
                         found = true;
                     }
                 }

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/67471d13/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/op/session/SessionOpProcessor.java
----------------------------------------------------------------------
diff --git a/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/op/session/SessionOpProcessor.java b/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/op/session/SessionOpProcessor.java
index fe3cc85..905ecdc 100644
--- a/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/op/session/SessionOpProcessor.java
+++ b/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/op/session/SessionOpProcessor.java
@@ -231,18 +231,18 @@ public class SessionOpProcessor extends AbstractEvalOpProcessor {
                     boolean found = false;
 
                     // first check if the alias refers to a Graph instance
-                    final Map<String, Graph> graphs = context.getGraphManager().getGraphs();
-                    if (graphs.containsKey(aliasKv.getValue())) {
-                        bindings.put(aliasKv.getKey(), graphs.get(aliasKv.getValue()));
+                    final Graph graph = context.getGraphManager().getGraph(aliasKv.getValue());
+                    if (null != graph) {
+                        bindings.put(aliasKv.getKey(), graph);
                         found = true;
                     }
 
                     // if the alias wasn't found as a Graph then perhaps it is a TraversalSource - it needs to be
                     // something
                     if (!found) {
-                        final Map<String, TraversalSource> traversalSources = context.getGraphManager().getTraversalSources();
-                        if (traversalSources.containsKey(aliasKv.getValue())) {
-                            bindings.put(aliasKv.getKey(), traversalSources.get(aliasKv.getValue()));
+                        final TraversalSource ts = context.getGraphManager().getTraversalSource(aliasKv.getValue());
+                        if (null != ts) {
+                            bindings.put(aliasKv.getKey(), ts);
                             found = true;
                         }
                     }

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/67471d13/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/op/standard/StandardOpProcessor.java
----------------------------------------------------------------------
diff --git a/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/op/standard/StandardOpProcessor.java b/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/op/standard/StandardOpProcessor.java
index 893ae75..aa03ef0 100644
--- a/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/op/standard/StandardOpProcessor.java
+++ b/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/op/standard/StandardOpProcessor.java
@@ -117,18 +117,18 @@ public class StandardOpProcessor extends AbstractEvalOpProcessor {
                     boolean found = false;
 
                     // first check if the alias refers to a Graph instance
-                    final Map<String, Graph> graphs = context.getGraphManager().getGraphs();
-                    if (graphs.containsKey(aliasKv.getValue())) {
-                        bindings.put(aliasKv.getKey(), graphs.get(aliasKv.getValue()));
+                    final Graph graph = context.getGraphManager().getGraph(aliasKv.getValue());
+                    if (null != graph) {
+                        bindings.put(aliasKv.getKey(), graph);
                         found = true;
                     }
 
                     // if the alias wasn't found as a Graph then perhaps it is a TraversalSource - it needs to be
                     // something
                     if (!found) {
-                        final Map<String, TraversalSource> traversalSources = context.getGraphManager().getTraversalSources();
-                        if (traversalSources.containsKey(aliasKv.getValue())) {
-                            bindings.put(aliasKv.getKey(), traversalSources.get(aliasKv.getValue()));
+                        final TraversalSource ts = context.getGraphManager().getTraversalSource(aliasKv.getValue());
+                        if (null != ts) {
+                            bindings.put(aliasKv.getKey(), ts);
                             found = true;
                         }
                     }

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/67471d13/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/util/BasicGraphManager.java
----------------------------------------------------------------------
diff --git a/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/util/BasicGraphManager.java b/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/util/BasicGraphManager.java
index 9f514b8..af5432f 100644
--- a/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/util/BasicGraphManager.java
+++ b/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/util/BasicGraphManager.java
@@ -76,6 +76,14 @@ public final class BasicGraphManager implements GraphManager {
         return graphs;
     }
 
+    public Graph getGraph(String gName) {
+        return graphs.get(gName);
+    }
+
+    public void addGraph(String gName, Graph g) {
+        graphs.put(gName, g);
+    }
+
     /**
      * Get a list of the {@link TraversalSource} instances and their binding names as defined by Gremlin Server
      * initialization scripts.
@@ -87,6 +95,14 @@ public final class BasicGraphManager implements GraphManager {
         return traversalSources;
     }
 
+    public TraversalSource getTraversalSource(String tsName) {
+        return traversalSources.get(tsName);
+    }
+
+    public void addTraversalSource(String tsName, TraversalSource ts) {
+        traversalSources.put(tsName, ts);
+    }
+
     /**
      * Get the {@link Graph} and {@link TraversalSource} list as a set of bindings.
      */

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/67471d13/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/util/ServerGremlinExecutor.java
----------------------------------------------------------------------
diff --git a/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/util/ServerGremlinExecutor.java b/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/util/ServerGremlinExecutor.java
index ea9e537..9fa28d7 100644
--- a/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/util/ServerGremlinExecutor.java
+++ b/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/util/ServerGremlinExecutor.java
@@ -189,14 +189,14 @@ public class ServerGremlinExecutor<T extends ScheduledExecutorService> {
         // re-apply those references back
         gremlinExecutor.getGlobalBindings().entrySet().stream()
                 .filter(kv -> kv.getValue() instanceof Graph)
-                .forEach(kv -> this.graphManager.getGraphs().put(kv.getKey(), (Graph) kv.getValue()));
+                .forEach(kv -> this.graphManager.addGraph(kv.getKey(), (Graph) kv.getValue()));
 
         // script engine init may have constructed the TraversalSource bindings - store them in Graphs object
         gremlinExecutor.getGlobalBindings().entrySet().stream()
                 .filter(kv -> kv.getValue() instanceof TraversalSource)
                 .forEach(kv -> {
                     logger.info("A {} is now bound to [{}] with {}", kv.getValue().getClass().getSimpleName(), kv.getKey(), kv.getValue());
-                    this.graphManager.getTraversalSources().put(kv.getKey(), (TraversalSource) kv.getValue());
+                    this.graphManager.addTraversalSource(kv.getKey(), (TraversalSource) kv.getValue());
                 });
 
         // determine if the initialization scripts introduced LifeCycleHook objects - if so we need to gather them