use of com.baidu.hugegraph.structure.HugeEdge in project incubator-hugegraph by apache.
the class GraphTransaction method joinTxEdges.
private Iterator<?> joinTxEdges(Query query, Iterator<HugeEdge> edges, Map<Id, HugeVertex> removingVertices) {
assert query.resultType().isEdge();
BiFunction<Query, HugeEdge, HugeEdge> matchTxFunc = (q, e) -> {
assert q.resultType() == HugeType.EDGE;
if (e.expired() && !q.showExpired()) {
// Filter expired edges with TTL
return null;
}
// Filter edges matched conditions
return q.test(e) ? e : q.test(e = e.switchOwner()) ? e : null;
};
edges = this.joinTxRecords(query, edges, matchTxFunc, this.addedEdges, this.removedEdges, this.updatedEdges);
if (removingVertices.isEmpty()) {
return edges;
}
// Filter edges that belong to deleted vertex
return new FilterIterator<HugeEdge>(edges, edge -> {
for (HugeVertex v : removingVertices.values()) {
if (edge.belongToVertex(v)) {
return false;
}
}
return true;
});
}
use of com.baidu.hugegraph.structure.HugeEdge in project incubator-hugegraph by apache.
the class GraphTransaction method prepareDeletions.
protected void prepareDeletions(Map<Id, HugeVertex> removedVertices, Map<Id, HugeEdge> removedEdges) {
// Remove related edges of each vertex
for (HugeVertex v : removedVertices.values()) {
if (!v.schemaLabel().existsLinkLabel()) {
continue;
}
// Query all edges of the vertex and remove them
Query query = constructEdgesQuery(v.id(), Directions.BOTH);
Iterator<HugeEdge> vedges = this.queryEdgesFromBackend(query);
try {
while (vedges.hasNext()) {
this.checkTxEdgesCapacity();
HugeEdge edge = vedges.next();
// NOTE: will change the input parameter
removedEdges.put(edge.id(), edge);
// Commit first if enabled commit-part mode
if (this.commitPartOfAdjacentEdges > 0 && removedEdges.size() >= this.commitPartOfAdjacentEdges) {
this.commitPartOfEdgeDeletions(removedEdges);
}
}
} finally {
CloseableIterator.closeIterator(vedges);
}
}
// Remove vertices
for (HugeVertex v : removedVertices.values()) {
this.checkAggregateProperty(v);
/*
* If the backend stores vertex together with edges, it's edges
* would be removed after removing vertex. Otherwise, if the
* backend stores vertex which is separated from edges, it's
* edges should be removed manually when removing vertex.
*/
this.doRemove(this.serializer.writeVertex(v.prepareRemoved()));
this.indexTx.updateVertexIndex(v, true);
this.indexTx.updateLabelIndex(v, true);
}
// Remove edges
this.prepareDeletions(removedEdges);
}
use of com.baidu.hugegraph.structure.HugeEdge 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.HugeEdge in project incubator-hugegraph by apache.
the class EdgeCoreTest method testQueryAdjacentVerticesOfEdges.
@Test
public void testQueryAdjacentVerticesOfEdges() {
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", CollectionFactory.newIntObjectMap());
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());
}
use of com.baidu.hugegraph.structure.HugeEdge in project incubator-hugegraph by apache.
the class EdgeAPI method update.
@PUT
@Timed(name = "single-update")
@Path("{id}")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed({ "admin", "$owner=$graph $action=edge_write" })
public String update(@Context GraphManager manager, @PathParam("graph") String graph, @PathParam("id") String id, @QueryParam("action") String action, JsonEdge jsonEdge) {
LOG.debug("Graph [{}] update edge: {}", graph, jsonEdge);
checkUpdatingBody(jsonEdge);
if (jsonEdge.id != null) {
E.checkArgument(id.equals(jsonEdge.id), "The ids are different between url and " + "request body ('%s' != '%s')", id, jsonEdge.id);
}
// Parse action param
boolean append = checkAndParseAction(action);
HugeGraph g = graph(manager, graph);
HugeEdge edge = (HugeEdge) g.edge(id);
EdgeLabel edgeLabel = edge.schemaLabel();
for (String key : jsonEdge.properties.keySet()) {
PropertyKey pkey = g.propertyKey(key);
E.checkArgument(edgeLabel.properties().contains(pkey.id()), "Can't update property for edge '%s' because " + "there is no property key '%s' in its edge label", id, key);
}
commit(g, () -> updateProperties(edge, jsonEdge, append));
return manager.serializer(g).writeEdge(edge);
}
Aggregations