public BytesRef next() throws IOException {
      for (LeafSource top : competitiveLeafs) {
        if (top.next() != null) {
          sources.add(top);
        }
      }
      competitiveLeafs.clear();
      if (sources.size() == 0) {
        return null;
      }

      do {
        LeafSource competitiveLeaf = sources.pop();
        competitiveLeafs.add(competitiveLeaf);
      } while (sources.size() > 0 && competitiveLeafs.get(0).current.equals(sources.top().current));
      return competitiveLeafs.get(0).current;
    }
 private TermIterator(
     IndexFieldData.WithOrdinals indexFieldData,
     List<AtomicReaderContext> leaves,
     AtomicFieldData.WithOrdinals[] withOrdinals)
     throws IOException {
   this.sources = new LeafSourceQueue(leaves.size());
   for (int i = 0; i < leaves.size(); i++) {
     AtomicReaderContext atomicReaderContext = leaves.get(i);
     AtomicFieldData.WithOrdinals afd = indexFieldData.load(atomicReaderContext);
     withOrdinals[i] = afd;
     LeafSource leafSource = new LeafSource(afd, atomicReaderContext);
     if (leafSource.current != null) {
       sources.add(leafSource);
     }
   }
 }