use of org.apache.jackrabbit.oak.jcr.query.PrefetchIterator.PrefetchOptions in project jackrabbit-oak by apache.
the class QueryResultImpl method getRows.
@Override
public RowIterator getRows() throws RepositoryException {
Iterator<RowImpl> rowIterator = new Iterator<RowImpl>() {
private final Iterator<? extends ResultRow> it = result.getRows().iterator();
private final String pathSelector;
private RowImpl current;
private int rowCount;
//Avoid log check for every row access
private final boolean debugEnabled = queryOpsLogger.isDebugEnabled();
{
String[] columnSelectorNames = result.getColumnSelectorNames();
if (columnSelectorNames.length == 1) {
pathSelector = columnSelectorNames[0];
} else {
pathSelector = null;
}
fetch();
}
private void fetch() {
if (it.hasNext()) {
current = new RowImpl(QueryResultImpl.this, it.next(), pathSelector);
if (debugEnabled) {
rowCount++;
if (rowCount % 100 == 0) {
queryOpsLogger.debug("Iterated over [{}] results so far", rowCount);
}
}
} else {
current = null;
}
}
@Override
public boolean hasNext() {
return current != null;
}
@Override
public RowImpl next() {
if (current == null) {
throw new NoSuchElementException();
}
RowImpl r = current;
fetch();
return r;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
final PrefetchIterator<RowImpl> prefIt = new PrefetchIterator<RowImpl>(sessionDelegate.sync(rowIterator), new PrefetchOptions() {
{
size = result.getSize();
fastSize = sessionContext.getFastQueryResultSize();
fastSizeCallback = result;
}
});
return new RowIteratorAdapter(prefIt) {
@Override
public long getSize() {
return prefIt.size();
}
};
}
use of org.apache.jackrabbit.oak.jcr.query.PrefetchIterator.PrefetchOptions in project jackrabbit-oak by apache.
the class QueryResultImpl method getNodes.
@Override
public NodeIterator getNodes() throws RepositoryException {
String[] columnSelectorNames = result.getColumnSelectorNames();
if (columnSelectorNames.length != 1) {
throw new RepositoryException("Query contains more than one selector: " + Arrays.toString(columnSelectorNames));
}
final String selectorName = columnSelectorNames[0];
if (selectorName == null) {
throw new RepositoryException("Query does not contain a selector: " + Arrays.toString(columnSelectorNames));
}
Iterator<NodeImpl<? extends NodeDelegate>> nodeIterator = new Iterator<NodeImpl<? extends NodeDelegate>>() {
private final Iterator<? extends ResultRow> it = result.getRows().iterator();
private NodeImpl<? extends NodeDelegate> current;
{
fetch();
}
private void fetch() {
current = null;
while (it.hasNext()) {
ResultRow r = it.next();
Tree tree = r.getTree(selectorName);
if (tree != null && tree.exists()) {
try {
current = getNode(tree);
break;
} catch (RepositoryException e) {
LOG.warn("Unable to fetch result node for path " + tree.getPath(), e);
}
}
}
}
@Override
public boolean hasNext() {
return current != null;
}
@Override
public NodeImpl<? extends NodeDelegate> next() {
if (current == null) {
throw new NoSuchElementException();
}
NodeImpl<? extends NodeDelegate> n = current;
fetch();
return n;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
final PrefetchIterator<NodeImpl<? extends NodeDelegate>> prefIt = new PrefetchIterator<NodeImpl<? extends NodeDelegate>>(sessionDelegate.sync(nodeIterator), new PrefetchOptions() {
{
size = result.getSize();
fastSize = sessionContext.getFastQueryResultSize();
fastSizeCallback = result;
}
});
return new NodeIteratorAdapter(prefIt) {
@Override
public long getSize() {
return prefIt.size();
}
};
}
use of org.apache.jackrabbit.oak.jcr.query.PrefetchIterator.PrefetchOptions in project jackrabbit-oak by apache.
the class PrefetchIteratorTest method test.
@Test
public void test() {
// for (int size = 0; size < 100; size++)
for (int size : seq(0, 100)) {
for (int readBefore : seq(0, 30)) {
// every 3th time, use a timeout
final long testTimeout = size % 3 == 0 ? 100 : 0;
Iterable<Integer> s = seq(0, size);
PrefetchIterator<Integer> it = new PrefetchIterator<Integer>(s.iterator(), new PrefetchOptions() {
{
min = 20;
timeout = testTimeout;
max = 30;
size = -1;
}
});
for (int x : seq(0, readBefore)) {
boolean hasNext = it.hasNext();
if (!hasNext) {
assertEquals(x, size);
break;
}
String m = "s:" + size + " b:" + readBefore + " x:" + x;
assertTrue(m, hasNext);
assertEquals(m, x, it.next().intValue());
}
String m = "s:" + size + " b:" + readBefore;
int max = testTimeout <= 0 ? 20 : 30;
if (size > max && readBefore <= size) {
assertEquals(m, -1, it.size());
// calling it twice must not change the result
assertEquals(m, -1, it.size());
} else {
assertEquals(m, size, it.size());
// calling it twice must not change the result
assertEquals(m, size, it.size());
}
for (int x : seq(readBefore, size)) {
m = "s:" + size + " b:" + readBefore + " x:" + x;
assertTrue(m, it.hasNext());
assertEquals(m, x, it.next().intValue());
}
assertFalse(it.hasNext());
try {
it.next();
fail();
} catch (NoSuchElementException e) {
// expected
}
try {
it.remove();
fail();
} catch (UnsupportedOperationException e) {
// expected
}
}
}
}
Aggregations