Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added mechanism to decorate GraphSail's DataStore #522

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public class GraphSail<T extends KeyIndexableGraph> extends NotifyingSailBase im

private static final String NAMESPACES_VERTEX_ID = "urn:com.tinkerpop.blueprints.pgm.oupls.sail:namespaces";

private final DataStore store = new DataStore();
private final DataStore<T> store;

/**
* Create a new RDF store using the provided Blueprints graph. Default edge indices ("p,c,pc") will be used.
Expand Down Expand Up @@ -133,6 +133,7 @@ public void handleComment(String s) throws RDFHandlerException {}
* To use GraphSail with a base Graph which does not support edge indices, provide "" as the argument.
*/
public GraphSail(final T graph, final String indexedPatterns) {
store = createStore();
store.sail = this;
store.graph = graph;

Expand Down Expand Up @@ -247,11 +248,15 @@ public String toString() {
}

////////////////////////////////////////////////////////////////////////////
protected DataStore<T> createStore() {
return new DataStore<T>();
}


/**
* A context object which is shared between the Sail and its Connections
*/
class DataStore {
protected static class DataStore<T extends KeyIndexableGraph> {
public T graph;
public NotifyingSailBase sail;

Expand All @@ -271,7 +276,7 @@ class DataStore {

public Vertex getReferenceVertex() {
//System.out.println("value = " + value);
Iterable<Vertex> i = store.graph.getVertices(VALUE, NAMESPACES_VERTEX_ID);
Iterable<Vertex> i = graph.getVertices(VALUE, NAMESPACES_VERTEX_ID);
// TODO: restore the close()
//try {
Iterator<Vertex> iter = i.iterator();
Expand Down Expand Up @@ -309,7 +314,7 @@ public Vertex addVertex(final Value value) {
}

public Vertex getVertex(final Value value) {
for (Vertex v : store.graph.getVertices(VALUE, value.stringValue())) {
for (Vertex v : graph.getVertices(VALUE, value.stringValue())) {
if (matches(v, value)) {
return v;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public Neo4jEdgeIterable(final Iterable<Relationship> relationships, final Neo4j
}

public Iterator<Neo4jEdge> iterator() {
graph.autoStartTransaction();
return new Iterator<Neo4jEdge>() {
private final Iterator<Relationship> itty = relationships.iterator();
private Relationship nextRelationship = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,6 @@ public <T extends Element> Index<T> getIndex(final String indexName, final Class

/**
* {@inheritDoc}
* <p/>
* Note that this method will force a successful closing of the current
* thread's transaction. As such, once the index is dropped, the operation
* is committed.
Expand Down Expand Up @@ -348,7 +347,6 @@ else if (id instanceof Number)

/**
* {@inheritDoc}
* <p/>
* The underlying Neo4j graph does not natively support this method within a
* transaction. If the graph is not currently in a transaction, then the
* operation runs efficiently and correctly. If the graph is currently in a
Expand All @@ -372,7 +370,6 @@ public Iterable<Vertex> getVertices(final String key, final Object value) {

/**
* {@inheritDoc}
* <p/>
* The underlying Neo4j graph does not natively support this method within a
* transaction. If the graph is not currently in a transaction, then the
* operation runs efficiently and correctly. If the graph is currently in a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public void put(final String key, final Object value, final T element) {

/**
* {@inheritDoc}
* <p/>
* The underlying Neo4j graph does not natively support this method within a transaction.
* If the graph is not currently in a transaction, then the operation runs efficiently.
* If the graph is in a transaction, then, for every element, a try/catch is used to determine if its in the current transaction.
Expand All @@ -70,7 +69,6 @@ public CloseableIterable<T> get(final String key, final Object value) {

/**
* {@inheritDoc}
* <p/>
* The underlying Neo4j graph does not natively support this method within a transaction.
* If the graph is not currently in a transaction, then the operation runs efficiently.
* If the graph is in a transaction, then, for every element, a try/catch is used to determine if its in the current transaction.
Expand All @@ -85,7 +83,6 @@ public CloseableIterable<T> query(final String key, final Object query) {

/**
* {@inheritDoc}
* <p/>
* The underlying Neo4j graph does not natively support this method within a transaction.
* If the graph is not currently in a transaction, then the operation runs efficiently.
* If the graph is in a transaction, then, for every element, a try/catch is used to determine if its in the current transaction.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
/**
* A Blueprints implementation of the Neo4j batch inserter for bulk loading data into a Neo4j graph.
* This is a single threaded, non-transactional bulk loader and should not be used for any other reason than for massive initial data loads.
* <p/>
* Neo4jBatchGraph is <b>not</b> a completely faithful Blueprints implementation.
* Many methods throw UnsupportedOperationExceptions and take unique arguments. Be sure to review each method's JavaDoc.
* The Neo4j "reference node" (vertex 0) is automatically created and is not removed until the database is shutdown() (do not add edges to the reference node).
Expand Down Expand Up @@ -205,7 +204,6 @@ public BatchInserter getRawGraph() {

/**
* {@inheritDoc}
* <p/>
* The object id can either be null, a long id, or a Map&lt;String,Object&gt;.
* If null, then an internal long is provided on the construction of the vertex.
* If a long id is provided, then the vertex is constructed with that long id.
Expand Down Expand Up @@ -268,30 +266,20 @@ public Vertex getVertex(final Object id) {
}
}

/**
* @throws UnsupportedOperationException
*/
public Iterable<Vertex> getVertices() throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}

/**
* @throws UnsupportedOperationException
*/
public Iterable<Vertex> getVertices(final String key, final Object value) throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}

/**
* @throws UnsupportedOperationException
*/
public void removeVertex(final Vertex vertex) throws UnsupportedOperationException {
throw new UnsupportedOperationException(Neo4jBatchTokens.DELETE_OPERATION_MESSAGE);
}

/**
* {@inheritDoc}
* <p/>
* The object id must be a Map&lt;String,Object&gt; or null.
* The id is the properties written when the vertex is created.
* While it is possible to Edge.setProperty(), this method is faster.
Expand All @@ -313,30 +301,18 @@ public Edge addEdge(final Object id, final Vertex outVertex, final Vertex inVert
return new Neo4jBatchEdge(this, finalId, label);
}

/**
* @throws UnsupportedOperationException
*/
public Edge getEdge(final Object id) throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}

/**
* @throws UnsupportedOperationException
*/
public Iterable<Edge> getEdges() throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}

/**
* @throws UnsupportedOperationException
*/
public Iterable<Edge> getEdges(final String key, final Object value) throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}

/**
* @throws UnsupportedOperationException
*/
public void removeEdge(final Edge edge) throws UnsupportedOperationException {
throw new UnsupportedOperationException(Neo4jBatchTokens.DELETE_OPERATION_MESSAGE);
}
Expand Down Expand Up @@ -366,9 +342,6 @@ public Iterable<Index<? extends Element>> getIndices() {
return (Iterable) this.indices.values();
}

/**
* @throws UnsupportedOperationException
*/
public void dropIndex(final String indexName) throws UnsupportedOperationException {
throw new UnsupportedOperationException(Neo4jBatchTokens.DELETE_OPERATION_MESSAGE);
}
Expand Down
7 changes: 6 additions & 1 deletion blueprints-neo4j2-graph/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<name>Blueprints-Neo4j2Graph</name>
<description>Blueprints property graph implementation for the Neo4j 2 graph database</description>
<properties>
<neo4j.version>2.1.6</neo4j.version>
<neo4j.version>2.2.1</neo4j.version>
</properties>
<dependencies>
<dependency>
Expand All @@ -38,6 +38,11 @@
<version>${neo4j.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jta_1.1_spec</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>com.tinkerpop.rexster</groupId>
<artifactId>rexster-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ public Neo4j2Edge(final Relationship relationship, final Neo4j2Graph graph) {
}

public String getLabel() {
this.graph.autoStartTransaction(false);
this.graph.autoStartTransaction();
return ((Relationship) this.rawElement).getType().name();
}

public Vertex getVertex(final Direction direction) {
this.graph.autoStartTransaction(false);
this.graph.autoStartTransaction();
if (direction.equals(Direction.OUT))
return new Neo4j2Vertex(((Relationship) this.rawElement).getStartNode(), this.graph);
else if (direction.equals(Direction.IN))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,18 @@ public Neo4j2EdgeIterable(final Iterable<Relationship> relationships, final Neo4
}

public Iterator<Neo4j2Edge> iterator() {
graph.autoStartTransaction();
return new Iterator<Neo4j2Edge>() {
private final Iterator<Relationship> itty = relationships.iterator();
private Relationship nextRelationship = null;

public void remove() {
graph.autoStartTransaction(true);
graph.autoStartTransaction();
this.itty.remove();
}

public Neo4j2Edge next() {
graph.autoStartTransaction(false);
graph.autoStartTransaction();
if (!checkTransaction) {
return new Neo4j2Edge(this.itty.next(), graph);
} else {
Expand All @@ -62,7 +63,7 @@ public Neo4j2Edge next() {
}

public boolean hasNext() {
graph.autoStartTransaction(false);
graph.autoStartTransaction();
if (!checkTransaction)
return this.itty.hasNext();
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@
import org.neo4j.graphdb.Relationship;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.*;

/**
* @author Marko A. Rodriguez (http://markorodriguez.com)
Expand All @@ -29,7 +25,7 @@ public Neo4j2Element(final Neo4j2Graph graph) {
}

public <T> T getProperty(final String key) {
this.graph.autoStartTransaction(false);
this.graph.autoStartTransaction();
if (this.rawElement.hasProperty(key))
return (T) tryConvertCollectionToArrayList(this.rawElement.getProperty(key));
else
Expand All @@ -38,7 +34,7 @@ public <T> T getProperty(final String key) {

public void setProperty(final String key, final Object value) {
ElementHelper.validateProperty(this, key, value);
this.graph.autoStartTransaction(true);
this.graph.autoStartTransaction();
// attempts to take a collection and convert it to an array so that Neo4j can consume it
this.rawElement.setProperty(key, tryConvertCollectionToArray(value));
}
Expand All @@ -47,13 +43,13 @@ public <T> T removeProperty(final String key) {
if (!this.rawElement.hasProperty(key))
return null;
else {
this.graph.autoStartTransaction(true);
this.graph.autoStartTransaction();
return (T) this.rawElement.removeProperty(key);
}
}

public Set<String> getPropertyKeys() {
this.graph.autoStartTransaction(false);
this.graph.autoStartTransaction();
final Set<String> keys = new HashSet<String>();
for (final String key : this.rawElement.getPropertyKeys()) {
keys.add(key);
Expand All @@ -70,7 +66,7 @@ public PropertyContainer getRawElement() {
}

public Object getId() {
this.graph.autoStartTransaction(false);
this.graph.autoStartTransaction();
if (this.rawElement instanceof Node) {
return ((Node) this.rawElement).getId();
} else {
Expand Down
Loading