Permalink
Browse files

Allow for multiple indexed relationships from a given node.

  • Loading branch information...
1 parent a3d55a8 commit 9e5117c1d856c4676461db6d5fe87203d689c782 @brycenz brycenz committed Sep 5, 2011
View
18 src/main/java/org/neo4j/collections/indexedrelationship/IndexedRelationship.java
@@ -234,7 +234,7 @@ public void remove() {
this.relType = relType;
this.graphDb = graphDb;
this.direction = direction;
- Relationship rel = node.getSingleRelationship(SortedTree.RelTypes.TREE_ROOT, Direction.OUTGOING);
+ Relationship rel = getIndexedRootRelationship();
rel.setProperty(PROPERTY_TYPE, propertyType.getName());
Node treeNode = ( rel == null ) ? createTreeRoot(node) : rel.getEndNode();
bTree = new PropertySortedTree<T>(graphDb, treeNode, propertyType, isUniqueIndex, relType.name());
@@ -258,7 +258,7 @@ public IndexedRelationship(RelationshipType relType, Direction direction, Compar
this.relType = relType;
this.graphDb = graphDb;
this.direction = direction;
- Relationship rel = node.getSingleRelationship(SortedTree.RelTypes.TREE_ROOT, Direction.OUTGOING);
+ Relationship rel = getIndexedRootRelationship();
Node treeNode = ( rel == null ) ? createTreeRoot(node) : rel.getEndNode();
bTree = new SortedTree(graphDb, treeNode, nodeComparator, isUniqueIndex, relType.name());
}
@@ -302,7 +302,19 @@ public Node getIndexedNode(){
* @return the {@link Relationship} pointing to the root of the index tree.
*/
public Relationship getIndexedRootRelationship(){
- return indexedNode.getSingleRelationship(SortedTree.RelTypes.TREE_ROOT, Direction.OUTGOING);
+ Iterable<Relationship> indexRelationships = this.indexedNode.getRelationships(SortedTree.RelTypes.TREE_ROOT);
+ for(Relationship indexRelationship: indexRelationships){
+ String relName = (String)indexRelationship.getProperty(SortedTree.TREE_NAME);
+ if(relName.equals(relType.name())){
+ if(indexRelationship.hasProperty(IndexedRelationship.directionPropertyName)){
+ String dir = (String)indexRelationship.getProperty(IndexedRelationship.directionPropertyName);
+ if(dir.equals(direction.name())){
+ return indexRelationship;
+ }
+ }
+ }
+ }
+ return null;
}
View
50 src/test/java/org/neo4j/collections/indexedrelationship/TestIndexedRelationship.java
@@ -49,7 +49,8 @@ public int compare(Node n1, Node n2){
{
DIRECT_RELATIONSHIP,
INDEXED_RELATIONSHIP,
- };
+ INDEXED_RELATIONSHIP_TWO
+ }
@Test
public void testIndexRelationshipBasic()
@@ -97,4 +98,51 @@ public void testIndexRelationshipBasic()
}
assertTrue(count == 2);
}
+
+ @Test
+ public void testTwoIndexRelationshipsOnSingleNode() {
+ Node indexedNode = graphDb().createNode();
+ IndexedRelationship ir = new IndexedRelationship(RelTypes.INDEXED_RELATIONSHIP, Direction.OUTGOING, new IdComparator(), true, indexedNode, graphDb());
+ IndexedRelationship ir2 = new IndexedRelationship(RelTypes.INDEXED_RELATIONSHIP_TWO, Direction.OUTGOING, new IdComparator(), true, indexedNode, graphDb());
+
+ Node n1 = graphDb().createNode();
+ n1.setProperty("name", "n1");
+ Node n2 = graphDb().createNode();
+ n2.setProperty("name", "n2");
+ Node n3 = graphDb().createNode();
+ n3.setProperty("name", "n3");
+ Node n4 = graphDb().createNode();
+ n4.setProperty("name", "n4");
+
+ ir.createRelationshipTo(n2);
+ ir.createRelationshipTo(n4);
+ ir2.createRelationshipTo(n1);
+ ir2.createRelationshipTo(n3);
+
+ IndexedRelationshipExpander re1 = new IndexedRelationshipExpander(graphDb(), Direction.OUTGOING, RelTypes.INDEXED_RELATIONSHIP_TWO);
+ IndexedRelationshipExpander re2 = new IndexedRelationshipExpander(graphDb(), Direction.OUTGOING, RelTypes.INDEXED_RELATIONSHIP);
+
+ int count = 0;
+ for(Relationship rel: re1.expand(indexedNode)){
+ if(count == 0){
+ assertTrue( rel.getEndNode().equals(n1) || rel.getEndNode().equals(n3));
+ }
+ if(count == 1){
+ assertTrue( rel.getEndNode().equals(n1) || rel.getEndNode().equals(n3));
+ }
+ count++;
+ }
+ assertTrue(count == 2);
+ count = 0;
+ for(Relationship rel: re2.expand(indexedNode)){
+ if(count == 0){
+ assertTrue( rel.getEndNode().equals(n2) );
+ }
+ if(count == 1){
+ assertTrue( rel.getEndNode().equals(n4) );
+ }
+ count++;
+ }
+ assertTrue(count == 2);
+ }
}

0 comments on commit 9e5117c

Please sign in to comment.