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);
}
}
}
}
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");
}
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);
}
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));
}
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]));
}
}
Aggregations