{
return old ;
}
Node resAsNode = old.asNode() ;
Model model = old.getModel() ;
Graph graph = model.getGraph() ;
Graph rawGraph = graph instanceof InfGraph ? ((InfGraph) graph).getRawGraph() : graph ;
Resource newRes = model.createResource(uri) ;
Node newResAsNode = newRes.asNode() ;
boolean changeOccured = false ;
List<Triple> triples = new ArrayList<Triple>(WINDOW_SIZE) ;
// An optimization to prevent concatenating the two find() operations together every time through the outer loop
boolean onFirstIterator = true;
// It's possible there are triples (old wossname old) that are in triples twice. It doesn't matter.
ExtendedIterator<Triple> it = rawGraph.find(resAsNode, Node.ANY, Node.ANY) ;
try
{
if ( !it.hasNext() )
{
it.close() ;
onFirstIterator = false ;
it = rawGraph.find(Node.ANY, Node.ANY, resAsNode) ;
}
changeOccured = it.hasNext() ;
while ( it.hasNext() )
{
int count = 0 ;
while ( it.hasNext() && count < WINDOW_SIZE )
{
triples.add(it.next()) ;
count++ ;
}
it.close() ;
// Iterate over the triples collection twice (this may be more efficient than interleaving deletes and adds)
for ( Triple t : triples )
{
rawGraph.delete(t) ;
}
for ( Triple t : triples )
{
Node oldS = t.getSubject(), oldO = t.getObject() ;
Node newS = oldS.equals(resAsNode) ? newResAsNode : oldS ;
Node newO = oldO.equals(resAsNode) ? newResAsNode : oldO ;
rawGraph.add(Triple.create(newS, t.getPredicate(), newO));
}
triples.clear();
it = onFirstIterator ? rawGraph.find(resAsNode, Node.ANY, Node.ANY) : rawGraph.find(Node.ANY, Node.ANY, resAsNode) ;
if ( onFirstIterator && !it.hasNext() )
{
it.close() ;
onFirstIterator = false ;
it = rawGraph.find(Node.ANY, Node.ANY, resAsNode) ;
}
}
}
finally
{