Repository: tinkerpop Updated Branches: refs/heads/TINKERPOP-1592 65a51d3d8 -> c94f4df03
added Edge support to DeflatedVertex. Started filling out DeflatedVertexTest -- and deflated test suite. Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/c94f4df0 Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/c94f4df0 Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/c94f4df0 Branch: refs/heads/TINKERPOP-1592 Commit: c94f4df03d7302f25386e0258d70dbb047166aa2 Parents: 65a51d3 Author: Marko A. Rodriguez <okramma...@gmail.com> Authored: Mon Jun 26 14:22:00 2017 -0600 Committer: Marko A. Rodriguez <okramma...@gmail.com> Committed: Mon Jun 26 14:22:00 2017 -0600 ---------------------------------------------------------------------- .../structure/util/deflated/DeflatedVertex.java | 68 +++++++++++- .../structure/StructureStandardSuite.java | 2 + .../util/deflated/DeflatedVertexTest.java | 103 +++++++++++++++++++ 3 files changed, 170 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/c94f4df0/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/deflated/DeflatedVertex.java ---------------------------------------------------------------------- diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/deflated/DeflatedVertex.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/deflated/DeflatedVertex.java index ab990da..a4cde6c 100644 --- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/deflated/DeflatedVertex.java +++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/deflated/DeflatedVertex.java @@ -25,6 +25,7 @@ import org.apache.tinkerpop.gremlin.structure.Element; import org.apache.tinkerpop.gremlin.structure.Vertex; import org.apache.tinkerpop.gremlin.structure.VertexProperty; import org.apache.tinkerpop.gremlin.structure.util.StringFactory; +import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils; import java.util.ArrayList; import java.util.Collections; @@ -33,12 +34,17 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.stream.Stream; /** * @author Marko A. Rodriguez (http://markorodriguez.com) */ public class DeflatedVertex extends DeflatedElement<Vertex> implements Vertex { + protected Map<String, List<Edge>> outEdges = null; + protected Map<String, List<Edge>> inEdges = null; + + public DeflatedVertex(final Vertex vertex, final Map<String, Set<String>> components) { super(vertex); if (components.containsKey(PROPERTIES)) { @@ -46,7 +52,7 @@ public class DeflatedVertex extends DeflatedElement<Vertex> implements Vertex { final Iterator<VertexProperty<Object>> itty = propertiesSet.isEmpty() ? vertex.properties() : vertex.properties(propertiesSet.toArray(new String[propertiesSet.size()])); - if(itty.hasNext() && null == this.properties) this.properties = new HashMap<>(); + if (itty.hasNext() && null == this.properties) this.properties = new HashMap<>(); while (itty.hasNext()) { final VertexProperty<Object> vertexProperty = itty.next(); if (!this.properties.containsKey(vertexProperty.key())) @@ -54,6 +60,40 @@ public class DeflatedVertex extends DeflatedElement<Vertex> implements Vertex { this.properties.get(vertexProperty.key()).add(new DeflatedVertexProperty<>(vertexProperty, components)); } } + // TODO: BOTH_E + if (components.containsKey(OUT_E)) { + final Set<String> edgesSet = components.get(OUT_E); + final Iterator<Edge> itty = edgesSet.isEmpty() ? + vertex.edges(Direction.OUT) : + vertex.edges(Direction.OUT, edgesSet.toArray(new String[edgesSet.size()])); + if (itty.hasNext() && null == this.outEdges) this.outEdges = new HashMap<>(); + while (itty.hasNext()) { + final DeflatedEdge deflatedEdge = new DeflatedEdge(itty.next(), components); + List<Edge> labeledEdges = this.outEdges.get(deflatedEdge.label()); + if (null == labeledEdges) { + labeledEdges = new ArrayList<>(); + this.outEdges.put(deflatedEdge.label(), labeledEdges); + } + labeledEdges.add(deflatedEdge); + } + } + if (components.containsKey(IN_E)) { + final Set<String> edgesSet = components.get(IN_E); + final Iterator<Edge> itty = edgesSet.isEmpty() ? + vertex.edges(Direction.IN) : + vertex.edges(Direction.IN, edgesSet.toArray(new String[edgesSet.size()])); + if (itty.hasNext() && null == this.inEdges) this.inEdges = new HashMap<>(); + while (itty.hasNext()) { + final DeflatedEdge deflatedEdge = new DeflatedEdge(itty.next(), components); + List<Edge> labeledEdges = this.inEdges.get(deflatedEdge.label()); + if (null == labeledEdges) { + labeledEdges = new ArrayList<>(); + this.inEdges.put(deflatedEdge.label(), labeledEdges); + } + labeledEdges.add(deflatedEdge); + } + } + } @Override @@ -100,12 +140,34 @@ public class DeflatedVertex extends DeflatedElement<Vertex> implements Vertex { @Override public Iterator<Edge> edges(final Direction direction, final String... edgeLabels) { - return Collections.emptyIterator(); + if (direction.equals(Direction.OUT)) { + if (null == this.outEdges || this.outEdges.isEmpty()) + return Collections.emptyIterator(); + else + return Stream.of(edgeLabels).map(label -> this.outEdges.get(label)).flatMap(List::stream).iterator(); + } else if (direction.equals(Direction.IN)) { + if (null == this.outEdges || this.outEdges.isEmpty()) + return Collections.emptyIterator(); + else + return Stream.of(edgeLabels).map(label -> this.inEdges.get(label)).flatMap(List::stream).iterator(); + } else { + if ((null == this.outEdges || this.outEdges.isEmpty()) && (null == this.inEdges || this.inEdges.isEmpty())) + return Collections.emptyIterator(); + else + return Stream.concat( + Stream.of(edgeLabels).map(label -> this.outEdges.get(label)).flatMap(List::stream), + Stream.of(edgeLabels).map(label -> this.outEdges.get(label)).flatMap(List::stream)).iterator(); + } } @Override public Iterator<Vertex> vertices(final Direction direction, final String... labels) { - return Collections.emptyIterator(); + if (direction.equals(Direction.OUT)) + return IteratorUtils.map(this.edges(direction, labels), Edge::inVertex); + else if (direction.equals(Direction.IN)) + return IteratorUtils.map(this.edges(direction, labels), Edge::outVertex); + else + return IteratorUtils.concat(this.vertices(Direction.IN, labels), this.vertices(Direction.OUT, labels)); } @Override http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/c94f4df0/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/structure/StructureStandardSuite.java ---------------------------------------------------------------------- diff --git a/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/structure/StructureStandardSuite.java b/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/structure/StructureStandardSuite.java index 844bbf5..f46da66 100644 --- a/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/structure/StructureStandardSuite.java +++ b/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/structure/StructureStandardSuite.java @@ -29,6 +29,7 @@ import org.apache.tinkerpop.gremlin.structure.io.IoGraphTest; import org.apache.tinkerpop.gremlin.structure.io.IoTest; import org.apache.tinkerpop.gremlin.structure.io.IoPropertyTest; import org.apache.tinkerpop.gremlin.structure.io.IoVertexTest; +import org.apache.tinkerpop.gremlin.structure.util.deflated.DeflatedVertexTest; import org.apache.tinkerpop.gremlin.structure.util.detached.DetachedEdgeTest; import org.apache.tinkerpop.gremlin.structure.util.detached.DetachedGraphTest; import org.apache.tinkerpop.gremlin.structure.util.detached.DetachedPropertyTest; @@ -78,6 +79,7 @@ public class StructureStandardSuite extends AbstractGremlinSuite { */ private static final Class<?>[] allTests = new Class<?>[]{ CommunityGeneratorTest.class, + DeflatedVertexTest.class, DetachedGraphTest.class, DetachedEdgeTest.class, DetachedVertexPropertyTest.class, http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/c94f4df0/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/structure/util/deflated/DeflatedVertexTest.java ---------------------------------------------------------------------- diff --git a/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/structure/util/deflated/DeflatedVertexTest.java b/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/structure/util/deflated/DeflatedVertexTest.java new file mode 100644 index 0000000..bac6c03 --- /dev/null +++ b/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/structure/util/deflated/DeflatedVertexTest.java @@ -0,0 +1,103 @@ +/* + * 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.structure.util.deflated; + +import org.apache.tinkerpop.gremlin.AbstractGremlinTest; +import org.apache.tinkerpop.gremlin.FeatureRequirementSet; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.apache.tinkerpop.gremlin.structure.util.detached.DetachedFactory; +import org.apache.tinkerpop.gremlin.structure.util.detached.DetachedVertex; +import org.junit.Test; + +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Set; +import java.util.concurrent.atomic.AtomicInteger; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +/** + * @author Marko A. Rodriguez (http://markorodriguez.com) + */ +public class DeflatedVertexTest extends AbstractGremlinTest { + + @Test + @FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY) + public void shouldIteratePropertiesOnDeflate() { + final Vertex v = graph.addVertex("name", "daniel", "favoriteColor", "red", "state", "happy"); + // ALL PROPERTIES + Vertex deflated = DeflatedFactory.detach(v, new HashMap<String, Set<String>>() {{ + put("properties", new HashSet<>(Arrays.asList("name", "favoriteColor", "state"))); + }}); + final AtomicInteger counter = new AtomicInteger(0); + assertTrue(deflated.properties().hasNext()); + deflated.properties().forEachRemaining(p -> { + counter.incrementAndGet(); + if (p.key().equals("name")) + assertEquals("daniel", p.value()); + else if (p.key().equals("favoriteColor")) + assertEquals("red", p.value()); + else if (p.key().equals("state")) + assertEquals("happy", p.value()); + else + fail("Should be one of the expected keys"); + }); + assertEquals(3, counter.get()); + // SOME PROPERTIES + counter.set(0); + deflated = DeflatedFactory.detach(v, new HashMap<String, Set<String>>() {{ + put("properties", new HashSet<>(Arrays.asList("name", "favoriteColor"))); + }}); + assertTrue(deflated.properties().hasNext()); + deflated.properties().forEachRemaining(p -> { + counter.incrementAndGet(); + if (p.key().equals("name")) + assertEquals("daniel", p.value()); + else if (p.key().equals("favoriteColor")) + assertEquals("red", p.value()); + else + fail("Should be one of the expected keys"); + }); + assertEquals(2, counter.get()); + // NO PROPERTIES + deflated = DeflatedFactory.detach(v, Collections.emptyMap()); + assertFalse(deflated.properties().hasNext()); + } + + @Test + @FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY) + public void shouldHashAndEqualCorrectly() { + final Vertex v = graph.addVertex("name","blah"); + final Set<Vertex> set = new HashSet<>(); + for (int i = 0; i < 100; i++) { + set.add(DeflatedFactory.detach(v, Collections.emptyMap())); + set.add(DeflatedFactory.detach(v, new HashMap<String, Set<String>>() {{ + put("properties", new HashSet<>(Arrays.asList("name"))); + }})); + set.add(v); + } + assertEquals(1, set.size()); + } +} \ No newline at end of file