Exemple #1
0
 /// <summary>
 /// Counts the number of incoming relationships from node where the cursor is positioned.
 /// <para>
 /// NOTE: The number of incoming relationships also includes eventual loops.
 ///
 /// </para>
 /// </summary>
 /// <param name="nodeCursor"> a cursor positioned at the node whose relationships we're counting </param>
 /// <param name="cursors"> a factory for cursors </param>
 /// <returns> the number of incoming - including loops - relationships from the node </returns>
 public static int CountIncoming(NodeCursor nodeCursor, CursorFactory cursors)
 {
     if (nodeCursor.Dense)
     {
         using (RelationshipGroupCursor group = cursors.AllocateRelationshipGroupCursor())
         {
             nodeCursor.Relationships(group);
             int count = 0;
             while (group.next())
             {
                 count += group.IncomingCount() + group.LoopCount();
             }
             return(count);
         }
     }
     else
     {
         using (RelationshipTraversalCursor traversal = cursors.AllocateRelationshipTraversalCursor())
         {
             int count = 0;
             nodeCursor.AllRelationships(traversal);
             while (traversal.next())
             {
                 if (traversal.TargetNodeReference() == nodeCursor.NodeReference())
                 {
                     count++;
                 }
             }
             return(count);
         }
     }
 }
Exemple #2
0
 /// <summary>
 /// Counts all the relationships of the given type from node where the cursor is positioned.
 /// </summary>
 /// <param name="nodeCursor"> a cursor positioned at the node whose relationships we're counting </param>
 /// <param name="cursors"> a factory for cursors </param>
 /// <param name="type"> the type of the relationship we're counting </param>
 /// <returns> the number relationships from the node with the given type </returns>
 public static int CountAll(NodeCursor nodeCursor, CursorFactory cursors, int type)
 {
     if (nodeCursor.Dense)
     {
         using (RelationshipGroupCursor group = cursors.AllocateRelationshipGroupCursor())
         {
             nodeCursor.Relationships(group);
             int count = 0;
             while (group.next())
             {
                 if (group.Type() == type)
                 {
                     return(group.TotalCount());
                 }
             }
             return(count);
         }
     }
     else
     {
         using (RelationshipTraversalCursor traversal = cursors.AllocateRelationshipTraversalCursor())
         {
             int count = 0;
             nodeCursor.AllRelationships(traversal);
             while (traversal.next())
             {
                 if (traversal.Type() == type)
                 {
                     count++;
                 }
             }
             return(count);
         }
     }
 }
Exemple #3
0
 /// <summary>
 /// Counts the number of outgoing relationships of the given type from node where the cursor is positioned.
 /// <para>
 /// NOTE: The number of outgoing relationships also includes eventual loops.
 ///
 /// </para>
 /// </summary>
 /// <param name="nodeCursor"> a cursor positioned at the node whose relationships we're counting </param>
 /// <param name="cursors"> a factory for cursors </param>
 /// <param name="type"> the type of the relationship we're counting </param>
 /// <returns> the number of outgoing - including loops - relationships from the node with the given type </returns>
 public static int CountOutgoing(NodeCursor nodeCursor, CursorFactory cursors, int type)
 {
     if (nodeCursor.Dense)
     {
         using (RelationshipGroupCursor group = cursors.AllocateRelationshipGroupCursor())
         {
             nodeCursor.Relationships(group);
             while (group.next())
             {
                 if (group.Type() == type)
                 {
                     return(group.OutgoingCount() + group.LoopCount());
                 }
             }
             return(0);
         }
     }
     else
     {
         using (RelationshipTraversalCursor traversal = cursors.AllocateRelationshipTraversalCursor())
         {
             int count = 0;
             nodeCursor.AllRelationships(traversal);
             while (traversal.next())
             {
                 if (traversal.SourceNodeReference() == nodeCursor.NodeReference() && traversal.Type() == type)
                 {
                     count++;
                 }
             }
             return(count);
         }
     }
 }
Exemple #4
0
        public override RelationshipTraversalCursor AllocateRelationshipTraversalCursor()
        {
            RelationshipTraversalCursor n = _cursors.allocateRelationshipTraversalCursor();

            _allCursors.Add(n);
            return(n);
        }
 private void Init(RelationshipTraversalCursor relationshipCursor, int[] types, Dir targetDirection)
 {
     this.Cursor           = relationshipCursor;
     this._types           = types;
     this._targetDirection = targetDirection;
     this._onRelationship  = false;
     this._firstNext       = true;
 }
Exemple #6
0
 /// <summary>
 /// Traverse all incoming relationships including loops of the provided relationship types.
 /// </summary>
 /// <param name="groupCursor"> Group cursor to use. Pre-initialized on node. </param>
 /// <param name="relationshipCursor"> Relationship traversal cursor to use. </param>
 /// <param name="types"> Relationship types to traverse </param>
 public void Incoming(RelationshipGroupCursor groupCursor, RelationshipTraversalCursor relationshipCursor, int[] types)
 {
     this._groupCursor       = groupCursor;
     this.RelationshipCursor = relationshipCursor;
     this._types             = types;
     this._directions[0]     = Dir.In;
     this._directions[1]     = Dir.Loop;
     this._nDirections       = 2;
     this._currentDirection  = _directions.Length;
     this._onRelationship    = false;
     this._onGroup           = false;
     this._foundTypes        = 0;
 }
 public virtual void Close()
 {
     try
     {
         if (Cursor != null)
         {
             Cursor.close();
         }
     }
     finally
     {
         Cursor = null;
     }
 }
Exemple #8
0
        private static void SetupAllSparse(RelationshipSparseSelection sparseSelection, CursorFactory cursors, NodeCursor node, int[] types)
        {
            RelationshipTraversalCursor traversalCursor = cursors.AllocateRelationshipTraversalCursor();

            try
            {
                node.AllRelationships(traversalCursor);
                sparseSelection.All(traversalCursor, types);
            }
            catch (Exception t)
            {
                traversalCursor.close();
                throw t;
            }
        }
Exemple #9
0
        private static void SetupAllDense(RelationshipDenseSelection denseSelection, CursorFactory cursors, NodeCursor node, int[] types)
        {
            RelationshipGroupCursor     groupCursor     = cursors.AllocateRelationshipGroupCursor();
            RelationshipTraversalCursor traversalCursor = cursors.AllocateRelationshipTraversalCursor();

            try
            {
                node.Relationships(groupCursor);
                denseSelection.All(groupCursor, traversalCursor, types);
            }
            catch (Exception t)
            {
                groupCursor.close();
                traversalCursor.close();
                throw t;
            }
        }
Exemple #10
0
        public virtual void Close()
        {
            Exception closeGroupError = null;

            try
            {
                if (_groupCursor != null)
                {
                    _groupCursor.close();
                }
            }
            catch (Exception t)
            {
                closeGroupError = t;
            }

            try
            {
                if (RelationshipCursor != null)
                {
                    RelationshipCursor.close();
                }
            }
            catch (Exception t)
            {
                if (closeGroupError != null)
                {
                    t.addSuppressed(closeGroupError);
                }
                throw t;
            }
            finally
            {
                RelationshipCursor = null;
                _groupCursor       = null;
            }
        }
Exemple #11
0
 /// <summary>
 /// Traverse all outgoing relationships including loops of the provided relationship types.
 /// </summary>
 /// <param name="groupCursor"> Group cursor to use. Pre-initialized on node. </param>
 /// <param name="relationshipCursor"> Relationship traversal cursor to use. </param>
 public void Outgoing(RelationshipGroupCursor groupCursor, RelationshipTraversalCursor relationshipCursor)
 {
     Outgoing(groupCursor, relationshipCursor, null);
 }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldTraverseRelationshipsOfGivenType()
        public virtual void ShouldTraverseRelationshipsOfGivenType()
        {
            // given
            using (NodeCursor node = cursors.allocateNodeCursor(), RelationshipGroupCursor group = cursors.allocateRelationshipGroupCursor(), RelationshipTraversalCursor relationship = cursors.allocateRelationshipTraversalCursor())
            {
                int empty = 0;
                // when
                read.allNodesScan(node);
                while (node.Next())
                {
                    node.Relationships(group);
                    bool none = true;
                    while (group.next())
                    {
                        none = false;
                        Sizes degree = new Sizes();
                        group.Outgoing(relationship);
                        while (relationship.next())
                        {
                            assertEquals("node #" + node.NodeReference() + " relationship should have same label as group", group.Type(), relationship.Type());
                            degree.Outgoing++;
                        }
                        group.Incoming(relationship);
                        while (relationship.next())
                        {
                            assertEquals("node #" + node.NodeReference() + "relationship should have same label as group", group.Type(), relationship.Type());
                            degree.Incoming++;
                        }
                        group.Loops(relationship);
                        while (relationship.next())
                        {
                            assertEquals("node #" + node.NodeReference() + "relationship should have same label as group", group.Type(), relationship.Type());
                            degree.Loop++;
                        }

                        // then
                        assertNotEquals("all", 0, degree.Incoming + degree.Outgoing + degree.Loop);
                        assertEquals("node #" + node.NodeReference() + " outgoing", group.OutgoingCount(), degree.Outgoing);
                        assertEquals("node #" + node.NodeReference() + " incoming", group.IncomingCount(), degree.Incoming);
                        assertEquals("node #" + node.NodeReference() + " loop", group.LoopCount(), degree.Loop);
                        assertEquals("node #" + node.NodeReference() + " all = incoming + outgoing - loop", group.TotalCount(), degree.Incoming + degree.Outgoing + degree.Loop);
                    }
                    if (none)
                    {
                        empty++;
                    }
                }

                // then
                assertEquals("number of empty nodes", 1, empty);
            }
        }
Exemple #13
0
 /// <summary>
 /// Traverse all relationships of the provided relationship types.
 /// </summary>
 /// <param name="groupCursor"> Group cursor to use. Pre-initialized on node. </param>
 /// <param name="relationshipCursor"> Relationship traversal cursor to use. </param>
 public void All(RelationshipGroupCursor groupCursor, RelationshipTraversalCursor relationshipCursor)
 {
     All(groupCursor, relationshipCursor, null);
 }
 /// <summary>
 /// Traverse all relationships of the provided relationship types.
 /// </summary>
 /// <param name="relationshipCursor"> Relationship traversal cursor to use. Pre-initialized on node. </param>
 public void All(RelationshipTraversalCursor relationshipCursor)
 {
     Init(relationshipCursor, null, Dir.Both);
 }
 /// <summary>
 /// Traverse all relationships of the provided relationship types.
 /// </summary>
 /// <param name="relationshipCursor"> Relationship traversal cursor to use. Pre-initialized on node. </param>
 /// <param name="types"> Relationship types to traverse </param>
 public void All(RelationshipTraversalCursor relationshipCursor, int[] types)
 {
     Init(relationshipCursor, types, Dir.Both);
 }
 private void assertEmptyAndClosed(Traverser traverser, RelationshipTraversalCursor inner)
 {
     AssertEmpty(traverser);
     assertTrue("closed traversal cursor", inner.Closed);
 }
 /// <summary>
 /// Traverse all incoming relationships including loops of the provided relationship types.
 /// </summary>
 /// <param name="relationshipCursor"> Relationship traversal cursor to use. Pre-initialized on node. </param>
 /// <param name="types"> Relationship types to traverse </param>
 public void Incoming(RelationshipTraversalCursor relationshipCursor, int[] types)
 {
     Init(relationshipCursor, types, Dir.In);
 }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: static void assertCount(org.neo4j.internal.kernel.api.Transaction transaction, RelationshipTraversalCursor relationship, java.util.Map<String,int> expectedCounts, int expectedType, org.neo4j.graphdb.Direction direction) throws org.neo4j.internal.kernel.api.exceptions.KernelException
        internal static void AssertCount([email protected] transaction, RelationshipTraversalCursor relationship, IDictionary <string, int> expectedCounts, int expectedType, Direction direction)
        {
            string key           = ComputeKey(transaction.Token().relationshipTypeName(expectedType), direction);
            int    expectedCount = expectedCounts.getOrDefault(key, 0);
            int    count         = 0;

            while (relationship.next())
            {
                assertEquals("same type", expectedType, relationship.Type());
                count++;
            }

            assertEquals(format("expected number of relationships for key '%s'", key), expectedCount, count);
        }
Exemple #19
0
 /// <summary>
 /// Traverse all incoming relationships including loops of the provided relationship types.
 /// </summary>
 /// <param name="groupCursor"> Group cursor to use. Pre-initialized on node. </param>
 /// <param name="relationshipCursor"> Relationship traversal cursor to use. </param>
 public void Incoming(RelationshipGroupCursor groupCursor, RelationshipTraversalCursor relationshipCursor)
 {
     Incoming(groupCursor, relationshipCursor, null);
 }
Exemple #20
0
 public override void Relationships(long nodeReference, long reference, RelationshipTraversalCursor cursor)
 {
     throw new System.NotSupportedException();
 }
Exemple #21
0
 public override void Relationships(long nodeReference, long reference, RelationshipTraversalCursor cursor)
 {
     (( DefaultRelationshipTraversalCursor )cursor).Init(nodeReference, reference, this);
 }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldManageRandomTraversals()
        public virtual void ShouldManageRandomTraversals()
        {
            // given
            try
            {
                using (NodeCursor node = cursors.allocateNodeCursor(), RelationshipGroupCursor group = cursors.allocateRelationshipGroupCursor(), RelationshipTraversalCursor relationship = cursors.allocateRelationshipTraversalCursor())
                {
                    for (int i = 0; i < N_TRAVERSALS; i++)
                    {
                        // when
                        long nodeId = _nodeIds[_random.Next(_nNodes)];
                        read.singleNode(nodeId, node);
                        assertTrue("access root node", node.Next());
                        node.Relationships(group);
                        assertFalse("single root", node.Next());

                        // then
                        while (group.next())
                        {
                            group.Incoming(relationship);
                            while (relationship.next())
                            {
                                assertEquals("incoming origin", nodeId, relationship.OriginNodeReference());
                                relationship.Neighbour(node);
                            }
                            group.Outgoing(relationship);
                            while (relationship.next())
                            {
                                assertEquals("outgoing origin", nodeId, relationship.OriginNodeReference());
                                relationship.Neighbour(node);
                            }
                            group.Loops(relationship);
                            while (relationship.next())
                            {
                                assertEquals("loop origin", nodeId, relationship.OriginNodeReference());
                                relationship.Neighbour(node);
                            }
                        }
                    }
                }
            }
            catch (Exception t)
            {
                throw new Exception("Failed with random seed " + _seed, t);
            }
        }
Exemple #23
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldTraverseTreeOfDepthThree()
        public virtual void ShouldTraverseTreeOfDepthThree()
        {
            using (NodeCursor node = cursors.allocateNodeCursor(), RelationshipGroupCursor group = cursors.allocateRelationshipGroupCursor(), RelationshipTraversalCursor relationship1 = cursors.allocateRelationshipTraversalCursor(), RelationshipTraversalCursor relationship2 = cursors.allocateRelationshipTraversalCursor())
            {
                MutableLongSet leafs = new LongHashSet();
                long           total = 0;

                // when
                read.singleNode(_threeRoot, node);
                assertTrue("access root node", node.Next());
                node.Relationships(group);
                assertFalse("single root", node.Next());

                assertTrue("access group of root", group.next());
                group.Incoming(relationship1);
                assertFalse("single group of root", group.next());

                while (relationship1.next())
                {
                    relationship1.Neighbour(node);

                    assertTrue("child level 1", node.Next());
                    node.Relationships(group);
                    assertFalse("single node", node.Next());

                    assertTrue("group of level 1 child", group.next());
                    group.Incoming(relationship2);
                    assertFalse("single group of level 1 child", group.next());

                    while (relationship2.next())
                    {
                        leafs.add(relationship2.NeighbourNodeReference());
                        total++;
                    }
                }

                // then
                assertEquals("total number of leaf nodes", _expectedTotal, total);
                assertEquals("number of distinct leaf nodes", _expectedUnique, leafs.size());
            }
        }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldFollowSpecificRelationship()
        public virtual void ShouldFollowSpecificRelationship()
        {
            // given
            using (NodeCursor node = cursors.allocateNodeCursor(), RelationshipGroupCursor group = cursors.allocateRelationshipGroupCursor(), RelationshipTraversalCursor relationship = cursors.allocateRelationshipTraversalCursor())
            {
                // when - traversing from start to end
                read.singleNode(_start, node);
                assertTrue("access start node", node.Next());
                node.Relationships(group);
                assertTrue("access relationship group", group.next());
                group.Outgoing(relationship);
                assertTrue("access outgoing relationships", relationship.next());

                // then
                assertEquals("source node", _start, relationship.SourceNodeReference());
                assertEquals("target node", _end, relationship.TargetNodeReference());

                assertEquals("node of origin", _start, relationship.OriginNodeReference());
                assertEquals("neighbouring node", _end, relationship.NeighbourNodeReference());

                assertEquals("relationship should have same label as group", group.Type(), relationship.Type());

                assertFalse("only a single relationship", relationship.next());

                group.Incoming(relationship);
                assertFalse("no incoming relationships", relationship.next());
                group.Loops(relationship);
                assertFalse("no loop relationships", relationship.next());

                assertFalse("only a single group", group.next());

                // when - traversing from end to start
                read.singleNode(_end, node);
                assertTrue("access start node", node.Next());
                node.Relationships(group);
                assertTrue("access relationship group", group.next());
                group.Incoming(relationship);
                assertTrue("access incoming relationships", relationship.next());

                // then
                assertEquals("source node", _start, relationship.SourceNodeReference());
                assertEquals("target node", _end, relationship.TargetNodeReference());

                assertEquals("node of origin", _end, relationship.OriginNodeReference());
                assertEquals("neighbouring node", _start, relationship.NeighbourNodeReference());

                assertEquals("relationship should have same label as group", group.Type(), relationship.Type());

                assertFalse("only a single relationship", relationship.next());

                group.Outgoing(relationship);
                assertFalse("no outgoing relationships", relationship.next());
                group.Loops(relationship);
                assertFalse("no loop relationships", relationship.next());

                assertFalse("only a single group", group.next());
            }
        }
 /// <summary>
 /// Traverse all incoming relationships including loops of the provided relationship types.
 /// </summary>
 /// <param name="relationshipCursor"> Relationship traversal cursor to use. Pre-initialized on node. </param>
 public void Incoming(RelationshipTraversalCursor relationshipCursor)
 {
     Init(relationshipCursor, null, Dir.In);
 }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private void traverseViaGroups(RelationshipTestSupport.StartNode start, boolean detached) throws org.neo4j.internal.kernel.api.exceptions.KernelException
        private void TraverseViaGroups(RelationshipTestSupport.StartNode start, bool detached)
        {
            // given
            IDictionary <string, int> expectedCounts = start.ExpectedCounts();

            using (NodeCursor node = cursors.allocateNodeCursor(), RelationshipGroupCursor group = cursors.allocateRelationshipGroupCursor(), RelationshipTraversalCursor relationship = cursors.allocateRelationshipTraversalCursor())
            {
                // when
                read.singleNode(start.Id, node);
                assertTrue("access node", node.Next());
                if (detached)
                {
                    read.relationshipGroups(start.Id, node.RelationshipGroupReference(), group);
                }
                else
                {
                    node.Relationships(group);
                }

                while (group.next())
                {
                    // outgoing
                    if (detached)
                    {
                        read.relationships(start.Id, group.OutgoingReference(), relationship);
                    }
                    else
                    {
                        group.Outgoing(relationship);
                    }
                    // then
                    assertCount(tx, relationship, expectedCounts, group.Type(), OUTGOING);

                    // incoming
                    if (detached)
                    {
                        read.relationships(start.Id, group.IncomingReference(), relationship);
                    }
                    else
                    {
                        group.Incoming(relationship);
                    }
                    // then
                    assertCount(tx, relationship, expectedCounts, group.Type(), INCOMING);

                    // loops
                    if (detached)
                    {
                        read.relationships(start.Id, group.LoopsReference(), relationship);
                    }
                    else
                    {
                        group.Loops(relationship);
                    }
                    // then
                    assertCount(tx, relationship, expectedCounts, group.Type(), BOTH);
                }
            }
        }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: static java.util.Map<String,int> count(org.neo4j.internal.kernel.api.Transaction transaction, RelationshipTraversalCursor relationship) throws org.neo4j.internal.kernel.api.exceptions.KernelException
        internal static IDictionary <string, int> Count([email protected] transaction, RelationshipTraversalCursor relationship)
        {
            Dictionary <string, int> counts = new Dictionary <string, int>();

            while (relationship.next())
            {
                string key = ComputeKey(transaction, relationship);
                Counts.compute(key, (k, value) => value == null ? 1 : value + 1);
            }
            return(counts);
        }
 /// <summary>
 /// Traverse all outgoing relationships including loops of the provided relationship types.
 /// </summary>
 /// <param name="relationshipCursor"> Relationship traversal cursor to use. Pre-initialized on node. </param>
 public void Outgoing(RelationshipTraversalCursor relationshipCursor)
 {
     Init(relationshipCursor, null, Dir.Out);
 }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private static String computeKey(org.neo4j.internal.kernel.api.Transaction transaction, RelationshipTraversalCursor r) throws org.neo4j.internal.kernel.api.exceptions.KernelException
        private static string ComputeKey([email protected] transaction, RelationshipTraversalCursor r)
        {
            Direction d;

            if (r.SourceNodeReference() == r.TargetNodeReference())
            {
                d = Direction.BOTH;
            }
            else if (r.SourceNodeReference() == r.OriginNodeReference())
            {
                d = Direction.OUTGOING;
            }
            else
            {
                d = Direction.INCOMING;
            }

            return(ComputeKey(transaction.Token().relationshipTypeName(r.Type()), d));
        }
 /// <summary>
 /// Traverse all outgoing relationships including loops of the provided relationship types.
 /// </summary>
 /// <param name="relationshipCursor"> Relationship traversal cursor to use. Pre-initialized on node. </param>
 /// <param name="types"> Relationship types to traverse </param>
 public void Outgoing(RelationshipTraversalCursor relationshipCursor, int[] types)
 {
     Init(relationshipCursor, types, Dir.Out);
 }