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:45:49 UTC

[03/31] incubator-tinkerpop git commit: StarGraph is now attachable -- though the adjacent vertices are NOT.. You don't want to attach them because they don't really exist and a CREATE will not add properties/etc. Gets to the point @spmallette made -- so

StarGraph is now attachable -- though the adjacent vertices are NOT.. You don't want to attach them because they don't really exist and a CREATE will not add properties/etc. Gets to the point @spmallette made -- sometimes you want GET, CREATE, GET_OR_CREATE, GET_OR_CREATE_OR_APPEND.... gets crazy.


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

Branch: refs/heads/master
Commit: 6bb950fc0bc348308b08628f7fc408db075cd35b
Parents: 30d0388
Author: Marko A. Rodriguez <ok...@gmail.com>
Authored: Thu Apr 23 09:56:16 2015 -0600
Committer: Marko A. Rodriguez <ok...@gmail.com>
Committed: Thu Apr 23 09:56:16 2015 -0600

----------------------------------------------------------------------
 .../gremlin/structure/io/gryo/GryoMapper.java   |  4 +-
 .../gremlin/structure/util/star/StarGraph.java  | 64 ++++++++------
 .../util/star/StarGraphSerializer.java          | 26 +++---
 .../tinkerpop/gremlin/util/tools/BiMap.java     | 92 ++++++++++++++++++++
 4 files changed, 150 insertions(+), 36 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/6bb950fc/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoMapper.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoMapper.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoMapper.java
index a5db767..97b3bbc 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoMapper.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoMapper.java
@@ -52,6 +52,7 @@ import org.apache.tinkerpop.gremlin.structure.util.reference.ReferenceVertex;
 import org.apache.tinkerpop.gremlin.structure.util.reference.ReferenceVertexProperty;
 import org.apache.tinkerpop.gremlin.structure.util.star.StarGraph;
 import org.apache.tinkerpop.gremlin.structure.util.star.StarGraphSerializer;
+import org.apache.tinkerpop.gremlin.util.tools.BiMap;
 import org.apache.tinkerpop.shaded.kryo.Kryo;
 import org.apache.tinkerpop.shaded.kryo.KryoSerializable;
 import org.apache.tinkerpop.shaded.kryo.Serializer;
@@ -198,7 +199,8 @@ public final class GryoMapper implements Mapper<Kryo> {
             add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(ReferenceVertex.class, null, 84));
             add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(ReferencePath.class, null, 85));
 
-            add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(StarGraph.class, kryo -> StarGraphSerializer.with(Direction.BOTH), 86)); // ***LAST ID**
+            add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(StarGraph.class, kryo -> StarGraphSerializer.with(Direction.BOTH), 86));
+            add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(BiMap.class, null, 87));// ***LAST ID**
 
             add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Edge.class, kryo -> new GraphSerializer.EdgeSerializer(), 65));
             add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Vertex.class, kryo -> new GraphSerializer.VertexSerializer(), 66));

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/6bb950fc/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 46b658e..a4bc203 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
@@ -33,9 +33,11 @@ import org.apache.tinkerpop.gremlin.structure.T;
 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.util.Attachable;
 import org.apache.tinkerpop.gremlin.structure.util.ElementHelper;
 import org.apache.tinkerpop.gremlin.structure.util.StringFactory;
 import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils;
+import org.apache.tinkerpop.gremlin.util.tools.BiMap;
 
 import java.io.Serializable;
 import java.util.ArrayList;
@@ -62,6 +64,8 @@ public final class StarGraph implements Graph, Serializable {
     protected StarVertex starVertex = null;
     protected Map<Object, Map<String, Object>> edgeProperties = null;
     protected Map<Object, Map<String, Object>> metaProperties = null;
+    protected BiMap<Short, String> labelsAndKeys = new BiMap<>();
+    protected short nextLabelsAndKeysId = Short.MIN_VALUE;  // 64k unique labels allowed
 
     private StarGraph() {
     }
@@ -186,22 +190,22 @@ public final class StarGraph implements Graph, Serializable {
         return starGraph;
     }
 
-    protected Long generateId() {
-        return this.nextId++;
-    }
-
     ///////////////////////
     //// STAR ELEMENT ////
     //////////////////////
 
-    public abstract class StarElement implements Element {
+    public abstract class StarElement<E extends Element> implements Element, Attachable<E> {
 
         protected final Object id;
-        protected final String label;
+        protected final short labelId;
 
         protected StarElement(final Object id, final String label) {
             this.id = id;
-            this.label = label.intern();
+            if (!labelsAndKeys.containsValue(label.intern())) {
+                this.labelId = nextLabelsAndKeysId++;
+                labelsAndKeys.put(this.labelId, label.intern());
+            } else
+                this.labelId = labelsAndKeys.getKey(label.intern());
         }
 
         @Override
@@ -211,7 +215,7 @@ public final class StarGraph implements Graph, Serializable {
 
         @Override
         public String label() {
-            return this.label;
+            return labelsAndKeys.getValue(this.labelId);
         }
 
         @Override
@@ -228,13 +232,18 @@ public final class StarGraph implements Graph, Serializable {
         public int hashCode() {
             return ElementHelper.hashCode(this);
         }
+
+        @Override
+        public E get() {
+            return (E) this;
+        }
     }
 
     //////////////////////
     //// STAR VERTEX ////
     /////////////////////
 
-    public final class StarVertex extends StarElement implements Vertex {
+    public final class StarVertex extends StarElement<Vertex> implements Vertex {
 
         protected Map<String, List<Edge>> outEdges = null;
         protected Map<String, List<Edge>> inEdges = null;
@@ -244,10 +253,6 @@ public final class StarGraph implements Graph, Serializable {
             super(id, label);
         }
 
-        public Vertex get() {
-            return this;
-        }
-
         public void dropEdges() {
             if (null != this.outEdges) this.outEdges.clear();
             if (null != this.inEdges) this.inEdges.clear();
@@ -279,7 +284,7 @@ public final class StarGraph implements Graph, Serializable {
                 outE = new ArrayList<>();
                 this.outEdges.put(label, outE);
             }
-            final StarEdge outEdge = new StarOutEdge(ElementHelper.getIdValue(keyValues).orElse(generateId()), 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;
@@ -293,7 +298,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(generateId()), 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;
@@ -304,7 +309,7 @@ public final class StarGraph implements Graph, Serializable {
             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(generateId()), 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);
@@ -375,7 +380,7 @@ public final class StarGraph implements Graph, Serializable {
     //// STAR VERTEX PROPERTY ////
     //////////////////////////////
 
-    public final class StarVertexProperty<V> extends StarElement implements VertexProperty<V> {
+    public final class StarVertexProperty<V> extends StarElement<VertexProperty<V>> implements VertexProperty<V> {
 
         private final V value;
 
@@ -386,7 +391,7 @@ public final class StarGraph implements Graph, Serializable {
 
         @Override
         public String key() {
-            return this.label;
+            return this.label();
         }
 
         @Override
@@ -407,7 +412,7 @@ public final class StarGraph implements Graph, Serializable {
         @Override
         public void remove() {
             if (null != StarGraph.this.starVertex.vertexProperties)
-                StarGraph.this.starVertex.vertexProperties.get(this.label).remove(this);
+                StarGraph.this.starVertex.vertexProperties.get(this.label()).remove(this);
         }
 
         @Override
@@ -536,7 +541,7 @@ public final class StarGraph implements Graph, Serializable {
     //// STAR EDGE ////
     ///////////////////
 
-    public abstract class StarEdge extends StarElement implements Edge {
+    public abstract class StarEdge extends StarElement<Edge> implements Edge {
 
         protected final Object otherId;
 
@@ -639,21 +644,25 @@ public final class StarGraph implements Graph, Serializable {
     //// STAR PROPERTY ////
     ///////////////////////
 
-    public final class StarProperty<V> implements Property<V> {
+    public final class StarProperty<V> implements Property<V>, Attachable<Property<V>> {
 
-        private final String key;
+        private final short keyId;
         private final V value;
         private final Element element;
 
         public StarProperty(final String key, final V value, final Element element) {
-            this.key = key.intern();
+            if (!labelsAndKeys.containsValue(key.intern())) {
+                this.keyId = nextLabelsAndKeysId++;
+                labelsAndKeys.put(this.keyId, key.intern());
+            } else
+                this.keyId = labelsAndKeys.getKey(key.intern());
             this.value = value;
             this.element = element;
         }
 
         @Override
         public String key() {
-            return this.key;
+            return labelsAndKeys.getValue(this.keyId);
         }
 
         @Override
@@ -673,7 +682,7 @@ public final class StarGraph implements Graph, Serializable {
 
         @Override
         public void remove() {
-            throw Exceptions.propertyRemovalNotSupported();
+            throw Property.Exceptions.propertyRemovalNotSupported();
         }
 
         @Override
@@ -690,5 +699,10 @@ public final class StarGraph implements Graph, Serializable {
         public int hashCode() {
             return ElementHelper.hashCode(this);
         }
+
+        @Override
+        public Property<V> get() {
+            return this;
+        }
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/6bb950fc/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraphSerializer.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraphSerializer.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraphSerializer.java
index f038fd9..9107142 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraphSerializer.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraphSerializer.java
@@ -25,6 +25,7 @@ import org.apache.tinkerpop.gremlin.structure.Direction;
 import org.apache.tinkerpop.gremlin.structure.Edge;
 import org.apache.tinkerpop.gremlin.structure.T;
 import org.apache.tinkerpop.gremlin.structure.VertexProperty;
+import org.apache.tinkerpop.gremlin.util.tools.BiMap;
 import org.apache.tinkerpop.shaded.kryo.Kryo;
 import org.apache.tinkerpop.shaded.kryo.Serializer;
 import org.apache.tinkerpop.shaded.kryo.io.Input;
@@ -40,7 +41,7 @@ import java.util.Map;
  */
 public final class StarGraphSerializer extends Serializer<StarGraph> {
 
-    private static final Map<Direction,StarGraphSerializer> CACHE = new HashMap<>();
+    private static final Map<Direction, StarGraphSerializer> CACHE = new HashMap<>();
 
     private final Direction edgeDirectionToSerialize;
 
@@ -68,15 +69,17 @@ public final class StarGraphSerializer extends Serializer<StarGraph> {
         kryo.writeObject(output, starGraph.nextId);
         kryo.writeObjectOrNull(output, starGraph.edgeProperties, HashMap.class);
         kryo.writeObjectOrNull(output, starGraph.metaProperties, HashMap.class);
+        kryo.writeObject(output, starGraph.labelsAndKeys);
+        kryo.writeObject(output, starGraph.nextLabelsAndKeysId);
         kryo.writeClassAndObject(output, starGraph.starVertex.id);
-        kryo.writeObject(output, starGraph.starVertex.label);
-        writeEdges(kryo, output, starGraph.starVertex.inEdges, Direction.IN);
-        writeEdges(kryo, output, starGraph.starVertex.outEdges, Direction.OUT);
+        kryo.writeObject(output, starGraph.starVertex.labelId);
+        writeEdges(kryo, output, starGraph, Direction.IN);
+        writeEdges(kryo, output, starGraph, Direction.OUT);
         kryo.writeObject(output, null != starGraph.starVertex.vertexProperties);
         if (null != starGraph.starVertex.vertexProperties) {
             kryo.writeObject(output, starGraph.starVertex.vertexProperties.size());
             for (final Map.Entry<String, List<VertexProperty>> vertexProperties : starGraph.starVertex.vertexProperties.entrySet()) {
-                kryo.writeObject(output, vertexProperties.getKey());
+                kryo.writeObject(output, starGraph.labelsAndKeys.getKey(vertexProperties.getKey()));
                 kryo.writeObject(output, vertexProperties.getValue().size());
                 for (final VertexProperty vertexProperty : vertexProperties.getValue()) {
                     kryo.writeClassAndObject(output, vertexProperty.id());
@@ -92,13 +95,15 @@ public final class StarGraphSerializer extends Serializer<StarGraph> {
         starGraph.nextId = kryo.readObject(input, Long.class);
         starGraph.edgeProperties = kryo.readObjectOrNull(input, HashMap.class);
         starGraph.metaProperties = kryo.readObjectOrNull(input, HashMap.class);
-        starGraph.addVertex(T.id, kryo.readClassAndObject(input), T.label, kryo.readObject(input, String.class));
+        starGraph.labelsAndKeys = kryo.readObject(input, BiMap.class);
+        starGraph.nextLabelsAndKeysId = kryo.readObject(input, Short.class);
+        starGraph.addVertex(T.id, kryo.readClassAndObject(input), T.label, starGraph.labelsAndKeys.getValue(kryo.readObject(input, Short.class)));
         readEdges(kryo, input, starGraph, Direction.IN);
         readEdges(kryo, input, starGraph, Direction.OUT);
         if (kryo.readObject(input, Boolean.class)) {
             final int numberOfUniqueKeys = kryo.readObject(input, Integer.class);
             for (int i = 0; i < numberOfUniqueKeys; i++) {
-                final String vertexPropertyKey = kryo.readObject(input, String.class);
+                final String vertexPropertyKey = starGraph.labelsAndKeys.getValue(kryo.readObject(input, Short.class));
                 final int numberOfVertexPropertiesWithKey = kryo.readObject(input, Integer.class);
                 for (int j = 0; j < numberOfVertexPropertiesWithKey; j++) {
                     final Object id = kryo.readClassAndObject(input);
@@ -110,16 +115,17 @@ public final class StarGraphSerializer extends Serializer<StarGraph> {
         return starGraph;
     }
 
-    private void writeEdges(final Kryo kryo, final Output output, final Map<String, List<Edge>> starEdges, final Direction direction) {
+    private void writeEdges(final Kryo kryo, final Output output, final StarGraph starGraph, final Direction direction) {
         // only write edges if there are some AND if the user requested them to be serialized AND if they match
         // the direction being serialized by the format
+        final Map<String, List<Edge>> starEdges = direction.equals(Direction.OUT) ? starGraph.starVertex.outEdges : starGraph.starVertex.inEdges;
         final boolean writeEdges = null != starEdges && edgeDirectionToSerialize != null
                 && (edgeDirectionToSerialize == direction || edgeDirectionToSerialize == Direction.BOTH);
         kryo.writeObject(output, writeEdges);
         if (writeEdges) {
             kryo.writeObject(output, starEdges.size());
             for (final Map.Entry<String, List<Edge>> edges : starEdges.entrySet()) {
-                kryo.writeObject(output, edges.getKey());
+                kryo.writeObject(output, starGraph.labelsAndKeys.getKey(edges.getKey()));
                 kryo.writeObject(output, edges.getValue().size());
                 for (final Edge edge : edges.getValue()) {
                     kryo.writeClassAndObject(output, edge.id());
@@ -133,7 +139,7 @@ public final class StarGraphSerializer extends Serializer<StarGraph> {
         if (kryo.readObject(input, Boolean.class)) {
             final int numberOfUniqueLabels = kryo.readObject(input, Integer.class);
             for (int i = 0; i < numberOfUniqueLabels; i++) {
-                final String edgeLabel = kryo.readObject(input, String.class);
+                final String edgeLabel = starGraph.labelsAndKeys.getValue(kryo.readObject(input, Short.class));
                 final int numberOfEdgesWithLabel = kryo.readObject(input, Integer.class);
                 for (int j = 0; j < numberOfEdgesWithLabel; j++) {
                     final Object edgeId = kryo.readClassAndObject(input);

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/6bb950fc/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/util/tools/BiMap.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/util/tools/BiMap.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/util/tools/BiMap.java
new file mode 100644
index 0000000..29dc4cf
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/util/tools/BiMap.java
@@ -0,0 +1,92 @@
+/*
+ *
+ *  * Licensed to the Apache Software Foundation (ASF) under one
+ *  * or more contributor license agreements.  See the NOTICE file
+ *  * distributed with this work for additional information
+ *  * regarding copyright ownership.  The ASF licenses this file
+ *  * to you under the Apache License, Version 2.0 (the
+ *  * "License"); you may not use this file except in compliance
+ *  * with the License.  You may obtain a copy of the License at
+ *  *
+ *  * http://www.apache.org/licenses/LICENSE-2.0
+ *  *
+ *  * Unless required by applicable law or agreed to in writing,
+ *  * software distributed under the License is distributed on an
+ *  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  * KIND, either express or implied.  See the License for the
+ *  * specific language governing permissions and limitations
+ *  * under the License.
+ *
+ */
+
+package org.apache.tinkerpop.gremlin.util.tools;
+
+import java.io.Serializable;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * @author Marko A. Rodriguez (http://markorodriguez.com)
+ */
+public class BiMap<K, V> implements Serializable {
+
+    private Map<K, V> leftMap = new HashMap<>();
+    private Map<V, K> rightMap = new HashMap<>();
+
+    public int size() {
+        return this.leftMap.size();
+    }
+
+    public boolean isEmpty() {
+        return this.leftMap.isEmpty();
+    }
+
+    public boolean containsKey(final K key) {
+        return this.leftMap.containsKey(key);
+    }
+
+    public boolean containsValue(final V value) {
+        return this.rightMap.containsKey(value);
+    }
+
+    public V getValue(final K key) {
+        return this.leftMap.get(key);
+    }
+
+    public K getKey(final V value) {
+        return this.rightMap.get(value);
+    }
+
+    public V put(final K key, final V value) {
+        this.rightMap.put(value, key);
+        return this.leftMap.put(key, value);
+    }
+
+    public V removeKey(final K key) {
+        final V v = this.leftMap.remove(key);
+        if (null != v)
+            this.rightMap.remove(v);
+        return v;
+    }
+
+    public K removeValue(final V value) {
+        final K k = this.rightMap.remove(value);
+        if (null != k)
+            this.leftMap.remove(k);
+        return k;
+    }
+
+    public void clear() {
+        this.leftMap.clear();
+        this.rightMap.clear();
+    }
+
+    public Set<K> keySet() {
+        return this.leftMap.keySet();
+    }
+
+    public Set<V> valueSet() {
+        return this.rightMap.keySet();
+    }
+}