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_r380076116
 
 

 ##########
 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:
   ord() to be consistent to TermsEnum Contract? Additionally It would be nice 
to have this in a different class as today TermsEnum are used for DocValues 
forcing to un-implement several methods such as postings and TermStates

----------------------------------------------------------------
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

Reply via email to