Buggy code:
public < BM > AggregateCountsAnswer getAggregateCounts ( MiruBitmaps < BM > bitmaps , MiruRequestContext < BM > requestContext , MiruRequest < AggregateCountsQuery > request , Optional < AggregateCountsReport > lastReport , BM answer , Optional < BM > counter ) throws Exception { AggregateCounts . log . debug ( "Get<seq2seq4repair_space>aggregate<seq2seq4repair_space>counts<seq2seq4repair_space>for<seq2seq4repair_space>answer={}<seq2seq4repair_space>request={}" , answer , request ) ; int collectedDistincts = 0 ; int skippedDistincts = 0 ; Set < String > aggregateTerms ; if ( lastReport . isPresent ( ) ) { collectedDistincts = lastReport . get ( ) . collectedDistincts ; skippedDistincts = lastReport . get ( ) . skippedDistincts ; aggregateTerms = Sets . newHashSet ( lastReport . get ( ) . aggregateTerms ) ; } else { aggregateTerms = Sets . newHashSet ( ) ; } List < AggregateCount > aggregateCounts = new ArrayList < > ( ) ; MiruTermComposer termComposer = requestContext . getTermComposer ( ) ; MiruFieldIndex < BM > fieldIndex = requestContext . getFieldIndexProvider ( ) . getFieldIndex ( primary ) ; int fieldId = requestContext . getSchema ( ) . getFieldId ( request . query . aggregateCountAroundField ) ; MiruFieldDefinition fieldDefinition = requestContext . getSchema ( ) . getFieldDefinition ( fieldId ) ; AggregateCounts . log . debug ( "fieldId={}" , fieldId ) ; if ( fieldId >= 0 ) { BM unreadIndex = null ; if ( ! ( NULL . equals ( request . query . streamId ) ) ) { Optional < BM > unread = requestContext . getUnreadTrackingIndex ( ) . getUnread ( request . query . streamId ) . getIndex ( ) ; if ( unread . isPresent ( ) ) { unreadIndex = unread . get ( ) ; } } final int numBuffers = ( 2 + ( counter . isPresent ( ) ? 2 : 0 ) ) + ( unreadIndex != null ? 1 : 0 ) ; ReusableBuffers < BM > reusable = new ReusableBuffers ( bitmaps , numBuffers ) ; long beforeCount = ( counter . isPresent ( ) ) ? bitmaps . cardinality ( counter . get ( ) ) : bitmaps . cardinality ( answer ) ; CardinalityAndLastSetBit answerCollector = null ; for ( String aggregateTerm : aggregateTerms ) { MiruTermId aggregateTermId = termComposer . compose ( fieldDefinition , aggregateTerm ) ; Optional < BM > optionalTermIndex = fieldIndex . get ( fieldId , aggregateTermId ) . getIndex ( ) ; if ( ! ( optionalTermIndex . isPresent ( ) ) ) { continue ; } BM termIndex = optionalTermIndex . get ( ) ; BM revisedAnswer = reusable . next ( ) ; answerCollector = bitmaps . andNotWithCardinalityAndLastSetBit ( revisedAnswer , answer , termIndex ) ; answer = revisedAnswer ; long afterCount ; if ( counter . isPresent ( ) ) { BM revisedCounter = reusable . next ( ) ; CardinalityAndLastSetBit counterCollector = bitmaps . andNotWithCardinalityAndLastSetBit ( revisedCounter , counter . get ( ) , termIndex ) ; counter = Optional . of ( revisedCounter ) ; afterCount = counterCollector . cardinality ; } else { afterCount = answerCollector . cardinality ; } boolean unread = false ; if ( unreadIndex != null ) { BM unreadAnswer = reusable . next ( ) ; CardinalityAndLastSetBit storage = bitmaps . andWithCardinalityAndLastSetBit ( unreadAnswer , Arrays . asList ( unreadIndex , termIndex ) ) ; if ( ( storage . cardinality ) > 0 ) { unread = true ; } } aggregateCounts . add ( new AggregateCount ( null , aggregateTerm , ( beforeCount - afterCount ) , unread ) ) ; beforeCount = afterCount ; } while ( true ) { int lastSetBit = ( answerCollector == null ) ? bitmaps . lastSetBit ( answer ) : answerCollector . lastSetBit ; AggregateCounts . log . trace ( "lastSetBit={}" , lastSetBit ) ; if ( lastSetBit < 0 ) { break ; } MiruInternalActivity activity = requestContext . getActivityIndex ( ) . get ( request . tenantId , lastSetBit ) ; MiruTermId [ ] fieldValues = activity . fieldsValues [ fieldId ] ; AggregateCounts . log . trace ( "fieldValues={}" , ( ( Object ) ( fieldValues ) ) ) ; if ( ( fieldValues == null ) || ( ( fieldValues . length ) == 0 ) ) { BM removeUnknownField = bitmaps . createWithBits ( lastSetBit ) ; BM revisedAnswer = reusable . next ( ) ; answerCollector = bitmaps . andNotWithCardinalityAndLastSetBit ( revisedAnswer , answer , removeUnknownField ) ; answer = revisedAnswer ; beforeCount -- ; } else { MiruTermId aggregateTermId = fieldValues [ 0 ] ; String aggregateTerm = termComposer . decompose ( fieldDefinition , aggregateTermId ) ; aggregateTerms . add ( aggregateTerm ) ; Optional < BM > optionalTermIndex = fieldIndex . get ( fieldId , aggregateTermId ) . getIndex ( ) ; Preconditions . checkState ( optionalTermIndex . isPresent ( ) , ( "Unable<seq2seq4repair_space>to<seq2seq4repair_space>load<seq2seq4repair_space>inverted<seq2seq4repair_space>index<seq2seq4repair_space>for<seq2seq4repair_space>aggregateTermId:<seq2seq4repair_space>" + aggregateTermId ) ) ; BM termIndex = optionalTermIndex . get ( ) ; BM revisedAnswer = reusable . next ( ) ; answerCollector = bitmaps . andNotWithCardinalityAndLastSetBit ( revisedAnswer , answer , termIndex ) ; answer = revisedAnswer ; long afterCount ; if ( counter . isPresent ( ) ) { BM revisedCounter = reusable . next ( ) ; CardinalityAndLastSetBit counterCollector = bitmaps . andNotWithCardinalityAndLastSetBit ( revisedCounter , counter . get ( ) , termIndex ) ; counter = Optional . of ( revisedCounter ) ; afterCount = counterCollector . cardinality ; } else { afterCount = answerCollector . cardinality ; } collectedDistincts ++ ; if ( collectedDistincts > ( request . query . startFromDistinctN ) ) { boolean unread = false ; if ( unreadIndex != null ) { BM unreadAnswer = reusable . next ( ) ; <START_BUG> CardinalityAndLastSetBit storage = bitmaps . andNotWithCardinalityAndLastSetBit ( unreadAnswer , unreadIndex , termIndex ) ; <END_BUG> if ( ( storage . cardinality ) > 0 ) { unread = true ; } } AggregateCount aggregateCount = new AggregateCount ( miruProvider . getActivityInternExtern ( request . tenantId ) . extern ( activity , requestContext . getSchema ( ) ) , aggregateTerm , ( beforeCount - afterCount ) , unread ) ; aggregateCounts . add ( aggregateCount ) ; if ( ( aggregateCounts . size ( ) ) >= ( request . query . desiredNumberOfDistincts ) ) { break ; } } else { skippedDistincts ++ ; } beforeCount = afterCount ; } } } AggregateCountsAnswer result = new AggregateCountsAnswer ( ImmutableList . copyOf ( aggregateCounts ) , ImmutableSet . copyOf ( aggregateTerms ) , skippedDistincts , collectedDistincts ) ; AggregateCounts . log . debug ( "result={}"

Fix:
CardinalityAndLastSetBit storage = bitmaps . andWithCardinalityAndLastSetBit ( unreadAnswer , Arrays . asList ( unreadIndex , termIndex ) ) ;