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 2015/06/09 13:56:03 UTC
incubator-tinkerpop git commit: Altered the method for rebinding
gremlin-driver Client.
Repository: incubator-tinkerpop
Updated Branches:
refs/heads/master 8f5b28ec3 -> d73fc21fa
Altered the method for rebinding gremlin-driver Client.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/d73fc21f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/d73fc21f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/d73fc21f
Branch: refs/heads/master
Commit: d73fc21fab8de1dfc47ebeca29f6fdb91e709c1d
Parents: 8f5b28e
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Tue Jun 9 07:55:27 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Tue Jun 9 07:55:27 2015 -0400
----------------------------------------------------------------------
.../apache/tinkerpop/gremlin/driver/Client.java | 101 +++++++++++++++++++
.../tinkerpop/gremlin/driver/Cluster.java | 4 +-
.../server/GremlinDriverIntegrateTest.java | 8 +-
3 files changed, 107 insertions(+), 6 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/d73fc21f/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/Client.java
----------------------------------------------------------------------
diff --git a/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/Client.java b/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/Client.java
index c8f562d..c2f8738 100644
--- a/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/Client.java
+++ b/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/Client.java
@@ -20,6 +20,8 @@ package org.apache.tinkerpop.gremlin.driver;
import org.apache.tinkerpop.gremlin.driver.exception.ConnectionException;
import org.apache.tinkerpop.gremlin.driver.message.RequestMessage;
+import org.apache.tinkerpop.gremlin.process.traversal.TraversalSource;
+import org.apache.tinkerpop.gremlin.structure.Graph;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -82,6 +84,14 @@ public abstract class Client {
public abstract CompletableFuture<Void> closeAsync();
/**
+ * Create a new {@code Client} that rebinds the specified {@link Graph} or {@link TraversalSource} name on the
+ * server to a variable called "g" for the context of the requests made through that {@code Client}.
+ *
+ * @param graphOrTraversalSource rebinds the specified global Gremlin Server variable to "g"
+ */
+ public abstract Client rebind(final String graphOrTraversalSource);
+
+ /**
* Initializes the client which typically means that a connection is established to the server. Depending on the
* implementation and configuration this blocking call may take some time. This method will be called
* automatically if it is not called directly and multiple calls will not have effect.
@@ -252,6 +262,23 @@ public abstract class Client {
}
/**
+ * {@inheritDoc}
+ */
+ @Override
+ public Client rebind(final String graphOrTraversalSource) {
+ return new ReboundClusteredClient(this, graphOrTraversalSource);
+ }
+
+ /**
+ * Creates a {@code Client} that supplies the specified set of rebindings, thus allowing the user to re-name
+ * one or more globally defined {@link Graph} or {@link TraversalSource} server bindings for the context of
+ * the created {@code Client}.
+ */
+ public Client rebind(final Map<String,String> rebindings) {
+ return new ReboundClusteredClient(this, rebindings);
+ }
+
+ /**
* Uses a {@link LoadBalancingStrategy} to choose the best {@link Host} and then selects the best connection
* from that host's connection pool.
*/
@@ -302,6 +329,75 @@ public abstract class Client {
}
/**
+ * Uses a {@link org.apache.tinkerpop.gremlin.driver.Client.ClusteredClient} that rebinds requests to a
+ * specified {@link Graph} or {@link TraversalSource} instances on the server-side.
+ */
+ public final static class ReboundClusteredClient extends Client {
+ private final ClusteredClient clusteredClient;
+ private final Map<String,String> rebindings = new HashMap<>();
+ final CompletableFuture<Void> close = new CompletableFuture<>();
+
+ ReboundClusteredClient(final ClusteredClient clusteredClient, final String graphOrTraversalSource) {
+ super(clusteredClient.cluster);
+ this.clusteredClient = clusteredClient;
+ rebindings.put("g", graphOrTraversalSource);
+ }
+
+ ReboundClusteredClient(final ClusteredClient clusteredClient, final Map<String,String> rebindings) {
+ super(clusteredClient.cluster);
+ this.clusteredClient = clusteredClient;
+ this.rebindings.putAll(rebindings);
+ }
+
+ @Override
+ public synchronized Client init() {
+ // no init required
+ if (close.isDone()) throw new IllegalStateException("Client is closed");
+ return this;
+ }
+
+ @Override
+ public RequestMessage buildMessage(final RequestMessage.Builder builder) {
+ if (close.isDone()) throw new IllegalStateException("Client is closed");
+ if (!rebindings.isEmpty())
+ builder.addArg(Tokens.ARGS_REBINDINGS, rebindings);
+
+ return builder.create();
+ }
+
+ @Override
+ protected void initializeImplementation() {
+ // no init required
+ if (close.isDone()) throw new IllegalStateException("Client is closed");
+ }
+
+ /**
+ * Delegates to the underlying {@link org.apache.tinkerpop.gremlin.driver.Client.ClusteredClient}.
+ */
+ @Override
+ protected Connection chooseConnection(final RequestMessage msg) throws TimeoutException, ConnectionException {
+ if (close.isDone()) throw new IllegalStateException("Client is closed");
+ return clusteredClient.chooseConnection(msg);
+ }
+
+ /**
+ * Prevents messages from being sent from this {@code Client}. Note that calling this method does not call
+ * close on the {@code Client} that created it.
+ */
+ @Override
+ public CompletableFuture<Void> closeAsync() {
+ close.complete(null);
+ return close;
+ }
+
+ @Override
+ public Client rebind(final String graphOrTraversalSource) {
+ if (close.isDone()) throw new IllegalStateException("Client is closed");
+ return new ReboundClusteredClient(clusteredClient, graphOrTraversalSource);
+ }
+ }
+
+ /**
* A {@code Client} implementation that operates in the context of a session. Requests are sent to a single
* server, where each request is bound to the same thread with the same set of bindings across requests.
* Transaction are not automatically committed. It is up the client to issue commit/rollback commands.
@@ -316,6 +412,11 @@ public abstract class Client {
this.sessionId = sessionId;
}
+ @Override
+ public Client rebind(final String graphOrTraversalSourceName){
+ throw new UnsupportedOperationException("Sessioned client do no support rebinding");
+ }
+
/**
* Adds the {@link Tokens#ARGS_SESSION} value to every {@link RequestMessage}.
*/
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/d73fc21f/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/Cluster.java
----------------------------------------------------------------------
diff --git a/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/Cluster.java b/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/Cluster.java
index d565bda..46bc33e 100644
--- a/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/Cluster.java
+++ b/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/Cluster.java
@@ -70,7 +70,7 @@ public final class Cluster {
* one or more servers (depending on the cluster configuration), where each request represents the entirety of a
* transaction. A commit or rollback (in case of error) is automatically executed at the end of the request.
*/
- public Client.ClusteredClient connect() {
+ public Client connect() {
return new Client.ClusteredClient(this);
}
@@ -82,7 +82,7 @@ public final class Cluster {
*
* @param sessionId user supplied id for the session which should be unique (a UUID is ideal).
*/
- public Client.SessionedClient connect(final String sessionId) {
+ public Client connect(final String sessionId) {
if (null == sessionId || sessionId.isEmpty())
throw new IllegalArgumentException("sessionId cannot be null or empty");
return new Client.SessionedClient(this, sessionId);
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/d73fc21f/gremlin-server/src/test/java/org/apache/tinkerpop/gremlin/server/GremlinDriverIntegrateTest.java
----------------------------------------------------------------------
diff --git a/gremlin-server/src/test/java/org/apache/tinkerpop/gremlin/server/GremlinDriverIntegrateTest.java b/gremlin-server/src/test/java/org/apache/tinkerpop/gremlin/server/GremlinDriverIntegrateTest.java
index a5797dc..b931a7a 100644
--- a/gremlin-server/src/test/java/org/apache/tinkerpop/gremlin/server/GremlinDriverIntegrateTest.java
+++ b/gremlin-server/src/test/java/org/apache/tinkerpop/gremlin/server/GremlinDriverIntegrateTest.java
@@ -465,7 +465,7 @@ public class GremlinDriverIntegrateTest extends AbstractGremlinServerIntegration
@Test
public void shouldRebindGraphVariables() throws Exception {
final Cluster cluster = Cluster.build().create();
- final Client.ClusteredClient client = cluster.connect();
+ final Client client = cluster.connect();
try {
client.submit("g.addVertex('name','stephen');").all().get().get(0).getVertex();
@@ -477,7 +477,7 @@ public class GremlinDriverIntegrateTest extends AbstractGremlinServerIntegration
assertEquals(ResponseStatusCode.SERVER_ERROR_SCRIPT_EVALUATION, re.getResponseStatusCode());
}
- final Vertex v = client.submit("g.addVertex('name','stephen')", "graph").all().get().get(0).getVertex();
+ final Vertex v = client.rebind("graph").submit("g.addVertex('name','stephen')").all().get().get(0).getVertex();
assertEquals("stephen", v.value("name"));
cluster.close();
@@ -486,7 +486,7 @@ public class GremlinDriverIntegrateTest extends AbstractGremlinServerIntegration
@Test
public void shouldRebindTraversalSourceVariables() throws Exception {
final Cluster cluster = Cluster.build().create();
- final Client.ClusteredClient client = cluster.connect();
+ final Client client = cluster.connect();
try {
client.submit("g.addV('name','stephen');").all().get().get(0).getVertex();
@@ -498,7 +498,7 @@ public class GremlinDriverIntegrateTest extends AbstractGremlinServerIntegration
assertEquals(ResponseStatusCode.SERVER_ERROR, re.getResponseStatusCode());
}
- final Vertex v = client.submit("g.addV('name','stephen')", "g1").all().get().get(0).getVertex();
+ final Vertex v = client.rebind("g1").submit("g.addV('name','stephen')").all().get().get(0).getVertex();
assertEquals("stephen", v.value("name"));
cluster.close();