use of com.baidu.hugegraph.structure.HugeVertex in project incubator-hugegraph by apache.
the class CachedGraphTransaction method queryVerticesByIds.
@Watched(prefix = "graphcache")
private Iterator<HugeVertex> queryVerticesByIds(IdQuery query) {
if (query.idsSize() == 1) {
Id vertexId = query.ids().iterator().next();
HugeVertex vertex = (HugeVertex) this.verticesCache.get(vertexId);
if (vertex != null) {
if (!vertex.expired()) {
return QueryResults.iterator(vertex);
}
this.verticesCache.invalidate(vertexId);
}
Iterator<HugeVertex> rs = super.queryVerticesFromBackend(query);
vertex = QueryResults.one(rs);
if (vertex == null) {
return QueryResults.emptyIterator();
}
if (needCacheVertex(vertex)) {
this.verticesCache.update(vertex.id(), vertex);
}
return QueryResults.iterator(vertex);
}
IdQuery newQuery = new IdQuery(HugeType.VERTEX, query);
List<HugeVertex> vertices = new ArrayList<>();
for (Id vertexId : query.ids()) {
HugeVertex vertex = (HugeVertex) this.verticesCache.get(vertexId);
if (vertex == null) {
newQuery.query(vertexId);
} else if (vertex.expired()) {
newQuery.query(vertexId);
this.verticesCache.invalidate(vertexId);
} else {
vertices.add(vertex);
}
}
// Join results from cache and backend
ExtendableIterator<HugeVertex> results = new ExtendableIterator<>();
if (!vertices.isEmpty()) {
results.extend(vertices.iterator());
} else {
// Just use the origin query if find none from the cache
newQuery = query;
}
if (!newQuery.empty()) {
Iterator<HugeVertex> rs = super.queryVerticesFromBackend(newQuery);
// Generally there are not too much data with id query
ListIterator<HugeVertex> listIterator = QueryResults.toList(rs);
for (HugeVertex vertex : listIterator.list()) {
// Skip large vertex
if (needCacheVertex(vertex)) {
this.verticesCache.update(vertex.id(), vertex);
}
}
results.extend(listIterator);
}
return results;
}
use of com.baidu.hugegraph.structure.HugeVertex in project incubator-hugegraph by apache.
the class EntityManager method save.
private Id save(T entity, boolean expectExists) {
// Construct vertex from task
HugeVertex vertex = this.constructVertex(entity);
E.checkArgument(this.exists(vertex.id()) == expectExists, "Can't save %s '%s' that %s exists", this.unhideLabel(), vertex.id(), expectExists ? "not" : "already");
// Add or update user in backend store, stale index might exist
vertex = this.tx().addVertex(vertex);
this.commitOrRollback();
return vertex.id();
}
use of com.baidu.hugegraph.structure.HugeVertex in project incubator-hugegraph by apache.
the class ServerInfoManager method removeServerInfo.
private HugeServerInfo removeServerInfo(Id server) {
if (server == null) {
return null;
}
LOG.info("Remove server info: {}", server);
return this.call(() -> {
Iterator<Vertex> vertices = this.tx().queryVertices(server);
Vertex vertex = QueryResults.one(vertices);
if (vertex == null) {
return null;
}
this.tx().removeVertex((HugeVertex) vertex);
return HugeServerInfo.fromVertex(vertex);
});
}
use of com.baidu.hugegraph.structure.HugeVertex in project incubator-hugegraph by apache.
the class CustomizePathsTraverser method customizedPaths.
public List<Path> customizedPaths(Iterator<Vertex> vertices, List<WeightedEdgeStep> steps, boolean sorted, long capacity, long limit) {
E.checkArgument(vertices.hasNext(), "The source vertices can't be empty");
E.checkArgument(!steps.isEmpty(), "The steps can't be empty");
checkCapacity(capacity);
checkLimit(limit);
MultivaluedMap<Id, Node> sources = newMultivalueMap();
while (vertices.hasNext()) {
HugeVertex vertex = (HugeVertex) vertices.next();
Node node = sorted ? new WeightNode(vertex.id(), null, 0) : new Node(vertex.id(), null);
sources.add(vertex.id(), node);
}
int stepNum = steps.size();
int pathCount = 0;
long access = 0;
MultivaluedMap<Id, Node> newVertices = null;
root: for (WeightedEdgeStep step : steps) {
stepNum--;
newVertices = newMultivalueMap();
Iterator<Edge> edges;
// Traversal vertices of previous level
for (Map.Entry<Id, List<Node>> entry : sources.entrySet()) {
List<Node> adjacency = newList();
edges = this.edgesOfVertex(entry.getKey(), step.step());
while (edges.hasNext()) {
HugeEdge edge = (HugeEdge) edges.next();
Id target = edge.id().otherVertexId();
for (Node n : entry.getValue()) {
// If have loop, skip target
if (n.contains(target)) {
continue;
}
Node newNode;
if (sorted) {
double w = step.weightBy() != null ? edge.value(step.weightBy().name()) : step.defaultWeight();
newNode = new WeightNode(target, n, w);
} else {
newNode = new Node(target, n);
}
adjacency.add(newNode);
checkCapacity(capacity, ++access, "customized paths");
}
}
if (step.sample() > 0) {
// Sample current node's adjacent nodes
adjacency = sample(adjacency, step.sample());
}
// Add current node's adjacent nodes
for (Node node : adjacency) {
newVertices.add(node.id(), node);
// Avoid exceeding limit
if (stepNum == 0) {
if (limit != NO_LIMIT && !sorted && ++pathCount >= limit) {
break root;
}
}
}
}
// Re-init sources
sources = newVertices;
}
if (stepNum != 0) {
return ImmutableList.of();
}
List<Path> paths = newList();
for (List<Node> nodes : newVertices.values()) {
for (Node n : nodes) {
if (sorted) {
WeightNode wn = (WeightNode) n;
paths.add(new WeightPath(wn.path(), wn.weights()));
} else {
paths.add(new Path(n.path()));
}
}
}
return paths;
}
use of com.baidu.hugegraph.structure.HugeVertex in project incubator-hugegraph by apache.
the class FusiformSimilarityTraverser method fusiformSimilarity.
public SimilarsMap fusiformSimilarity(Iterator<Vertex> vertices, Directions direction, String label, int minNeighbors, double alpha, int minSimilars, int top, String groupProperty, int minGroups, long degree, long capacity, long limit, boolean withIntermediary) {
checkCapacity(capacity);
checkLimit(limit);
checkGroupArgs(groupProperty, minGroups);
int foundCount = 0;
SimilarsMap results = new SimilarsMap();
while (vertices.hasNext()) {
checkCapacity(capacity, ++this.accessed, "fusiform similarity");
HugeVertex vertex = (HugeVertex) vertices.next();
// Find fusiform similarity for current vertex
Set<Similar> result = this.fusiformSimilarityForVertex(vertex, direction, label, minNeighbors, alpha, minSimilars, top, groupProperty, minGroups, degree, capacity, withIntermediary);
if (result.isEmpty()) {
continue;
}
results.put(vertex.id(), result);
// Reach limit
if (limit != NO_LIMIT && ++foundCount >= limit) {
break;
}
}
this.reset();
return results;
}
Aggregations