juanka588 commented on a change in pull request #1263: LUCENE-9228: Sort dv updates by terms by applying URL: https://github.com/apache/lucene-solr/pull/1263#discussion_r380112482
########## File path: lucene/core/src/java/org/apache/lucene/util/BytesRefArray.java ########## @@ -185,20 +186,48 @@ public BytesRefIterator iterator() { */ @Override public BytesRefIterator iterator(final Comparator<BytesRef> comp) { - final BytesRefBuilder spare = new BytesRefBuilder(); - final BytesRef result = new BytesRef(); + return iteratorProvider(comp).get(); + } + + /** + * Prefer using either {@link #iterator()} or {@link #iterator(Comparator)}. + * This method is only useful if multiple iterators with a non-null {@link Comparator} + * are requires as it avoids sorting the array multiple times. + */ + public Supplier<Iterator> iteratorProvider(final Comparator<BytesRef> comp) { final int size = size(); final int[] indices = comp == null ? null : sort(comp); - return new BytesRefIterator() { - int pos = 0; + + return () -> new Iterator() { + final BytesRefBuilder spare = new BytesRefBuilder(); + final BytesRef result = new BytesRef(); + int pos = -1; + @Override public BytesRef next() { + ++pos; if (pos < size) { - setBytesRef(spare, result, indices == null ? pos++ : indices[pos++]); + setBytesRef(spare, result, currentIndex()); return result; } return null; } + + @Override + public int currentIndex() { + return indices == null ? pos : indices[pos]; + } }; } + + /** + * An extension of {@link BytesRefIterator} that allows retrieving the index of the current element + */ + public interface Iterator extends BytesRefIterator { + /** + * Returns the index of the element that was returned by the latest {@link #next()}. Do not call + * this method if {@link #next()} is not called yet or the last call returned a null value. + */ + int currentIndex(); Review comment: @jpountz my mistake I wanted to say a different file like "SeekableBytesRefIterator" decoupling postings and terms iteration. But still I dont thinks this PR should introduce this kind of modification, this is a comment just to open discussion ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For additional commands, e-mail: issues-h...@lucene.apache.org