use of com.baidu.hugegraph.structure.HugeVertex in project incubator-hugegraph by apache.
the class GraphTransaction method constructVertex.
@Watched(prefix = "graph")
public HugeVertex constructVertex(boolean verifyVL, Object... keyValues) {
HugeElement.ElementKeys elemKeys = HugeElement.classifyKeys(keyValues);
if (possibleOlapVertex(elemKeys)) {
Id id = HugeVertex.getIdValue(elemKeys.id());
HugeVertex vertex = HugeVertex.create(this, id, VertexLabel.OLAP_VL);
ElementHelper.attachProperties(vertex, keyValues);
Iterator<HugeProperty<?>> iterator = vertex.getProperties().iterator();
assert iterator.hasNext();
if (iterator.next().propertyKey().olap()) {
return vertex;
}
}
VertexLabel vertexLabel = this.checkVertexLabel(elemKeys.label(), verifyVL);
Id id = HugeVertex.getIdValue(elemKeys.id());
List<Id> keys = this.graph().mapPkName2Id(elemKeys.keys());
// Check whether id match with id strategy
this.checkId(id, keys, vertexLabel);
// Create HugeVertex
HugeVertex vertex = HugeVertex.create(this, null, vertexLabel);
// Set properties
ElementHelper.attachProperties(vertex, keyValues);
// Assign vertex id
if (this.params().mode().maintaining() && vertexLabel.idStrategy() == IdStrategy.AUTOMATIC) {
// Resume id for AUTOMATIC id strategy in restoring mode
vertex.assignId(id, true);
} else {
vertex.assignId(id);
}
return vertex;
}
use of com.baidu.hugegraph.structure.HugeVertex in project incubator-hugegraph by apache.
the class GraphTransaction method removeVertexProperty.
@Watched(prefix = "graph")
public <V> void removeVertexProperty(HugeVertexProperty<V> prop) {
HugeVertex vertex = prop.element();
PropertyKey propKey = prop.propertyKey();
E.checkState(vertex != null, "No owner for removing property '%s'", prop.key());
// Maybe have ever been removed (compatible with tinkerpop)
if (!vertex.hasProperty(propKey.id())) {
// PropertyTest shouldAllowRemovalFromVertexWhenAlreadyRemoved()
return;
}
// Check is removing primary key
List<Id> primaryKeyIds = vertex.schemaLabel().primaryKeys();
E.checkArgument(!primaryKeyIds.contains(propKey.id()), "Can't remove primary key '%s'", prop.key());
// Remove property in memory for new created vertex
if (vertex.fresh()) {
// The owner will do property update
vertex.removeProperty(propKey.id());
return;
}
// Check is updating property of added/removed vertex
E.checkArgument(!this.addedVertices.containsKey(vertex.id()) || this.updatedVertices.containsKey(vertex.id()), "Can't remove property '%s' for adding-state vertex", prop.key());
E.checkArgument(!this.removedVertices.containsKey(vertex.id()), "Can't remove property '%s' for removing-state vertex", prop.key());
// Do property update
this.lockForUpdateProperty(vertex.schemaLabel(), prop, () -> {
// Update old vertex to remove index (with the property)
this.indexTx.updateVertexIndex(vertex, true);
// Update(remove) vertex property
HugeProperty<?> removed = vertex.removeProperty(propKey.id());
this.propertyUpdated(vertex, null, removed);
});
}
use of com.baidu.hugegraph.structure.HugeVertex in project incubator-hugegraph by apache.
the class FusiformSimilarityTraverser method fusiformSimilarityForVertex.
private Set<Similar> fusiformSimilarityForVertex(HugeVertex vertex, Directions direction, String label, int minNeighbors, double alpha, int minSimilars, int top, String groupProperty, int minGroups, long degree, long capacity, boolean withIntermediary) {
boolean matched = this.matchMinNeighborCount(vertex, direction, label, minNeighbors, degree);
if (!matched) {
// Ignore current vertex if its neighbors number is not enough
return ImmutableSet.of();
}
Id labelId = this.getEdgeLabelId(label);
// Get similar nodes and counts
Iterator<Edge> edges = this.edgesOfVertex(vertex.id(), direction, labelId, degree);
Map<Id, MutableInt> similars = newMap();
MultivaluedMap<Id, Id> intermediaries = new MultivaluedHashMap<>();
Set<Id> neighbors = newIdSet();
while (edges.hasNext()) {
Id target = ((HugeEdge) edges.next()).id().otherVertexId();
if (neighbors.contains(target)) {
continue;
}
neighbors.add(target);
checkCapacity(capacity, ++this.accessed, "fusiform similarity");
Directions backDir = direction.opposite();
Iterator<Edge> backEdges = this.edgesOfVertex(target, backDir, labelId, degree);
Set<Id> currentSimilars = newIdSet();
while (backEdges.hasNext()) {
Id node = ((HugeEdge) backEdges.next()).id().otherVertexId();
if (currentSimilars.contains(node)) {
continue;
}
currentSimilars.add(node);
if (withIntermediary) {
intermediaries.add(node, target);
}
MutableInt count = similars.get(node);
if (count == null) {
count = new MutableInt(0);
similars.put(node, count);
checkCapacity(capacity, ++this.accessed, "fusiform similarity");
}
count.increment();
}
}
// Delete source vertex
assert similars.containsKey(vertex.id());
similars.remove(vertex.id());
if (similars.isEmpty()) {
return ImmutableSet.of();
}
// Match alpha
double neighborNum = neighbors.size();
Map<Id, Double> matchedAlpha = newMap();
for (Map.Entry<Id, MutableInt> entry : similars.entrySet()) {
double score = entry.getValue().intValue() / neighborNum;
if (score >= alpha) {
matchedAlpha.put(entry.getKey(), score);
}
}
if (matchedAlpha.size() < minSimilars) {
return ImmutableSet.of();
}
// Sorted and topN if needed
Map<Id, Double> topN;
if (top > 0) {
topN = topN(matchedAlpha, true, top);
} else {
topN = matchedAlpha;
}
// Filter by groupCount by property
if (groupProperty != null) {
Set<Object> values = newSet();
// Add groupProperty value of source vertex
values.add(vertex.value(groupProperty));
for (Id id : topN.keySet()) {
Vertex v = graph().vertices(id).next();
values.add(v.value(groupProperty));
}
if (values.size() < minGroups) {
return ImmutableSet.of();
}
}
// Construct result
Set<Similar> result = InsertionOrderUtil.newSet();
for (Map.Entry<Id, Double> entry : topN.entrySet()) {
Id similar = entry.getKey();
double score = entry.getValue();
List<Id> inters = withIntermediary ? intermediaries.get(similar) : ImmutableList.of();
result.add(new Similar(similar, score, inters));
}
return result;
}
use of com.baidu.hugegraph.structure.HugeVertex in project incubator-hugegraph by apache.
the class ServerInfoManager method save.
private Id save(HugeServerInfo serverInfo) {
return this.call(() -> {
// Construct vertex from server info
HugeServerInfo.Schema schema = HugeServerInfo.schema(this.graph);
if (!schema.existVertexLabel(HugeServerInfo.P.SERVER)) {
throw new HugeException("Schema is missing for %s '%s'", HugeServerInfo.P.SERVER, serverInfo);
}
HugeVertex vertex = this.tx().constructVertex(false, serverInfo.asArray());
// Add or update server info in backend store
vertex = this.tx().addVertex(vertex);
return vertex.id();
});
}
use of com.baidu.hugegraph.structure.HugeVertex in project incubator-hugegraph by apache.
the class ServerInfoManager method save.
private int save(Collection<HugeServerInfo> serverInfos) {
return this.call(() -> {
if (serverInfos.isEmpty()) {
return serverInfos.size();
}
HugeServerInfo.Schema schema = HugeServerInfo.schema(this.graph);
if (!schema.existVertexLabel(HugeServerInfo.P.SERVER)) {
throw new HugeException("Schema is missing for %s", HugeServerInfo.P.SERVER);
}
// Save server info in batch
GraphTransaction tx = this.tx();
int updated = 0;
for (HugeServerInfo server : serverInfos) {
if (!server.updated()) {
continue;
}
HugeVertex vertex = tx.constructVertex(false, server.asArray());
tx.addVertex(vertex);
updated++;
}
// NOTE: actually it is auto-commit, to be improved
tx.commitOrRollback();
return updated;
});
}
Aggregations