CachedPostingListCounter { public void countPostingListsPerDocument(List<PostingList> postingLists, byte[] nPostingListsForDocument) { Arrays.fill(nPostingListsForDocument, (byte) 0); List<int[]> nonCachedPostingLists = new ArrayList<>(postingLists.size()); List<int[]> cachedPostingLists = new ArrayList<>(postingLists.size()); long nDocumentsCachedPostingLists = 0; int postingListBitmap = 0; for (PostingList postingList : postingLists) { int[] docIds = postingList.getDocIds(); int index = postingListMapping.getIfAbsent(docIds, -1); if (index >= 0) { cachedPostingLists.add(docIds); postingListBitmap |= (1 << index); nDocumentsCachedPostingLists += docIds.length; } else { nonCachedPostingLists.add(docIds); } } if (postingListBitmap != 0) { if (nDocumentsCachedPostingLists > nDocuments * THRESHOLD_USE_BIT_VECTOR) { countUsingBitVector(nPostingListsForDocument, postingListBitmap); } else { nonCachedPostingLists.addAll(cachedPostingLists); } } if (!nonCachedPostingLists.isEmpty()) { countUsingDocIdIteration(nPostingListsForDocument, nonCachedPostingLists); } } CachedPostingListCounter(int nDocuments); private  CachedPostingListCounter(ObjectIntHashMap<int[]> postingListMapping, int[] bitVector); synchronized void registerUsage(List<PostingList> postingLists); void countPostingListsPerDocument(List<PostingList> postingLists, byte[] nPostingListsForDocument); CachedPostingListCounter rebuildCache();  }
@Test public void require_that_docids_are_counted_correctly() { int nDocuments = 4; byte[] nPostingListsPerDocument = new byte[nDocuments]; CachedPostingListCounter c = new CachedPostingListCounter(nDocuments); c.countPostingListsPerDocument( list( postingList(0, 1, 2, 3), postingList(1, 2), postingList(1, 3), postingList(3)), nPostingListsPerDocument); assertArrayEquals(new byte[]{1, 3, 2, 3}, nPostingListsPerDocument); }