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/09/30 17:26:53 UTC
[2/7] incubator-tinkerpop git commit: Get basic persistence into
TinkerGraph via configuration.
Get basic persistence into TinkerGraph via configuration.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/5aa2886a
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/5aa2886a
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/5aa2886a
Branch: refs/heads/master
Commit: 5aa2886a957bddde377560dbe35be0d6d441922a
Parents: 6686935
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Wed Sep 30 09:31:04 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Wed Sep 30 09:31:04 2015 -0400
----------------------------------------------------------------------
.../tinkergraph/structure/TinkerGraph.java | 76 ++++++++++++++++----
.../tinkergraph/TinkerGraphProvider.java | 22 +++++-
2 files changed, 82 insertions(+), 16 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/5aa2886a/tinkergraph-gremlin/src/main/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/TinkerGraph.java
----------------------------------------------------------------------
diff --git a/tinkergraph-gremlin/src/main/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/TinkerGraph.java b/tinkergraph-gremlin/src/main/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/TinkerGraph.java
index 54be94d..af64e3a 100644
--- a/tinkergraph-gremlin/src/main/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/TinkerGraph.java
+++ b/tinkergraph-gremlin/src/main/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/TinkerGraph.java
@@ -28,6 +28,8 @@ import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.structure.Transaction;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.structure.VertexProperty;
+import org.apache.tinkerpop.gremlin.structure.io.IoCore;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.GryoMapper;
import org.apache.tinkerpop.gremlin.structure.util.ElementHelper;
import org.apache.tinkerpop.gremlin.structure.util.GraphFactory;
import org.apache.tinkerpop.gremlin.structure.util.StringFactory;
@@ -36,6 +38,8 @@ import org.apache.tinkerpop.gremlin.tinkergraph.process.computer.TinkerGraphComp
import org.apache.tinkerpop.gremlin.tinkergraph.process.traversal.strategy.optimization.TinkerGraphStepStrategy;
import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils;
+import java.io.File;
+import java.io.IOException;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
@@ -77,6 +81,8 @@ public final class TinkerGraph implements Graph {
public static final String CONFIG_EDGE_ID = "gremlin.tinkergraph.edgeIdManager";
public static final String CONFIG_VERTEX_PROPERTY_ID = "gremlin.tinkergraph.vertexPropertyIdManager";
public static final String CONFIG_DEFAULT_VERTEX_PROPERTY_CARDINALITY = "gremlin.tinkergraph.defaultVertexPropertyCardinality";
+ public static final String CONFIG_GRAPH_LOCATION = "gremlin.tinkergraph.graphLocation";
+ public static final String CONFIG_GRAPH_FORMAT = "gremlin.tinkergraph.graphFormat";
private final TinkerGraphFeatures features = new TinkerGraphFeatures();
@@ -95,17 +101,28 @@ public final class TinkerGraph implements Graph {
protected final VertexProperty.Cardinality defaultVertexPropertyCardinality;
private final Configuration configuration;
+ private final String graphLocation;
+ private final String graphFormat;
/**
* An empty private constructor that initializes {@link TinkerGraph}.
*/
private TinkerGraph(final Configuration configuration) {
this.configuration = configuration;
- this.vertexIdManager = selectIdManager(configuration, CONFIG_VERTEX_ID, Vertex.class);
- this.edgeIdManager = selectIdManager(configuration, CONFIG_EDGE_ID, Edge.class);
- this.vertexPropertyIdManager = selectIdManager(configuration, CONFIG_VERTEX_PROPERTY_ID, VertexProperty.class);
- this.defaultVertexPropertyCardinality = VertexProperty.Cardinality.valueOf(
+ vertexIdManager = selectIdManager(configuration, CONFIG_VERTEX_ID, Vertex.class);
+ edgeIdManager = selectIdManager(configuration, CONFIG_EDGE_ID, Edge.class);
+ vertexPropertyIdManager = selectIdManager(configuration, CONFIG_VERTEX_PROPERTY_ID, VertexProperty.class);
+ defaultVertexPropertyCardinality = VertexProperty.Cardinality.valueOf(
configuration.getString(CONFIG_DEFAULT_VERTEX_PROPERTY_CARDINALITY, VertexProperty.Cardinality.single.name()));
+
+ graphLocation = configuration.getString(CONFIG_GRAPH_LOCATION, null);
+ graphFormat = configuration.getString(CONFIG_GRAPH_FORMAT, null);
+
+ if ((graphLocation != null && null == graphFormat) || (null == graphLocation && graphFormat != null))
+ throw new IllegalStateException(String.format("The %s and %s must both be specified if either is present",
+ CONFIG_GRAPH_LOCATION, CONFIG_GRAPH_FORMAT));
+
+ if (graphLocation != null) loadGraph();
}
/**
@@ -195,6 +212,7 @@ public final class TinkerGraph implements Graph {
@Override
public void close() {
+ if (graphLocation != null) saveGraph();
}
@Override
@@ -217,6 +235,40 @@ public final class TinkerGraph implements Graph {
return createElementIterator(Edge.class, edges, edgeIdManager, edgeIds);
}
+ private void loadGraph() {
+ final File f = new File(graphLocation);
+ if (f.exists() && f.isFile()) {
+ try {
+ if (graphFormat.equals("graphml")) {
+ io(IoCore.graphml()).readGraph(graphLocation);
+ } else if (graphFormat.equals("graphson")) {
+ io(IoCore.graphson()).readGraph(graphLocation);
+ } else if (graphFormat.equals("gryo")) {
+ io(IoCore.gryo()).readGraph(graphLocation);
+ }
+ } catch (IOException ioe) {
+ throw new RuntimeException(String.format("Could not load graph at %s with %s", graphLocation, graphFormat));
+ }
+ }
+ }
+
+ private void saveGraph() {
+ final File f = new File(graphLocation);
+ if (f.exists()) f.delete();
+
+ try {
+ if (graphFormat.equals("graphml")) {
+ io(IoCore.graphml()).writeGraph(graphLocation);
+ } else if (graphFormat.equals("graphson")) {
+ io(IoCore.graphson()).writeGraph(graphLocation);
+ } else if (graphFormat.equals("gryo")) {
+ io(IoCore.gryo()).writeGraph(graphLocation);
+ }
+ } catch (IOException ioe) {
+ throw new RuntimeException(String.format("Could not save graph at %s with %s", graphLocation, graphFormat));
+ }
+ }
+
private <T extends Element> Iterator<T> createElementIterator(final Class<T> clazz, final Map<Object, T> elements,
final IdManager idManager,
final Object... ids) {
@@ -267,9 +319,9 @@ public final class TinkerGraph implements Graph {
public class TinkerGraphFeatures implements Features {
private final TinkerGraphGraphFeatures graphFeatures = new TinkerGraphGraphFeatures();
+
private final TinkerGraphEdgeFeatures edgeFeatures = new TinkerGraphEdgeFeatures();
private final TinkerGraphVertexFeatures vertexFeatures = new TinkerGraphVertexFeatures();
-
private TinkerGraphFeatures() {
}
@@ -292,11 +344,11 @@ public final class TinkerGraph implements Graph {
public String toString() {
return StringFactory.featureString(this);
}
- }
+ }
public class TinkerGraphVertexFeatures implements Features.VertexFeatures {
- private final TinkerGraphVertexPropertyFeatures vertexPropertyFeatures = new TinkerGraphVertexPropertyFeatures();
+ private final TinkerGraphVertexPropertyFeatures vertexPropertyFeatures = new TinkerGraphVertexPropertyFeatures();
private TinkerGraphVertexFeatures() {
}
@@ -317,11 +369,10 @@ public final class TinkerGraph implements Graph {
@Override
public VertexProperty.Cardinality getCardinality(final String key) {
- //return VertexProperty.Cardinality.single;
return defaultVertexPropertyCardinality;
}
- }
+ }
public class TinkerGraphEdgeFeatures implements Features.EdgeFeatures {
private TinkerGraphEdgeFeatures() {
@@ -336,8 +387,8 @@ public final class TinkerGraph implements Graph {
public boolean willAllowId(final Object id) {
return edgeIdManager.allow(id);
}
- }
+ }
public class TinkerGraphGraphFeatures implements Features.GraphFeatures {
private TinkerGraphGraphFeatures() {
@@ -354,11 +405,6 @@ public final class TinkerGraph implements Graph {
}
@Override
- public boolean supportsPersistence() {
- return false;
- }
-
- @Override
public boolean supportsThreadedTransactions() {
return false;
}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/5aa2886a/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/TinkerGraphProvider.java
----------------------------------------------------------------------
diff --git a/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/TinkerGraphProvider.java b/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/TinkerGraphProvider.java
index cf85f2d..868597c 100644
--- a/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/TinkerGraphProvider.java
+++ b/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/TinkerGraphProvider.java
@@ -21,11 +21,11 @@ package org.apache.tinkerpop.gremlin.tinkergraph;
import org.apache.commons.configuration.Configuration;
import org.apache.tinkerpop.gremlin.AbstractGraphProvider;
import org.apache.tinkerpop.gremlin.LoadGraphWith;
+import org.apache.tinkerpop.gremlin.TestHelper;
import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.structure.GraphTest;
import org.apache.tinkerpop.gremlin.structure.VertexProperty;
import org.apache.tinkerpop.gremlin.structure.io.IoEdgeTest;
-import org.apache.tinkerpop.gremlin.structure.io.IoTest;
import org.apache.tinkerpop.gremlin.structure.io.IoVertexTest;
import org.apache.tinkerpop.gremlin.structure.util.detached.DetachedGraphTest;
import org.apache.tinkerpop.gremlin.structure.util.star.StarGraphTest;
@@ -37,6 +37,7 @@ import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerProperty;
import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerVertex;
import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerVertexProperty;
+import java.io.File;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
@@ -69,6 +70,11 @@ public class TinkerGraphProvider extends AbstractGraphProvider {
put(TinkerGraph.CONFIG_VERTEX_PROPERTY_ID, idMaker);
if (requiresListCardinalityAsDefault(loadGraphWith, test, testMethodName))
put(TinkerGraph.CONFIG_DEFAULT_VERTEX_PROPERTY_CARDINALITY, VertexProperty.Cardinality.list.name());
+ if (requiresPersistence(test, testMethodName)) {
+ put(TinkerGraph.CONFIG_GRAPH_FORMAT, "gryo");
+ put(TinkerGraph.CONFIG_GRAPH_LOCATION,
+ TestHelper.makeTestDataPath(test, "temp").getAbsolutePath() + File.separator + testMethodName + ".kryo");
+ }
}};
}
@@ -76,6 +82,13 @@ public class TinkerGraphProvider extends AbstractGraphProvider {
public void clear(final Graph graph, final Configuration configuration) throws Exception {
if (graph != null)
graph.close();
+
+ // in the even the graph is persisted we need to clean up
+ final String graphLocation = configuration.getString(TinkerGraph.CONFIG_GRAPH_LOCATION, null);
+ if (graphLocation != null) {
+ final File f = new File(graphLocation);
+ f.delete();
+ }
}
@Override
@@ -84,6 +97,13 @@ public class TinkerGraphProvider extends AbstractGraphProvider {
}
/**
+ * Determines if a test requires TinkerGraph persistence to be configured with graph location and format.
+ */
+ private static boolean requiresPersistence(final Class<?> test, final String testMethodName) {
+ return test == GraphTest.class && testMethodName.equals("shouldPersistDataOnClose");
+ }
+
+ /**
* Determines if a test requires a different cardinality as the default or not.
*/
private static boolean requiresListCardinalityAsDefault(final LoadGraphWith.GraphData loadGraphWith,