lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dsmi...@apache.org
Subject lucene-solr:master: LUCENE-8552: Optimize getMergedFieldInfos for zero or one segment
Date Fri, 16 Nov 2018 21:46:04 GMT
Repository: lucene-solr
Updated Branches:
  refs/heads/master cd61a926f -> db9542526


LUCENE-8552: Optimize getMergedFieldInfos for zero or one segment


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/db954252
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/db954252
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/db954252

Branch: refs/heads/master
Commit: db9542526ecc742234bff77d609a0f7071a78b46
Parents: cd61a92
Author: Christophe Bismuth <christophe.bismuth@gmail.com>
Authored: Fri Nov 16 16:45:09 2018 -0500
Committer: David Smiley <dsmiley@apache.org>
Committed: Fri Nov 16 16:45:09 2018 -0500

----------------------------------------------------------------------
 lucene/CHANGES.txt                              |  7 ++-
 .../org/apache/lucene/index/FieldInfos.java     | 23 ++++++----
 .../org/apache/lucene/index/TestFieldInfos.java | 46 ++++++++++++++++++++
 3 files changed, 67 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/db954252/lucene/CHANGES.txt
----------------------------------------------------------------------
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index ca87bfe..a0cfa71 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -212,11 +212,16 @@ New Features
   IndexWriterConfig#setIndexCreatedVersionMajor. This is an expert feature.
   (Adrien Grand)
 
-Improvements:
+Improvements
 
 * LUCENE-8463: TopFieldCollector can now early-terminates queries when sorting by SortField.DOC.
   (Christophe Bismuth via Jim Ferenczi)
 
+Optimizations
+
+* LUCENE-8552: FieldInfos.getMergedFieldInfos no longer does any merging if there is <=
1 segment.
+  (Christophe Bismuth via David Smiley)
+
 ======================= Lucene 7.6.0 =======================
 
 Build

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/db954252/lucene/core/src/java/org/apache/lucene/index/FieldInfos.java
----------------------------------------------------------------------
diff --git a/lucene/core/src/java/org/apache/lucene/index/FieldInfos.java b/lucene/core/src/java/org/apache/lucene/index/FieldInfos.java
index 5cd9639..88f092a 100644
--- a/lucene/core/src/java/org/apache/lucene/index/FieldInfos.java
+++ b/lucene/core/src/java/org/apache/lucene/index/FieldInfos.java
@@ -139,15 +139,22 @@ public class FieldInfos implements Iterable<FieldInfo> {
    *  will be unavailable.
    */
   public static FieldInfos getMergedFieldInfos(IndexReader reader) {
-    final String softDeletesField = reader.leaves().stream()
-        .map(l -> l.reader().getFieldInfos().getSoftDeletesField())
-        .filter(Objects::nonNull)
-        .findAny().orElse(null);
-    final Builder builder = new Builder(new FieldNumbers(softDeletesField));
-    for(final LeafReaderContext ctx : reader.leaves()) {
-      builder.add(ctx.reader().getFieldInfos());
+    final List<LeafReaderContext> leaves = reader.leaves();
+    if (leaves.isEmpty()) {
+      return FieldInfos.EMPTY;
+    } else if (leaves.size() == 1) {
+      return leaves.get(0).reader().getFieldInfos();
+    } else {
+      final String softDeletesField = leaves.stream()
+          .map(l -> l.reader().getFieldInfos().getSoftDeletesField())
+          .filter(Objects::nonNull)
+          .findAny().orElse(null);
+      final Builder builder = new Builder(new FieldNumbers(softDeletesField));
+      for (final LeafReaderContext ctx : leaves) {
+        builder.add(ctx.reader().getFieldInfos());
+      }
+      return builder.finish();
     }
-    return builder.finish();
   }
 
   /** Returns a set of names of fields that have a terms index.  The order is undefined.
*/

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/db954252/lucene/core/src/test/org/apache/lucene/index/TestFieldInfos.java
----------------------------------------------------------------------
diff --git a/lucene/core/src/test/org/apache/lucene/index/TestFieldInfos.java b/lucene/core/src/test/org/apache/lucene/index/TestFieldInfos.java
index 308e11e..3fe5fa9 100644
--- a/lucene/core/src/test/org/apache/lucene/index/TestFieldInfos.java
+++ b/lucene/core/src/test/org/apache/lucene/index/TestFieldInfos.java
@@ -17,6 +17,7 @@
 package org.apache.lucene.index;
 
 
+import java.io.IOException;
 import java.util.Iterator;
 
 import org.apache.lucene.analysis.MockAnalyzer;
@@ -26,6 +27,9 @@ import org.apache.lucene.document.StringField;
 import org.apache.lucene.store.Directory;
 import org.apache.lucene.util.LuceneTestCase;
 
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.sameInstance;
+
 public class TestFieldInfos extends LuceneTestCase {
 
   public void testFieldInfos() throws Exception{
@@ -89,4 +93,46 @@ public class TestFieldInfos extends LuceneTestCase {
     dir.close();
   }
 
+  public void testMergedFieldInfos_empty() throws IOException {
+    Directory dir = newDirectory();
+    IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())));
+
+    IndexReader reader = writer.getReader();
+    FieldInfos actual = FieldInfos.getMergedFieldInfos(reader);
+    FieldInfos expected = FieldInfos.EMPTY;
+
+    assertThat(actual, sameInstance(expected));
+
+    reader.close();
+    writer.close();
+    dir.close();
+  }
+
+  public void testMergedFieldInfos_singleLeaf() throws IOException {
+    Directory dir = newDirectory();
+    IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())));
+
+    Document d1 = new Document();
+    d1.add(new StringField("f1", "v1", Field.Store.YES));
+    writer.addDocument(d1);
+    writer.commit();
+
+    Document d2 = new Document();
+    d2.add(new StringField("f2", "v2", Field.Store.YES));
+    writer.addDocument(d2);
+    writer.commit();
+
+    writer.forceMerge(1);
+
+    IndexReader reader = writer.getReader();
+    FieldInfos actual = FieldInfos.getMergedFieldInfos(reader);
+    FieldInfos expected = reader.leaves().get(0).reader().getFieldInfos();
+
+    assertThat(reader.leaves().size(), equalTo(1));
+    assertThat(actual, sameInstance(expected));
+
+    reader.close();
+    writer.close();
+    dir.close();
+  }
 }


Mime
View raw message