class TLDFieldIndexAggregator extends SeekingAggregator implements FieldIndexAggregator {

    public Key apply(SortedKeyValueIterator<Key,Value> itr, Document d, AttributeFactory af) throws IOException {
        Key key = itr.getTopKey();
        ByteSequence parentId = parseRootPointerFromFI(key.getColumnQualifierData());
        Text row = key.getRow();
        ByteSequence docId = null;
        Key nextKey = key;
        do {
            key = nextKey;
            String field = key.getColumnFamily().toString().substring(3);
            String value = key.getColumnQualifier().toString();
            value = value.substring(0, value.indexOf('\0'));
            Attribute<?> attr = af.create(field, value, key, true);
            // in addition to keeping fields that the filter indicates should be kept, also keep fields that the filter applies. This is due to inconsistent
            // behavior between event/tld queries where an index only field index will be kept except when it is a child of a tld
            attr.setToKeep((fieldsToAggregate == null || fieldsToAggregate.contains(JexlASTHelper.removeGroupingContext(field)))
                            && (attrFilter == null || attrFilter.keep(key)));
            d.put(field, attr);
            
            ByteSequence thisId = parsePointerFromFI(key.getColumnQualifierData());
            if (docId == null || !docId.equals(thisId)) {
                docId = thisId;
                Key docKey = new Key(key.getRow(), new Text(docId.toArray()), new Text(), ColumnVisibilityCache.get(key.getColumnVisibilityData()),
                                key.getTimestamp());
                attr = new DocumentKey(docKey, false);
                d.put(Document.DOCKEY_FIELD_NAME, attr);
            }
            itr.next();
            nextKey = itr.hasTop() ? itr.getTopKey() : null;
        } while (skip(nextKey, row, parentId));
        return getResult(key, parentId);
    }

    public  TLDFieldIndexAggregator(Set<String> fieldsToAggregate, EventDataQueryFilter attrFilter);
    public  TLDFieldIndexAggregator(Set<String> fieldsToAggregate, EventDataQueryFilter attrFilter, int maxNextCount);

}