protected CloseableIteration<? extends Resource, SailException> getContextIDsInternal()
throws SailException
{
// Which resources are used as context identifiers is not stored
// separately. Iterate over all statements and extract their context.
Lock readLock = nativeStore.getReadLock();
try {
// Iterator over all statements
CloseableIteration<? extends Statement, IOException> stIter;
stIter = nativeStore.createStatementIterator(null, null, null, true, transactionActive());
// Filter statements without context resource
stIter = new FilterIteration<Statement, IOException>(stIter) {
@Override
protected boolean accept(Statement st) {
return st.getContext() != null;
}
};
// Return the contexts of the statements, filtering any duplicates,
// releasing the read lock when the iterator is closed
CloseableIteration<? extends Resource, IOException> contextIter;
contextIter = new DistinctIteration<Resource, IOException>(
new ConvertingIteration<Statement, Resource, IOException>(stIter) {
@Override
protected Resource convert(Statement st) {
return st.getContext();
}
});
contextIter = new LockingIteration<Resource, IOException>(readLock, contextIter);
return new ExceptionConvertingIteration<Resource, SailException>(contextIter) {
@Override
protected SailException convert(Exception e) {
if (e instanceof IOException) {
return new SailException(e);
}
else if (e instanceof RuntimeException) {
throw (RuntimeException)e;
}
else if (e == null) {
throw new IllegalArgumentException("e must not be null");
}
else {
throw new IllegalArgumentException("Unexpected exception type: " + e.getClass());
}
}
};
}
catch (IOException e) {
readLock.release();
throw new SailException(e);
}
catch (RuntimeException e) {
readLock.release();
throw e;
}
}