Exemple #1
0
 public static void AddRelation(this BasePenguin self, PenguinRelationshipDirection direction, string relationName, List <long> id)
 {
     id.ForEach(i =>
     {
         var basePenguinRelationship = new BasePenguinRelationship(direction, new RelationDatastore(relationName), i);
         self.DirtyDatastore.Relations.CreateOrAddToList(relationName, basePenguinRelationship);
     });
 }
Exemple #2
0
 private static object ConvertToNeo4jRelationObj(long penguinId, BasePenguinRelationship relation)
 {
     if (relation.Direction == PenguinRelationshipDirection.OUT)
     {
         return new { name = relation.RelationName, from = penguinId, to = relation.Target.ID.Value, param = RelationDatastore.Combine(relation.Datastore, relation.DirtyDatastore).Attributes }
     }
     ;
     else
     {
         return new { name = relation.RelationName, from = relation.Target.ID.Value, to = penguinId, param = RelationDatastore.Combine(relation.Datastore, relation.DirtyDatastore).Attributes }
     };
 }
Exemple #3
0
        public static List <BasePenguin> GetPenguinById(ISession session, List <long> ids)
        {
            var objResult       = session.Run("MATCH (obj) WHERE ID(obj) IN $ids RETURN obj", new { ids });
            var relationsResult = session.Run(
                "MATCH (relatedObj)-[relation]->(obj) WHERE id(obj) IN $ids RETURN id(obj) as id, 'IN' AS direction, relatedObj, relation " +
                "UNION MATCH (obj)-[relation]->(relatedObj) WHERE id(obj) IN $ids RETURN id(obj) as id, 'OUT' AS direction, relatedObj, relation", new { ids });

            var objs = objResult.Select(i => i["obj"].As <INode>()).ToList();

            if (objs.Count != ids.Count)
            {
                throw new PersistenceException("Key not Exist:" + string.Join(",", ids.Except(objs.ConvertAll(i => i.Id))));
            }

            var datastoreDic = new Dictionary <long, Datastore>();

            objs.ForEach(i =>
            {
                var ds = new Datastore(i.Labels.First())
                {
                    Attributes = i.Properties.ToDictionary()
                };
                datastoreDic.Add(i.Id, ds);
            });

            foreach (var relationRecord in relationsResult)
            {
                long id           = relationRecord["id"].As <long>();
                var  relationInfo = relationRecord["relation"].As <IRelationship>();

                var relationDatastore = new RelationDatastore(relationInfo.Type)
                {
                    Attributes = relationInfo.Properties.ToDictionary()
                };
                var direction = relationRecord["direction"].As <string>() == "IN" ? PenguinRelationshipDirection.IN : PenguinRelationshipDirection.OUT;
                var relation  = new BasePenguinRelationship(direction, relationDatastore, direction == PenguinRelationshipDirection.IN ? relationInfo.StartNodeId : relationInfo.EndNodeId);
                datastoreDic[id].Relations.CreateOrAddToList(relationInfo.Type, relation);
            }

            var resultPenguins = datastoreDic.ToList().Select(i => new BasePenguin(i.Key, i.Value)).ToList();

            return(resultPenguins);
        }