/// <summary> /// /// </summary> /// <param name="connection"></param> /// <param name="vertexObject"></param> /// <param name="edgeDocId"></param> /// <param name="isReverse"></param> /// <param name="newEdgeObject"></param> public static void UpdateEdgeProperty( GraphViewConnection connection, JObject vertexObject, string edgeDocId, // Can be null bool isReverse, JObject newEdgeObject // With all metadata (including id, partition, srcV/sinkV, edgeId) ) { bool tooLarge; string srcOrSinkVInEdgeObject = isReverse ? KW_EDGE_SRCV : KW_EDGE_SINKV; if (edgeDocId == null) { JArray edgeContainer = (JArray)vertexObject[isReverse ? KW_VERTEX_REV_EDGE : KW_VERTEX_EDGE]; // Don't use JToken.Replace() here. // Make sure the currently modified edge is the last child of edgeContainer, which // garantees the newly created edge-document won't be too large. // // NOTE: The following line applies for both incomming and outgoing edge. edgeContainer.Children <JObject>().First( e => (string)e[KW_EDGE_ID] == (string)newEdgeObject[KW_EDGE_ID] && (string)e[srcOrSinkVInEdgeObject] == (string)newEdgeObject[srcOrSinkVInEdgeObject] ).Remove(); edgeContainer.Add(newEdgeObject); UploadOne(connection, (string)vertexObject[KW_DOC_ID], vertexObject, out tooLarge); if (tooLarge) { // Handle this situation: The updated edge is too large to be filled into the vertex-document string existEdgeDocId, newEdgeDocId; bool? spillReverse = null; SpillVertexEdgesToDocument(connection, vertexObject, ref spillReverse, out existEdgeDocId, out newEdgeDocId); } } else { // Large vertex JObject edgeDocObject = connection.RetrieveDocumentById(edgeDocId); edgeDocObject[KW_EDGEDOC_EDGE].Children <JObject>().First( e => (string)e[KW_EDGE_ID] == (string)newEdgeObject[KW_EDGE_ID] && (string)e[srcOrSinkVInEdgeObject] == (string)newEdgeObject[srcOrSinkVInEdgeObject] ).Remove(); ((JArray)edgeDocObject[KW_EDGEDOC_EDGE]).Add(newEdgeObject); UploadOne(connection, edgeDocId, edgeDocObject, out tooLarge); if (tooLarge) { // Handle this situation: The modified edge is too large to be filled into the original edge-document // Remove the edgeObject added just now, and upload the original edge-document ((JArray)edgeDocObject[KW_EDGEDOC_EDGE]).Last.Remove(); UploadOne(connection, edgeDocId, edgeDocObject, out tooLarge); Debug.Assert(!tooLarge); // Insert the edgeObject to one of the vertex's edge-documents InsertEdgeObjectInternal(connection, vertexObject, null, newEdgeObject, isReverse, out edgeDocId); } } }
public static void RemoveEdge( Dictionary <string, Tuple <JObject, string> > documentMap, GraphViewConnection connection, string edgeDocId, JObject vertexObject, bool isReverse, string srcVertexId, long edgeOffset) { JToken edgeContainer = vertexObject[isReverse ? "_reverse_edge" : "_edge"]; if (edgeContainer is JObject) { // Now it is a large-degree vertex, and contains at least 1 edge-document Debug.Assert(!string.IsNullOrEmpty(edgeDocId), "!string.IsNullOrEmpty(edgeDocId)"); JArray edgeDocumentsArray = (JArray)edgeContainer["_edges"]; Debug.Assert(edgeDocumentsArray != null, "edgeDocuments != null"); Debug.Assert(edgeDocumentsArray.Count > 0, "edgeDocuments.Count > 0"); JObject edgeDocument = connection.RetrieveDocumentById(edgeDocId); Debug.Assert(edgeDocument["_partition"] != null); Debug.Assert(vertexObject["_partition"] != null); Debug.Assert(((string)edgeDocument["id"]).Equals(edgeDocId), "((string)edgeDocument['id']).Equals(edgeDocId)"); Debug.Assert((bool)edgeDocument["_is_reverse"] == isReverse, "(bool)edgeDocument['_is_reverse'] == isReverse"); Debug.Assert((string)edgeDocument["_vertex_id"] == (string)vertexObject["id"], "(string)edgeDocument['_vertex_id'] == (string)vertexObject['id']"); // The edge to be removed must exist! (garanteed by caller) JArray edgesArray = (JArray)edgeDocument["_edge"]; Debug.Assert(edgesArray != null, "edgesArray != null"); Debug.Assert(edgesArray.Count > 0, "edgesArray.Count > 0"); if (isReverse) { edgesArray.First(e => (string)e["_srcV"] == srcVertexId && (long)e["_offset"] == edgeOffset).Remove(); } else { edgesArray.First(e => (long)e["_offset"] == edgeOffset).Remove(); } // // If the edge-document contains no edge after the removal, delete this edge-document. // Don't forget to update the vertex-document at the same time. // if (edgesArray.Count == 0) { edgeDocumentsArray.First(edoc => ((string)edoc["id"]).Equals(edgeDocId)).Remove(); // // If the vertex-document contains no (incoming or outgoing) edges now, set edgeContainer // ("_edge" or "_reverse_edge") to an empry JArray! Anyway, we ensure that if edgeContainer // is JObject, it contains at least one edge-document // if (edgeDocumentsArray.Count == 0) { vertexObject[isReverse ? "_reverse_edge" : "_edge"] = new JArray(); } // Delete the edge-document, and add the vertex-document to the upload list documentMap[edgeDocId] = new Tuple <JObject, string>(null, (string)edgeDocument["_partition"]); documentMap[(string)vertexObject["id"]] = new Tuple <JObject, string>(vertexObject, (string)vertexObject["_partition"]); } else { documentMap[edgeDocId] = new Tuple <JObject, string>(edgeDocument, (string)edgeDocument["_partition"]); } } else if (edgeContainer is JArray) { Debug.Assert(edgeDocId == null, "edgeDocId == null"); if (isReverse) { ((JArray)edgeContainer).First(e => (string)e["_srcV"] == srcVertexId && (long)e["_offset"] == edgeOffset).Remove(); } else { ((JArray)edgeContainer).First(e => (long)e["_offset"] == edgeOffset).Remove(); } documentMap[(string)vertexObject["id"]] = new Tuple <JObject, string>(vertexObject, (string)vertexObject["_partition"]); } else { throw new Exception($"BUG: edgeContainer should either be JObject or JArray, but now: {edgeContainer?.GetType()}"); } }
/// <summary> /// Insert edgeObject to one a vertex. /// NOTE: vertex-document and edge-document(s) are uploaded. /// NOTE: If changing _edge/_reverse_edge field from JArray to JObject, the "EdgeDocId" of existing /// edges in VertexCache are updated (from null to the newly created edge-document's id) /// NOTE: Adding the newly created edge into VertexCache is not operated by this function. Actually, /// if called by <see cref="UpdateEdgeProperty"/>, VertexCache should be updated by setting an /// edge's property, but not adding a new edge. /// </summary> /// <param name="connection"></param> /// <param name="vertexObject"></param> /// <param name="vertexField">Can be null if we already know edgeContainer is JObject</param> /// <param name="edgeObject"></param> /// <param name="isReverse"></param> /// <param name="newEdgeDocId"></param> private static void InsertEdgeObjectInternal( GraphViewConnection connection, JObject vertexObject, VertexField vertexField, JObject edgeObject, bool isReverse, out string newEdgeDocId) { bool tooLarge; JToken edgeContainer = vertexObject[isReverse ? "_reverse_edge" : "_edge"]; // JArray or JObject if (edgeContainer is JObject) { // Now it is a large-degree vertex, and contains at least 1 edge-document JArray edgeDocumentsArray = (JArray)edgeContainer["_edges"]; Debug.Assert(edgeDocumentsArray != null, "edgeDocuments != null"); Debug.Assert(edgeDocumentsArray.Count > 0, "edgeDocuments.Count > 0"); string lastEdgeDocId = (string)edgeDocumentsArray.Last["id"]; JObject edgeDocument = connection.RetrieveDocumentById(lastEdgeDocId); Debug.Assert(((string)edgeDocument["id"]).Equals(lastEdgeDocId), "((string)edgeDocument['id']).Equals(lastEdgeDocId)"); Debug.Assert((bool)edgeDocument["_is_reverse"] == isReverse, "(bool)edgeDocument['_is_reverse'] == isReverse"); Debug.Assert((string)edgeDocument["_vertex_id"] == (string)vertexObject["id"], "(string)edgeDocument['_vertex_id'] == (string)vertexObject['id']"); JArray edgesArray = (JArray)edgeDocument["_edge"]; Debug.Assert(edgesArray != null, "edgesArray != null"); Debug.Assert(edgesArray.Count > 0, "edgesArray.Count > 0"); if (connection.EdgeSpillThreshold == 0) { // Don't spill an edge-document until it is too large edgesArray.Add(edgeObject); tooLarge = false; } else { // Explicitly specified a threshold Debug.Assert(connection.EdgeSpillThreshold > 0, "connection.EdgeSpillThreshold > 0"); if (edgesArray.Count >= connection.EdgeSpillThreshold) { // The threshold is reached! tooLarge = true; } else { // The threshold is not reached edgesArray.Add(edgeObject); tooLarge = false; } } // If the edge-document is not too large (reach the threshold), try to // upload the edge into the document if (!tooLarge) { UploadOne(connection, lastEdgeDocId, edgeDocument, out tooLarge); } if (tooLarge) { // The edge is too large to be filled into the last edge-document // or the threashold is reached: // Create a new edge-document to store the edge. JObject edgeDocObject = new JObject { ["id"] = GraphViewConnection.GenerateDocumentId(), ["_partition"] = vertexObject["_partition"], ["_is_reverse"] = isReverse, ["_vertex_id"] = (string)vertexObject["id"], ["_edge"] = new JArray(edgeObject) }; lastEdgeDocId = connection.CreateDocumentAsync(edgeDocObject).Result; // Add the newly create edge-document to vertexObject & upload the vertexObject edgeDocumentsArray.Add(new JObject { ["id"] = lastEdgeDocId }); } newEdgeDocId = lastEdgeDocId; // Upload the vertex documention (at least, its _nextXxx is changed) bool dummyTooLarge; UploadOne(connection, (string)vertexObject["id"], vertexObject, out dummyTooLarge); Debug.Assert(!dummyTooLarge); } else if (edgeContainer is JArray) { ((JArray)edgeContainer).Add(edgeObject); if (connection.EdgeSpillThreshold == 0) { // Don't spill an edge-document until it is too large tooLarge = false; } else { // Explicitly specified a threshold Debug.Assert(connection.EdgeSpillThreshold > 0, "connection.EdgeSpillThreshold > 0"); tooLarge = (((JArray)edgeContainer).Count >= connection.EdgeSpillThreshold); } if (!tooLarge) { UploadOne(connection, (string)vertexObject["id"], vertexObject, out tooLarge); } if (tooLarge) { string existEdgeDocId; SpillVertexEdgesToDocument(connection, vertexObject, out existEdgeDocId, out newEdgeDocId); // Update the in & out edges in vertex field if (isReverse) { Debug.Assert(vertexField.RevAdjacencyList.AllEdges.All(edge => edge.EdgeDocID == null)); foreach (EdgeField edge in vertexField.RevAdjacencyList.AllEdges) { edge.EdgeDocID = existEdgeDocId; } } else { Debug.Assert(vertexField.AdjacencyList.AllEdges.All(edge => edge.EdgeDocID == null)); foreach (EdgeField edge in vertexField.AdjacencyList.AllEdges) { edge.EdgeDocID = existEdgeDocId; } } } else { newEdgeDocId = null; } } else { throw new Exception($"BUG: edgeContainer should either be JObject or JArray, but now: {edgeContainer?.GetType()}"); } }
public static void RemoveEdge( Dictionary <string, Tuple <JObject, string> > documentMap, GraphViewConnection connection, string edgeDocId, JObject vertexObject, bool isReverse, string srcVertexId, string edgeId) { JArray edgeContainer = (JArray)vertexObject[isReverse ? KW_VERTEX_REV_EDGE : KW_VERTEX_EDGE]; if (IsSpilledVertex(vertexObject, isReverse)) { // Now it is a large-degree vertex, and contains at least 1 edge-document Debug.Assert(!string.IsNullOrEmpty(edgeDocId), "!string.IsNullOrEmpty(edgeDocId)"); JArray edgeDocumentsArray = edgeContainer; Debug.Assert(edgeDocumentsArray != null, "edgeDocuments != null"); Debug.Assert(edgeDocumentsArray.Count == 1, "edgeDocuments.Count == 1"); JObject edgeDocument = connection.RetrieveDocumentById(edgeDocId); Debug.Assert(edgeDocument[KW_DOC_PARTITION] != null); Debug.Assert(vertexObject[KW_DOC_PARTITION] != null); Debug.Assert(((string)edgeDocument[KW_DOC_ID]).Equals(edgeDocId), $"((string)edgeDocument['{KW_DOC_ID}']).Equals(edgeDocId)"); Debug.Assert((bool)edgeDocument[KW_EDGEDOC_ISREVERSE] == isReverse, $"(bool)edgeDocument['{KW_EDGEDOC_ISREVERSE}'] == isReverse"); Debug.Assert((string)edgeDocument[KW_EDGEDOC_VERTEXID] == (string)vertexObject[KW_DOC_ID], $"(string)edgeDocument['{KW_EDGEDOC_VERTEXID}'] == (string)vertexObject['id']"); // The edge to be removed must exist! (garanteed by caller) JArray edgesArray = (JArray)edgeDocument[KW_EDGEDOC_EDGE]; Debug.Assert(edgesArray != null, "edgesArray != null"); Debug.Assert(edgesArray.Count > 0, "edgesArray.Count > 0"); if (isReverse) { edgesArray.First(e => (string)e[KW_EDGE_SRCV] == srcVertexId && (string)e[KW_EDGE_ID] == edgeId).Remove(); } else { edgesArray.First(e => (string)e[KW_EDGE_ID] == edgeId).Remove(); } // // If the edge-document contains no edge after the removal, delete this edge-document. // Don't forget to update the vertex-document at the same time. // if (edgesArray.Count == 0) { // // Currently, // If the modified edge document contains no edges, just do nothing, leave it here. // This means the lastest edge document is empty // // If the empty document is not the latest document, delete the edge-document, // and add the vertex-document to the upload list if ((string)edgeDocumentsArray[0][KW_DOC_ID] != edgeDocId) { documentMap[edgeDocId] = new Tuple <JObject, string>(null, (string)edgeDocument[KW_DOC_PARTITION]); } else { documentMap[edgeDocId] = new Tuple <JObject, string>(edgeDocument, (string)edgeDocument[KW_DOC_PARTITION]); } // The vertex object needn't change //documentMap[(string)vertexObject[KW_DOC_ID]] = new Tuple<JObject, string>(vertexObject, (string)vertexObject[KW_DOC_PARTITION]); } else { documentMap[edgeDocId] = new Tuple <JObject, string>(edgeDocument, (string)edgeDocument[KW_DOC_PARTITION]); } } else { // This vertex is not spilled Debug.Assert(edgeDocId == null, "edgeDocId == null"); if (isReverse) { ((JArray)edgeContainer).First(e => (string)e[KW_EDGE_SRCV] == srcVertexId && (string)e[KW_EDGE_ID] == edgeId).Remove(); } else { ((JArray)edgeContainer).First(e => (string)e[KW_EDGE_ID] == edgeId).Remove(); } documentMap[(string)vertexObject[KW_DOC_ID]] = new Tuple <JObject, string>(vertexObject, (string)vertexObject[KW_DOC_PARTITION]); } }
/// <summary> /// Insert edgeObject to one a vertex. /// NOTE: vertex-document and edge-document(s) are uploaded. /// NOTE: If changing _edge/_reverse_edge field from JArray to JObject, the "EdgeDocId" of existing /// edges in VertexCache are updated (from null to the newly created edge-document's id) /// NOTE: Adding the newly created edge into VertexCache is not operated by this function. Actually, /// if called by <see cref="UpdateEdgeProperty"/>, VertexCache should be updated by setting an /// edge's property, but not adding a new edge. /// </summary> /// <param name="connection"></param> /// <param name="vertexObject"></param> /// <param name="vertexField">Can be null if we already know edgeContainer is JObject</param> /// <param name="edgeObject"></param> /// <param name="isReverse"></param> /// <param name="newEdgeDocId"></param> private static void InsertEdgeObjectInternal( GraphViewConnection connection, JObject vertexObject, VertexField vertexField, JObject edgeObject, bool isReverse, out string newEdgeDocId) { bool tooLarge; JArray edgeContainer = (JArray)vertexObject[isReverse ? KW_VERTEX_REV_EDGE : KW_VERTEX_EDGE]; // JArray or JObject bool isSpilled = IsSpilledVertex(vertexObject, isReverse); if (isSpilled) { // Now it is a large-degree vertex, and contains at least 1 edge-document JArray edgeDocumentsArray = edgeContainer; Debug.Assert(edgeDocumentsArray != null, "edgeDocumentsArray != null"); Debug.Assert(edgeDocumentsArray.Count == 1, "edgeDocumentsArray.Count == 1"); string lastEdgeDocId = (string)edgeDocumentsArray.Last[KW_DOC_ID]; JObject edgeDocument = connection.RetrieveDocumentById(lastEdgeDocId); Debug.Assert(((string)edgeDocument[KW_DOC_ID]).Equals(lastEdgeDocId), $"((string)edgeDocument[{KW_DOC_ID}]).Equals(lastEdgeDocId)"); Debug.Assert((bool)edgeDocument[KW_EDGEDOC_ISREVERSE] == isReverse, $"(bool)edgeDocument['{KW_EDGEDOC_ISREVERSE}'] == isReverse"); Debug.Assert((string)edgeDocument[KW_EDGEDOC_VERTEXID] == (string)vertexObject[KW_DOC_ID], $"(string)edgeDocument['{KW_EDGEDOC_VERTEXID}'] == (string)vertexObject['{KW_DOC_ID}']"); JArray edgesArray = (JArray)edgeDocument[KW_EDGEDOC_EDGE]; Debug.Assert(edgesArray != null, "edgesArray != null"); Debug.Assert(edgesArray.Count > 0, "edgesArray.Count > 0"); if (connection.EdgeSpillThreshold == 0) { // Don't spill an edge-document until it is too large edgesArray.Add(edgeObject); tooLarge = false; } else { // Explicitly specified a threshold Debug.Assert(connection.EdgeSpillThreshold > 0, "connection.EdgeSpillThreshold > 0"); if (edgesArray.Count >= connection.EdgeSpillThreshold) { // The threshold is reached! tooLarge = true; } else { // The threshold is not reached edgesArray.Add(edgeObject); tooLarge = false; } } // If the edge-document is not too large (reach the threshold), try to // upload the edge into the document if (!tooLarge) { UploadOne(connection, lastEdgeDocId, edgeDocument, out tooLarge); } if (tooLarge) { // The edge is too large to be filled into the last edge-document // or the threashold is reached: // Create a new edge-document to store the edge. JObject edgeDocObject = new JObject { [KW_DOC_ID] = GraphViewConnection.GenerateDocumentId(), [KW_DOC_PARTITION] = vertexObject[KW_DOC_PARTITION], [KW_EDGEDOC_ISREVERSE] = isReverse, [KW_EDGEDOC_VERTEXID] = (string)vertexObject[KW_DOC_ID], [KW_EDGEDOC_EDGE] = new JArray(edgeObject) }; lastEdgeDocId = connection.CreateDocumentAsync(edgeDocObject).Result; //// Add the newly create edge-document to vertexObject & upload the vertexObject //edgeDocumentsArray.Add(new JObject { // [KW_DOC_ID] = lastEdgeDocId //}); // Replace the newly created edge-document to vertexObject Debug.Assert(edgeDocumentsArray.Count == 1); edgeDocumentsArray[0][KW_DOC_ID] = lastEdgeDocId; } newEdgeDocId = lastEdgeDocId; // Upload the vertex documention (at least, its _nextXxx is changed) bool dummyTooLarge; UploadOne(connection, (string)vertexObject[KW_DOC_ID], vertexObject, out dummyTooLarge); Debug.Assert(!dummyTooLarge); } else { // This vertex is not spilled bool?spillReverse; ((JArray)edgeContainer).Add(edgeObject); if (connection.EdgeSpillThreshold == 0) { // Don't spill an edge-document until it is too large tooLarge = false; spillReverse = null; } else { // Explicitly specified a threshold Debug.Assert(connection.EdgeSpillThreshold > 0, "connection.EdgeSpillThreshold > 0"); tooLarge = (((JArray)edgeContainer).Count > connection.EdgeSpillThreshold); spillReverse = isReverse; } if (!tooLarge) { UploadOne(connection, (string)vertexObject[KW_DOC_ID], vertexObject, out tooLarge); } if (tooLarge) { string existEdgeDocId; // The vertex object is uploaded in SpillVertexEdgesToDocument SpillVertexEdgesToDocument(connection, vertexObject, ref spillReverse, out existEdgeDocId, out newEdgeDocId); // Update the in & out edges in vertex field Debug.Assert(spillReverse != null); Debug.Assert(vertexField != null); if (spillReverse.Value) { foreach (EdgeField edge in vertexField.RevAdjacencyList.AllEdges) { Debug.Assert(edge.EdgeDocID == null); edge.EdgeDocID = existEdgeDocId; } } else { foreach (EdgeField edge in vertexField.AdjacencyList.AllEdges) { Debug.Assert(edge.EdgeDocID == null); edge.EdgeDocID = existEdgeDocId; } } } else { newEdgeDocId = null; } } }