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); }
internal override RawRecord DataModify(RawRecord record) { VertexField srcVertexField = _srcFunction.Evaluate(record) as VertexField; VertexField sinkVertexField = _sinkFunction.Evaluate(record) as VertexField; if (srcVertexField == null || sinkVertexField == null) { return(null); } string srcId = srcVertexField["id"].ToValue; string sinkId = sinkVertexField["id"].ToValue; //string srcJsonDocument = srcVertexField.JsonDocument; //string sinkJsonDocument = sinkVertexField.JsonDocument; JObject srcVertexObject = this.Connection.RetrieveDocumentById(srcId); JObject sinkVertexObject; if (srcId.Equals(sinkId)) { // MUST not use JObject.DeepClone() here! sinkVertexObject = srcVertexObject; } else { sinkVertexObject = this.Connection.RetrieveDocumentById(sinkId); } //VertexField srcVertexField = (srcFieldObject as VertexField) // ?? Connection.VertexCache.GetVertexField(srcId, srcVertexObject); //VertexField sinkVertexField = (sinkFieldObject as VertexField) // ?? Connection.VertexCache.GetVertexField(sinkId, sinkVertexObject); // // Interact with DocDB and add the edge // - For a small-degree vertex (now filled into one document), insert the edge in-place // - If the upload succeeds, done! // - If the upload fails with size-limit-exceeded(SLE), put either incoming or outgoing edges into a seperate document // - For a large-degree vertex (already spilled) // - Update either incoming or outgoing edges in the seperate edge-document // - If the upload fails with SLE, create a new document to store the edge, and update the vertex document // JObject outEdgeObject, inEdgeObject; string outEdgeDocID, inEdgeDocID; EdgeDocumentHelper.InsertEdgeAndUpload(this.Connection, srcId, sinkId, srcVertexField, sinkVertexField, this._edgeJsonObject, srcVertexObject, sinkVertexObject, out outEdgeObject, out outEdgeDocID, out inEdgeObject, out inEdgeDocID); // // Update vertex's adjacency list and reverse adjacency list (in vertex field) // EdgeField outEdgeField = EdgeField.ConstructForwardEdgeField(srcId, srcVertexField["label"]?.ToValue, outEdgeDocID, outEdgeObject); EdgeField inEdgeField = EdgeField.ConstructBackwardEdgeField(sinkId, sinkVertexField["label"]?.ToValue, inEdgeDocID, inEdgeObject); srcVertexField.AdjacencyList.AddEdgeField(srcId, outEdgeField.Offset, outEdgeField); sinkVertexField.RevAdjacencyList.AddEdgeField(srcId, inEdgeField.Offset, inEdgeField); // Construct the newly added edge's RawRecord RawRecord result = new RawRecord(); // source, sink, other, offset, * result.Append(new StringField(srcId)); result.Append(new StringField(sinkId)); result.Append(new StringField(_otherVTag == 0 ? srcId : sinkId)); result.Append(new StringField(outEdgeObject["_offset"].ToString())); result.Append(outEdgeField); for (int i = GraphViewReservedProperties.ReservedEdgeProperties.Count; i < _edgeProperties.Count; i++) { FieldObject fieldValue = outEdgeField[_edgeProperties[i]]; result.Append(fieldValue); } return(result); }