internal override RawRecord DataModify(RawRecord record)
        {
            string srcId      = record[this._srcIdIndex].ToValue;
            long   edgeOffset = long.Parse(record[this._edgeOffsetIndex].ToValue);

            JObject srcEdgeObject;
            string  srcEdgeDocId, sinkEdgeDocId;
            JObject srcVertexObject = this.Connection.RetrieveDocumentById(srcId);

            EdgeDocumentHelper.FindEdgeBySourceAndOffset(
                this.Connection, srcVertexObject, srcId, edgeOffset, false,
                out srcEdgeObject, out srcEdgeDocId);
            if (srcEdgeObject == null)
            {
                //TODO: Check is this condition alright?
                return(null);
            }

            string  sinkId = (string)srcEdgeObject["_sinkV"];
            JObject sinkVertexObject;

            if (!string.Equals(sinkId, srcId))
            {
                sinkVertexObject = this.Connection.RetrieveDocumentById(sinkId);
                JObject sinkEdgeObject;
                EdgeDocumentHelper.FindEdgeBySourceAndOffset(
                    this.Connection, sinkVertexObject, srcId, edgeOffset, true,
                    out sinkEdgeObject, out sinkEdgeDocId);
            }
            else
            {
                sinkVertexObject = srcVertexObject;  // NOTE: Must not use DeepClone() here!
                sinkEdgeDocId    = srcEdgeDocId;
            }

            Dictionary <string, Tuple <JObject, string> > uploadDocuments = new Dictionary <string, Tuple <JObject, string> >();

            EdgeDocumentHelper.RemoveEdge(uploadDocuments, this.Connection, srcEdgeDocId, srcVertexObject, false, srcId, edgeOffset);
            EdgeDocumentHelper.RemoveEdge(uploadDocuments, this.Connection, sinkEdgeDocId, sinkVertexObject, true, srcId, edgeOffset);
            this.Connection.ReplaceOrDeleteDocumentsAsync(uploadDocuments).Wait();


            VertexField srcVertexField  = this.Connection.VertexCache.GetVertexField(srcId, srcVertexObject);
            VertexField sinkVertexField = this.Connection.VertexCache.GetVertexField(sinkId, sinkVertexObject);

            srcVertexField.AdjacencyList.RemoveEdgeField(srcId, edgeOffset);
            sinkVertexField.RevAdjacencyList.RemoveEdgeField(srcId, edgeOffset);

            return(null);
        }
Пример #2
0
        public void Upload(GraphViewCommand command)
        {
            if (this.isAddE && this.isDropE)
            {
                return;
            }
            else if (this.isAddE)
            {
                string outEdgeDocId;
                EdgeDocumentHelper.InsertEdgeObjectInternal(command, this.srcVertexField.VertexJObject, this.srcVertexField,
                                                            this.outEdgeField.EdgeJObject, false, out outEdgeDocId);
                this.outEdgeField.EdgeDocID = outEdgeDocId;

                if (this.UseReverseEdges)
                {
                    string inEdgeDocId;
                    EdgeDocumentHelper.InsertEdgeObjectInternal(command, this.sinkVertexField.VertexJObject, this.sinkVertexField,
                                                                this.inEdgeField.EdgeJObject, true, out inEdgeDocId);
                    this.inEdgeField.EdgeDocID = inEdgeDocId;
                }
            }
            else if (this.isDropE)
            {
                string edgeId = outEdgeField.EdgeId;
                string srcId  = outEdgeField.OutV;
                string sinkId = outEdgeField.InV;

                JObject outEdgeObject;
                string  outEdgeDocId;
                EdgeDocumentHelper.FindEdgeBySourceAndEdgeId(
                    command, this.srcVertexField.VertexJObject, srcId, edgeId, false,
                    out outEdgeObject, out outEdgeDocId);

                if (outEdgeObject == null)
                {
                    // something wrong. the edge that we want to drop does not exist in db
                    return;
                }

                string inEdgeDocId = null;
                if (this.UseReverseEdges)
                {
                    if (!string.Equals(sinkId, srcId))
                    {
                        JObject dummySinkEdgeObject;
                        EdgeDocumentHelper.FindEdgeBySourceAndEdgeId(
                            command, this.sinkVertexField.VertexJObject, srcId, edgeId, true,
                            out dummySinkEdgeObject, out inEdgeDocId);
                    }
                    else
                    {
                        Debug.Assert(object.ReferenceEquals(this.sinkVertexField, this.srcVertexField));
                        Debug.Assert(this.sinkVertexField.VertexJObject == this.srcVertexField.VertexJObject);
                        inEdgeDocId = outEdgeDocId;
                    }
                }

                // <docId, <docJson, partition>>
                Dictionary <string, Tuple <JObject, string> > uploadDocuments = new Dictionary <string, Tuple <JObject, string> >();
                EdgeDocumentHelper.RemoveEdge(uploadDocuments, command, outEdgeDocId,
                                              this.srcVertexField, false, srcId, edgeId);
                if (this.UseReverseEdges)
                {
                    EdgeDocumentHelper.RemoveEdge(uploadDocuments, command, inEdgeDocId,
                                                  this.sinkVertexField, true, srcId, edgeId);
                }
                command.Connection.ReplaceOrDeleteDocumentsAsync(uploadDocuments, command).Wait();
            }
            else if (this.deltaProperties.Count > 0)
            {
                string edgeId = outEdgeField.EdgeId;
                string srcId  = outEdgeField.OutV;
                string sinkId = outEdgeField.InV;

                JObject outEdgeObject;
                string  outEdgeDocId;
                EdgeDocumentHelper.FindEdgeBySourceAndEdgeId(
                    command, this.srcVertexField.VertexJObject, srcId, edgeId, false,
                    out outEdgeObject, out outEdgeDocId);

                if (outEdgeObject == null)
                {
                    // something wrong. the edge that we want to update does not exist in db
                    return;
                }

                string inEdgeDocId = null;
                if (this.UseReverseEdges)
                {
                    if (!string.Equals(sinkId, srcId))
                    {
                        JObject inEdgeObject;
                        EdgeDocumentHelper.FindEdgeBySourceAndEdgeId(
                            command, this.sinkVertexField.VertexJObject, srcId, edgeId, true,
                            out inEdgeObject, out inEdgeDocId);
                    }
                    else
                    {
                        Debug.Assert(object.ReferenceEquals(this.sinkVertexField, this.srcVertexField));
                        Debug.Assert(this.sinkVertexField.VertexJObject == this.srcVertexField.VertexJObject);
                        inEdgeDocId = outEdgeDocId;
                    }
                }

                // Interact with DocDB to update the property
                EdgeDocumentHelper.UpdateEdgeProperty(command, this.srcVertexField.VertexJObject, outEdgeDocId, false,
                                                      this.outEdgeField.EdgeJObject);
                if (this.UseReverseEdges)
                {
                    EdgeDocumentHelper.UpdateEdgeProperty(command, this.sinkVertexField.VertexJObject, inEdgeDocId, true,
                                                          this.inEdgeField.EdgeJObject);
                }
            }
        }