Skip to content
This repository has been archived by the owner on Dec 5, 2023. It is now read-only.

Commit

Permalink
Delegate implementation of Relationship interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Kleppmann committed Jun 14, 2009
1 parent c2a0a22 commit 0ad82dd
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 8 deletions.
@@ -0,0 +1,60 @@
package com.eptcomputing.neo4j.version;

import org.neo4j.api.core.Node;
import org.neo4j.api.core.Relationship;
import org.neo4j.api.core.RelationshipType;


/**
* Implementation of the boring/repetitive parts of the Neo4j Relationship interface,
* to support alternative implementations like versioned resources.
*
* See the Neo4j Relationship interface for documentation.
*/
abstract class RelationshipImplBase extends PrimitiveBase implements Relationship, Comparable<Relationship> {

public abstract long getId();

public abstract Node getStartNode();

public abstract Node getEndNode();

public abstract RelationshipType getType();

public abstract void delete();

public Node[] getNodes() {
return new Node[] { getStartNode(), getEndNode() };
}

public Node getOtherNode(Node node) {
if (getStartNode().equals(node)) return getEndNode();
else if (getEndNode().equals(node)) return getStartNode();
throw new RuntimeException("Node[" + node.getId() + "] not connected to this relationship[" + getId() + "]");
}

public boolean isType(RelationshipType otherType) {
return (otherType != null) && otherType.name().equals(getType().name());
}

public String toString() {
return "Relationship #" + this.getId() + " of type " + getType().name() +
" between Node[" + getStartNode().getId() + "] and Node[" + getEndNode().getId() + "]";
}

public int compareTo(Relationship r) {
int ourId = (int) this.getId(), theirId = (int) r.getId();
if (ourId < theirId) return -1;
else if (ourId > theirId) return 1;
else return 0;
}

public boolean equals(Object o) {
if (!(o instanceof Relationship)) return false;
return this.getId() == ((Relationship) o).getId();
}

public int hashCode() {
return (int) getId();
}
}
47 changes: 39 additions & 8 deletions src/main/scala/com/eptcomputing/neo4j/version/NeoClasses.scala
Expand Up @@ -3,19 +3,39 @@ package com.eptcomputing.neo4j.version
import java.io.Serializable import java.io.Serializable
import org.neo4j.api.core._ import org.neo4j.api.core._


// THIS IS WORK IN PROGRESS
//
// The idea is to support features such as version control or access control via
// additional properties and relationships in the graph, but to hide them from
// application query code by wrapping them transparently and exposing an API
// fully compatible to Neo4j's own API. Fortunately that API is not too complicated,
// so implemeting the interfaces with our own classes is not too troublesome.
//
// At the moment, these classes simply delegate to an underlying 'real' Neo4j object.

private class VersionedNeo(delegate: NeoService) extends NeoService { private class VersionedNeo(delegate: NeoService) extends NeoService {
def beginTx: Transaction = delegate.beginTx def beginTx: Transaction = delegate.beginTx

def createNode: Node = delegate.createNode def createNode: Node = delegate.createNode

def enableRemoteShell = delegate.enableRemoteShell def enableRemoteShell = delegate.enableRemoteShell

def enableRemoteShell(initialProperties: java.util.Map[String,Serializable]) = delegate.enableRemoteShell(initialProperties) def enableRemoteShell(initialProperties: java.util.Map[String,Serializable]) = delegate.enableRemoteShell(initialProperties)

def getAllNodes: java.lang.Iterable[Node] = delegate.getAllNodes def getAllNodes: java.lang.Iterable[Node] = delegate.getAllNodes

def getNodeById(id: Long): Node = delegate.getNodeById(id) def getNodeById(id: Long): Node = delegate.getNodeById(id)

def getReferenceNode: Node = delegate.getReferenceNode def getReferenceNode: Node = delegate.getReferenceNode

def getRelationshipById(id: Long): Relationship = delegate.getRelationshipById(id) def getRelationshipById(id: Long): Relationship = delegate.getRelationshipById(id)

def getRelationshipTypes: java.lang.Iterable[RelationshipType] = delegate.getRelationshipTypes def getRelationshipTypes: java.lang.Iterable[RelationshipType] = delegate.getRelationshipTypes

def shutdown = delegate.shutdown def shutdown = delegate.shutdown
} }



private class NodeImpl(delegate: Node) extends NodeImplBase { private class NodeImpl(delegate: Node) extends NodeImplBase {
def getId: Long = delegate.getId def getId: Long = delegate.getId


Expand Down Expand Up @@ -45,14 +65,25 @@ private class NodeImpl(delegate: Node) extends NodeImplBase {
def removeProperty(key: String): java.lang.Object = delegate.removeProperty(key) def removeProperty(key: String): java.lang.Object = delegate.removeProperty(key)
} }


private abstract class RelationshipImpl extends Relationship {

}


private abstract class TraversalPositionImpl extends TraversalPosition { private class RelationshipImpl(delegate: Relationship) extends RelationshipImplBase {

def getId: Long = delegate.getId
}


private abstract class TraverserImpl extends Traverser { def getStartNode: Node = delegate.getStartNode


def getEndNode: Node = delegate.getEndNode

def getType: RelationshipType = delegate.getType

def delete = delegate.delete

def getProperty(key: String): java.lang.Object = delegate.getProperty(key)

def getPropertyKeys: java.lang.Iterable[String] = delegate.getPropertyKeys

def getPropertyValues: java.lang.Iterable[java.lang.Object] = delegate.getPropertyValues

def setProperty(key: String, value: java.lang.Object) = delegate.setProperty(key, value)

def removeProperty(key: String): java.lang.Object = delegate.removeProperty(key)
} }

0 comments on commit 0ad82dd

Please sign in to comment.