use of com.baidu.hugegraph.structure.HugeEdge in project incubator-hugegraph by apache.
the class GraphTransaction method queryEdgesByIds.
protected Iterator<Edge> queryEdgesByIds(Object[] edgeIds, boolean verifyId) {
Query.checkForceCapacity(edgeIds.length);
// NOTE: allowed duplicated edges if query by duplicated ids
List<Id> ids = InsertionOrderUtil.newList();
Map<Id, HugeEdge> edges = new HashMap<>(edgeIds.length);
IdQuery query = new IdQuery(HugeType.EDGE);
for (Object edgeId : edgeIds) {
HugeEdge edge;
EdgeId id = HugeEdge.getIdValue(edgeId, !verifyId);
if (id == null) {
continue;
}
if (id.direction() == Directions.IN) {
id = id.switchDirection();
}
if (this.removedEdges.containsKey(id)) {
// The record has been deleted
continue;
} else if ((edge = this.addedEdges.get(id)) != null || (edge = this.updatedEdges.get(id)) != null) {
if (edge.expired()) {
continue;
}
// Found from local tx
edges.put(edge.id(), edge);
} else {
// Prepare to query from backend store
query.query(id);
}
ids.add(id);
}
if (!query.empty()) {
// Query from backend store
if (edges.isEmpty() && query.idsSize() == ids.size()) {
/*
* Sort at the lower layer and return directly if there is no
* local vertex and duplicated id.
*/
Iterator<HugeEdge> it = this.queryEdgesFromBackend(query);
@SuppressWarnings({ "unchecked", "rawtypes" }) Iterator<Edge> r = (Iterator) it;
return r;
}
query.mustSortByInput(false);
Iterator<HugeEdge> it = this.queryEdgesFromBackend(query);
QueryResults.fillMap(it, edges);
}
return new MapperIterator<>(ids.iterator(), id -> {
Edge edge = edges.get(id);
return edge;
});
}
use of com.baidu.hugegraph.structure.HugeEdge in project incubator-hugegraph by apache.
the class RamTable method loadFromDB.
private void loadFromDB() throws Exception {
Query query = new Query(HugeType.VERTEX);
query.capacity(this.verticesCapacityHalf * 2L);
query.limit(Query.NO_LIMIT);
Iterator<Vertex> vertices = this.graph.vertices(query);
// switch concurrent loading here
boolean concurrent = true;
if (concurrent) {
try (LoadTraverser traverser = new LoadTraverser()) {
traverser.load(vertices);
}
return;
}
Iterator<Edge> adjEdges;
Id lastId = IdGenerator.ZERO;
while (vertices.hasNext()) {
Id vertex = (Id) vertices.next().id();
LOG.info("scan from hbase {} loadfromDB", vertex);
if (vertex.compareTo(lastId) < 0) {
throw new HugeException("The ramtable feature is not " + "supported by %s backend", this.graph.backend());
}
ensureNumberId(vertex);
lastId = vertex;
adjEdges = this.graph.adjacentEdges(vertex);
if (adjEdges.hasNext()) {
HugeEdge edge = (HugeEdge) adjEdges.next();
this.addEdge(true, edge);
}
while (adjEdges.hasNext()) {
HugeEdge edge = (HugeEdge) adjEdges.next();
this.addEdge(false, edge);
}
}
}
use of com.baidu.hugegraph.structure.HugeEdge in project incubator-hugegraph by apache.
the class FusiformSimilarityTraverser method matchMinNeighborCount.
private boolean matchMinNeighborCount(HugeVertex vertex, Directions direction, String label, int minNeighbors, long degree) {
Iterator<Edge> edges;
long neighborCount;
EdgeLabel edgeLabel = null;
Id labelId = null;
if (label != null) {
edgeLabel = this.graph().edgeLabel(label);
labelId = edgeLabel.id();
}
if (edgeLabel != null && edgeLabel.frequency() == Frequency.SINGLE) {
edges = this.edgesOfVertex(vertex.id(), direction, labelId, minNeighbors);
neighborCount = IteratorUtils.count(edges);
} else {
edges = this.edgesOfVertex(vertex.id(), direction, labelId, degree);
Set<Id> neighbors = newIdSet();
while (edges.hasNext()) {
Id target = ((HugeEdge) edges.next()).id().otherVertexId();
neighbors.add(target);
if (neighbors.size() >= minNeighbors) {
break;
}
}
neighborCount = neighbors.size();
}
return neighborCount >= minNeighbors;
}
use of com.baidu.hugegraph.structure.HugeEdge 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.HugeEdge in project incubator-hugegraph by apache.
the class PathTraverser method traverseOne.
private void traverseOne(Id v, EdgeStep step, boolean forward) {
if (this.reachLimit()) {
return;
}
Iterator<Edge> edges = this.traverser.edgesOfVertex(v, step);
while (edges.hasNext()) {
HugeEdge edge = (HugeEdge) edges.next();
Id target = edge.id().otherVertexId();
this.processOne(v, target, forward);
}
}
Aggregations