Search in sources :

Example 1 with CompositeId

use of com.alibaba.maxgraph.sdkcommon.graph.CompositeId 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 CompositeId

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

the class VertexStreamObserver method onNext.

@Override
public void onNext(GremlinQuery.VertexResponse vertexResponse) {
    GremlinQuery.VertexId id = vertexResponse.getId();
    CompositeId rId = new CompositeId(id.getId(), id.getTypeId());
    GraphElement type = schema.getElement(id.getTypeId());
    Map<String, Object> properties = null;
    try {
        properties = RpcProcessorUtils.deserializeProperty(vertexResponse.getPros().toByteArray(), type, schema);
    } catch (Exception e) {
        throw new RuntimeException("query properties for vertex=>" + rId.toString() + " fail", e);
    }
    Map<String, Object> existProp = existPropMap.get(rId);
    if (null != existProp) {
        if (properties == null) {
            properties = Maps.newHashMap();
        }
        properties.putAll(existProp);
    }
    MxVertex vertex = new MxVertex(new Vertex(rId, type.getLabel(), properties, this.graph.getBaseGraph()), this.graph);
    if (vertexCacheFlag) {
        vertexCache.put(rId, vertex);
    }
    int count = vertexCountList.remove(rId);
    if (count > 1) {
        for (int i = 0; i < count; i++) {
            resultProcessor.process(vertex);
        }
    } else {
        resultProcessor.process(vertex);
    }
}
Also used : MxVertex(com.alibaba.maxgraph.structure.MxVertex) Vertex(com.alibaba.maxgraph.structure.Vertex) MxVertex(com.alibaba.maxgraph.structure.MxVertex) GraphElement(com.alibaba.maxgraph.compiler.api.schema.GraphElement) CompositeId(com.alibaba.maxgraph.sdkcommon.graph.CompositeId) GremlinQuery(com.alibaba.maxgraph.proto.GremlinQuery)

Example 3 with CompositeId

use of com.alibaba.maxgraph.sdkcommon.graph.CompositeId 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)

Example 4 with CompositeId

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

the class GremlinResultTransform method parseResultValue.

private Object parseResultValue(QueryResult queryResult, GraphSchema schema, Graph graph, Map<Integer, String> labelIndexNameList, Context context, int batchSize, String queryId) {
    if (queryResult instanceof VertexPropertyResult || queryResult instanceof PropertyResult) {
        return queryResult;
    } else if (queryResult instanceof MapValueResult) {
        Map<Object, Object> resultMap = Maps.newLinkedHashMap();
        for (Map.Entry<QueryResult, QueryResult> entry : ((MapValueResult) queryResult).getValueMap().entrySet()) {
            resultMap.put(parseResultValue(entry.getKey(), schema, graph, labelIndexNameList, context, batchSize, queryId), parseResultValue(entry.getValue(), schema, graph, labelIndexNameList, context, batchSize, queryId));
        }
        return resultMap;
    } else if (queryResult instanceof EdgeResult) {
        return DetachedFactory.detach((Edge) queryResult, true);
    } else if (queryResult instanceof ListResult) {
        List<QueryResult> resultList = ((ListResult) queryResult).getResultList();
        if (resultList.isEmpty()) {
            return Lists.newArrayList();
        }
        if (resultList.get(0) instanceof PathValueResult) {
            Path path = MutablePath.make();
            resultList.forEach(v -> {
                PathValueResult pathValueResult = PathValueResult.class.cast(v);
                Set<String> labelNameList = Sets.newHashSet();
                pathValueResult.getLabelIdList().forEach(labelId -> labelNameList.add(labelIndexNameList.get(labelId)));
                path.extend(parseResultValue(pathValueResult.getValue(), schema, graph, labelIndexNameList, context, batchSize, queryId), labelNameList);
            });
            return DetachedFactory.detach(path, true);
        } else {
            List<Object> listValue = Lists.newArrayList();
            resultList.forEach(v -> listValue.add(parseResultValue(v, schema, graph, labelIndexNameList, context, batchSize, queryId)));
            return listValue;
        }
    } else if (queryResult instanceof EntryValueResult) {
        Map<Object, Object> mapValue = Maps.newHashMap();
        mapValue.put(parseResultValue(((EntryValueResult) queryResult).getKey(), schema, graph, labelIndexNameList, context, batchSize, queryId), parseResultValue(((EntryValueResult) queryResult).getValue(), schema, graph, labelIndexNameList, context, batchSize, queryId));
        return mapValue.entrySet().iterator().next();
    } else if (queryResult instanceof PropertyValueResult) {
        return ((PropertyValueResult) queryResult).getValue();
    } else if (queryResult instanceof VertexResult) {
        VertexResult vertexResult = (VertexResult) queryResult;
        CompositeId compositeId = CompositeId.class.cast(vertexResult.id());
        org.apache.tinkerpop.gremlin.structure.Vertex cachedVertex = vertexCache.getIfPresent(compositeId);
        if (null == cachedVertex) {
            List<Object> resultList = Lists.newArrayList();
            Map<ElementId, Integer> elementIdIntegerMap = Maps.newHashMap();
            elementIdIntegerMap.put(compositeId, 1);
            Map<CompositeId, Map<String, Object>> existPropMap = Maps.newHashMap();
            extractExistProp(existPropMap, vertexResult, compositeId);
            Map<Integer, Set<ElementId>> storeVertexList = Maps.newHashMap();
            storeVertexList.put(vertexResult.getStoreId(), Sets.newHashSet(compositeId));
            queryVertices(schema, context, batchSize, resultList, elementIdIntegerMap, storeVertexList, existPropMap, RpcProcessorType.MEMORY, queryId);
            cachedVertex = org.apache.tinkerpop.gremlin.structure.Vertex.class.cast(resultList.get(0));
        }
        return cachedVertex;
    } else {
        return queryResult;
    }
}
Also used : GraphSchema(com.alibaba.maxgraph.compiler.api.schema.GraphSchema) ElementId(com.alibaba.maxgraph.sdkcommon.graph.ElementId) RpcProcessorType(com.alibaba.maxgraph.server.query.RpcProcessorType) CacheFactory(com.alibaba.maxgraph.cache.CacheFactory) TinkerMaxGraph(com.alibaba.maxgraph.structure.graph.TinkerMaxGraph) CompositeId(com.alibaba.maxgraph.sdkcommon.graph.CompositeId) Graph(org.apache.tinkerpop.gremlin.structure.Graph) MxVertex(com.alibaba.maxgraph.structure.MxVertex) LoggerFactory(org.slf4j.LoggerFactory) Vertex(com.alibaba.maxgraph.structure.Vertex) PropertyResult(com.alibaba.maxgraph.result.PropertyResult) Lists(com.google.common.collect.Lists) RemoteRpcProcessor(com.alibaba.maxgraph.server.query.RemoteRpcProcessor) Map(java.util.Map) VertexResult(com.alibaba.maxgraph.result.VertexResult) ListResult(com.alibaba.maxgraph.result.ListResult) RemoteRpcConnector(com.alibaba.maxgraph.server.query.RemoteRpcConnector) Path(org.apache.tinkerpop.gremlin.process.traversal.Path) EdgeResult(com.alibaba.maxgraph.result.EdgeResult) Edge(org.apache.tinkerpop.gremlin.structure.Edge) PathValueResult(com.alibaba.maxgraph.result.PathValueResult) PropertyValueResult(com.alibaba.maxgraph.result.PropertyValueResult) MapValueResult(com.alibaba.maxgraph.result.MapValueResult) Logger(org.slf4j.Logger) Set(java.util.Set) Maps(com.google.common.collect.Maps) Sets(com.google.common.collect.Sets) List(java.util.List) VertexPropertyResult(com.alibaba.maxgraph.result.VertexPropertyResult) QueryResult(com.alibaba.maxgraph.sdkcommon.graph.QueryResult) ValueType(com.alibaba.maxgraph.compiler.tree.value.ValueType) ResponseStatusCode(org.apache.tinkerpop.gremlin.driver.message.ResponseStatusCode) Cache(com.google.common.cache.Cache) Context(org.apache.tinkerpop.gremlin.server.Context) DetachedFactory(org.apache.tinkerpop.gremlin.structure.util.detached.DetachedFactory) EntryValueResult(com.alibaba.maxgraph.sdkcommon.graph.EntryValueResult) MutablePath(org.apache.tinkerpop.gremlin.process.traversal.step.util.MutablePath) MxVertex(com.alibaba.maxgraph.structure.MxVertex) Vertex(com.alibaba.maxgraph.structure.Vertex) Set(java.util.Set) CompositeId(com.alibaba.maxgraph.sdkcommon.graph.CompositeId) PropertyResult(com.alibaba.maxgraph.result.PropertyResult) VertexPropertyResult(com.alibaba.maxgraph.result.VertexPropertyResult) VertexResult(com.alibaba.maxgraph.result.VertexResult) MapValueResult(com.alibaba.maxgraph.result.MapValueResult) List(java.util.List) EntryValueResult(com.alibaba.maxgraph.sdkcommon.graph.EntryValueResult) ElementId(com.alibaba.maxgraph.sdkcommon.graph.ElementId) VertexPropertyResult(com.alibaba.maxgraph.result.VertexPropertyResult) Path(org.apache.tinkerpop.gremlin.process.traversal.Path) MutablePath(org.apache.tinkerpop.gremlin.process.traversal.step.util.MutablePath) PropertyValueResult(com.alibaba.maxgraph.result.PropertyValueResult) ListResult(com.alibaba.maxgraph.result.ListResult) PathValueResult(com.alibaba.maxgraph.result.PathValueResult) Map(java.util.Map) EdgeResult(com.alibaba.maxgraph.result.EdgeResult)

Example 5 with CompositeId

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

the class EdgeResponseFunction method apply.

@Override
public Edge apply(StoreApi.GraphEdgeReponse edgeReponse) {
    CompositeId eid = new CompositeId(edgeReponse.getEdgeId(), edgeReponse.getTypeId());
    GraphElement element = schema.getElement(eid.typeId());
    String label = element.getLabel();
    Map<String, Object> properties = RpcProcessorUtils.deserializeProperty(edgeReponse.getPros().toByteArray(), element, schema);
    GremlinQuery.VertexId srcId = edgeReponse.getSrcId();
    GremlinQuery.VertexId dstId = edgeReponse.getDstId();
    Iterator<Vertex> vertexIterator = graph.getVertex(Sets.newHashSet(new CompositeId(srcId.getId(), srcId.getTypeId()), new CompositeId(dstId.getId(), dstId.getTypeId())));
    Vertex srcVertex = null, dstVertex = null;
    while (vertexIterator.hasNext()) {
        Vertex vertex = vertexIterator.next();
        if (vertex.id.id() == srcId.getId()) {
            srcVertex = vertex;
        }
        if (vertex.id.id() == dstId.getId()) {
            dstVertex = vertex;
        }
    }
    if (null == srcVertex) {
        try {
            GraphElement graphElement = schema.getElement(srcId.getTypeId());
            srcVertex = new Vertex(new CompositeId(srcId.getId(), srcId.getTypeId()), graphElement.getLabel(), Maps.newHashMap(), graph);
        } catch (Exception ignored) {
            srcVertex = new Vertex(new CompositeId(srcId.getId(), srcId.getTypeId()), "", Maps.newHashMap(), graph);
        }
    }
    if (null == dstVertex) {
        try {
            GraphElement graphElement = schema.getElement(dstId.getTypeId());
            dstVertex = new Vertex(new CompositeId(dstId.getId(), dstId.getTypeId()), graphElement.getLabel(), Maps.newHashMap(), graph);
        } catch (Exception ignored) {
            dstVertex = new Vertex(new CompositeId(dstId.getId(), dstId.getTypeId()), "", Maps.newHashMap(), graph);
        }
    }
    return new Edge(eid, label, properties, srcVertex, dstVertex, this.graph);
}
Also used : Vertex(com.alibaba.maxgraph.structure.Vertex) GraphElement(com.alibaba.maxgraph.compiler.api.schema.GraphElement) CompositeId(com.alibaba.maxgraph.sdkcommon.graph.CompositeId) GremlinQuery(com.alibaba.maxgraph.proto.GremlinQuery) Edge(com.alibaba.maxgraph.structure.Edge)

Aggregations

CompositeId (com.alibaba.maxgraph.sdkcommon.graph.CompositeId)6 Vertex (com.alibaba.maxgraph.structure.Vertex)5 MxVertex (com.alibaba.maxgraph.structure.MxVertex)4 GraphElement (com.alibaba.maxgraph.compiler.api.schema.GraphElement)3 GremlinQuery (com.alibaba.maxgraph.proto.GremlinQuery)3 ElementId (com.alibaba.maxgraph.sdkcommon.graph.ElementId)3 VertexResult (com.alibaba.maxgraph.result.VertexResult)2 QueryResult (com.alibaba.maxgraph.sdkcommon.graph.QueryResult)2 Map (java.util.Map)2 Set (java.util.Set)2 CacheFactory (com.alibaba.maxgraph.cache.CacheFactory)1 GraphSchema (com.alibaba.maxgraph.compiler.api.schema.GraphSchema)1 GraphVertex (com.alibaba.maxgraph.compiler.api.schema.GraphVertex)1 ValueType (com.alibaba.maxgraph.compiler.tree.value.ValueType)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 PropertyValueResult (com.alibaba.maxgraph.result.PropertyValueResult)1