@Test public void testPerformActionRequiringMultipleBatches() { final String request = "{ \"count\": 2, \"maxId\": 2817 }"; final ArrayList<Long> activityIdsFirstPass = new ArrayList<Long>(); activityIdsFirstPass.add(allActivityIds.get(0)); activityIdsFirstPass.add(allActivityIds.get(1)); activityIdsFirstPass.add(allActivityIds.get(2)); activityIdsFirstPass.add(allActivityIds.get(3)); final ArrayList<Long> activityIdsFirstPassNewItems = new ArrayList<Long>(); activityIdsFirstPassNewItems.add(allActivityIds.get(0)); activityIdsFirstPassNewItems.add(allActivityIds.get(1)); activityIdsFirstPassNewItems.add(allActivityIds.get(2)); activityIdsFirstPassNewItems.add(allActivityIds.get(3)); final ArrayList<Long> activityIdsSecondPass = new ArrayList<Long>(); activityIdsSecondPass.add(allActivityIds.get(0)); activityIdsSecondPass.add(allActivityIds.get(1)); activityIdsSecondPass.add(allActivityIds.get(2)); activityIdsSecondPass.add(allActivityIds.get(3)); activityIdsSecondPass.add(allActivityIds.get(4)); activityIdsSecondPass.add(allActivityIds.get(5)); activityIdsSecondPass.add(allActivityIds.get(6)); activityIdsSecondPass.add(allActivityIds.get(7)); final ArrayList<Long> activityIdsSecondPassNewItems = new ArrayList<Long>(); activityIdsSecondPassNewItems.add(allActivityIds.get(4)); activityIdsSecondPassNewItems.add(allActivityIds.get(5)); activityIdsSecondPassNewItems.add(allActivityIds.get(6)); activityIdsSecondPassNewItems.add(allActivityIds.get(7)); final ArrayList<Long> activityIdsThirdPass = new ArrayList<Long>(); activityIdsThirdPass.add(allActivityIds.get(0)); activityIdsThirdPass.add(allActivityIds.get(1)); activityIdsThirdPass.add(allActivityIds.get(2)); activityIdsThirdPass.add(allActivityIds.get(3)); activityIdsThirdPass.add(allActivityIds.get(4)); activityIdsThirdPass.add(allActivityIds.get(5)); activityIdsThirdPass.add(allActivityIds.get(6)); activityIdsThirdPass.add(allActivityIds.get(7)); activityIdsThirdPass.add(allActivityIds.get(8)); final ArrayList<Long> activityIdsThirdPassNewItems = new ArrayList<Long>(); activityIdsThirdPassNewItems.add(allActivityIds.get(8)); context.checking(new Expectations() { { allowing(luceneDS).fetch(with(any(JSONObject.class)), with(any(Long.class))); will(returnValue(null)); oneOf(memcacheDS).fetch(with(any(JSONObject.class)), with(any(Long.class))); will(returnValue(activityIdsFirstPass)); oneOf(securityTrimmer).trim(activityIdsFirstPassNewItems, personId); will(returnValue(Arrays.asList(9L))); oneOf(memcacheDS).fetch(with(any(JSONObject.class)), with(any(Long.class))); will(returnValue(activityIdsSecondPass)); oneOf(securityTrimmer).trim(activityIdsSecondPassNewItems, personId); will(returnValue(new ArrayList<Long>())); oneOf(memcacheDS).fetch(with(any(JSONObject.class)), with(any(Long.class))); will(returnValue(activityIdsThirdPass)); oneOf(securityTrimmer).trim(activityIdsThirdPassNewItems, personId); will(returnValue(Arrays.asList(1L))); } }); List<Long> results = sut.execute(request, personId); assertEquals(2, results.size()); assertSame(9L, results.get(0)); assertSame(1L, results.get(1)); context.assertIsSatisfied(); }
public List<Long> execute(final String inRequest, final Long userEntityId) { String request = inRequest; log.debug("Attempted to parse: " + inRequest); if (request.contains(userReplaceString)) { List<Long> peopleIds = new ArrayList<Long>(); peopleIds.add(userEntityId); request = request.replaceAll(userReplaceString, personMapper.execute(peopleIds).get(0).getAccountId()); } final JSONObject jsonRequest = JSONObject.fromObject(request); int maxResults = 0; long minActivityId = 0; long maxActivityId = Long.MAX_VALUE; if (jsonRequest.containsKey("count")) { maxResults = jsonRequest.getInt("count"); } if (jsonRequest.containsKey("minId")) { minActivityId = jsonRequest.getInt("minId"); } if (jsonRequest.containsKey("maxId")) { maxActivityId = jsonRequest.getLong("maxId"); } List<ListTrimmer> trimmers = new ArrayList<ListTrimmer>(); for (ActivityQueryListTrimmerFactory factory : listTrimmerFactories) { ListTrimmer trimmer = factory.getTrimmer(jsonRequest, userEntityId); if (trimmer != null) { trimmers.add(trimmer); } } int startingIndex = 0; List<Long> results = new ArrayList<Long>(); List<Long> allKeys = new ArrayList<Long>(); final List<Long> sortedDataSet = sortedDataSource.fetch(jsonRequest, userEntityId); int pass = 1; int batchSize = 0; List<Long> page = new ArrayList<Long>(); boolean reachedMinActivityIdWhenSortingByIdDescending = false; boolean sortedByIdDescending = false; do { allKeys.clear(); batchSize = maxResults * (int) (Math.pow(2, pass)); jsonRequest.put("count", batchSize); final List<Long> descendingOrderDataSet = descendingOrderdataSource.fetch(jsonRequest, userEntityId); if (descendingOrderDataSet != null) { sortedByIdDescending = true; if (sortedDataSet != null) { allKeys = andCollider.collide(descendingOrderDataSet, sortedDataSet, batchSize); } else { allKeys = descendingOrderDataSet; } } else if (sortedDataSet != null) { sortedByIdDescending = false; allKeys = sortedDataSet; } if (allKeys.size() < startingIndex) { log.debug("No more results to page through after " + allKeys.size()); break; } for (int i = startingIndex; i < allKeys.size(); i++, startingIndex++) { if (sortedByIdDescending && allKeys.get(i) <= minActivityId) { reachedMinActivityIdWhenSortingByIdDescending = true; break; } if (allKeys.get(i) < maxActivityId && allKeys.get(i) > minActivityId) { page.add(allKeys.get(i)); if (page.size() == batchSize || i == allKeys.size() - 1) { log.debug("Sending a page of " + page.size() + " out for security trimming."); for (ListTrimmer trimmer : trimmers) { page = trimmer.trim(page, userEntityId); } for (Long item : page) { results.add(item); if (results.size() >= maxResults) { log.debug("Filled a full page of " + results.size() + " results."); return results; } } log.info("Return results now has " + results.size() + " results - looking for more"); page = new ArrayList<Long>(); } } } log.trace("Done looping?: " + (reachedMinActivityIdWhenSortingByIdDescending || allKeys.size() >= batchSize) + ", pass: " + pass + ", results.size(): " + results.size() + ", maxResults: " + maxResults + ", allKeys.size(): " + allKeys.size() + ", batchSize: " + batchSize + ", sorting by id descending? " + sortedByIdDescending + ", reached min activity id when sorted by id descending? " + reachedMinActivityIdWhenSortingByIdDescending); pass++; } while (!reachedMinActivityIdWhenSortingByIdDescending && allKeys.size() >= batchSize); if (results.size() < maxResults && page.size() > 0) { for (ListTrimmer trimmer : trimmers) { page = trimmer.trim(page, userEntityId); } for (Long item : page) { results.add(item); if (results.size() >= maxResults) { log.debug("Filled a full page of " + results.size() + " results."); return results; } } } return results; }
GetActivityIdsByJson { public List<Long> execute(final String inRequest, final Long userEntityId) { String request = inRequest; log.debug("Attempted to parse: " + inRequest); if (request.contains(userReplaceString)) { List<Long> peopleIds = new ArrayList<Long>(); peopleIds.add(userEntityId); request = request.replaceAll(userReplaceString, personMapper.execute(peopleIds).get(0).getAccountId()); } final JSONObject jsonRequest = JSONObject.fromObject(request); int maxResults = 0; long minActivityId = 0; long maxActivityId = Long.MAX_VALUE; if (jsonRequest.containsKey("count")) { maxResults = jsonRequest.getInt("count"); } if (jsonRequest.containsKey("minId")) { minActivityId = jsonRequest.getInt("minId"); } if (jsonRequest.containsKey("maxId")) { maxActivityId = jsonRequest.getLong("maxId"); } List<ListTrimmer> trimmers = new ArrayList<ListTrimmer>(); for (ActivityQueryListTrimmerFactory factory : listTrimmerFactories) { ListTrimmer trimmer = factory.getTrimmer(jsonRequest, userEntityId); if (trimmer != null) { trimmers.add(trimmer); } } int startingIndex = 0; List<Long> results = new ArrayList<Long>(); List<Long> allKeys = new ArrayList<Long>(); final List<Long> sortedDataSet = sortedDataSource.fetch(jsonRequest, userEntityId); int pass = 1; int batchSize = 0; List<Long> page = new ArrayList<Long>(); boolean reachedMinActivityIdWhenSortingByIdDescending = false; boolean sortedByIdDescending = false; do { allKeys.clear(); batchSize = maxResults * (int) (Math.pow(2, pass)); jsonRequest.put("count", batchSize); final List<Long> descendingOrderDataSet = descendingOrderdataSource.fetch(jsonRequest, userEntityId); if (descendingOrderDataSet != null) { sortedByIdDescending = true; if (sortedDataSet != null) { allKeys = andCollider.collide(descendingOrderDataSet, sortedDataSet, batchSize); } else { allKeys = descendingOrderDataSet; } } else if (sortedDataSet != null) { sortedByIdDescending = false; allKeys = sortedDataSet; } if (allKeys.size() < startingIndex) { log.debug("No more results to page through after " + allKeys.size()); break; } for (int i = startingIndex; i < allKeys.size(); i++, startingIndex++) { if (sortedByIdDescending && allKeys.get(i) <= minActivityId) { reachedMinActivityIdWhenSortingByIdDescending = true; break; } if (allKeys.get(i) < maxActivityId && allKeys.get(i) > minActivityId) { page.add(allKeys.get(i)); if (page.size() == batchSize || i == allKeys.size() - 1) { log.debug("Sending a page of " + page.size() + " out for security trimming."); for (ListTrimmer trimmer : trimmers) { page = trimmer.trim(page, userEntityId); } for (Long item : page) { results.add(item); if (results.size() >= maxResults) { log.debug("Filled a full page of " + results.size() + " results."); return results; } } log.info("Return results now has " + results.size() + " results - looking for more"); page = new ArrayList<Long>(); } } } log.trace("Done looping?: " + (reachedMinActivityIdWhenSortingByIdDescending || allKeys.size() >= batchSize) + ", pass: " + pass + ", results.size(): " + results.size() + ", maxResults: " + maxResults + ", allKeys.size(): " + allKeys.size() + ", batchSize: " + batchSize + ", sorting by id descending? " + sortedByIdDescending + ", reached min activity id when sorted by id descending? " + reachedMinActivityIdWhenSortingByIdDescending); pass++; } while (!reachedMinActivityIdWhenSortingByIdDescending && allKeys.size() >= batchSize); if (results.size() < maxResults && page.size() > 0) { for (ListTrimmer trimmer : trimmers) { page = trimmer.trim(page, userEntityId); } for (Long item : page) { results.add(item); if (results.size() >= maxResults) { log.debug("Filled a full page of " + results.size() + " results."); return results; } } } return results; } }
GetActivityIdsByJson { public List<Long> execute(final String inRequest, final Long userEntityId) { String request = inRequest; log.debug("Attempted to parse: " + inRequest); if (request.contains(userReplaceString)) { List<Long> peopleIds = new ArrayList<Long>(); peopleIds.add(userEntityId); request = request.replaceAll(userReplaceString, personMapper.execute(peopleIds).get(0).getAccountId()); } final JSONObject jsonRequest = JSONObject.fromObject(request); int maxResults = 0; long minActivityId = 0; long maxActivityId = Long.MAX_VALUE; if (jsonRequest.containsKey("count")) { maxResults = jsonRequest.getInt("count"); } if (jsonRequest.containsKey("minId")) { minActivityId = jsonRequest.getInt("minId"); } if (jsonRequest.containsKey("maxId")) { maxActivityId = jsonRequest.getLong("maxId"); } List<ListTrimmer> trimmers = new ArrayList<ListTrimmer>(); for (ActivityQueryListTrimmerFactory factory : listTrimmerFactories) { ListTrimmer trimmer = factory.getTrimmer(jsonRequest, userEntityId); if (trimmer != null) { trimmers.add(trimmer); } } int startingIndex = 0; List<Long> results = new ArrayList<Long>(); List<Long> allKeys = new ArrayList<Long>(); final List<Long> sortedDataSet = sortedDataSource.fetch(jsonRequest, userEntityId); int pass = 1; int batchSize = 0; List<Long> page = new ArrayList<Long>(); boolean reachedMinActivityIdWhenSortingByIdDescending = false; boolean sortedByIdDescending = false; do { allKeys.clear(); batchSize = maxResults * (int) (Math.pow(2, pass)); jsonRequest.put("count", batchSize); final List<Long> descendingOrderDataSet = descendingOrderdataSource.fetch(jsonRequest, userEntityId); if (descendingOrderDataSet != null) { sortedByIdDescending = true; if (sortedDataSet != null) { allKeys = andCollider.collide(descendingOrderDataSet, sortedDataSet, batchSize); } else { allKeys = descendingOrderDataSet; } } else if (sortedDataSet != null) { sortedByIdDescending = false; allKeys = sortedDataSet; } if (allKeys.size() < startingIndex) { log.debug("No more results to page through after " + allKeys.size()); break; } for (int i = startingIndex; i < allKeys.size(); i++, startingIndex++) { if (sortedByIdDescending && allKeys.get(i) <= minActivityId) { reachedMinActivityIdWhenSortingByIdDescending = true; break; } if (allKeys.get(i) < maxActivityId && allKeys.get(i) > minActivityId) { page.add(allKeys.get(i)); if (page.size() == batchSize || i == allKeys.size() - 1) { log.debug("Sending a page of " + page.size() + " out for security trimming."); for (ListTrimmer trimmer : trimmers) { page = trimmer.trim(page, userEntityId); } for (Long item : page) { results.add(item); if (results.size() >= maxResults) { log.debug("Filled a full page of " + results.size() + " results."); return results; } } log.info("Return results now has " + results.size() + " results - looking for more"); page = new ArrayList<Long>(); } } } log.trace("Done looping?: " + (reachedMinActivityIdWhenSortingByIdDescending || allKeys.size() >= batchSize) + ", pass: " + pass + ", results.size(): " + results.size() + ", maxResults: " + maxResults + ", allKeys.size(): " + allKeys.size() + ", batchSize: " + batchSize + ", sorting by id descending? " + sortedByIdDescending + ", reached min activity id when sorted by id descending? " + reachedMinActivityIdWhenSortingByIdDescending); pass++; } while (!reachedMinActivityIdWhenSortingByIdDescending && allKeys.size() >= batchSize); if (results.size() < maxResults && page.size() > 0) { for (ListTrimmer trimmer : trimmers) { page = trimmer.trim(page, userEntityId); } for (Long item : page) { results.add(item); if (results.size() >= maxResults) { log.debug("Filled a full page of " + results.size() + " results."); return results; } } } return results; } GetActivityIdsByJson();  GetActivityIdsByJson(final DescendingOrderDataSource inDescendingOrderdataSource,
            final SortedDataSource inSortedDataSource, final ListCollider inAndCollider,
            final Collection<ActivityQueryListTrimmerFactory> inListTrimmerFactories,
            final DomainMapper<List<Long>, List<PersonModelView>> inPersonMapper, final String inUserRelaceString); }
GetActivityIdsByJson { public List<Long> execute(final String inRequest, final Long userEntityId) { String request = inRequest; log.debug("Attempted to parse: " + inRequest); if (request.contains(userReplaceString)) { List<Long> peopleIds = new ArrayList<Long>(); peopleIds.add(userEntityId); request = request.replaceAll(userReplaceString, personMapper.execute(peopleIds).get(0).getAccountId()); } final JSONObject jsonRequest = JSONObject.fromObject(request); int maxResults = 0; long minActivityId = 0; long maxActivityId = Long.MAX_VALUE; if (jsonRequest.containsKey("count")) { maxResults = jsonRequest.getInt("count"); } if (jsonRequest.containsKey("minId")) { minActivityId = jsonRequest.getInt("minId"); } if (jsonRequest.containsKey("maxId")) { maxActivityId = jsonRequest.getLong("maxId"); } List<ListTrimmer> trimmers = new ArrayList<ListTrimmer>(); for (ActivityQueryListTrimmerFactory factory : listTrimmerFactories) { ListTrimmer trimmer = factory.getTrimmer(jsonRequest, userEntityId); if (trimmer != null) { trimmers.add(trimmer); } } int startingIndex = 0; List<Long> results = new ArrayList<Long>(); List<Long> allKeys = new ArrayList<Long>(); final List<Long> sortedDataSet = sortedDataSource.fetch(jsonRequest, userEntityId); int pass = 1; int batchSize = 0; List<Long> page = new ArrayList<Long>(); boolean reachedMinActivityIdWhenSortingByIdDescending = false; boolean sortedByIdDescending = false; do { allKeys.clear(); batchSize = maxResults * (int) (Math.pow(2, pass)); jsonRequest.put("count", batchSize); final List<Long> descendingOrderDataSet = descendingOrderdataSource.fetch(jsonRequest, userEntityId); if (descendingOrderDataSet != null) { sortedByIdDescending = true; if (sortedDataSet != null) { allKeys = andCollider.collide(descendingOrderDataSet, sortedDataSet, batchSize); } else { allKeys = descendingOrderDataSet; } } else if (sortedDataSet != null) { sortedByIdDescending = false; allKeys = sortedDataSet; } if (allKeys.size() < startingIndex) { log.debug("No more results to page through after " + allKeys.size()); break; } for (int i = startingIndex; i < allKeys.size(); i++, startingIndex++) { if (sortedByIdDescending && allKeys.get(i) <= minActivityId) { reachedMinActivityIdWhenSortingByIdDescending = true; break; } if (allKeys.get(i) < maxActivityId && allKeys.get(i) > minActivityId) { page.add(allKeys.get(i)); if (page.size() == batchSize || i == allKeys.size() - 1) { log.debug("Sending a page of " + page.size() + " out for security trimming."); for (ListTrimmer trimmer : trimmers) { page = trimmer.trim(page, userEntityId); } for (Long item : page) { results.add(item); if (results.size() >= maxResults) { log.debug("Filled a full page of " + results.size() + " results."); return results; } } log.info("Return results now has " + results.size() + " results - looking for more"); page = new ArrayList<Long>(); } } } log.trace("Done looping?: " + (reachedMinActivityIdWhenSortingByIdDescending || allKeys.size() >= batchSize) + ", pass: " + pass + ", results.size(): " + results.size() + ", maxResults: " + maxResults + ", allKeys.size(): " + allKeys.size() + ", batchSize: " + batchSize + ", sorting by id descending? " + sortedByIdDescending + ", reached min activity id when sorted by id descending? " + reachedMinActivityIdWhenSortingByIdDescending); pass++; } while (!reachedMinActivityIdWhenSortingByIdDescending && allKeys.size() >= batchSize); if (results.size() < maxResults && page.size() > 0) { for (ListTrimmer trimmer : trimmers) { page = trimmer.trim(page, userEntityId); } for (Long item : page) { results.add(item); if (results.size() >= maxResults) { log.debug("Filled a full page of " + results.size() + " results."); return results; } } } return results; } GetActivityIdsByJson();  GetActivityIdsByJson(final DescendingOrderDataSource inDescendingOrderdataSource,
            final SortedDataSource inSortedDataSource, final ListCollider inAndCollider,
            final Collection<ActivityQueryListTrimmerFactory> inListTrimmerFactories,
            final DomainMapper<List<Long>, List<PersonModelView>> inPersonMapper, final String inUserRelaceString); List<Long> execute(final String inRequest, final Long userEntityId); }
GetActivityIdsByJson { public List<Long> execute(final String inRequest, final Long userEntityId) { String request = inRequest; log.debug("Attempted to parse: " + inRequest); if (request.contains(userReplaceString)) { List<Long> peopleIds = new ArrayList<Long>(); peopleIds.add(userEntityId); request = request.replaceAll(userReplaceString, personMapper.execute(peopleIds).get(0).getAccountId()); } final JSONObject jsonRequest = JSONObject.fromObject(request); int maxResults = 0; long minActivityId = 0; long maxActivityId = Long.MAX_VALUE; if (jsonRequest.containsKey("count")) { maxResults = jsonRequest.getInt("count"); } if (jsonRequest.containsKey("minId")) { minActivityId = jsonRequest.getInt("minId"); } if (jsonRequest.containsKey("maxId")) { maxActivityId = jsonRequest.getLong("maxId"); } List<ListTrimmer> trimmers = new ArrayList<ListTrimmer>(); for (ActivityQueryListTrimmerFactory factory : listTrimmerFactories) { ListTrimmer trimmer = factory.getTrimmer(jsonRequest, userEntityId); if (trimmer != null) { trimmers.add(trimmer); } } int startingIndex = 0; List<Long> results = new ArrayList<Long>(); List<Long> allKeys = new ArrayList<Long>(); final List<Long> sortedDataSet = sortedDataSource.fetch(jsonRequest, userEntityId); int pass = 1; int batchSize = 0; List<Long> page = new ArrayList<Long>(); boolean reachedMinActivityIdWhenSortingByIdDescending = false; boolean sortedByIdDescending = false; do { allKeys.clear(); batchSize = maxResults * (int) (Math.pow(2, pass)); jsonRequest.put("count", batchSize); final List<Long> descendingOrderDataSet = descendingOrderdataSource.fetch(jsonRequest, userEntityId); if (descendingOrderDataSet != null) { sortedByIdDescending = true; if (sortedDataSet != null) { allKeys = andCollider.collide(descendingOrderDataSet, sortedDataSet, batchSize); } else { allKeys = descendingOrderDataSet; } } else if (sortedDataSet != null) { sortedByIdDescending = false; allKeys = sortedDataSet; } if (allKeys.size() < startingIndex) { log.debug("No more results to page through after " + allKeys.size()); break; } for (int i = startingIndex; i < allKeys.size(); i++, startingIndex++) { if (sortedByIdDescending && allKeys.get(i) <= minActivityId) { reachedMinActivityIdWhenSortingByIdDescending = true; break; } if (allKeys.get(i) < maxActivityId && allKeys.get(i) > minActivityId) { page.add(allKeys.get(i)); if (page.size() == batchSize || i == allKeys.size() - 1) { log.debug("Sending a page of " + page.size() + " out for security trimming."); for (ListTrimmer trimmer : trimmers) { page = trimmer.trim(page, userEntityId); } for (Long item : page) { results.add(item); if (results.size() >= maxResults) { log.debug("Filled a full page of " + results.size() + " results."); return results; } } log.info("Return results now has " + results.size() + " results - looking for more"); page = new ArrayList<Long>(); } } } log.trace("Done looping?: " + (reachedMinActivityIdWhenSortingByIdDescending || allKeys.size() >= batchSize) + ", pass: " + pass + ", results.size(): " + results.size() + ", maxResults: " + maxResults + ", allKeys.size(): " + allKeys.size() + ", batchSize: " + batchSize + ", sorting by id descending? " + sortedByIdDescending + ", reached min activity id when sorted by id descending? " + reachedMinActivityIdWhenSortingByIdDescending); pass++; } while (!reachedMinActivityIdWhenSortingByIdDescending && allKeys.size() >= batchSize); if (results.size() < maxResults && page.size() > 0) { for (ListTrimmer trimmer : trimmers) { page = trimmer.trim(page, userEntityId); } for (Long item : page) { results.add(item); if (results.size() >= maxResults) { log.debug("Filled a full page of " + results.size() + " results."); return results; } } } return results; } GetActivityIdsByJson();  GetActivityIdsByJson(final DescendingOrderDataSource inDescendingOrderdataSource,
            final SortedDataSource inSortedDataSource, final ListCollider inAndCollider,
            final Collection<ActivityQueryListTrimmerFactory> inListTrimmerFactories,
            final DomainMapper<List<Long>, List<PersonModelView>> inPersonMapper, final String inUserRelaceString); List<Long> execute(final String inRequest, final Long userEntityId);  }