Skip to content

Commit

Permalink
fix: the vertex from edges is missing properties (#604)
Browse files Browse the repository at this point in the history
fix: #595
Change-Id: I7cacb37f7d7592cedbdcc6f98db7cc8bbb1bf537
  • Loading branch information
javeme authored and zhoney committed Jul 9, 2019
1 parent 251be0c commit 011ac85
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,8 @@ public void serialize(HugeVertex vertex, JsonGenerator generator,
generator.writeStringField("label", vertex.label());
generator.writeStringField("type", "vertex");

this.writePropertiesField(vertex.getProperties(), generator,
provider);
this.writePropertiesField(vertex.getFilledProperties(),
generator, provider);

generator.writeEndObject();
}
Expand Down Expand Up @@ -399,7 +399,8 @@ public void serialize(HugeEdge edge, JsonGenerator generator,
this.writeIdField("inV", inVertex.id(), generator);
generator.writeStringField("inVLabel", inVertex.label());

this.writePropertiesField(edge.getProperties(), generator, provider);
this.writePropertiesField(edge.getFilledProperties(),
generator, provider);

generator.writeEndObject();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,21 +221,28 @@ protected <V> void onUpdateProperty(Cardinality cardinality,
}

@Watched(prefix = "edge")
protected void ensureEdgeProperties() {
@Override
protected boolean ensureFilledProperties(boolean throwIfNotExist) {
if (this.propLoaded) {
return;
return true;
}

Iterator<Edge> edges = tx().queryEdges(this.id());
E.checkState(edges.hasNext(), "Edge '%s' does not exist", this.id);
boolean exist = edges.hasNext();
if (!exist && !throwIfNotExist) {
return false;
}
E.checkState(exist, "Edge '%s' does not exist", this.id);
this.copyProperties((HugeEdge) edges.next());
assert exist;
return true;
}

@Watched(prefix = "edge")
@Override
@SuppressWarnings("unchecked") // (Property<V>) prop
@Override
public <V> Iterator<Property<V>> properties(String... keys) {
this.ensureEdgeProperties();
this.ensureFilledProperties(true);

// Capacity should be about the following size
int propsCapacity = keys.length == 0 ?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ public HugeElement(final HugeGraph graph, Id id) {
protected abstract <V> void onUpdateProperty(Cardinality cardinality,
HugeProperty<V> prop);

protected abstract boolean ensureFilledProperties(boolean throwIfNotExist);

@Override
public Id id() {
return this.id;
Expand Down Expand Up @@ -114,6 +116,11 @@ public Map<Id, HugeProperty<?>> getProperties() {
return Collections.unmodifiableMap(this.properties);
}

public Map<Id, HugeProperty<?>> getFilledProperties() {
this.ensureFilledProperties(true);
return Collections.unmodifiableMap(this.properties);
}

public Map<Id, Object> getPropertiesMap() {
Map<Id, Object> props = new HashMap<>();
for (Map.Entry<Id, HugeProperty<?>> entry :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,8 @@ protected <V> void onUpdateProperty(Cardinality cardinality,
}

@Watched(prefix = "vertex")
protected boolean ensureVertexProperties(boolean throwIfNotExist) {
@Override
protected boolean ensureFilledProperties(boolean throwIfNotExist) {
if (this.propLoaded) {
return true;
}
Expand All @@ -442,12 +443,12 @@ protected boolean ensureVertexProperties(boolean throwIfNotExist) {
}

@Watched(prefix = "vertex")
@Override
@SuppressWarnings("unchecked") // (VertexProperty<V>) prop
@Override
public <V> Iterator<VertexProperty<V>> properties(String... keys) {
// TODO: Compatible with TinkerPop properties() (HugeGraph-742)

this.ensureVertexProperties(true);
this.ensureFilledProperties(true);

// Capacity should be about the following size
int propsCapacity = keys.length == 0 ?
Expand Down Expand Up @@ -498,7 +499,7 @@ public Object sysprop(HugeKeys key) {

public boolean valid() {
try {
return this.ensureVertexProperties(false);
return this.ensureFilledProperties(false);
} catch (Throwable e) {
// Generally the program can't get here
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package com.baidu.hugegraph.core;

import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
Expand Down Expand Up @@ -55,12 +56,16 @@
import com.baidu.hugegraph.exception.LimitExceedException;
import com.baidu.hugegraph.exception.NotFoundException;
import com.baidu.hugegraph.schema.SchemaManager;
import com.baidu.hugegraph.structure.HugeEdge;
import com.baidu.hugegraph.structure.HugeVertex;
import com.baidu.hugegraph.testutil.Assert;
import com.baidu.hugegraph.testutil.FakeObjects.FakeEdge;
import com.baidu.hugegraph.testutil.Utils;
import com.baidu.hugegraph.testutil.Whitebox;
import com.baidu.hugegraph.traversal.optimize.Text;
import com.baidu.hugegraph.traversal.optimize.TraversalUtil;
import com.baidu.hugegraph.type.HugeType;
import com.baidu.hugegraph.type.define.Directions;
import com.baidu.hugegraph.type.define.HugeKeys;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
Expand Down Expand Up @@ -972,21 +977,88 @@ public void testQueryEdgesByDirection() {
}

@Test
public void testQueryBothEdgesOfVertex() {
public void testQueryEdgesOfVertex() {
HugeGraph graph = graph();
init18Edges();

// Query edges of a vertex
Vertex james = vertex("author", "id", 1);

// Query BOTH edges of a vertex
List<Edge> edges = graph.traversal().V(james.id()).bothE().toList();
Assert.assertEquals(6, edges.size());

edges = ImmutableList.copyOf(james.edges(Direction.BOTH));
Assert.assertEquals(6, edges.size());

// Query OUT edges of a vertex
edges = graph.traversal().V(james.id()).outE().toList();
Assert.assertEquals(4, edges.size());

edges = ImmutableList.copyOf(james.edges(Direction.OUT));
Assert.assertEquals(4, edges.size());

// Query IN edges of a vertex
edges = graph.traversal().V(james.id()).inE().toList();
Assert.assertEquals(2, edges.size());

edges = ImmutableList.copyOf(james.edges(Direction.IN));
Assert.assertEquals(2, edges.size());
}

@Test
public void testQueryEdgesOfVertexWithoutCommit() {
HugeGraph graph = graph();
init18Edges(false);

HugeVertex james = (HugeVertex) vertex("author", "id", 1);

List<Vertex> vertices = ImmutableList.copyOf(
james.getVertices(Directions.BOTH));
Assert.assertEquals(6, vertices.size());

vertices = ImmutableList.copyOf(james.getVertices(Directions.OUT));
Assert.assertEquals(4, vertices.size());

vertices = ImmutableList.copyOf(james.getVertices(Directions.IN));
Assert.assertEquals(2, vertices.size());

vertices = ImmutableList.copyOf(james.getVertices(Directions.OUT,
"authored"));
Assert.assertEquals(3, vertices.size());

vertices = ImmutableList.copyOf(james.getVertices(Directions.OUT,
"authored",
"created"));
Assert.assertEquals(4, vertices.size());

vertices = ImmutableList.copyOf(james.getVertices(Directions.IN,
"know"));
Assert.assertEquals(1, vertices.size());

// Query BOTH edges of a vertex
List<Edge> edges = graph.traversal().V(james.id()).bothE().toList();
Assert.assertEquals(6, edges.size());

edges = ImmutableList.copyOf(james.edges(Direction.BOTH));
Assert.assertEquals(6, edges.size());

// Query OUT edges of a vertex
edges = graph.traversal().V(james.id()).outE().toList();
Assert.assertEquals(4, edges.size());

edges = ImmutableList.copyOf(james.edges(Direction.OUT));
Assert.assertEquals(4, edges.size());

// Query IN edges of a vertex
edges = graph.traversal().V(james.id()).inE().toList();
Assert.assertEquals(2, edges.size());

edges = ImmutableList.copyOf(james.edges(Direction.IN));
Assert.assertEquals(2, edges.size());
}

@Test
public void testQueryBothEdgesOfVertexWithGraphAPI() {
public void testQueryEdgesOfVertexWithGraphAPI() {
HugeGraph graph = graph();
init18Edges();

Expand All @@ -996,24 +1068,82 @@ public void testQueryBothEdgesOfVertexWithGraphAPI() {
graph.adjacentEdges((Id) james.id()));
Assert.assertEquals(6, edges.size());

edges = ImmutableList.copyOf(james.edges(Direction.BOTH));
Assert.assertEquals(6, edges.size());
List<Edge> edges2 = ImmutableList.copyOf(james.edges(Direction.BOTH));
Assert.assertEquals(6, edges2.size());

Assert.assertEquals(edges2, edges);
}

@Test
public void testQueryBothVerticesOfVertex() {
public void testQueryVerticesOfVertex() {
HugeGraph graph = graph();
init18Edges();

Vertex jeff = vertex("person", "name", "Jeff");

// BOTH
List<Vertex> vertices = graph.traversal().V(jeff.id())
.both("friend").toList();
.both("friend").toList();
Assert.assertEquals(2, vertices.size());

vertices = ImmutableList.copyOf(
jeff.vertices(Direction.BOTH, "friend"));
Assert.assertEquals(2, vertices.size());

// OUT
vertices = graph.traversal().V(jeff.id()).out("look").toList();
Assert.assertEquals(1, vertices.size());

vertices = ImmutableList.copyOf(jeff.vertices(Direction.OUT, "look"));
Assert.assertEquals(1, vertices.size());

// IN
vertices = graph.traversal().V(jeff.id()).in("follow").toList();
Assert.assertEquals(0, vertices.size());

vertices = ImmutableList.copyOf(jeff.vertices(Direction.IN, "follow"));
Assert.assertEquals(0, vertices.size());
}

@Test
public void testQueryVerticesOfEdges() {
HugeGraph graph = graph();
init18Edges();

Vertex jeff = vertex("person", "name", "Jeff");
Vertex java3 = vertex("book", "name", "java-3");

// BOTH
List<Vertex> vertices = graph.traversal().V(jeff.id())
.bothE("friend").as("e").otherV()
.toList();
Assert.assertEquals(2, vertices.size());

// OUT
List<Edge> edges = graph.traversal().V(jeff.id())
.outE("look").toList();
Assert.assertEquals(1, edges.size());

HugeEdge edge = (HugeEdge) edges.get(0);
Assert.assertEquals(jeff, edge.ownerVertex());
Assert.assertEquals(jeff, edge.sourceVertex());
Assert.assertEquals(java3, edge.otherVertex());
Assert.assertEquals(java3, edge.targetVertex());
Assert.assertEquals(jeff, edge.vertices(Direction.OUT).next());
Assert.assertEquals(java3, edge.vertices(Direction.IN).next());

// Fill edge properties
Assert.assertEquals(2, edge.getProperties().size());
Whitebox.setInternalState(edge, "propLoaded", false);
Whitebox.setInternalState(edge, "properties", new HashMap<>());
Assert.assertEquals(0, edge.getProperties().size());
Assert.assertEquals(2, edge.getFilledProperties().size());
Assert.assertEquals(2, edge.getProperties().size());

// Fill vertex properties
Assert.assertTrue(edge.otherVertex().getProperties().isEmpty());
Assert.assertEquals(1, edge.otherVertex().getFilledProperties().size());
Assert.assertEquals(1, edge.otherVertex().getProperties().size());
}

@Test
Expand Down Expand Up @@ -2778,6 +2908,10 @@ public void testQueryEdgeByPageWithOffset() {
}

private void init18Edges() {
this.init18Edges(true);
}

private void init18Edges(boolean commit) {
HugeGraph graph = graph();

Vertex james = graph.addVertex(T.label, "author", "id", 1,
Expand Down Expand Up @@ -2827,7 +2961,9 @@ private void init18Edges() {
jeff.addEdge("friend", sean);
jeff.addEdge("follow", james);

graph.tx().commit();
if (commit) {
graph.tx().commit();
}
}

private void init100LookEdges() {
Expand Down

0 comments on commit 011ac85

Please sign in to comment.