Search in sources :

Example 1 with ElementId

use of com.alibaba.maxgraph.sdkcommon.graph.ElementId in project GraphScope by alibaba.

the class GremlinResultTransform method transform.

public void transform(List<QueryResult> resultObjectList, GraphSchema schema, Map<Integer, String> labelIndexNameList, Context context, int batchSize, List<Object> resultList, String queryId) {
    Map<ElementId, Integer> vertexCountList = Maps.newHashMap();
    List<CompositeId> resultVertexIdList = Lists.newArrayListWithCapacity(resultObjectList.size());
    Map<CompositeId, org.apache.tinkerpop.gremlin.structure.Vertex> idToVertexList = Maps.newHashMap();
    Map<CompositeId, Map<String, Object>> existPropMap = Maps.newHashMap();
    Map<Integer, Set<ElementId>> storeVertexList = Maps.newHashMap();
    for (int i = 0; i < resultObjectList.size(); i++) {
        QueryResult queryResult = resultObjectList.get(i);
        if (queryResult instanceof VertexResult) {
            VertexResult vertexResult = VertexResult.class.cast(queryResult);
            CompositeId compositeId = new CompositeId(vertexResult.id, schema.getElement(vertexResult.label).getLabelId());
            org.apache.tinkerpop.gremlin.structure.Vertex cachedVertex = vertexCache.getIfPresent(compositeId);
            if (null != cachedVertex) {
                resultVertexIdList.add(compositeId);
                idToVertexList.put(compositeId, cachedVertex);
            } else {
                resultVertexIdList.add(compositeId);
                vertexCountList.compute(compositeId, (key, oldValue) -> {
                    if (null == oldValue) {
                        return 1;
                    } else {
                        return oldValue + 1;
                    }
                });
                Set<ElementId> storeVertices = storeVertexList.computeIfAbsent(vertexResult.getStoreId(), k -> Sets.newHashSet());
                storeVertices.add(compositeId);
                extractExistProp(existPropMap, vertexResult, compositeId);
            }
        } else {
            Object result = parseResultValue(queryResult, schema, this.graph, labelIndexNameList, context, batchSize, queryId);
            if (null != resultList) {
                resultList.add(result);
            }
            remoteRpcProcessor.process(result);
        }
    }
    if (!vertexCountList.isEmpty()) {
        List<Object> currResultList = Lists.newArrayListWithCapacity(vertexCountList.size());
        queryVertices(schema, context, batchSize, currResultList, vertexCountList, storeVertexList, existPropMap, RpcProcessorType.MEMORY, queryId);
        for (Object currResult : currResultList) {
            org.apache.tinkerpop.gremlin.structure.Vertex currVertex = org.apache.tinkerpop.gremlin.structure.Vertex.class.cast(currResult);
            CompositeId vertexId = CompositeId.class.cast(currVertex.id());
            idToVertexList.put(vertexId, currVertex);
        }
    }
    if (!resultVertexIdList.isEmpty()) {
        for (CompositeId compositeId : resultVertexIdList) {
            org.apache.tinkerpop.gremlin.structure.Vertex resultVertex = idToVertexList.get(compositeId);
            remoteRpcProcessor.process(resultVertex);
            if (null != resultList) {
                resultList.add(resultVertex);
            }
        }
    }
}
Also used : MxVertex(com.alibaba.maxgraph.structure.MxVertex) Vertex(com.alibaba.maxgraph.structure.Vertex) Set(java.util.Set) CompositeId(com.alibaba.maxgraph.sdkcommon.graph.CompositeId) QueryResult(com.alibaba.maxgraph.sdkcommon.graph.QueryResult) VertexResult(com.alibaba.maxgraph.result.VertexResult) Map(java.util.Map) ElementId(com.alibaba.maxgraph.sdkcommon.graph.ElementId)

Example 2 with ElementId

use of com.alibaba.maxgraph.sdkcommon.graph.ElementId in project GraphScope by alibaba.

the class GremlinResultTransform method queryVertices.

private void queryVertices(GraphSchema schema, Context context, int batchSize, List<Object> resultList, Map<ElementId, Integer> vertexCountList, Map<Integer, Set<ElementId>> classified, Map<CompositeId, Map<String, Object>> existPropMap, RpcProcessorType rpcProcessorType, String queryId) {
    logger.info("Start to fetch detail for vertex in query " + queryId);
    Map<Integer, Map<ElementId, Integer>> classifiedList = Maps.newHashMap();
    if (classified.size() == 1) {
        int key = Lists.newArrayList(classified.keySet()).get(0);
        classifiedList.put(key, vertexCountList);
    } else {
        for (Map.Entry<Integer, Set<ElementId>> entry : classified.entrySet()) {
            Map<ElementId, Integer> classifiedCountList = Maps.newHashMap();
            for (ElementId elementId : entry.getValue()) {
                classifiedCountList.put(elementId, vertexCountList.remove(elementId));
            }
            classifiedList.put(entry.getKey(), classifiedCountList);
        }
    }
    if (null == rpcProcessorType) {
        remoteRpcConnector.queryVertices(classifiedList, schema, graph, context, batchSize, resultList, vertexCacheFlag, existPropMap);
    } else {
        remoteRpcConnector.queryVertices(classifiedList, schema, graph, context, batchSize, resultList, vertexCacheFlag, rpcProcessorType, existPropMap);
    }
    if (!vertexCountList.isEmpty()) {
        for (Map.Entry<ElementId, Integer> entry : vertexCountList.entrySet()) {
            ElementId elementId = entry.getKey();
            MxVertex vertex = new MxVertex(new Vertex(elementId, schema.getElement(elementId.typeId()).getLabel(), Maps.newHashMap(), this.graph.getBaseGraph()), this.graph);
            if (vertexCacheFlag) {
                vertexCache.put(elementId, vertex);
            }
            if (null != resultList) {
                resultList.add(vertex);
            }
            if (rpcProcessorType != RpcProcessorType.MEMORY) {
                remoteRpcProcessor.process(vertex);
            }
        }
    }
    logger.info("Fetch detail for vertex in query " + queryId + " finish");
}
Also used : MxVertex(com.alibaba.maxgraph.structure.MxVertex) Vertex(com.alibaba.maxgraph.structure.Vertex) Set(java.util.Set) MxVertex(com.alibaba.maxgraph.structure.MxVertex) Map(java.util.Map) ElementId(com.alibaba.maxgraph.sdkcommon.graph.ElementId)

Example 3 with ElementId

use of com.alibaba.maxgraph.sdkcommon.graph.ElementId in project GraphScope by alibaba.

the class MaxGraphRecordProcessorManager method getEdgeSrcDstPair.

private Pair<com.alibaba.maxgraph.structure.Vertex, com.alibaba.maxgraph.structure.Vertex> getEdgeSrcDstPair(AbstractEdgeManager edgeManager) {
    ElementId srcId = edgeManager.getSrcId();
    ElementId dstId = edgeManager.getDstId();
    Iterator<com.alibaba.maxgraph.structure.Vertex> vertexList = this.graph.getBaseGraph().getVertex(Sets.newHashSet(srcId, dstId));
    com.alibaba.maxgraph.structure.Vertex srcVertex = null, dstVertex = null;
    while (vertexList.hasNext()) {
        com.alibaba.maxgraph.structure.Vertex vertex = vertexList.next();
        if (vertex.id.id() == srcId.id()) {
            srcVertex = vertex;
        }
        if (vertex.id.id() == dstId.id()) {
            dstVertex = vertex;
        }
    }
    if (srcVertex == null || dstVertex == null) {
        throw new IllegalArgumentException("cant get src or dst vertex for srcId[" + srcId + "] and dstId[" + dstId + "]");
    }
    return Pair.of(srcVertex, dstVertex);
}
Also used : MxVertex(com.alibaba.maxgraph.structure.MxVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) ElementId(com.alibaba.maxgraph.sdkcommon.graph.ElementId)

Example 4 with ElementId

use of com.alibaba.maxgraph.sdkcommon.graph.ElementId in project GraphScope by alibaba.

the class TinkerMaxGraph method vertices.

@Override
public Iterator<Vertex> vertices(Object... ids) {
    Iterator<com.alibaba.maxgraph.structure.Vertex> vertex;
    if (null == ids || ids.length == 0) {
        vertex = this.getBaseGraph().getVertex();
    } else {
        Set<ElementId> idSet = Sets.newHashSet();
        for (Object id : ids) {
            idSet.add(parseCompositeId(id));
        }
        if (idSet.isEmpty()) {
            List<Vertex> emptyList = Lists.newArrayList();
            return emptyList.iterator();
        }
        vertex = this.getBaseGraph().getVertex(idSet);
    }
    return Iterators.transform(vertex, v -> new MxVertex(v, this));
}
Also used : GraphVertex(com.alibaba.maxgraph.compiler.api.schema.GraphVertex) MxVertex(com.alibaba.maxgraph.structure.MxVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) MxVertex(com.alibaba.maxgraph.structure.MxVertex) ElementId(com.alibaba.maxgraph.sdkcommon.graph.ElementId)

Example 5 with ElementId

use of com.alibaba.maxgraph.sdkcommon.graph.ElementId in project GraphScope by alibaba.

the class TinkerMaxGraph method parseCompositeId.

private CompositeId parseCompositeId(Object id) {
    if (id instanceof Long) {
        return new CompositeId((long) id, 0);
    } else if (id instanceof ElementId) {
        return new CompositeId(((ElementId) id).id(), ((ElementId) id).typeId());
    } else if (id instanceof Vertex) {
        return (CompositeId) ((Vertex) id).id();
    } else {
        String[] idValueList = StringUtils.split(StringUtils.removeEnd(StringUtils.removeStart(id.toString(), "v["), "]"), ".");
        checkArgument(idValueList.length == 2, "Invalid vertex id format labelId.vertexId");
        return new CompositeId(Long.parseLong(idValueList[1]), Integer.parseInt(idValueList[0]));
    }
}
Also used : GraphVertex(com.alibaba.maxgraph.compiler.api.schema.GraphVertex) MxVertex(com.alibaba.maxgraph.structure.MxVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) CompositeId(com.alibaba.maxgraph.sdkcommon.graph.CompositeId) ElementId(com.alibaba.maxgraph.sdkcommon.graph.ElementId)

Aggregations

ElementId (com.alibaba.maxgraph.sdkcommon.graph.ElementId)8 MxVertex (com.alibaba.maxgraph.structure.MxVertex)7 Map (java.util.Map)5 Vertex (com.alibaba.maxgraph.structure.Vertex)4 CompositeId (com.alibaba.maxgraph.sdkcommon.graph.CompositeId)3 Set (java.util.Set)3 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)3 GraphVertex (com.alibaba.maxgraph.compiler.api.schema.GraphVertex)2 VertexResult (com.alibaba.maxgraph.result.VertexResult)2 QueryResult (com.alibaba.maxgraph.sdkcommon.graph.QueryResult)2 CacheFactory (com.alibaba.maxgraph.cache.CacheFactory)1 RpcAddress (com.alibaba.maxgraph.common.rpc.RpcAddress)1 GraphSchema (com.alibaba.maxgraph.compiler.api.schema.GraphSchema)1 ValueType (com.alibaba.maxgraph.compiler.tree.value.ValueType)1 GremlinServiceGrpc (com.alibaba.maxgraph.proto.GremlinServiceGrpc)1 EdgeResult (com.alibaba.maxgraph.result.EdgeResult)1 ListResult (com.alibaba.maxgraph.result.ListResult)1 MapValueResult (com.alibaba.maxgraph.result.MapValueResult)1 PathValueResult (com.alibaba.maxgraph.result.PathValueResult)1 PropertyResult (com.alibaba.maxgraph.result.PropertyResult)1