Search in sources :

Example 1 with HugeVertex

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;
}
Also used : ExtendableIterator(com.baidu.hugegraph.iterator.ExtendableIterator) ArrayList(java.util.ArrayList) IdQuery(com.baidu.hugegraph.backend.query.IdQuery) QueryId(com.baidu.hugegraph.backend.cache.CachedBackendStore.QueryId) Id(com.baidu.hugegraph.backend.id.Id) HugeVertex(com.baidu.hugegraph.structure.HugeVertex) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched)

Example 2 with HugeVertex

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();
}
Also used : HugeVertex(com.baidu.hugegraph.structure.HugeVertex)

Example 3 with HugeVertex

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);
    });
}
Also used : HugeVertex(com.baidu.hugegraph.structure.HugeVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex)

Example 4 with HugeVertex

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;
}
Also used : HugeEdge(com.baidu.hugegraph.structure.HugeEdge) HugeVertex(com.baidu.hugegraph.structure.HugeVertex) WeightedEdgeStep(com.baidu.hugegraph.traversal.algorithm.steps.WeightedEdgeStep) Iterator(java.util.Iterator) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Id(com.baidu.hugegraph.backend.id.Id)

Example 5 with HugeVertex

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;
}
Also used : HugeVertex(com.baidu.hugegraph.structure.HugeVertex)

Aggregations

HugeVertex (com.baidu.hugegraph.structure.HugeVertex)58 Id (com.baidu.hugegraph.backend.id.Id)27 HugeEdge (com.baidu.hugegraph.structure.HugeEdge)26 VertexLabel (com.baidu.hugegraph.schema.VertexLabel)18 EdgeId (com.baidu.hugegraph.backend.id.EdgeId)16 Test (org.junit.Test)16 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)11 BaseUnitTest (com.baidu.hugegraph.unit.BaseUnitTest)10 FakeObjects (com.baidu.hugegraph.unit.FakeObjects)9 HugeGraph (com.baidu.hugegraph.HugeGraph)8 BackendEntry (com.baidu.hugegraph.backend.store.BackendEntry)8 PropertyKey (com.baidu.hugegraph.schema.PropertyKey)8 Watched (com.baidu.hugegraph.perf.PerfUtil.Watched)7 EdgeLabel (com.baidu.hugegraph.schema.EdgeLabel)7 Edge (org.apache.tinkerpop.gremlin.structure.Edge)7 IdQuery (com.baidu.hugegraph.backend.query.IdQuery)6 HugeException (com.baidu.hugegraph.HugeException)5 HugeConfig (com.baidu.hugegraph.config.HugeConfig)5 HugeProperty (com.baidu.hugegraph.structure.HugeProperty)4 ConditionQuery (com.baidu.hugegraph.backend.query.ConditionQuery)3