You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@lucene.apache.org by GitBox <gi...@apache.org> on 2020/05/04 08:48:44 UTC

[GitHub] [lucene-solr] romseygeek commented on a change in pull request #1462: LUCENE-9328: open group.sort docvalues once per segment.

romseygeek commented on a change in pull request #1462:
URL: https://github.com/apache/lucene-solr/pull/1462#discussion_r419289810



##########
File path: lucene/grouping/src/test/org/apache/lucene/search/grouping/DocValuesPoolingReaderTest.java
##########
@@ -0,0 +1,150 @@
+/*
+ * 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 org.apache.lucene.analysis.MockAnalyzer;
+import org.apache.lucene.document.BinaryDocValuesField;
+import org.apache.lucene.document.Document;
+import org.apache.lucene.document.NumericDocValuesField;
+import org.apache.lucene.document.SortedDocValuesField;
+import org.apache.lucene.document.SortedNumericDocValuesField;
+import org.apache.lucene.document.SortedSetDocValuesField;
+import org.apache.lucene.index.BinaryDocValues;
+import org.apache.lucene.index.DirectoryReader;
+import org.apache.lucene.index.LeafReader;
+import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.index.NumericDocValues;
+import org.apache.lucene.index.RandomIndexWriter;
+import org.apache.lucene.index.SortedDocValues;
+import org.apache.lucene.index.SortedNumericDocValues;
+import org.apache.lucene.index.SortedSetDocValues;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.util.BytesRef;
+import org.apache.lucene.util.LuceneTestCase;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+
+public class DocValuesPoolingReaderTest extends LuceneTestCase {
+  
+  private static RandomIndexWriter w;
+  private static Directory dir;
+  private static DirectoryReader reader;
+
+  @BeforeClass
+  public static void index() throws IOException {
+    dir = newDirectory();
+    w = new RandomIndexWriter(
+        random(),
+        dir,
+        newIndexWriterConfig(new MockAnalyzer(random())).setMergePolicy(newLogMergePolicy()));
+    Document doc = new Document();
+    doc.add(new BinaryDocValuesField("bin", new BytesRef("binary")));
+    doc.add(new BinaryDocValuesField("bin2", new BytesRef("binary2")));
+    
+    doc.add(new NumericDocValuesField("num", 1L));
+    doc.add(new NumericDocValuesField("num2", 2L));
+    
+    doc.add(new SortedNumericDocValuesField("sortnum", 3L));
+    doc.add(new SortedNumericDocValuesField("sortnum2", 4L));
+    
+    doc.add(new SortedDocValuesField("sort",  new BytesRef("sorted")));
+    doc.add(new SortedDocValuesField("sort2",  new BytesRef("sorted2")));
+    
+    doc.add(new SortedSetDocValuesField("sortset", new BytesRef("sortedset")));
+    doc.add(new SortedSetDocValuesField("sortset2", new BytesRef("sortedset2")));
+    
+    w.addDocument(doc);
+    w.commit();
+    reader = w.getReader();
+    w.close();
+  }
+  
+  public void testDVCache() throws IOException {
+    assertFalse(reader.leaves().isEmpty());
+    for (LeafReaderContext leaf : reader.leaves()) {
+      final DocValuesPoolingReader caching = new DocValuesPoolingReader(leaf.reader());
+      
+      assertSame(assertBinaryDV(caching, "bin", "binary"), 
+          caching.getBinaryDocValues("bin"));
+      assertSame(assertBinaryDV(caching, "bin2", "binary2"), 
+          caching.getBinaryDocValues("bin2"));
+      
+      assertSame(assertNumericDV(caching, "num", 1L), 
+          caching.getNumericDocValues("num"));
+      assertSame(assertNumericDV(caching, "num2", 2L), 
+          caching.getNumericDocValues("num2"));
+      
+      assertSame(assertSortedNumericDV(caching, "sortnum", 3L), 
+          caching.getSortedNumericDocValues("sortnum"));
+      assertSame(assertSortedNumericDV(caching, "sortnum2", 4L), 
+          caching.getSortedNumericDocValues("sortnum2"));
+      
+      assertSame(assertSortedDV(caching, "sort", "sorted"), 
+          caching.getSortedDocValues("sort"));
+      assertSame(assertSortedDV(caching, "sort2", "sorted2"), 
+          caching.getSortedDocValues("sort2"));

Review comment:
       I think this still doesn't test iteration through a single doc's values on two instances of the shared DV?  We need to pull the iterator twice, advance both to the same doc, and then iterate through the values on both - as I read it, currently if you iterate through the values on one, then you can't iterate through them on the other because the within-document iterator is shared between the two of them.  We need to cache the values.

##########
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:
       I don't understand why we need to unwrap it here?




----------------------------------------------------------------
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org