use of com.baidu.hugegraph.structure.HugeVertex in project incubator-hugegraph by apache.
the class GraphTransaction method removeLeftIndexIfNeeded.
private void removeLeftIndexIfNeeded(Map<Id, HugeVertex> vertices) {
Set<Id> ids = vertices.keySet();
if (ids.isEmpty()) {
return;
}
IdQuery idQuery = new IdQuery(HugeType.VERTEX, ids);
Iterator<HugeVertex> results = this.queryVerticesFromBackend(idQuery);
try {
while (results.hasNext()) {
HugeVertex existedVertex = results.next();
this.indexTx.updateVertexIndex(existedVertex, true);
}
} finally {
CloseableIterator.closeIterator(results);
}
}
use of com.baidu.hugegraph.structure.HugeVertex in project incubator-hugegraph by apache.
the class GraphTransaction method queryEdgesFromBackend.
protected Iterator<HugeEdge> queryEdgesFromBackend(Query query) {
assert query.resultType().isEdge();
QueryResults<BackendEntry> results = this.query(query);
Iterator<BackendEntry> entries = results.iterator();
Iterator<HugeEdge> edges = new FlatMapperIterator<>(entries, entry -> {
// Edges are in a vertex
HugeVertex vertex = this.parseEntry(entry);
if (vertex == null) {
return null;
}
if (query.idsSize() == 1) {
assert vertex.getEdges().size() == 1;
}
/*
* Copy to avoid ConcurrentModificationException when removing edge
* because HugeEdge.remove() will update edges in owner vertex
*/
return new ListIterator<>(ImmutableList.copyOf(vertex.getEdges()));
});
edges = this.filterExpiredResultFromFromBackend(query, edges);
if (!this.store().features().supportsQuerySortByInputIds()) {
// There is no id in BackendEntry, so sort after deserialization
edges = results.keepInputOrderIfNeeded(edges);
}
return edges;
}
use of com.baidu.hugegraph.structure.HugeVertex in project incubator-hugegraph by apache.
the class GraphIndexTransaction method updateLabelIndex.
@Watched(prefix = "index")
public void updateLabelIndex(HugeElement element, boolean removed) {
if (element instanceof HugeVertex && ((HugeVertex) element).olap()) {
return;
}
if (!this.needIndexForLabel()) {
return;
}
// Don't update label index if it's not enabled
SchemaLabel label = element.schemaLabel();
if (!label.enableLabelIndex()) {
return;
}
// Update label index if backend store not supports label-query
HugeIndex index = new HugeIndex(this.graph(), IndexLabel.label(element.type()));
index.fieldValues(element.schemaLabel().id());
index.elementIds(element.id(), element.expiredTime());
if (removed) {
this.doEliminate(this.serializer.writeIndex(index));
} else {
this.doAppend(this.serializer.writeIndex(index));
}
}
use of com.baidu.hugegraph.structure.HugeVertex in project incubator-hugegraph by apache.
the class EdgeCoreTest method testQueryAdjacentVerticesOfEdgesWithoutVertex.
@Test
public void testQueryAdjacentVerticesOfEdgesWithoutVertex() throws InterruptedException, ExecutionException {
HugeGraph graph = graph();
Vertex james = graph.addVertex(T.label, "author", "id", 1, "name", "James Gosling", "age", 62, "lived", "Canadian");
Vertex java = new HugeVertex(graph, IdGenerator.of("java"), graph.vertexLabel("book"));
james.addEdge("authored", java, "score", 3);
graph.tx().commit();
List<Edge> edges = graph.traversal().V(james.id()).outE().toList();
Assert.assertEquals(1, edges.size());
Assert.assertEquals(0, graph.traversal().V(java).toList().size());
List<Vertex> vertices = graph.traversal().V(james.id()).out().toList();
Assert.assertEquals(1, vertices.size());
HugeVertex adjacent = (HugeVertex) vertices.get(0);
Assert.assertFalse(adjacent.schemaLabel().undefined());
// force load
adjacent.forceLoad();
Assert.assertTrue("label: " + adjacent.schemaLabel(), adjacent.schemaLabel().undefined());
Assert.assertEquals("~undefined", adjacent.label());
vertices = graph.traversal().V(james.id()).outE().otherV().toList();
Assert.assertEquals(1, vertices.size());
adjacent = (HugeVertex) vertices.get(0);
Assert.assertTrue(adjacent.isPropLoaded());
Assert.assertTrue(adjacent.schemaLabel().undefined());
adjacent.forceLoad();
Assert.assertTrue(adjacent.schemaLabel().undefined());
Assert.assertEquals("~undefined", adjacent.label());
params().graphEventHub().notify(Events.CACHE, "clear", null).get();
vertices = graph.traversal().V(james.id()).outE().otherV().toList();
Assert.assertEquals(1, vertices.size());
adjacent = (HugeVertex) vertices.get(0);
Assert.assertFalse(adjacent.isPropLoaded());
Assert.assertFalse(adjacent.schemaLabel().undefined());
adjacent.forceLoad();
Assert.assertTrue(adjacent.schemaLabel().undefined());
Assert.assertEquals("~undefined", adjacent.label());
vertices = graph.traversal().V(james.id()).outE().has("score", 3).otherV().toList();
Assert.assertEquals(1, vertices.size());
adjacent = (HugeVertex) vertices.get(0);
adjacent.forceLoad();
// NOTE: if not commit, adjacent.label() will return 'book'
Assert.assertTrue(adjacent.schemaLabel().undefined());
Assert.assertEquals("~undefined", adjacent.label());
Assert.assertFalse(adjacent.properties().hasNext());
vertices = graph.traversal().V(james.id()).outE().has("score", 3).otherV().toList();
Assert.assertEquals(1, vertices.size());
adjacent = (HugeVertex) vertices.get(0);
Assert.assertTrue(adjacent.schemaLabel().undefined());
Assert.assertEquals("~undefined", adjacent.label());
Whitebox.setInternalState(params().graphTransaction(), "checkAdjacentVertexExist", true);
try {
Assert.assertThrows(HugeException.class, () -> {
// read from cache
graph.traversal().V(james.id()).outE().has("score", 3).otherV().values().toList();
}, e -> {
Assert.assertContains("Vertex 'java' does not exist", e.getMessage());
});
} finally {
Whitebox.setInternalState(params().graphTransaction(), "checkAdjacentVertexExist", false);
}
Whitebox.setInternalState(params().graphTransaction(), "checkAdjacentVertexExist", true);
params().graphEventHub().notify(Events.CACHE, "clear", null).get();
try {
Assert.assertEquals(0, graph.traversal().V(java).toList().size());
Assert.assertThrows(HugeException.class, () -> {
graph.traversal().V(james.id()).out().values().toList();
}, e -> {
Assert.assertContains("Vertex 'java' does not exist", e.getMessage());
});
Assert.assertThrows(HugeException.class, () -> {
graph.traversal().V(james.id()).outE().otherV().values().toList();
}, e -> {
Assert.assertContains("Vertex 'java' does not exist", e.getMessage());
});
Assert.assertThrows(HugeException.class, () -> {
Vertex v = graph.traversal().V(james.id()).outE().has("score", 3).otherV().next();
// throw
v.properties();
}, e -> {
Assert.assertContains("Vertex 'java' does not exist", e.getMessage());
});
Assert.assertThrows(HugeException.class, () -> {
Vertex v = graph.traversal().V(james.id()).outE().has("score", 3).otherV().next();
// throw
v.values();
}, e -> {
Assert.assertContains("Vertex 'java' does not exist", e.getMessage());
});
Assert.assertThrows(HugeException.class, () -> {
Vertex v = graph.traversal().V(james.id()).outE().has("score", 3).otherV().next();
// throw
((HugeVertex) v).forceLoad();
}, e -> {
Assert.assertContains("Vertex 'java' does not exist", e.getMessage());
});
} finally {
Whitebox.setInternalState(params().graphTransaction(), "checkAdjacentVertexExist", false);
}
}
use of com.baidu.hugegraph.structure.HugeVertex in project incubator-hugegraph by apache.
the class EdgeCoreTest method testQueryEdgesOfVertexWithoutCommit.
@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());
}
Aggregations