Beispiel #1
0
        public JObject ConstructNodeJsonDocument(out List <string> projectedFieldList)
        {
            JObject nodeJsonDocument = new JObject();

            projectedFieldList = new List <string>(GraphViewReservedProperties.ReservedNodeProperties);

            for (var i = 0; i < Parameters.Count; i += 2)
            {
                var key = (Parameters[i] as WValueExpression).Value;

                //GraphViewJsonCommand.UpdateProperty(nodeJsonDocument, Parameters[i] as WValueExpression,
                //    Parameters[i + 1] as WValueExpression);
                GraphViewJsonCommand.UpdateProperty(nodeJsonDocument, Parameters[i] as WValueExpression,
                                                    Parameters[i + 1] as WValueExpression);
                string name  = (Parameters[i] as WValueExpression).Value;
                JToken value = (Parameters[i + 1] as WValueExpression).ToJValue();
                if (value != null)
                {
                    if (VertexField.IsVertexMetaProperty(name))
                    {
                        nodeJsonDocument[name] = value;
                    }
                    else
                    {
                        nodeJsonDocument[name] = new JArray {
                            new JObject {
                                ["_value"]  = value,
                                ["_propId"] = GraphViewConnection.GenerateDocumentId(),
                                ["_meta"]   = new JObject(),
                            },
                        };
                    }
                }

                if (!projectedFieldList.Contains(key))
                {
                    projectedFieldList.Add(key);
                }
            }

            //nodeJsonDocument = GraphViewJsonCommand.insert_property(nodeJsonDocument, "[]", "_edge").ToString();
            //nodeJsonDocument = GraphViewJsonCommand.insert_property(nodeJsonDocument, "[]", "_reverse_edge").ToString();
            nodeJsonDocument["_edge"]           = new JArray();
            nodeJsonDocument["_reverse_edge"]   = new JArray();
            nodeJsonDocument["_nextEdgeOffset"] = 0;

            return(nodeJsonDocument);
        }
        internal override List <RawRecord> CrossApply(RawRecord record)
        {
            List <RawRecord> results = new List <RawRecord>();

            FieldObject inputTarget = record[this.inputTargetIndex];

            if (inputTarget is VertexField)
            {
                VertexField vertexField = (VertexField)inputTarget;
                foreach (VertexPropertyField property in vertexField.VertexProperties.Values)
                {
                    string propertyName = property.PropertyName;
                    Debug.Assert(!VertexField.IsVertexMetaProperty(propertyName));
                    Debug.Assert(propertyName == "_edge");
                    Debug.Assert(propertyName == "_reverse_edge");

                    switch (propertyName)
                    {
                    case "_rid":
                    case "_self":
                    case "_etag":
                    case "_attachments":
                    case "_ts":
                        continue;

                    default:
                        foreach (VertexSinglePropertyField singleVp in property.Multiples.Values)
                        {
                            RawRecord r = new RawRecord();
                            r.Append(new StringField(singleVp.PropertyValue, singleVp.JsonDataType));
                            results.Add(r);
                        }
                        break;
                    }
                }
            }
            else if (inputTarget is EdgeField)
            {
                EdgeField edgeField = (EdgeField)inputTarget;

                foreach (KeyValuePair <string, EdgePropertyField> propertyPair in edgeField.EdgeProperties)
                {
                    string            propertyName      = propertyPair.Key;
                    EdgePropertyField edgePropertyField = propertyPair.Value;

                    switch (propertyName)
                    {
                    // Reserved properties for meta-data
                    case "_edgeId":
                    case "_offset":
                    case "_srcV":
                    case "_sinkV":
                    case "_srcVLabel":
                    case "_sinkVLabel":
                        continue;

                    default:
                        RawRecord r = new RawRecord();
                        r.Append(new StringField(edgePropertyField.PropertyValue, edgePropertyField.JsonDataType));
                        results.Add(r);
                        break;
                    }
                }
            }
            else if (inputTarget is VertexSinglePropertyField)
            {
                VertexSinglePropertyField singleVp = inputTarget as VertexSinglePropertyField;
                foreach (KeyValuePair <string, ValuePropertyField> kvp in singleVp.MetaProperties)
                {
                    RawRecord          r = new RawRecord();
                    ValuePropertyField metaPropertyField = kvp.Value;
                    r.Append(new StringField(metaPropertyField.PropertyValue, metaPropertyField.JsonDataType));
                    results.Add(r);
                }
            }
            else
            {
                throw new GraphViewException("The input of values() cannot be a meta or edge property.");
            }
            return(results);
        }
Beispiel #3
0
        internal override List <RawRecord> CrossApply(RawRecord record)
        {
            MapField valueMap = new MapField();

            FieldObject inputTarget = record[this.inputTargetIndex];

            if (inputTarget is VertexField)
            {
                VertexField vertexField = (VertexField)inputTarget;

                if (this.propertyNameList.Any())
                {
                    foreach (string propertyName in this.propertyNameList)
                    {
                        FieldObject property = vertexField[propertyName];
                        if (property == null)
                        {
                            continue;
                        }

                        List <FieldObject>  values = new List <FieldObject>();
                        VertexPropertyField vp     = property as VertexPropertyField;
                        if (vp != null)
                        {
                            foreach (VertexSinglePropertyField vsp in vp.Multiples.Values)
                            {
                                values.Add(vsp);
                            }
                        }

                        valueMap.Add(new StringField(propertyName), new CollectionField(values));
                    }
                }
                else
                {
                    foreach (VertexPropertyField property in vertexField.VertexProperties.Values)
                    {
                        string propertyName = property.PropertyName;
                        Debug.Assert(!VertexField.IsVertexMetaProperty(propertyName));
                        Debug.Assert(!propertyName.Equals(KW_VERTEX_EDGE));
                        Debug.Assert(!propertyName.Equals(KW_VERTEX_REV_EDGE));

                        switch (propertyName)
                        {
                        case "_rid":
                        case "_self":
                        case "_etag":
                        case "_attachments":
                        case "_ts":
                            continue;

                        default:
                            List <FieldObject> values = new List <FieldObject>();
                            foreach (VertexSinglePropertyField singleVp in property.Multiples.Values)
                            {
                                values.Add(singleVp);
                            }
                            valueMap.Add(new StringField(propertyName), new CollectionField(values));
                            break;
                        }
                    }
                }
            }
            else if (inputTarget is EdgeField)
            {
                EdgeField edgeField = (EdgeField)inputTarget;

                if (this.propertyNameList.Any())
                {
                    foreach (string propertyName in this.propertyNameList)
                    {
                        FieldObject property = edgeField[propertyName];
                        if (property == null)
                        {
                            continue;
                        }

                        EdgePropertyField edgePf = property as EdgePropertyField;
                        if (edgePf != null)
                        {
                            valueMap.Add(new StringField(propertyName), edgePf);
                        }
                    }
                }
                else
                {
                    foreach (KeyValuePair <string, EdgePropertyField> propertyPair in edgeField.EdgeProperties)
                    {
                        string            propertyName      = propertyPair.Key;
                        EdgePropertyField edgePropertyField = propertyPair.Value;

                        switch (propertyName)
                        {
                        // Reserved properties for meta-data
                        case KW_EDGE_ID:
                        //case KW_EDGE_OFFSET:
                        case KW_EDGE_SRCV:
                        case KW_EDGE_SINKV:
                        case KW_EDGE_SRCV_LABEL:
                        case KW_EDGE_SINKV_LABEL:
                            continue;

                        default:
                            valueMap.Add(new StringField(propertyName), edgePropertyField);
                            break;
                        }
                    }
                }
            }
            else if (inputTarget is VertexSinglePropertyField)
            {
                VertexSinglePropertyField singleVp = inputTarget as VertexSinglePropertyField;

                if (this.propertyNameList.Any())
                {
                    foreach (string propertyName in this.propertyNameList)
                    {
                        FieldObject property = singleVp[propertyName];
                        if (property == null)
                        {
                            continue;
                        }

                        ValuePropertyField metaPf = property as ValuePropertyField;
                        if (metaPf != null)
                        {
                            valueMap.Add(new StringField(propertyName), metaPf);
                        }
                    }
                }
                else
                {
                    foreach (KeyValuePair <string, ValuePropertyField> kvp in singleVp.MetaProperties)
                    {
                        valueMap.Add(new StringField(kvp.Key), kvp.Value);
                    }
                }
            }
            else
            {
                throw new GraphViewException("The input of valueMap() cannot be a meta or edge property.");
            }

            RawRecord result = new RawRecord();

            result.Append(valueMap);
            return(new List <RawRecord> {
                result
            });
        }
Beispiel #4
0
        public FieldObject Terminate()
        {
            if (this.subgraphState.graph != null)
            {
                return(this.subgraphState.graph);
            }


            if (this.subgraphState.vertexIds.Any())
            {
                // this API would modify the parameter, so deep copy it first.
                List <VertexField> vertices = this.subgraphState.command.Connection.CreateDatabasePortal()
                                              .GetVerticesByIds(new HashSet <string>(this.subgraphState.vertexIds), this.subgraphState.command, null, true);

                List <string> vertexGraphSON = new List <string>();

                foreach (VertexField vertexField in vertices)
                {
                    JObject vertex = new JObject
                    {
                        new JProperty("type", "vertex"),
                        new JProperty("id", vertexField.VertexMetaProperties[KW_DOC_ID].PropertyValue)
                    };

                    Debug.Assert(vertexField.VertexMetaProperties.ContainsKey(KW_VERTEX_LABEL));
                    if (vertexField.VertexMetaProperties[KW_VERTEX_LABEL] != null)
                    {
                        vertex.Add(new JProperty("label", vertexField.VertexMetaProperties[KW_VERTEX_LABEL].PropertyValue));
                    }

                    // Add in Edges
                    JObject inE = new JObject();
                    if (vertexField.RevAdjacencyList != null && vertexField.RevAdjacencyList.AllEdges.Any())
                    {
                        var groupByLabel = vertexField.RevAdjacencyList.AllEdges.GroupBy(e => e.Label);
                        foreach (var g in groupByLabel)
                        {
                            string edgelLabel = g.Key;
                            JArray group      = new JArray();

                            foreach (EdgeField edgeField in g)
                            {
                                string edgeId = edgeField.EdgeProperties[KW_EDGE_ID].ToValue;
                                if (!this.subgraphState.edgeIds.Contains(edgeId))
                                {
                                    continue;
                                }
                                JObject edge = new JObject
                                {
                                    new JProperty("id", edgeField.EdgeProperties[KW_EDGE_ID].ToValue),
                                    new JProperty("outV", edgeField.OutV)
                                };

                                // Add edge properties
                                JObject properties = new JObject();
                                foreach (string propertyName in edgeField.EdgeProperties.Keys)
                                {
                                    switch (propertyName)
                                    {
                                    case KW_EDGE_ID:
                                    case KW_EDGE_LABEL:
                                    case KW_EDGE_SRCV:
                                    case KW_EDGE_SINKV:
                                    case KW_EDGE_SRCV_LABEL:
                                    case KW_EDGE_SINKV_LABEL:
                                    case KW_EDGE_SRCV_PARTITION:
                                    case KW_EDGE_SINKV_PARTITION:
                                        continue;

                                    default:
                                        break;
                                    }

                                    properties.Add(new JProperty(propertyName,
                                                                 JsonDataTypeHelper.GetStringFieldData(edgeField.EdgeProperties[propertyName].PropertyValue,
                                                                                                       edgeField.EdgeProperties[propertyName].JsonDataType)));
                                }
                                edge.Add(new JProperty("properties", properties));
                                group.Add(edge);
                            }

                            if (group.Count != 0)
                            {
                                inE.Add(edgelLabel, group);
                            }
                        }
                    }
                    if (inE.Count != 0)
                    {
                        vertex.Add(new JProperty("inE", inE));
                    }



                    // Add out Edges
                    JObject outE = new JObject();
                    if (vertexField.AdjacencyList != null && vertexField.AdjacencyList.AllEdges.Any())
                    {
                        var groupByLabel = vertexField.AdjacencyList.AllEdges.GroupBy(e => e.Label);
                        foreach (var g in groupByLabel)
                        {
                            string edgelLabel = g.Key;
                            JArray group      = new JArray();

                            foreach (EdgeField edgeField in g)
                            {
                                string edgeId = edgeField.EdgeProperties[KW_EDGE_ID].ToValue;
                                if (!this.subgraphState.edgeIds.Contains(edgeId))
                                {
                                    continue;
                                }
                                JObject edge = new JObject
                                {
                                    new JProperty("id", edgeField.EdgeProperties[KW_EDGE_ID].ToValue),
                                    new JProperty("inV", edgeField.InV)
                                };

                                // Add edge properties
                                JObject properties = new JObject();
                                foreach (string propertyName in edgeField.EdgeProperties.Keys)
                                {
                                    switch (propertyName)
                                    {
                                    case KW_EDGE_ID:
                                    case KW_EDGE_LABEL:
                                    //case KW_EDGE_OFFSET:
                                    case KW_EDGE_SRCV:
                                    case KW_EDGE_SINKV:
                                    case KW_EDGE_SRCV_LABEL:
                                    case KW_EDGE_SINKV_LABEL:
                                    case KW_EDGE_SRCV_PARTITION:
                                    case KW_EDGE_SINKV_PARTITION:
                                        continue;

                                    default:
                                        break;
                                    }

                                    properties.Add(new JProperty(propertyName,
                                                                 JsonDataTypeHelper.GetStringFieldData(edgeField.EdgeProperties[propertyName].PropertyValue,
                                                                                                       edgeField.EdgeProperties[propertyName].JsonDataType)));
                                }
                                edge.Add(new JProperty("properties", properties));
                                group.Add(edge);
                            }

                            if (group.Count != 0)
                            {
                                outE.Add(edgelLabel, group);
                            }
                        }
                    }
                    if (outE.Count != 0)
                    {
                        vertex.Add(new JProperty("outE", outE));
                    }


                    // Add vertex properties
                    JObject vertexProperties = new JObject();
                    foreach (KeyValuePair <string, VertexPropertyField> kvp in vertexField.VertexProperties)
                    {
                        string propertyName = kvp.Key;

                        Debug.Assert(!VertexField.IsVertexMetaProperty(propertyName), "Bug!");
                        Debug.Assert(!(propertyName == KW_VERTEX_EDGE || propertyName == KW_VERTEX_REV_EDGE), "Bug!");

                        JArray propertyArray = new JArray();
                        foreach (VertexSinglePropertyField vsp in kvp.Value.Multiples.Values)
                        {
                            JObject property = new JObject
                            {
                                new JProperty("id", vsp.PropertyId),
                                new JProperty("value",
                                              JsonDataTypeHelper.GetStringFieldData(vsp.PropertyValue, vsp.JsonDataType))
                            };

                            if (vsp.MetaProperties.Count > 0)
                            {
                                JObject metaProperties = new JObject();

                                foreach (KeyValuePair <string, ValuePropertyField> metaKvp in vsp.MetaProperties)
                                {
                                    string             key   = metaKvp.Key;
                                    ValuePropertyField value = metaKvp.Value;

                                    metaProperties.Add(new JProperty(key, JsonDataTypeHelper.GetStringFieldData(value.PropertyValue, value.JsonDataType)));
                                }
                                property.Add(new JProperty("properties", metaProperties));
                            }

                            propertyArray.Add(property);
                        }
                        vertexProperties.Add(new JProperty(propertyName, propertyArray));
                    }

                    vertex.Add(new JProperty("properties", vertexProperties));

                    vertexGraphSON.Add(vertex.ToString(Formatting.None));
                }

                this.subgraphState.graph = new StringField("[" + string.Join(", ", vertexGraphSON) + "]");
            }
            else
            {
                this.subgraphState.graph = new StringField("[]");
            }

            return(this.subgraphState.graph);
        }
Beispiel #5
0
        internal override List <RawRecord> CrossApply(RawRecord record)
        {
            List <RawRecord> results = new List <RawRecord>();

            FieldObject inputTarget = record[this.inputTargetIndex];

            if (inputTarget is VertexField)
            {
                VertexField vertexField = (VertexField)inputTarget;
                foreach (VertexPropertyField property in vertexField.VertexProperties.Values)
                {
                    string propertyName = property.PropertyName;
                    Debug.Assert(!VertexField.IsVertexMetaProperty(propertyName));
                    Debug.Assert(!propertyName.Equals(KW_VERTEX_EDGE));
                    Debug.Assert(!propertyName.Equals(KW_VERTEX_REV_EDGE));

                    switch (propertyName)
                    {
                    case "_rid":
                    case "_self":
                    case "_etag":
                    case "_attachments":
                    case "_ts":
                        continue;

                    default:
                        foreach (VertexSinglePropertyField singleVp in property.Multiples.Values)
                        {
                            RawRecord r = new RawRecord();
                            r.Append(new VertexSinglePropertyField(singleVp));
                            foreach (string metaPropertyName in this.populateMetaProperties)
                            {
                                r.Append(singleVp[metaPropertyName]);
                            }
                            results.Add(r);
                        }
                        break;
                    }
                }
            }
            else if (inputTarget is EdgeField)
            {
                EdgeField edgeField = (EdgeField)inputTarget;
                foreach (KeyValuePair <string, EdgePropertyField> propertyPair in edgeField.EdgeProperties)
                {
                    string            propertyName      = propertyPair.Key;
                    EdgePropertyField edgePropertyField = propertyPair.Value;

                    switch (propertyName)
                    {
                    // Reserved properties for meta-data
                    case KW_EDGE_LABEL:
                    case KW_EDGE_ID:
                    //case KW_EDGE_OFFSET:
                    case KW_EDGE_SRCV:
                    case KW_EDGE_SINKV:
                    case KW_EDGE_SRCV_LABEL:
                    case KW_EDGE_SINKV_LABEL:
                        continue;

                    default:
                        RawRecord r = new RawRecord();
                        r.Append(new EdgePropertyField(edgePropertyField));
                        results.Add(r);
                        break;
                    }
                }

                if (this.populateMetaProperties.Count > 0 && results.Count > 0)
                {
                    throw new GraphViewException("An edge property cannot contain meta properties.");
                }
            }
            else if (inputTarget is VertexSinglePropertyField)
            {
                VertexSinglePropertyField singleVp = (VertexSinglePropertyField)inputTarget;
                foreach (KeyValuePair <string, ValuePropertyField> kvp in singleVp.MetaProperties)
                {
                    RawRecord          r = new RawRecord();
                    ValuePropertyField metaPropertyField = kvp.Value;
                    r.Append(new ValuePropertyField(metaPropertyField));
                    results.Add(r);
                }

                if (this.populateMetaProperties.Count > 0 && results.Count > 0)
                {
                    throw new GraphViewException("An edge property cannot contain meta properties.");
                }
            }
            else
            {
                throw new GraphViewException("The input of properties() cannot be a meta or edge property.");
            }
            return(results);
        }