Skip to content

Commit

Permalink
Merge pull request #490 from juharris/GraphSailConnection-track_prev_…
Browse files Browse the repository at this point in the history
…subject

GraphSailConnection: Tracking the previous subject -- yes, this is smart. Nice.
  • Loading branch information
joshsh committed May 29, 2014
2 parents 97d2e82 + 3b0c21b commit 796e404
Showing 1 changed file with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ public class GraphSailConnection extends NotifyingSailConnectionBase implements
private boolean statementsAdded;
private boolean statementsRemoved;

/**
* The subject that was just seen when adding a statement.
*/
private Resource prevSubject = null;

/**
* The vertex corresponding to {@link #prevSubject}.
*/
private Vertex prevOutVertex = null;

public GraphSailConnection(final GraphSail.DataStore store) {
super(store.sail);
this.store = store;
Expand All @@ -67,6 +77,8 @@ public GraphSailConnection(final GraphSail.DataStore store) {
protected void startTransactionInternal() throws SailException {
statementsAdded = false;
statementsRemoved = false;
prevSubject = null;
prevOutVertex = null;
}

public void commitInternal() throws SailException {
Expand Down Expand Up @@ -259,7 +271,10 @@ private void addStatementInternal(final boolean inferred,
for (Resource context : ((0 == contexts.length) ? NULL_CONTEXT_ARRAY : contexts)) {
String c = null == context ? GraphSail.NULL_CONTEXT_NATIVE : store.resourceToNative(context);

Vertex out = getOrCreateVertex(subject);
// Track the subject since data will often list relations for the same subject consecutively.
Vertex out = subject.equals(prevSubject) ? prevOutVertex
: (prevOutVertex = getOrCreateVertex(prevSubject = subject));

// object-level identity of subject and object facilitates creation of self-loop edges in some Graph implementations
Vertex in = subject.equals(object) ? out : getOrCreateVertex(object);

Expand Down Expand Up @@ -381,6 +396,8 @@ private void removeStatementsInternal(final boolean inferred,

if (0 < edgesToRemove.size()) {
statementsRemoved = true;
prevSubject = null;
prevOutVertex = null;
}

//System.out.println("\tdone removing");
Expand Down Expand Up @@ -446,6 +463,8 @@ private void deleteEdgesInIterator(final boolean inferred,
}

statementsRemoved = true;
prevSubject = null;
prevOutVertex = null;
}
}
}
Expand Down

0 comments on commit 796e404

Please sign in to comment.