You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by ok...@apache.org on 2015/04/08 23:24:17 UTC

[2/2] incubator-tinkerpop git commit: StarGraph edges and vertex properties have half the number of references.

StarGraph edges and vertex properties have half the number of references.


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

Branch: refs/heads/master
Commit: aabc66fa0a2929129e32f6fc53d8aaeaa6441473
Parents: d8fdf79
Author: Marko A. Rodriguez <ok...@gmail.com>
Authored: Wed Apr 8 15:24:14 2015 -0600
Committer: Marko A. Rodriguez <ok...@gmail.com>
Committed: Wed Apr 8 15:24:14 2015 -0600

----------------------------------------------------------------------
 .../gremlin/structure/util/star/StarGraph.java  | 137 +++++++++++--------
 1 file changed, 83 insertions(+), 54 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/aabc66fa/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 a153d7f..fd9b52a 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
@@ -53,10 +53,16 @@ import java.util.stream.Stream;
  */
 public final class StarGraph implements Graph {
 
-    private static final Configuration EMPTY_CONFIGURATION = new BaseConfiguration();
+    private static final Configuration STAR_GRAPH_CONFIGURATION = new BaseConfiguration();
+    static {
+        STAR_GRAPH_CONFIGURATION.setProperty(Graph.GRAPH, StarGraph.class.getCanonicalName());
+    }
     private StarVertex starVertex = null;
     private Long nextId = 0l;
 
+    private final Map<Object, Map<String, Object>> edgeProperties = new HashMap<>();
+    private final Map<Object, Map<String, Object>> metaProperties = new HashMap<>();
+
     @Override
     public Vertex addVertex(final Object... keyValues) {
         return null == this.starVertex ?
@@ -124,7 +130,7 @@ public final class StarGraph implements Graph {
 
     @Override
     public Configuration configuration() {
-        return EMPTY_CONFIGURATION;
+        return STAR_GRAPH_CONFIGURATION;
     }
 
     @Override
@@ -154,7 +160,7 @@ public final class StarGraph implements Graph {
                 keyValues.add(detachedVertexPropertyProperty.key());
                 keyValues.add(detachedVertexPropertyProperty.value());
             });
-            graph.starVertex.property(VertexProperty.Cardinality.list, detachedVertexProperty.key(), detachedVertexProperty.value(), keyValues.toArray(new Object[keyValues.size()]));
+            graph.starVertex.property(VertexProperty.Cardinality.list, detachedVertexProperty.key(), detachedVertexProperty.value(), keyValues.toArray());
         });
         return graph.starVertex;
     }
@@ -168,8 +174,8 @@ public final class StarGraph implements Graph {
             keyValues.add(property.value());
         });
         return !graph.starVertex.id().equals(edge.inVertex().id()) ?
-                graph.starVertex.addOutEdge(edge.label(), edge.inVertex(), keyValues.toArray(new Object[keyValues.size()])) :
-                graph.starVertex.addInEdge(edge.label(), edge.outVertex(), keyValues.toArray(new Object[keyValues.size()]));
+                graph.starVertex.addOutEdge(edge.label(), edge.inVertex(), keyValues.toArray()) :
+                graph.starVertex.addInEdge(edge.label(), edge.outVertex(), keyValues.toArray());
     }
 
     protected Long generateId() {
@@ -246,7 +252,7 @@ public final class StarGraph implements Graph {
                 outE = new ArrayList<>();
                 this.outEdges.put(label, outE);
             }
-            final StarEdge outEdge = new StarEdge(ElementHelper.getIdValue(keyValues).orElse(generateId()), label, inVertex.id(), Direction.OUT);
+            final StarEdge outEdge = new StarOutEdge(ElementHelper.getIdValue(keyValues).orElse(generateId()), label, inVertex.id());
             ElementHelper.attachProperties(outEdge, keyValues);
             outE.add(outEdge);
             return outEdge;
@@ -258,7 +264,7 @@ public final class StarGraph implements Graph {
                 inE = new ArrayList<>();
                 this.inEdges.put(label, inE);
             }
-            final StarEdge inEdge = new StarEdge(ElementHelper.getIdValue(keyValues).orElse(generateId()), label, outVertex.id(), Direction.IN);
+            final StarEdge inEdge = new StarInEdge(ElementHelper.getIdValue(keyValues).orElse(generateId()), label, outVertex.id());
             ElementHelper.attachProperties(inEdge, keyValues);
             inE.add(inEdge);
             return inEdge;
@@ -345,7 +351,6 @@ public final class StarGraph implements Graph {
     public final class StarVertexProperty<V> extends StarElement implements VertexProperty<V> {
 
         private final V value;
-        private Map<String, Object> metaProperties = null;
 
         public StarVertexProperty(final Object id, final String key, final V value) {
             super(id, key);
@@ -379,19 +384,20 @@ public final class StarGraph implements Graph {
 
         @Override
         public <U> Iterator<Property<U>> properties(final String... propertyKeys) {
-            if (null == this.metaProperties)
+            final Map<String, Object> properties = metaProperties.get(this.id);
+            if (null == properties || properties.isEmpty())
                 return Collections.emptyIterator();
             else if (propertyKeys.length == 0)
-                return (Iterator) this.metaProperties.entrySet().stream()
+                return (Iterator) properties.entrySet().stream()
                         .map(entry -> new StarProperty<>(entry.getKey(), entry.getValue(), this))
                         .iterator();
             else if (propertyKeys.length == 1) {
-                final Object v = this.metaProperties.get(propertyKeys[0]);
+                final Object v = properties.get(propertyKeys[0]);
                 return null == v ?
                         Collections.emptyIterator() :
                         (Iterator) IteratorUtils.of(new StarProperty<>(propertyKeys[0], v, this));
             } else {
-                return (Iterator) this.metaProperties.entrySet().stream()
+                return (Iterator) properties.entrySet().stream()
                         .filter(entry -> ElementHelper.keyExists(entry.getKey(), propertyKeys))
                         .map(entry -> new StarProperty<>(entry.getKey(), entry.getValue(), this))
                         .iterator();
@@ -399,15 +405,13 @@ public final class StarGraph implements Graph {
         }
 
         @Override
-        public Object id() {
-            return this.id;
-        }
-
-        @Override
         public <U> Property<U> property(final String key, final U value) {
-            if (null == this.metaProperties)
-                this.metaProperties = new HashMap<>();
-            this.metaProperties.put(key, value);
+            Map<String, Object> properties = metaProperties.get(this.id);
+            if (null == properties) {
+                properties = new HashMap<>();
+                metaProperties.put(this.id, properties);
+            }
+            properties.put(key, value);
             return new StarProperty<>(key, value, this);
         }
 
@@ -502,61 +506,42 @@ public final class StarGraph implements Graph {
     //// STAR EDGE ////
     ///////////////////
 
-    public final class StarEdge extends StarElement implements Edge {
+    public abstract class StarEdge extends StarElement implements Edge {
 
-        private final Object otherId;
-        private final Direction direction;
-        private Map<String, Object> edgeProperties = null;
+        protected final Object otherId;
 
-        public StarEdge(final Object id, final String label, final Object otherId, final Direction direction) {
+        public StarEdge(final Object id, final String label, final Object otherId) {
             super(id, label);
             this.otherId = otherId;
-            this.direction = direction;
-        }
-
-        @Override
-        public Iterator<Vertex> vertices(final Direction direction) {
-            if (direction.equals(Direction.OUT))
-                return IteratorUtils.of(this.outVertex());
-            else if (direction.equals(Direction.IN))
-                return IteratorUtils.of(this.inVertex());
-            else
-                return this.direction.equals(Direction.OUT) ?
-                        IteratorUtils.of(starVertex, new StarAdjacentVertex(this.otherId)) :
-                        IteratorUtils.of(new StarAdjacentVertex(this.otherId), starVertex);
-        }
-
-        public Vertex outVertex() {
-            return this.direction.equals(Direction.OUT) ? starVertex : new StarAdjacentVertex(this.otherId);
-        }
-
-        public Vertex inVertex() {
-            return this.direction.equals(Direction.OUT) ? new StarAdjacentVertex(this.otherId) : starVertex;
         }
 
         @Override
         public <V> Property<V> property(final String key, final V value) {
-            if (null == this.edgeProperties)
-                this.edgeProperties = new HashMap<>();
-            this.edgeProperties.put(key, value);
+            Map<String, Object> properties = edgeProperties.get(this.id);
+            if (null == properties) {
+                properties = new HashMap<>();
+                edgeProperties.put(this.id, properties);
+            }
+            properties.put(key, value);
             return new StarProperty<>(key, value, this);
         }
 
         @Override
         public <V> Iterator<Property<V>> properties(final String... propertyKeys) {
-            if (null == this.edgeProperties)
+            Map<String, Object> properties = edgeProperties.get(this.id);
+            if (null == properties || properties.isEmpty())
                 return Collections.emptyIterator();
             else if (propertyKeys.length == 0)
-                return (Iterator) this.edgeProperties.entrySet().stream()
+                return (Iterator) properties.entrySet().stream()
                         .map(entry -> new StarProperty<>(entry.getKey(), entry.getValue(), this))
                         .iterator();
             else if (propertyKeys.length == 1) {
-                final Object v = this.edgeProperties.get(propertyKeys[0]);
+                final Object v = properties.get(propertyKeys[0]);
                 return null == v ?
                         Collections.emptyIterator() :
                         (Iterator) IteratorUtils.of(new StarProperty<>(propertyKeys[0], v, this));
             } else {
-                return (Iterator) this.edgeProperties.entrySet().stream()
+                return (Iterator) properties.entrySet().stream()
                         .filter(entry -> ElementHelper.keyExists(entry.getKey(), propertyKeys))
                         .map(entry -> new StarProperty<>(entry.getKey(), entry.getValue(), this))
                         .iterator();
@@ -564,8 +549,18 @@ public final class StarGraph implements Graph {
         }
 
         @Override
+        public Iterator<Vertex> vertices(final Direction direction) {
+            if (direction.equals(Direction.OUT))
+                return IteratorUtils.of(this.outVertex());
+            else if (direction.equals(Direction.IN))
+                return IteratorUtils.of(this.inVertex());
+            else
+                return IteratorUtils.of(this.outVertex(), this.inVertex());
+        }
+
+        @Override
         public void remove() {
-            //TODO: throw Edge.Exceptions.edgeRemovalNotSupported();
+            throw Edge.Exceptions.edgeRemovalNotSupported();
         }
 
         @Override
@@ -584,6 +579,40 @@ public final class StarGraph implements Graph {
         }
     }
 
+    public final class StarOutEdge extends StarEdge {
+
+        public StarOutEdge(final Object id, final String label, final Object otherId) {
+            super(id, label, otherId);
+        }
+
+        @Override
+        public Vertex outVertex() {
+            return starVertex;
+        }
+
+        @Override
+        public Vertex inVertex() {
+            return new StarAdjacentVertex(this.otherId);
+        }
+    }
+
+    public final class StarInEdge extends StarEdge {
+
+        public StarInEdge(final Object id, final String label, final Object otherId) {
+            super(id, label, otherId);
+        }
+
+        @Override
+        public Vertex outVertex() {
+            return new StarAdjacentVertex(this.otherId);
+        }
+
+        @Override
+        public Vertex inVertex() {
+            return starVertex;
+        }
+    }
+
     ////////////////////////
     //// STAR PROPERTY ////
     ///////////////////////