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/04/24 17:46:16 UTC

[30/31] incubator-tinkerpop git commit: Attachable.Method.CREATE test for StarGraph complete. Made it so StarGraph does NOT require user provided ids or labels.

Attachable.Method.CREATE test for StarGraph complete. Made it so StarGraph does NOT require user provided ids or labels.


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

Branch: refs/heads/master
Commit: 5fcbedf6d9dcb1622ee1f67feb10e7ea47500e0a
Parents: 256f47d
Author: Marko A. Rodriguez <ok...@gmail.com>
Authored: Fri Apr 24 09:34:12 2015 -0600
Committer: Marko A. Rodriguez <ok...@gmail.com>
Committed: Fri Apr 24 09:34:47 2015 -0600

----------------------------------------------------------------------
 .../gremlin/structure/util/star/StarGraph.java  | 39 +++++++++++++-------
 .../apache/tinkerpop/gremlin/TestHelper.java    |  2 +
 .../structure/util/star/StarGraphTest.java      | 30 ++++++++++++++-
 3 files changed, 56 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/5fcbedf6/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraph.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraph.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraph.java
index a4bc203..98e745c 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraph.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraph.java
@@ -74,14 +74,19 @@ public final class StarGraph implements Graph, Serializable {
         return this.starVertex;
     }
 
+    private Long nextId() {
+        return this.nextId++;
+    }
+
     @Override
     public Vertex addVertex(final Object... keyValues) {
         if (null == this.starVertex) {
-            this.starVertex = new StarVertex(ElementHelper.getIdValue(keyValues).get(), ElementHelper.getLabelValue(keyValues).get());
+            ElementHelper.legalPropertyKeyValueArray(keyValues);
+            this.starVertex = new StarVertex(ElementHelper.getIdValue(keyValues).orElse(this.nextId()), ElementHelper.getLabelValue(keyValues).orElse(Vertex.DEFAULT_LABEL));
             ElementHelper.attachProperties(this.starVertex, VertexProperty.Cardinality.list, keyValues); // TODO: is this smart? I say no... cause vertex property ids are not preserved.
             return this.starVertex;
         } else
-            return new StarAdjacentVertex(ElementHelper.getIdValue(keyValues).get());
+            return new StarAdjacentVertex(ElementHelper.getIdValue(keyValues).orElse(this.nextId()));
     }
 
     @Override
@@ -254,8 +259,8 @@ public final class StarGraph implements Graph, Serializable {
         }
 
         public void dropEdges() {
-            if (null != this.outEdges) this.outEdges.clear();
-            if (null != this.inEdges) this.inEdges.clear();
+            this.outEdges = null;
+            this.inEdges = null;
         }
 
         public void dropVertexProperties(final String... propertyKeys) {
@@ -273,10 +278,14 @@ public final class StarGraph implements Graph, Serializable {
 
         @Override
         public <V> VertexProperty<V> property(final String key, final V value, final Object... keyValues) {
+            ElementHelper.validateProperty(key, value);
+            ElementHelper.legalPropertyKeyValueArray(keyValues);
             return this.property(VertexProperty.Cardinality.single, key, value, keyValues);
         }
 
         protected Edge addOutEdge(final String label, final Vertex inVertex, final Object... keyValues) {
+            ElementHelper.validateLabel(label);
+            ElementHelper.legalPropertyKeyValueArray(keyValues);
             if (null == this.outEdges)
                 this.outEdges = new HashMap<>();
             List<Edge> outE = this.outEdges.get(label);
@@ -284,13 +293,15 @@ public final class StarGraph implements Graph, Serializable {
                 outE = new ArrayList<>();
                 this.outEdges.put(label, outE);
             }
-            final StarEdge outEdge = new StarOutEdge(ElementHelper.getIdValue(keyValues).orElse(nextId++), label, inVertex.id());
+            final StarEdge outEdge = new StarOutEdge(ElementHelper.getIdValue(keyValues).orElse(nextId()), label, inVertex.id());
             ElementHelper.attachProperties(outEdge, keyValues);
             outE.add(outEdge);
             return outEdge;
         }
 
         protected Edge addInEdge(final String label, final Vertex outVertex, final Object... keyValues) {
+            ElementHelper.validateLabel(label);
+            ElementHelper.legalPropertyKeyValueArray(keyValues);
             if (null == this.inEdges)
                 this.inEdges = new HashMap<>();
             List<Edge> inE = this.inEdges.get(label);
@@ -298,7 +309,7 @@ public final class StarGraph implements Graph, Serializable {
                 inE = new ArrayList<>();
                 this.inEdges.put(label, inE);
             }
-            final StarEdge inEdge = new StarInEdge(ElementHelper.getIdValue(keyValues).orElse(nextId++), label, outVertex.id());
+            final StarEdge inEdge = new StarInEdge(ElementHelper.getIdValue(keyValues).orElse(nextId()), label, outVertex.id());
             ElementHelper.attachProperties(inEdge, keyValues);
             inE.add(inEdge);
             return inEdge;
@@ -306,10 +317,11 @@ public final class StarGraph implements Graph, Serializable {
 
         @Override
         public <V> VertexProperty<V> property(final VertexProperty.Cardinality cardinality, final String key, V value, final Object... keyValues) {
+            ElementHelper.legalPropertyKeyValueArray(keyValues);
             if (null == this.vertexProperties)
                 this.vertexProperties = new HashMap<>();
             final List<VertexProperty> list = cardinality.equals(VertexProperty.Cardinality.single) ? new ArrayList<>(1) : this.vertexProperties.getOrDefault(key, new ArrayList<>());
-            final VertexProperty<V> vertexProperty = new StarVertexProperty<>(ElementHelper.getIdValue(keyValues).orElse(nextId++), key, value);
+            final VertexProperty<V> vertexProperty = new StarVertexProperty<>(ElementHelper.getIdValue(keyValues).orElse(nextId()), key, value);
             ElementHelper.attachProperties(vertexProperty, keyValues);
             list.add(vertexProperty);
             this.vertexProperties.put(key, list);
@@ -384,7 +396,7 @@ public final class StarGraph implements Graph, Serializable {
 
         private final V value;
 
-        public StarVertexProperty(final Object id, final String key, final V value) {
+        private StarVertexProperty(final Object id, final String key, final V value) {
             super(id, key);
             this.value = value;
         }
@@ -465,7 +477,7 @@ public final class StarGraph implements Graph, Serializable {
 
         private final Object id;
 
-        protected StarAdjacentVertex(final Object id) {
+        private StarAdjacentVertex(final Object id) {
             this.id = id;
         }
 
@@ -545,13 +557,14 @@ public final class StarGraph implements Graph, Serializable {
 
         protected final Object otherId;
 
-        public StarEdge(final Object id, final String label, final Object otherId) {
+        private StarEdge(final Object id, final String label, final Object otherId) {
             super(id, label);
             this.otherId = otherId;
         }
 
         @Override
         public <V> Property<V> property(final String key, final V value) {
+            ElementHelper.validateProperty(key, value);
             if (null == edgeProperties)
                 edgeProperties = new HashMap<>();
             Map<String, Object> properties = edgeProperties.get(this.id);
@@ -608,7 +621,7 @@ public final class StarGraph implements Graph, Serializable {
 
     public final class StarOutEdge extends StarEdge {
 
-        public StarOutEdge(final Object id, final String label, final Object otherId) {
+        private StarOutEdge(final Object id, final String label, final Object otherId) {
             super(id, label, otherId);
         }
 
@@ -625,7 +638,7 @@ public final class StarGraph implements Graph, Serializable {
 
     public final class StarInEdge extends StarEdge {
 
-        public StarInEdge(final Object id, final String label, final Object otherId) {
+        private StarInEdge(final Object id, final String label, final Object otherId) {
             super(id, label, otherId);
         }
 
@@ -650,7 +663,7 @@ public final class StarGraph implements Graph, Serializable {
         private final V value;
         private final Element element;
 
-        public StarProperty(final String key, final V value, final Element element) {
+        private StarProperty(final String key, final V value, final Element element) {
             if (!labelsAndKeys.containsValue(key.intern())) {
                 this.keyId = nextLabelsAndKeysId++;
                 labelsAndKeys.put(this.keyId, key.intern());

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/5fcbedf6/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/TestHelper.java
----------------------------------------------------------------------
diff --git a/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/TestHelper.java b/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/TestHelper.java
index 9ebc62c..533d5a7 100644
--- a/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/TestHelper.java
+++ b/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/TestHelper.java
@@ -20,7 +20,9 @@ package org.apache.tinkerpop.gremlin;
 
 import org.apache.tinkerpop.gremlin.structure.Direction;
 import org.apache.tinkerpop.gremlin.structure.Edge;
+import org.apache.tinkerpop.gremlin.structure.Graph;
 import org.apache.tinkerpop.gremlin.structure.Property;
+import org.apache.tinkerpop.gremlin.structure.T;
 import org.apache.tinkerpop.gremlin.structure.Vertex;
 import org.apache.tinkerpop.gremlin.structure.VertexProperty;
 import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils;

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/5fcbedf6/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraphTest.java
----------------------------------------------------------------------
diff --git a/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraphTest.java b/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraphTest.java
index 526ef99..9f27fed 100644
--- a/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraphTest.java
+++ b/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraphTest.java
@@ -29,6 +29,7 @@ import org.apache.tinkerpop.gremlin.structure.Direction;
 import org.apache.tinkerpop.gremlin.structure.Edge;
 import org.apache.tinkerpop.gremlin.structure.Graph;
 import org.apache.tinkerpop.gremlin.structure.Property;
+import org.apache.tinkerpop.gremlin.structure.T;
 import org.apache.tinkerpop.gremlin.structure.Vertex;
 import org.apache.tinkerpop.gremlin.structure.VertexProperty;
 import org.apache.tinkerpop.gremlin.structure.io.gryo.GryoReader;
@@ -36,6 +37,7 @@ import org.apache.tinkerpop.gremlin.structure.io.gryo.GryoWriter;
 import org.apache.tinkerpop.gremlin.structure.util.Attachable;
 import org.apache.tinkerpop.gremlin.structure.util.detached.DetachedFactory;
 import org.javatuples.Pair;
+import org.junit.Ignore;
 import org.junit.Test;
 
 import java.io.ByteArrayInputStream;
@@ -46,7 +48,8 @@ import java.util.Random;
 import java.util.Set;
 import java.util.UUID;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
 
 /**
  * @author Marko A. Rodriguez (http://markorodriguez.com)
@@ -98,6 +101,30 @@ public class StarGraphTest extends AbstractGremlinTest {
     }
 
     @Test
+    @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_USER_SUPPLIED_IDS)
+    @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexPropertyFeatures.FEATURE_USER_SUPPLIED_IDS)
+    @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_USER_SUPPLIED_IDS)
+    @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES)
+    @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_PROPERTY)
+    @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_META_PROPERTIES)
+    @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_MULTI_PROPERTIES)
+    @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES)
+    @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_PROPERTY)
+    public void testAttachableCreateMethod() {
+        final Random random = new Random(234335l);
+        StarGraph starGraph = StarGraph.open();
+        Vertex starVertex = starGraph.addVertex(T.label, "person", "name", "stephen", "name", "spmallete");
+        starVertex.property("acl", true, "timestamp", random.nextLong(), "creator", "marko");
+        for (int i = 0; i < 100; i++) {
+            starVertex.addEdge("knows", starGraph.addVertex("person", "name", new UUID(random.nextLong(), random.nextLong()), "since", random.nextLong()));
+            starGraph.addVertex(T.label, "project").addEdge("developedBy", starVertex, "public", random.nextBoolean());
+        }
+        final Vertex createdVertex = starGraph.getStarVertex().attach(Attachable.Method.create(graph));
+        starGraph.getStarVertex().edges(Direction.BOTH).forEachRemaining(edge -> ((Attachable<Edge>) edge).attach(Attachable.Method.create(random.nextBoolean() ? graph : createdVertex)));
+        TestHelper.validateEquality(starVertex, createdVertex);
+    }
+
+    @Test
     @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES)
     @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_PROPERTY)
     @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_META_PROPERTIES)
@@ -158,5 +185,4 @@ public class StarGraphTest extends AbstractGremlinTest {
     }
 
 
-
 }