Search in sources :

Example 1 with Id

use of com.baidu.hugegraph.backend.id.Id in project incubator-hugegraph by apache.

the class StandardAuthManager method createProject.

@Override
public Id createProject(HugeProject project) {
    E.checkArgument(!StringUtils.isEmpty(project.name()), "The name of project can't be null or empty");
    return commit(() -> {
        // Create project admin group
        if (project.adminGroupId() == null) {
            HugeGroup adminGroup = new HugeGroup("admin_" + project.name());
            /*
                 * "creator" is a necessary parameter, other places are passed
                 * in "AuthManagerProxy", but here is the underlying module, so
                 * pass it directly here
                 */
            adminGroup.creator(project.creator());
            Id adminGroupId = this.createGroup(adminGroup);
            project.adminGroupId(adminGroupId);
        }
        // Create project op group
        if (project.opGroupId() == null) {
            HugeGroup opGroup = new HugeGroup("op_" + project.name());
            // Ditto
            opGroup.creator(project.creator());
            Id opGroupId = this.createGroup(opGroup);
            project.opGroupId(opGroupId);
        }
        // Create project target to verify permission
        final String targetName = "project_res_" + project.name();
        HugeResource resource = new HugeResource(ResourceType.PROJECT, project.name(), null);
        HugeTarget target = new HugeTarget(targetName, this.graph.name(), "localhost:8080", ImmutableList.of(resource));
        // Ditto
        target.creator(project.creator());
        Id targetId = this.targets.add(target);
        project.targetId(targetId);
        Id adminGroupId = project.adminGroupId();
        Id opGroupId = project.opGroupId();
        HugeAccess adminGroupWriteAccess = new HugeAccess(adminGroupId, targetId, HugePermission.WRITE);
        // Ditto
        adminGroupWriteAccess.creator(project.creator());
        HugeAccess adminGroupReadAccess = new HugeAccess(adminGroupId, targetId, HugePermission.READ);
        // Ditto
        adminGroupReadAccess.creator(project.creator());
        HugeAccess opGroupReadAccess = new HugeAccess(opGroupId, targetId, HugePermission.READ);
        // Ditto
        opGroupReadAccess.creator(project.creator());
        this.access.add(adminGroupWriteAccess);
        this.access.add(adminGroupReadAccess);
        this.access.add(opGroupReadAccess);
        return this.project.add(project);
    });
}
Also used : Id(com.baidu.hugegraph.backend.id.Id)

Example 2 with Id

use of com.baidu.hugegraph.backend.id.Id in project incubator-hugegraph by apache.

the class StandardAuthManager method findUser.

@Override
public HugeUser findUser(String name) {
    Id username = IdGenerator.of(name);
    HugeUser user = this.usersCache.get(username);
    if (user != null) {
        return user;
    }
    List<HugeUser> users = this.users.query(P.NAME, name, 2L);
    if (users.size() > 0) {
        assert users.size() == 1;
        user = users.get(0);
        this.usersCache.update(username, user);
    }
    return user;
}
Also used : Id(com.baidu.hugegraph.backend.id.Id)

Example 3 with Id

use of com.baidu.hugegraph.backend.id.Id in project incubator-hugegraph by apache.

the class CachedGraphTransaction method queryEdgesFromBackend.

@Override
@Watched(prefix = "graphcache")
protected final Iterator<HugeEdge> queryEdgesFromBackend(Query query) {
    RamTable ramtable = this.params().ramtable();
    if (ramtable != null && ramtable.matched(query)) {
        return ramtable.query(query);
    }
    if (!this.enableCacheEdge() || query.empty() || query.paging() || query.bigCapacity()) {
        // Query all edges or query edges in paging, don't cache it
        return super.queryEdgesFromBackend(query);
    }
    Id cacheKey = new QueryId(query);
    Object value = this.edgesCache.get(cacheKey);
    @SuppressWarnings("unchecked") Collection<HugeEdge> edges = (Collection<HugeEdge>) value;
    if (value != null) {
        for (HugeEdge edge : edges) {
            if (edge.expired()) {
                this.edgesCache.invalidate(cacheKey);
                value = null;
            }
        }
    }
    if (value != null) {
        // Not cached or the cache expired
        return edges.iterator();
    }
    Iterator<HugeEdge> rs = super.queryEdgesFromBackend(query);
    /*
         * Iterator can't be cached, caching list instead
         * there may be super node and too many edges in a query,
         * try fetch a few of the head results and determine whether to cache.
         */
    final int tryMax = 1 + MAX_CACHE_EDGES_PER_QUERY;
    assert tryMax > MAX_CACHE_EDGES_PER_QUERY;
    edges = new ArrayList<>(tryMax);
    for (int i = 0; rs.hasNext() && i < tryMax; i++) {
        edges.add(rs.next());
    }
    if (edges.size() == 0) {
        this.edgesCache.update(cacheKey, Collections.emptyList());
    } else if (edges.size() <= MAX_CACHE_EDGES_PER_QUERY) {
        this.edgesCache.update(cacheKey, edges);
    }
    return new ExtendableIterator<>(edges.iterator(), rs);
}
Also used : ExtendableIterator(com.baidu.hugegraph.iterator.ExtendableIterator) QueryId(com.baidu.hugegraph.backend.cache.CachedBackendStore.QueryId) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) Collection(java.util.Collection) RamTable(com.baidu.hugegraph.backend.store.ram.RamTable) QueryId(com.baidu.hugegraph.backend.cache.CachedBackendStore.QueryId) Id(com.baidu.hugegraph.backend.id.Id) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched)

Example 4 with Id

use of com.baidu.hugegraph.backend.id.Id 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 5 with Id

use of com.baidu.hugegraph.backend.id.Id in project incubator-hugegraph by apache.

the class HugeGraph method mapElId2Name.

default List<String> mapElId2Name(Collection<Id> ids) {
    List<String> names = new ArrayList<>(ids.size());
    for (Id id : ids) {
        SchemaElement schema = this.edgeLabel(id);
        names.add(schema.name());
    }
    return names;
}
Also used : ArrayList(java.util.ArrayList) SchemaElement(com.baidu.hugegraph.schema.SchemaElement) Id(com.baidu.hugegraph.backend.id.Id)

Aggregations

Id (com.baidu.hugegraph.backend.id.Id)381 Test (org.junit.Test)124 HugeGraph (com.baidu.hugegraph.HugeGraph)104 EdgeId (com.baidu.hugegraph.backend.id.EdgeId)71 BaseUnitTest (com.baidu.hugegraph.unit.BaseUnitTest)54 Timed (com.codahale.metrics.annotation.Timed)41 Produces (jakarta.ws.rs.Produces)40 AuthManager (com.baidu.hugegraph.auth.AuthManager)39 HugeVertex (com.baidu.hugegraph.structure.HugeVertex)37 ArrayList (java.util.ArrayList)35 HugeEdge (com.baidu.hugegraph.structure.HugeEdge)33 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)33 Watched (com.baidu.hugegraph.perf.PerfUtil.Watched)32 PropertyKey (com.baidu.hugegraph.schema.PropertyKey)32 Directions (com.baidu.hugegraph.type.define.Directions)29 ConditionQuery (com.baidu.hugegraph.backend.query.ConditionQuery)25 IndexLabel (com.baidu.hugegraph.schema.IndexLabel)25 VertexLabel (com.baidu.hugegraph.schema.VertexLabel)24 Edge (org.apache.tinkerpop.gremlin.structure.Edge)22 HugeType (com.baidu.hugegraph.type.HugeType)21