Search in sources :

Example 6 with ElementId

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

the class RemoteRpcConnector method queryVertices.

public void queryVertices(Map<Integer, Map<ElementId, Integer>> classified, GraphSchema schema, TinkerMaxGraph graph, Context context, int batchSize, List<Object> resultList, boolean vertexCacheFlag, RpcProcessorType rpcProcessorType, Map<CompositeId, Map<String, Object>> existPropMap) {
    List<RpcAddress> remoteAddressList = addressFetcher.getAddressList();
    if (classified.size() > remoteAddressList.size()) {
        throw new IllegalArgumentException("classified=>" + classified.keySet() + " count > remote address list=>" + remoteAddressList.toString());
    }
    int classifiedCount = classified.size();
    CountDownLatch latch = new CountDownLatch(classifiedCount);
    ExceptionHolder exceptionHolder = new ExceptionHolder();
    List<RemoteRpcProcessor> remoteRpcProcessorList = Lists.newArrayListWithCapacity(classifiedCount);
    for (Map.Entry<Integer, Map<ElementId, Integer>> entry : classified.entrySet()) {
        if (entry.getKey() < 0) {
            throw new RuntimeException("Invalid key in " + classified + " when get detail of vertex");
        }
        RpcAddress rpcAddress = remoteAddressList.get(entry.getKey());
        LOG.info("Get rpc address " + rpcAddress + " for store id " + entry.getKey());
        try {
            ManagedChannel managedChannel = channels.get(rpcAddress);
            GremlinServiceGrpc.GremlinServiceStub gremlinServiceStub = GremlinServiceGrpc.newStub(managedChannel);
            GremlinQuery.VertexRequest.Builder reqBuilder = GremlinQuery.VertexRequest.newBuilder();
            Map<ElementId, Integer> vertexCountList = entry.getValue();
            for (ElementId elementId : vertexCountList.keySet()) {
                reqBuilder.addIds(GremlinQuery.VertexId.newBuilder().setId(elementId.id()).setTypeId(elementId.typeId()).build());
            }
            RemoteRpcProcessor remoteRpcProcessor = createRemoteRpcProcessor(rpcProcessorType, context, batchSize);
            remoteRpcProcessorList.add(remoteRpcProcessor);
            VertexStreamObserver vertexStreamObserver = new VertexStreamObserver(remoteRpcProcessor, schema, graph, exceptionHolder, latch, vertexCountList, vertexCacheFlag, existPropMap);
            gremlinServiceStub.getVertexs(reqBuilder.build(), vertexStreamObserver);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
    }
    try {
        if (!latch.await(rpcConfig.getQueryTimeoutSec(), TimeUnit.SECONDS)) {
            throw new RuntimeException("Wait query vertex complete time out");
        }
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
    if (exceptionHolder.getException() != null) {
        LOG.error("query vertex from store service fail", exceptionHolder.getException());
        throw new RuntimeException(exceptionHolder.getException());
    }
    if (null != resultList) {
        for (RemoteRpcProcessor remoteRpcProcessor : remoteRpcProcessorList) {
            resultList.addAll(remoteRpcProcessor.getResultList());
        }
    }
}
Also used : ExceptionHolder(com.alibaba.maxgraph.sdk.exception.ExceptionHolder) CountDownLatch(java.util.concurrent.CountDownLatch) RpcAddress(com.alibaba.maxgraph.common.rpc.RpcAddress) ManagedChannel(io.grpc.ManagedChannel) ExecutionException(java.util.concurrent.ExecutionException) Map(java.util.Map) ElementId(com.alibaba.maxgraph.sdkcommon.graph.ElementId) GremlinServiceGrpc(com.alibaba.maxgraph.proto.GremlinServiceGrpc)

Example 7 with ElementId

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

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

the class VertexStreamObserver method onCompleted.

@Override
public void onCompleted() {
    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);
            }
            for (int i = 0; i < entry.getValue(); i++) {
                resultProcessor.process(vertex);
            }
        }
    }
    resultProcessor.finish();
    latch.countDown();
}
Also used : MxVertex(com.alibaba.maxgraph.structure.MxVertex) Vertex(com.alibaba.maxgraph.structure.Vertex) MxVertex(com.alibaba.maxgraph.structure.MxVertex) Map(java.util.Map) 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