diff --git a/mongo/src/main/java/com/redhat/lightblue/mongo/crud/BasicDocFinder.java b/mongo/src/main/java/com/redhat/lightblue/mongo/crud/BasicDocFinder.java index b9662ebc0..af8d32cc6 100644 --- a/mongo/src/main/java/com/redhat/lightblue/mongo/crud/BasicDocFinder.java +++ b/mongo/src/main/java/com/redhat/lightblue/mongo/crud/BasicDocFinder.java @@ -85,7 +85,7 @@ public long find(CRUDOperationContext ctx, cursor.setReadPreference(readPreference); } - if (maxQueryTimeMS > 0) { + if (maxQueryTimeMS > 0&&ctx.isLimitQueryTime()) { cursor.maxTime(maxQueryTimeMS, TimeUnit.MILLISECONDS); } @@ -96,33 +96,48 @@ public long find(CRUDOperationContext ctx, cursor = cursor.sort(mongoSort); LOGGER.debug("Result set sorted"); } - int numMatched = cursor.count(); - int nRetrieve=numMatched; + + int numMatched=0; + int nRetrieve=0; + if(ctx.isComputeCounts()) { + numMatched = cursor.count(); + } LOGGER.debug("Applying limits: {} - {}", from, to); - // f and t are from and to indexes, both inclusive - int f=from==null?0:from.intValue(); - int t=to==null?numMatched-1:to.intValue(); - if(f<0) - f=0; - if(t>=numMatched) - t=numMatched-1; - if(t0) { - nRetrieve=t-f+1; - cursor.skip(f); - cursor.limit(nRetrieve); - if (maxResultSetSize > 0 && nRetrieve > maxResultSetSize) { + int f=from==null?0:from.intValue(); + if(f<0) + f=0; + + boolean empty=false; + if(to!=null) { + if(to.intValue()numMatched) { + nRetrieve=numMatched-f+1; + } + } + cursor.limit(nRetrieve); + } + } else { + cursor.skip(f); + if(ctx.isComputeCounts()) { + nRetrieve=numMatched-f+1; + } + } + if(!empty) { + if (ctx.isComputeCounts() && maxResultSetSize > 0 && nRetrieve > maxResultSetSize) { LOGGER.warn("Too many results:{} of {}", nRetrieve, numMatched); RESULTSET_LOGGER.debug("resultset_size={}, requested={}, query={}", numMatched, nRetrieve, mongoQuery); throw Error.get(MongoCrudConstants.ERR_TOO_MANY_RESULTS, Integer.toString(nRetrieve)); } LOGGER.debug("Retrieving results"); - CursorStream stream=new CursorStream(cursor,translator,mongoQuery,executionTime,f,t); + CursorStream stream=new CursorStream(cursor,translator,mongoQuery,executionTime,f,to==null?-1:to); ctx.setDocumentStream(stream); cursorInUse=true; } else { @@ -132,7 +147,7 @@ public long find(CRUDOperationContext ctx, RESULTSET_LOGGER.debug("execution_time={}, query={}, from={}, to={}", executionTime, mongoQuery, - f, t); + from, to); } return numMatched; } finally {