mkhludnev commented on a change in pull request #1462: URL: https://github.com/apache/lucene-solr/pull/1462#discussion_r419713904
########## File path: lucene/grouping/src/java/org/apache/lucene/search/grouping/DocValuesPoolingReader.java ########## @@ -0,0 +1,175 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.lucene.search.grouping; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import org.apache.lucene.index.BinaryDocValues; +import org.apache.lucene.index.DocValues; +import org.apache.lucene.index.FilterLeafReader; +import org.apache.lucene.index.LeafReader; +import org.apache.lucene.index.NumericDocValues; +import org.apache.lucene.index.SortedDocValues; +import org.apache.lucene.index.SortedNumericDocValues; +import org.apache.lucene.index.SortedSetDocValues; +import org.apache.lucene.index.TermsEnum; +import org.apache.lucene.search.DocIdSetIterator; +import org.apache.lucene.util.BytesRef; + +/** + * Caches docValues for the given {@linkplain LeafReader}. + * It only necessary when consumer retrieves same docValues many times per + * segment. Returned docValues should be iterated forward only. + * Caveat: {@link #getContext()} is completely misguiding for this class since + * it looses baseDoc, ord from underneath context. + * @lucene.experimental + * */ +class DocValuesPoolingReader extends FilterLeafReader { + + @FunctionalInterface + interface DVSupplier<T extends DocIdSetIterator>{ + T getDocValues(String field) throws IOException; + } + + private Map<String, ? super DocIdSetIterator> cache = new HashMap<>(); + + DocValuesPoolingReader(LeafReader in) { + super(in); + } + + @SuppressWarnings("unchecked") + protected <T extends DocIdSetIterator> T computeIfAbsent(String field, DVSupplier<T> supplier) throws IOException { + T dv; + if ((dv = (T) cache.get(field)) == null) { + dv = supplier.getDocValues(field); + cache.put(field, dv); + } + return dv; + } + + @Override + public CacheHelper getReaderCacheHelper() { + return null; + } + + @Override + public CacheHelper getCoreCacheHelper() { + return null; + } + + @Override + public BinaryDocValues getBinaryDocValues(String field) throws IOException { + return computeIfAbsent(field, in::getBinaryDocValues); + } + + @Override + public NumericDocValues getNumericDocValues(String field) throws IOException { + return computeIfAbsent(field, in::getNumericDocValues); + } + + @Override + public SortedNumericDocValues getSortedNumericDocValues(String field) throws IOException { + return computeIfAbsent(field, in::getSortedNumericDocValues); + } + + public SortedDocValues getSortedDocValues(String field) throws IOException { + return computeIfAbsent(field, in::getSortedDocValues); + } + + @Override + public SortedSetDocValues getSortedSetDocValues(String field) throws IOException { + return computeIfAbsent(field, field1 -> { + final SortedSetDocValues sortedSet = in.getSortedSetDocValues(field1); + final SortedDocValues singleton = DocValues.unwrapSingleton(sortedSet); Review comment: To pass by default strict singleton wrapper. If I comment it I've got `java.lang.IllegalStateException: iterator has already been used: docID=0 at __randomizedtesting.SeedInfo.seed([2825543CB6DDF1D2:83DF4929690177FC]:0) at org.apache.lucene.index.SingletonSortedSetDocValues.getSortedDocValues(SingletonSortedSetDocValues.java:45) at org.apache.lucene.index.DocValues.unwrapSingleton(DocValues.java:280) at org.apache.lucene.search.SortedSetSelector.wrap(SortedSetSelector.java:74) at org.apache.lucene.search.SortedSetSortField$1.getSortedDocValues(SortedSetSortField.java:125) at org.apache.lucene.search.FieldComparator$TermOrdValComparator.getLeafComparator(FieldComparator.java:714) at org.apache.lucene.search.grouping.AllGroupHeadsCollector$SortingGroupHead.<init>(AllGroupHeadsCollector.java:276)` While second group head obtains DV, SortedSetSelector unwraps SetDV, then Singleton shouted out already moved iter. ---------------------------------------------------------------- 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 --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For additional commands, e-mail: issues-h...@lucene.apache.org