Skip to content

Commit

Permalink
add Neo4J access module
Browse files Browse the repository at this point in the history
  • Loading branch information
danthe1st committed Aug 1, 2024
1 parent 02b807b commit 1c62da6
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
arebac-neo4j/db

# Created by https://www.toptal.com/developers/gitignore/api/eclipse,java,maven
# Edit at https://www.toptal.com/developers/gitignore?templates=eclipse,java,maven

Expand Down
25 changes: 25 additions & 0 deletions arebac-neo4j/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.github.danthe1st</groupId>
<artifactId>AReBAC</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>arebac-neo4j</artifactId>
<name>AReBAC-Neo4J</name>

<dependencies>
<dependency>
<groupId>io.github.danthe1st</groupId>
<artifactId>arebac-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j</artifactId>
<version>5.22.0</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package io.github.danthe1st.arebac.neo4j.graph;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import io.github.danthe1st.arebac.data.commongraph.attributed.AttributedGraph;
import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.ResourceIterable;
import org.neo4j.graphdb.Transaction;

public class Neo4jDB implements AttributedGraph<Neo4jNode, Neo4jEdge> {
private final Transaction tx;

public Neo4jDB(Transaction tx) {
this.tx = tx;
}

@Override
public Neo4jNode findNodeById(String id) {
return new Neo4jNode(tx.getNodeByElementId(id));
}

@Override
public Collection<Neo4jEdge> findOutgoingEdges(Neo4jNode node) {
return findEdges(node, Direction.OUTGOING);
}

@Override
public Collection<Neo4jEdge> findIncomingEdges(Neo4jNode node) {
return findEdges(node, Direction.INCOMING);
}

private Collection<Neo4jEdge> findEdges(Neo4jNode node, Direction direction) {
Node internalNode = node.getDBNode();
ResourceIterable<Relationship> relationships = internalNode.getRelationships(direction);
List<Neo4jEdge> edges = new ArrayList<>();
for(Relationship relationship : relationships){
edges.add(new Neo4jEdge(relationship));
}
return Collections.unmodifiableList(edges);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package io.github.danthe1st.arebac.neo4j.graph;

import io.github.danthe1st.arebac.data.commongraph.attributed.AttributeValue;
import io.github.danthe1st.arebac.data.commongraph.attributed.AttributedGraphEdge;
import org.neo4j.graphdb.Relationship;

public class Neo4jEdge implements AttributedGraphEdge<Neo4jNode> {
private final Relationship relationship;

public Neo4jEdge(Relationship relationship) {
this.relationship = relationship;
}

@Override
public Neo4jNode source() {
return new Neo4jNode(relationship.getStartNode());
}

@Override
public Neo4jNode target() {
return new Neo4jNode(relationship.getEndNode());
}

@Override
public String id() {
return relationship.getElementId();
}

@Override
public String edgeType() {
return relationship.getType().name();
}

@Override
public AttributeValue<?> getAttribute(String key) {
if(!relationship.hasProperty(key)){
return null;
}
Object property = relationship.getProperty(key);
return switch(property) {
case String s -> AttributeValue.attribute(s);
case Boolean b -> AttributeValue.attribute(b);
case Integer l -> AttributeValue.attribute(l);
default -> throw new UnsupportedOperationException("unknown property type");
};
}

public Relationship getDBEdge() {
return relationship;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.github.danthe1st.arebac.neo4j.graph;

import java.util.Iterator;

import io.github.danthe1st.arebac.data.commongraph.attributed.AttributeValue;
import io.github.danthe1st.arebac.data.commongraph.attributed.AttributedNode;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Node;

public class Neo4jNode implements AttributedNode {
private static final String NODE_TYPE = "_NO_LABEL";

private final Node node;

public Neo4jNode(Node node) {
this.node = node;
}

@Override
public String id() {
return node.getElementId();
}

@Override
public String nodeType() {
Iterator<Label> labelIt = node.getLabels().iterator();
if(labelIt.hasNext()){
return labelIt.next().name();
}
return NODE_TYPE;
}

@Override
public AttributeValue<?> getAttribute(String key) {
if(!node.hasProperty(key)){
return null;
}
Object property = node.getProperty(key);
return switch(property) {
case String s -> AttributeValue.attribute(s);
case Boolean b -> AttributeValue.attribute(b);
case Integer l -> AttributeValue.attribute(l);
default -> throw new UnsupportedOperationException("unknown property type");
};
}

public Node getDBNode() {
return node;
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
</properties>
<modules>
<module>arebac-core</module>
<module>arebac-neo4j</module>
</modules>
<build>
<plugins>
Expand Down

0 comments on commit 1c62da6

Please sign in to comment.