public static AdjacencyListField GetReverseAdjacencyListOfVertex(GraphViewConnection connection, string vertexId) { AdjacencyListField result = new AdjacencyListField(); string query = $"SELECT {{\"edge\": edge, " + $"\"_srcV\": doc.id, " + $"\"_srcVLabel\": doc.label}} AS incomingEdgeMetadata\n" + $"FROM doc\n" + $"JOIN edge IN doc._edge\n" + $"WHERE edge._sinkV = '{vertexId}'\n"; foreach (JObject edgeDocument in connection.ExecuteQuery(query)) { JObject edgeMetadata = (JObject)edgeDocument["incomingEdgeMetadata"]; JObject edgeObject = (JObject)edgeMetadata["edge"]; string srcV = edgeMetadata["_srcV"].ToString(); string srcVLabel = edgeMetadata["_srcVLabel"]?.ToString(); EdgeField edgeField = EdgeField.ConstructForwardEdgeField(srcV, srcVLabel, null, edgeObject); edgeField.EdgeProperties.Add("_srcV", new EdgePropertyField("_srcV", srcV, JsonDataType.String, edgeField)); if (srcVLabel != null) { edgeField.EdgeProperties.Add("_srcVLabel", new EdgePropertyField("_srcVLabel", srcVLabel, JsonDataType.String, edgeField)); } result.AddEdgeField(srcV, (long)edgeObject["_offset"], edgeField); } return(result); }
public static Dictionary <string, AdjacencyListField> GetReverseAdjacencyListsOfVertexCollection(GraphViewConnection connection, HashSet <string> vertexIdSet) { Dictionary <string, AdjacencyListField> revAdjacencyListCollection = new Dictionary <string, AdjacencyListField>(); StringBuilder vertexIdList = new StringBuilder(); foreach (string vertexId in vertexIdSet) { if (vertexIdList.Length > 0) { vertexIdList.Append(", "); } vertexIdList.AppendFormat("'{0}'", vertexId); revAdjacencyListCollection[vertexId] = new AdjacencyListField(); } string query = $"SELECT {{\"edge\": edge, " + $"\"vertexId\": edge._sinkV, " + $"\"_srcV\": doc.id, " + $"\"_srcVLabel\": doc.label}} AS incomingEdgeMetadata\n" + $"FROM doc\n" + $"JOIN edge IN doc._edge\n" + $"WHERE edge._sinkV IN ({vertexIdList.ToString()})\n"; foreach (JObject edgeDocument in connection.ExecuteQuery(query)) { JObject edgeMetadata = (JObject)edgeDocument["incomingEdgeMetadata"]; JObject edgeObject = (JObject)edgeMetadata["edge"]; string vertexId = edgeMetadata["vertexId"].ToString(); string srcV = edgeMetadata["_srcV"].ToString(); string srcVLabel = edgeMetadata["_srcVLabel"]?.ToString(); EdgeField edgeField = EdgeField.ConstructForwardEdgeField(srcV, srcVLabel, null, edgeObject); edgeField.EdgeProperties.Add("_srcV", new EdgePropertyField("_srcV", srcV, JsonDataType.String, edgeField)); if (srcVLabel != null) { edgeField.EdgeProperties.Add("_srcVLabel", new EdgePropertyField("_srcVLabel", srcVLabel, JsonDataType.String, edgeField)); } AdjacencyListField revAdjList = revAdjacencyListCollection[vertexId]; revAdjList.AddEdgeField(srcV, (long)edgeObject["_offset"], edgeField); } return(revAdjacencyListCollection); }
private static Dictionary <string, string> GetLabelOfSrcVertexOfSpilledEdges( GraphViewConnection connection, List <dynamic> virtualReverseEdges) { Dictionary <string, string> labelOfVertexCollection = new Dictionary <string, string>(); HashSet <string> vertexIdSet = new HashSet <string>(); foreach (JObject virtualReverseEdge in virtualReverseEdges) { JObject virtualReverseEdgeObject = (JObject)virtualReverseEdge[EdgeDocumentHelper.VirtualReverseEdge]; string vertexId = virtualReverseEdgeObject[KW_EDGEDOC_VERTEXID]?.ToString(); if (vertexId != null) { // // this is a spilled edge, so the srcVLabel is not in the spilled document and needs to be fetched // vertexIdSet.Add(vertexId); } } if (vertexIdSet.Any()) { string inClause = string.Join(", ", vertexIdSet.Select(vertexId => $"'{vertexId}'")); string labelQuery = $"SELECT doc.{KW_VERTEX_LABEL}, doc.{KW_DOC_ID} " + $"FROM doc " + $"WHERE doc.{KW_DOC_ID} IN ({inClause})"; IQueryable <dynamic> vertexIdAndLabels = connection.ExecuteQuery(labelQuery); foreach (JObject vertexIdAndLabel in vertexIdAndLabels) { string vertexId = vertexIdAndLabel[KW_DOC_ID].ToString(); string vertexLabel = vertexIdAndLabel[KW_VERTEX_LABEL]?.ToString(); labelOfVertexCollection[vertexId] = vertexLabel; } } return(labelOfVertexCollection); }
/// <summary> /// For every vertex in the vertexIdSet, retrieve their spilled edge documents to construct their forward or backward /// adjacency list. /// When connection.useReverseEdge is false, this method will retrieve all the forward edge documents whose sink /// is the target vertex to build a virtual reverse adjacency list. /// </summary> /// <param name="connection"></param> /// <param name="vertexIdSet"></param> public static void ConstructSpilledAdjListsOrVirtualRevAdjListsOfVertices(GraphViewConnection connection, HashSet <string> vertexIdSet) { if (!vertexIdSet.Any()) { return; } string inClause = string.Join(", ", vertexIdSet.Select(vertexId => $"'{vertexId}'")); string edgeDocumentsQuery = $"SELECT * " + $"FROM edgeDoc " + $"WHERE edgeDoc.{KW_EDGEDOC_VERTEXID} IN ({inClause})"; List <dynamic> edgeDocuments = connection.ExecuteQuery(edgeDocumentsQuery).ToList(); // Dictionary<vertexId, Dictionary<edgeDocumentId, edgeDocument>> Dictionary <string, Dictionary <string, JObject> > edgeDict = new Dictionary <string, Dictionary <string, JObject> >(); foreach (JObject edgeDocument in edgeDocuments) { // Save edgeDocument's etag if necessary connection.VertexCache.SaveCurrentEtagNoOverride(edgeDocument); } EdgeDocumentHelper.FillEdgeDict(edgeDict, edgeDocuments); // // Use all edges whose sink is vertexId to construct a virtual reverse adjacency list of this vertex // if (!connection.UseReverseEdges) { edgeDocumentsQuery = $"SELECT {{" + $" \"{EdgeDocumentHelper.VirtualReverseEdgeObject}\": edge, " + $" \"{KW_EDGE_SRCV}\": doc.{KW_DOC_ID}, " + $" \"{KW_EDGE_SRCV_LABEL}\": doc.{KW_EDGE_SRCV_LABEL}," + $" \"{KW_EDGEDOC_VERTEXID}\": doc.{KW_EDGEDOC_VERTEXID}" + $"}} AS {EdgeDocumentHelper.VirtualReverseEdge}\n" + $"FROM doc\n" + $"JOIN edge IN doc.{GraphViewKeywords.KW_VERTEX_EDGE}\n" + $"WHERE edge.{KW_EDGE_SINKV} IN ({inClause})"; edgeDocuments = connection.ExecuteQuery(edgeDocumentsQuery).ToList(); Dictionary <string, string> labelOfSrcVertexOfSpilledEdges = EdgeDocumentHelper.GetLabelOfSrcVertexOfSpilledEdges(connection, edgeDocuments); List <JObject> virtualReverseEdgeDocuments = EdgeDocumentHelper.ConstructVirtualReverseEdgeDocuments(edgeDocuments, labelOfSrcVertexOfSpilledEdges); EdgeDocumentHelper.FillEdgeDict(edgeDict, virtualReverseEdgeDocuments.Cast <dynamic>().ToList()); } foreach (KeyValuePair <string, Dictionary <string, JObject> > pair in edgeDict) { string vertexId = pair.Key; Dictionary <string, JObject> edgeDocDict = pair.Value; // contains both in & out edges VertexField vertexField; connection.VertexCache.TryGetVertexField(vertexId, out vertexField); vertexField.ConstructSpilledOrVirtualAdjacencyListField(edgeDocDict); vertexIdSet.Remove(vertexId); } foreach (string vertexId in vertexIdSet) { VertexField vertexField; connection.VertexCache.TryGetVertexField(vertexId, out vertexField); vertexField.ConstructSpilledOrVirtualAdjacencyListField(new Dictionary <string, JObject>()); } }