use of com.baidu.hugegraph.structure.HugeVertex in project incubator-hugegraph by apache.
the class EdgeAPI method newVertex.
private static Vertex newVertex(HugeGraph g, Object id, String label) {
VertexLabel vl = vertexLabel(g, label, "Invalid vertex label '%s'");
Id idValue = HugeVertex.getIdValue(id);
return new HugeVertex(g, idValue, vl);
}
use of com.baidu.hugegraph.structure.HugeVertex in project incubator-hugegraph by apache.
the class StandardHugeGraph method removeVertex.
@Override
public void removeVertex(String label, Object id) {
if (label != null) {
VertexLabel vl = this.vertexLabel(label);
// It's OK even if exist adjacent edges `vl.existsLinkLabel()`
if (!vl.existsIndexLabel()) {
// Improve perf by removeVertex(id)
Id idValue = HugeVertex.getIdValue(id);
HugeVertex vertex = new HugeVertex(this, idValue, vl);
this.removeVertex(vertex);
return;
}
}
this.vertex(id).remove();
}
use of com.baidu.hugegraph.structure.HugeVertex in project incubator-hugegraph by apache.
the class EntityManager method delete.
public T delete(Id id) {
T entity = null;
Iterator<Vertex> vertices = this.tx().queryVertices(id);
if (vertices.hasNext()) {
HugeVertex vertex = (HugeVertex) vertices.next();
entity = this.deser.apply(vertex);
this.tx().removeVertex(vertex);
this.commitOrRollback();
assert !vertices.hasNext();
}
return entity;
}
use of com.baidu.hugegraph.structure.HugeVertex in project incubator-hugegraph by apache.
the class RelationshipManager method save.
private Id save(T relationship, boolean expectExists) {
if (!this.graph().existsEdgeLabel(relationship.label())) {
throw new HugeException("Schema is missing for %s '%s'", relationship.label(), relationship.source());
}
HugeVertex source = this.newVertex(relationship.source(), relationship.sourceLabel());
HugeVertex target = this.newVertex(relationship.target(), relationship.targetLabel());
HugeEdge edge = source.constructEdge(relationship.label(), target, relationship.asArray());
E.checkArgument(this.exists(edge.id()) == expectExists, "Can't save %s '%s' that %s exists", this.unhideLabel(), edge.id(), expectExists ? "not" : "already");
this.tx().addEdge(edge);
this.commitOrRollback();
return edge.id();
}
use of com.baidu.hugegraph.structure.HugeVertex in project incubator-hugegraph by apache.
the class CachedGraphTransaction method commitMutation2Backend.
@Override
@Watched(prefix = "graphcache")
protected final void commitMutation2Backend(BackendMutation... mutations) {
// Collect changes before commit
Collection<HugeVertex> updates = this.verticesInTxUpdated();
Collection<HugeVertex> deletions = this.verticesInTxRemoved();
Id[] vertexIds = new Id[updates.size() + deletions.size()];
int vertexOffset = 0;
int edgesInTxSize = this.edgesInTxSize();
try {
super.commitMutation2Backend(mutations);
// Update vertex cache
if (this.enableCacheVertex()) {
for (HugeVertex vertex : updates) {
vertexIds[vertexOffset++] = vertex.id();
if (needCacheVertex(vertex)) {
// Update cache
this.verticesCache.updateIfPresent(vertex.id(), vertex);
} else {
// Skip large vertex
this.verticesCache.invalidate(vertex.id());
}
}
}
} finally {
// Update removed vertex in cache whatever success or fail
if (this.enableCacheVertex()) {
for (HugeVertex vertex : deletions) {
vertexIds[vertexOffset++] = vertex.id();
this.verticesCache.invalidate(vertex.id());
}
if (vertexOffset > 0) {
this.notifyChanges(Cache.ACTION_INVALIDED, HugeType.VERTEX, vertexIds);
}
}
/*
* Update edge cache if any vertex or edge changed
* For vertex change, the edges linked with should also be updated
* Before we find a more precise strategy, just clear all the edge cache now
*/
boolean invalidEdgesCache = (edgesInTxSize + updates.size() + deletions.size()) > 0;
if (invalidEdgesCache && this.enableCacheEdge()) {
// TODO: Use a more precise strategy to update the edge cache
this.edgesCache.clear();
this.notifyChanges(Cache.ACTION_CLEARED, HugeType.EDGE, null);
}
}
}
Aggregations