Search in sources :

Example 1 with Decompress

use of com.baidu.hugegraph.api.filter.DecompressInterceptor.Decompress in project incubator-hugegraph by apache.

the class EdgeAPI method create.

@POST
@Timed(name = "batch-create")
@Decompress
@Path("batch")
@Status(Status.CREATED)
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed({ "admin", "$owner=$graph $action=edge_write" })
public String create(@Context HugeConfig config, @Context GraphManager manager, @PathParam("graph") String graph, @QueryParam("check_vertex") @DefaultValue("true") boolean checkVertex, List<JsonEdge> jsonEdges) {
    LOG.debug("Graph [{}] create edges: {}", graph, jsonEdges);
    checkCreatingBody(jsonEdges);
    checkBatchSize(config, jsonEdges);
    HugeGraph g = graph(manager, graph);
    TriFunction<HugeGraph, Object, String, Vertex> getVertex = checkVertex ? EdgeAPI::getVertex : EdgeAPI::newVertex;
    return this.commit(config, g, jsonEdges.size(), () -> {
        List<Id> ids = new ArrayList<>(jsonEdges.size());
        for (JsonEdge jsonEdge : jsonEdges) {
            /*
                 * NOTE: If the query param 'checkVertex' is false,
                 * then the label is correct and not matched id,
                 * it will be allowed currently
                 */
            Vertex srcVertex = getVertex.apply(g, jsonEdge.source, jsonEdge.sourceLabel);
            Vertex tgtVertex = getVertex.apply(g, jsonEdge.target, jsonEdge.targetLabel);
            Edge edge = srcVertex.addEdge(jsonEdge.label, tgtVertex, jsonEdge.properties());
            ids.add((Id) edge.id());
        }
        return manager.serializer(g).writeIds(ids);
    });
}
Also used : HugeVertex(com.baidu.hugegraph.structure.HugeVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) HugeGraph(com.baidu.hugegraph.HugeGraph) ArrayList(java.util.ArrayList) Id(com.baidu.hugegraph.backend.id.Id) EdgeId(com.baidu.hugegraph.backend.id.EdgeId) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge) Path(jakarta.ws.rs.Path) Status(com.baidu.hugegraph.api.filter.StatusFilter.Status) RolesAllowed(jakarta.annotation.security.RolesAllowed) POST(jakarta.ws.rs.POST) Consumes(jakarta.ws.rs.Consumes) Produces(jakarta.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) Decompress(com.baidu.hugegraph.api.filter.DecompressInterceptor.Decompress)

Example 2 with Decompress

use of com.baidu.hugegraph.api.filter.DecompressInterceptor.Decompress in project incubator-hugegraph by apache.

the class VertexAPI method create.

@POST
@Timed(name = "batch-create")
@Decompress
@Path("batch")
@Status(Status.CREATED)
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed({ "admin", "$owner=$graph $action=vertex_write" })
public String create(@Context HugeConfig config, @Context GraphManager manager, @PathParam("graph") String graph, List<JsonVertex> jsonVertices) {
    LOG.debug("Graph [{}] create vertices: {}", graph, jsonVertices);
    checkCreatingBody(jsonVertices);
    checkBatchSize(config, jsonVertices);
    HugeGraph g = graph(manager, graph);
    return this.commit(config, g, jsonVertices.size(), () -> {
        List<Id> ids = new ArrayList<>(jsonVertices.size());
        for (JsonVertex vertex : jsonVertices) {
            ids.add((Id) g.addVertex(vertex.properties()).id());
        }
        return manager.serializer(g).writeIds(ids);
    });
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) ArrayList(java.util.ArrayList) Id(com.baidu.hugegraph.backend.id.Id) Path(jakarta.ws.rs.Path) Status(com.baidu.hugegraph.api.filter.StatusFilter.Status) RolesAllowed(jakarta.annotation.security.RolesAllowed) POST(jakarta.ws.rs.POST) Consumes(jakarta.ws.rs.Consumes) Produces(jakarta.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) Decompress(com.baidu.hugegraph.api.filter.DecompressInterceptor.Decompress)

Example 3 with Decompress

use of com.baidu.hugegraph.api.filter.DecompressInterceptor.Decompress in project incubator-hugegraph by apache.

the class EdgeAPI method update.

/**
 * Batch update steps are same like vertices
 */
@PUT
@Timed(name = "batch-update")
@Decompress
@Path("batch")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed({ "admin", "$owner=$graph $action=edge_write" })
public String update(@Context HugeConfig config, @Context GraphManager manager, @PathParam("graph") String graph, BatchEdgeRequest req) {
    BatchEdgeRequest.checkUpdate(req);
    LOG.debug("Graph [{}] update edges: {}", graph, req);
    checkUpdatingBody(req.jsonEdges);
    checkBatchSize(config, req.jsonEdges);
    HugeGraph g = graph(manager, graph);
    Map<Id, JsonEdge> map = new HashMap<>(req.jsonEdges.size());
    TriFunction<HugeGraph, Object, String, Vertex> getVertex = req.checkVertex ? EdgeAPI::getVertex : EdgeAPI::newVertex;
    return this.commit(config, g, map.size(), () -> {
        // 1.Put all newEdges' properties into map (combine first)
        req.jsonEdges.forEach(newEdge -> {
            Id newEdgeId = getEdgeId(graph(manager, graph), newEdge);
            JsonEdge oldEdge = map.get(newEdgeId);
            this.updateExistElement(oldEdge, newEdge, req.updateStrategies);
            map.put(newEdgeId, newEdge);
        });
        // 2.Get all oldEdges and update with new ones
        Object[] ids = map.keySet().toArray();
        Iterator<Edge> oldEdges = g.edges(ids);
        oldEdges.forEachRemaining(oldEdge -> {
            JsonEdge newEdge = map.get(oldEdge.id());
            this.updateExistElement(g, oldEdge, newEdge, req.updateStrategies);
        });
        // 3.Add all finalEdges
        List<Edge> edges = new ArrayList<>(map.size());
        map.values().forEach(finalEdge -> {
            Vertex srcVertex = getVertex.apply(g, finalEdge.source, finalEdge.sourceLabel);
            Vertex tgtVertex = getVertex.apply(g, finalEdge.target, finalEdge.targetLabel);
            edges.add(srcVertex.addEdge(finalEdge.label, tgtVertex, finalEdge.properties()));
        });
        // If return ids, the ids.size() maybe different with the origins'
        return manager.serializer(g).writeEdges(edges.iterator(), false);
    });
}
Also used : HugeVertex(com.baidu.hugegraph.structure.HugeVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) HugeGraph(com.baidu.hugegraph.HugeGraph) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Id(com.baidu.hugegraph.backend.id.Id) EdgeId(com.baidu.hugegraph.backend.id.EdgeId) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge) Path(jakarta.ws.rs.Path) RolesAllowed(jakarta.annotation.security.RolesAllowed) Consumes(jakarta.ws.rs.Consumes) Produces(jakarta.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) Decompress(com.baidu.hugegraph.api.filter.DecompressInterceptor.Decompress) PUT(jakarta.ws.rs.PUT)

Example 4 with Decompress

use of com.baidu.hugegraph.api.filter.DecompressInterceptor.Decompress in project incubator-hugegraph by apache.

the class VertexAPI method update.

/**
 * Batch update steps like:
 * 1. Get all newVertices' ID & combine first
 * 2. Get all oldVertices & update
 * 3. Add the final vertex together
 */
@PUT
@Timed(name = "batch-update")
@Decompress
@Path("batch")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed({ "admin", "$owner=$graph $action=vertex_write" })
public String update(@Context HugeConfig config, @Context GraphManager manager, @PathParam("graph") String graph, BatchVertexRequest req) {
    BatchVertexRequest.checkUpdate(req);
    LOG.debug("Graph [{}] update vertices: {}", graph, req);
    checkUpdatingBody(req.jsonVertices);
    checkBatchSize(config, req.jsonVertices);
    HugeGraph g = graph(manager, graph);
    Map<Id, JsonVertex> map = new HashMap<>(req.jsonVertices.size());
    return this.commit(config, g, map.size(), () -> {
        /*
             * 1.Put all newVertices' properties into map (combine first)
             * - Consider primary-key & user-define ID mode first
             */
        req.jsonVertices.forEach(newVertex -> {
            Id newVertexId = getVertexId(g, newVertex);
            JsonVertex oldVertex = map.get(newVertexId);
            this.updateExistElement(oldVertex, newVertex, req.updateStrategies);
            map.put(newVertexId, newVertex);
        });
        // 2.Get all oldVertices and update with new vertices
        Object[] ids = map.keySet().toArray();
        Iterator<Vertex> oldVertices = g.vertices(ids);
        oldVertices.forEachRemaining(oldVertex -> {
            JsonVertex newVertex = map.get(oldVertex.id());
            this.updateExistElement(g, oldVertex, newVertex, req.updateStrategies);
        });
        // 3.Add finalVertices and return them
        List<Vertex> vertices = new ArrayList<>(map.size());
        map.values().forEach(finalVertex -> {
            vertices.add(g.addVertex(finalVertex.properties()));
        });
        // If return ids, the ids.size() maybe different with the origins'
        return manager.serializer(g).writeVertices(vertices.iterator(), false);
    });
}
Also used : HugeVertex(com.baidu.hugegraph.structure.HugeVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) HugeGraph(com.baidu.hugegraph.HugeGraph) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Id(com.baidu.hugegraph.backend.id.Id) Path(jakarta.ws.rs.Path) RolesAllowed(jakarta.annotation.security.RolesAllowed) Consumes(jakarta.ws.rs.Consumes) Produces(jakarta.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) Decompress(com.baidu.hugegraph.api.filter.DecompressInterceptor.Decompress) PUT(jakarta.ws.rs.PUT)

Aggregations

HugeGraph (com.baidu.hugegraph.HugeGraph)4 Decompress (com.baidu.hugegraph.api.filter.DecompressInterceptor.Decompress)4 Id (com.baidu.hugegraph.backend.id.Id)4 Timed (com.codahale.metrics.annotation.Timed)4 RolesAllowed (jakarta.annotation.security.RolesAllowed)4 Consumes (jakarta.ws.rs.Consumes)4 Path (jakarta.ws.rs.Path)4 Produces (jakarta.ws.rs.Produces)4 ArrayList (java.util.ArrayList)4 HugeVertex (com.baidu.hugegraph.structure.HugeVertex)3 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)3 Status (com.baidu.hugegraph.api.filter.StatusFilter.Status)2 EdgeId (com.baidu.hugegraph.backend.id.EdgeId)2 HugeEdge (com.baidu.hugegraph.structure.HugeEdge)2 POST (jakarta.ws.rs.POST)2 PUT (jakarta.ws.rs.PUT)2 HashMap (java.util.HashMap)2 Edge (org.apache.tinkerpop.gremlin.structure.Edge)2