@Test public void testFetchPageWithResultsRequiringThreePagesDueToLastSeenId() { final String searchQuery = "hey now"; final int start = 21; final int pageSize = 4; final int expectedPage1ToIndex = 19; final int expectedPage2ToIndex = 39; final List<Long> results1 = new ArrayList<Long>(); final List<Long> results2 = new ArrayList<Long>(); final int twentyFive = 25; for (long i = twentyFive; i >= 6; i--) { results1.add(i); } for (long i = twentyFive; i >= 1; i--) { results2.add(i); } context.checking(new Expectations() { { oneOf(projectionSearchRequestBuilderMock).buildQueryFromNativeSearchString(searchQuery); will(returnValue(fullTextQueryMock)); oneOf(fullTextQueryMock).setSort(with(any(Sort.class))); oneOf(projectionSearchRequestBuilderMock).setPaging(fullTextQueryMock, 0, expectedPage1ToIndex); oneOf(fullTextQueryMock).getResultList(); will(returnValue(results1)); oneOf(fullTextQueryMock).getResultSize(); will(returnValue(twentyFive)); oneOf(projectionSearchRequestBuilderMock).setPaging(fullTextQueryMock, 0, expectedPage2ToIndex); oneOf(fullTextQueryMock).getResultList(); will(returnValue(results2)); } }); sut = new ActivityIdSearchPageFetcher(searchQuery, projectionSearchRequestBuilderMock, 7L, 5L); List<Long> expectedResults = new ArrayList<Long>(); expectedResults.add(6L); expectedResults.add(5L); expectedResults.add(4L); expectedResults.add(3L); assertEquals(expectedResults, sut.fetchPage(start, pageSize)); context.assertIsSatisfied(); }
@SuppressWarnings("unchecked") @Override public List<Long> fetchPage(final int inStartIndex, final int inPageSize) { int pageSize = inPageSize; if (lastSeenActivityId != Long.MAX_VALUE) { pageSize *= pageSizeMultiplierForSubsequentPages; } FullTextQuery query = searchRequestBuilder.buildQueryFromNativeSearchString(searchQuery); query.setSort(new Sort("id", true)); List<Long> results = new ArrayList<Long>(); int totalResults = 0; int batchSize = 0; do { if (log.isTraceEnabled()) { log.trace("Preparing a query - search: '" + searchQuery + "'; paging:[0," + (pageSize - 1) + "]"); } searchRequestBuilder.setPaging(query, 0, pageSize - 1); List<Long> queryResults = query.getResultList(); if (totalResults == 0) { totalResults = query.getResultSize(); } batchSize = queryResults.size(); if (log.isTraceEnabled()) { log.trace("Found: " + batchSize + " of " + totalResults + " results."); } for (Long activityId : queryResults) { if (results.size() >= inPageSize) { break; } if (activityId < lastSeenActivityId) { if (log.isTraceEnabled()) { log.trace("Found activity id: " + activityId + ", which is lower than " + lastSeenActivityId + ", adding to the result list"); } lastSeenActivityId = activityId; results.add(activityId); } } pageSize += pageSize; } while ( results.size() < inPageSize && batchSize < totalResults); return results; }
ActivityIdSearchPageFetcher implements PageFetcher<Long> { @SuppressWarnings("unchecked") @Override public List<Long> fetchPage(final int inStartIndex, final int inPageSize) { int pageSize = inPageSize; if (lastSeenActivityId != Long.MAX_VALUE) { pageSize *= pageSizeMultiplierForSubsequentPages; } FullTextQuery query = searchRequestBuilder.buildQueryFromNativeSearchString(searchQuery); query.setSort(new Sort("id", true)); List<Long> results = new ArrayList<Long>(); int totalResults = 0; int batchSize = 0; do { if (log.isTraceEnabled()) { log.trace("Preparing a query - search: '" + searchQuery + "'; paging:[0," + (pageSize - 1) + "]"); } searchRequestBuilder.setPaging(query, 0, pageSize - 1); List<Long> queryResults = query.getResultList(); if (totalResults == 0) { totalResults = query.getResultSize(); } batchSize = queryResults.size(); if (log.isTraceEnabled()) { log.trace("Found: " + batchSize + " of " + totalResults + " results."); } for (Long activityId : queryResults) { if (results.size() >= inPageSize) { break; } if (activityId < lastSeenActivityId) { if (log.isTraceEnabled()) { log.trace("Found activity id: " + activityId + ", which is lower than " + lastSeenActivityId + ", adding to the result list"); } lastSeenActivityId = activityId; results.add(activityId); } } pageSize += pageSize; } while ( results.size() < inPageSize && batchSize < totalResults); return results; } }
ActivityIdSearchPageFetcher implements PageFetcher<Long> { @SuppressWarnings("unchecked") @Override public List<Long> fetchPage(final int inStartIndex, final int inPageSize) { int pageSize = inPageSize; if (lastSeenActivityId != Long.MAX_VALUE) { pageSize *= pageSizeMultiplierForSubsequentPages; } FullTextQuery query = searchRequestBuilder.buildQueryFromNativeSearchString(searchQuery); query.setSort(new Sort("id", true)); List<Long> results = new ArrayList<Long>(); int totalResults = 0; int batchSize = 0; do { if (log.isTraceEnabled()) { log.trace("Preparing a query - search: '" + searchQuery + "'; paging:[0," + (pageSize - 1) + "]"); } searchRequestBuilder.setPaging(query, 0, pageSize - 1); List<Long> queryResults = query.getResultList(); if (totalResults == 0) { totalResults = query.getResultSize(); } batchSize = queryResults.size(); if (log.isTraceEnabled()) { log.trace("Found: " + batchSize + " of " + totalResults + " results."); } for (Long activityId : queryResults) { if (results.size() >= inPageSize) { break; } if (activityId < lastSeenActivityId) { if (log.isTraceEnabled()) { log.trace("Found activity id: " + activityId + ", which is lower than " + lastSeenActivityId + ", adding to the result list"); } lastSeenActivityId = activityId; results.add(activityId); } } pageSize += pageSize; } while ( results.size() < inPageSize && batchSize < totalResults); return results; } ActivityIdSearchPageFetcher(final String inSearchQuery,
            final ProjectionSearchRequestBuilder inSearchRequestBuilder, final Long inLastSeenActivityId,
            final Long inPageSizeMultiplierForSubsequentPages); }
ActivityIdSearchPageFetcher implements PageFetcher<Long> { @SuppressWarnings("unchecked") @Override public List<Long> fetchPage(final int inStartIndex, final int inPageSize) { int pageSize = inPageSize; if (lastSeenActivityId != Long.MAX_VALUE) { pageSize *= pageSizeMultiplierForSubsequentPages; } FullTextQuery query = searchRequestBuilder.buildQueryFromNativeSearchString(searchQuery); query.setSort(new Sort("id", true)); List<Long> results = new ArrayList<Long>(); int totalResults = 0; int batchSize = 0; do { if (log.isTraceEnabled()) { log.trace("Preparing a query - search: '" + searchQuery + "'; paging:[0," + (pageSize - 1) + "]"); } searchRequestBuilder.setPaging(query, 0, pageSize - 1); List<Long> queryResults = query.getResultList(); if (totalResults == 0) { totalResults = query.getResultSize(); } batchSize = queryResults.size(); if (log.isTraceEnabled()) { log.trace("Found: " + batchSize + " of " + totalResults + " results."); } for (Long activityId : queryResults) { if (results.size() >= inPageSize) { break; } if (activityId < lastSeenActivityId) { if (log.isTraceEnabled()) { log.trace("Found activity id: " + activityId + ", which is lower than " + lastSeenActivityId + ", adding to the result list"); } lastSeenActivityId = activityId; results.add(activityId); } } pageSize += pageSize; } while ( results.size() < inPageSize && batchSize < totalResults); return results; } ActivityIdSearchPageFetcher(final String inSearchQuery,
            final ProjectionSearchRequestBuilder inSearchRequestBuilder, final Long inLastSeenActivityId,
            final Long inPageSizeMultiplierForSubsequentPages); @SuppressWarnings("unchecked") @Override List<Long> fetchPage(final int inStartIndex, final int inPageSize); }
ActivityIdSearchPageFetcher implements PageFetcher<Long> { @SuppressWarnings("unchecked") @Override public List<Long> fetchPage(final int inStartIndex, final int inPageSize) { int pageSize = inPageSize; if (lastSeenActivityId != Long.MAX_VALUE) { pageSize *= pageSizeMultiplierForSubsequentPages; } FullTextQuery query = searchRequestBuilder.buildQueryFromNativeSearchString(searchQuery); query.setSort(new Sort("id", true)); List<Long> results = new ArrayList<Long>(); int totalResults = 0; int batchSize = 0; do { if (log.isTraceEnabled()) { log.trace("Preparing a query - search: '" + searchQuery + "'; paging:[0," + (pageSize - 1) + "]"); } searchRequestBuilder.setPaging(query, 0, pageSize - 1); List<Long> queryResults = query.getResultList(); if (totalResults == 0) { totalResults = query.getResultSize(); } batchSize = queryResults.size(); if (log.isTraceEnabled()) { log.trace("Found: " + batchSize + " of " + totalResults + " results."); } for (Long activityId : queryResults) { if (results.size() >= inPageSize) { break; } if (activityId < lastSeenActivityId) { if (log.isTraceEnabled()) { log.trace("Found activity id: " + activityId + ", which is lower than " + lastSeenActivityId + ", adding to the result list"); } lastSeenActivityId = activityId; results.add(activityId); } } pageSize += pageSize; } while ( results.size() < inPageSize && batchSize < totalResults); return results; } ActivityIdSearchPageFetcher(final String inSearchQuery,
            final ProjectionSearchRequestBuilder inSearchRequestBuilder, final Long inLastSeenActivityId,
            final Long inPageSizeMultiplierForSubsequentPages); @SuppressWarnings("unchecked") @Override List<Long> fetchPage(final int inStartIndex, final int inPageSize);  }