You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ro...@apache.org on 2021/09/30 08:50:26 UTC

[lucene] branch main updated: Support addition of diagnostics by custom merge policies (#329)

This is an automated email from the ASF dual-hosted git repository.

romseygeek pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/lucene.git


The following commit(s) were added to refs/heads/main by this push:
     new 3e568b9  Support addition of diagnostics by custom merge policies (#329)
3e568b9 is described below

commit 3e568b911fba60bc944fc4147525e4ce03930ceb
Author: Chris Hegarty <62...@users.noreply.github.com>
AuthorDate: Thu Sep 30 09:50:22 2021 +0100

    Support addition of diagnostics by custom merge policies (#329)
    
    This commit adds a new `addDiagnostics` method to `SegmentInfo` that
    allows custom merge policies to add new diagnostic information to the
    segment's diagnostic map.
---
 lucene/CHANGES.txt                                 |  2 +
 .../java/org/apache/lucene/index/MergePolicy.java  |  2 +-
 .../java/org/apache/lucene/index/SegmentInfo.java  | 15 +++++++
 .../lucene/index/TestDemoParallelLeafReader.java   |  5 +--
 .../org/apache/lucene/index/TestSegmentInfos.java  | 46 ++++++++++++++++++++++
 5 files changed, 65 insertions(+), 5 deletions(-)

diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index adf4c14..0e337f3 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -153,6 +153,8 @@ API Changes
 * LUCENE-10115: Add an extension point, BaseQueryParser#getFuzzyDistance, to allow custom
   query parsers to determine the similarity distance for fuzzy queries. (Chris Hegarty)
 
+*  LUCENE-10132: Support addition of diagnostics by custom merge policies (Chris Hegarty)
+
 Improvements
 
 * LUCENE-9960: Avoid unnecessary top element replacement for equal elements in PriorityQueue. (Dawid Weiss)
diff --git a/lucene/core/src/java/org/apache/lucene/index/MergePolicy.java b/lucene/core/src/java/org/apache/lucene/index/MergePolicy.java
index 6652c0c..2d9717a 100644
--- a/lucene/core/src/java/org/apache/lucene/index/MergePolicy.java
+++ b/lucene/core/src/java/org/apache/lucene/index/MergePolicy.java
@@ -269,7 +269,7 @@ public abstract class MergePolicy {
 
     /**
      * Expert: Sets the {@link SegmentCommitInfo} of the merged segment. Allows sub-classes to e.g.
-     * set diagnostics properties.
+     * {@link SegmentInfo#addDiagnostics(Map) add diagnostic} properties.
      */
     public void setMergeInfo(SegmentCommitInfo info) {
       this.info = info;
diff --git a/lucene/core/src/java/org/apache/lucene/index/SegmentInfo.java b/lucene/core/src/java/org/apache/lucene/index/SegmentInfo.java
index e09cc3a..de87d73 100644
--- a/lucene/core/src/java/org/apache/lucene/index/SegmentInfo.java
+++ b/lucene/core/src/java/org/apache/lucene/index/SegmentInfo.java
@@ -85,6 +85,21 @@ public final class SegmentInfo {
     this.diagnostics = Map.copyOf(Objects.requireNonNull(diagnostics));
   }
 
+  /**
+   * Adds or modifies this segment's diagnostics.
+   *
+   * <p>Entries in the given map whose keys are not present in the current diagnostics are added.
+   * Otherwise, existing entries are modified with the given map's value.
+   *
+   * @param diagnostics the additional diagnostics
+   */
+  public void addDiagnostics(Map<String, String> diagnostics) {
+    Objects.requireNonNull(diagnostics);
+    Map<String, String> copy = new HashMap<>(this.diagnostics);
+    copy.putAll(diagnostics);
+    setDiagnostics(copy);
+  }
+
   /** Returns diagnostics saved into the segment when it was written. The map is immutable. */
   public Map<String, String> getDiagnostics() {
     return diagnostics;
diff --git a/lucene/core/src/test/org/apache/lucene/index/TestDemoParallelLeafReader.java b/lucene/core/src/test/org/apache/lucene/index/TestDemoParallelLeafReader.java
index 7c939e3..2e1f2eb 100644
--- a/lucene/core/src/test/org/apache/lucene/index/TestDemoParallelLeafReader.java
+++ b/lucene/core/src/test/org/apache/lucene/index/TestDemoParallelLeafReader.java
@@ -25,7 +25,6 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.Comparator;
-import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
@@ -676,9 +675,7 @@ public class TestDemoParallelLeafReader extends LuceneTestCase {
         @Override
         public void setMergeInfo(SegmentCommitInfo info) {
           // Record that this merged segment is current as of this schemaGen:
-          Map<String, String> copy = new HashMap<>(info.info.getDiagnostics());
-          copy.put(SCHEMA_GEN_KEY, Long.toString(schemaGen));
-          info.info.setDiagnostics(copy);
+          info.info.addDiagnostics(Map.of(SCHEMA_GEN_KEY, Long.toString(schemaGen)));
           super.setMergeInfo(info);
         }
       }
diff --git a/lucene/core/src/test/org/apache/lucene/index/TestSegmentInfos.java b/lucene/core/src/test/org/apache/lucene/index/TestSegmentInfos.java
index 91e537a..99b772e 100644
--- a/lucene/core/src/test/org/apache/lucene/index/TestSegmentInfos.java
+++ b/lucene/core/src/test/org/apache/lucene/index/TestSegmentInfos.java
@@ -392,4 +392,50 @@ public class TestSegmentInfos extends LuceneTestCase {
     dir.close();
     corruptDir.close();
   }
+
+  /** Test addDiagnostics method */
+  public void testAddDiagnostics() throws Throwable {
+    SegmentInfo si;
+    final Directory dir = newDirectory();
+    Codec codec = Codec.getDefault();
+
+    // diagnostics map
+    Map<String, String> diagnostics = Map.of("key1", "value1", "key2", "value2");
+
+    // adds an additional key/value pair
+    si =
+        new SegmentInfo(
+            dir,
+            Version.LATEST,
+            Version.LATEST,
+            "TEST",
+            10000,
+            false,
+            codec,
+            diagnostics,
+            StringHelper.randomId(),
+            new HashMap<>(),
+            Sort.INDEXORDER);
+    si.addDiagnostics(Map.of("key3", "value3"));
+    assertEquals(Map.of("key1", "value1", "key2", "value2", "key3", "value3"), si.getDiagnostics());
+
+    // modifies an existing key/value pair
+    si =
+        new SegmentInfo(
+            dir,
+            Version.LATEST,
+            Version.LATEST,
+            "TEST",
+            10000,
+            false,
+            codec,
+            diagnostics,
+            StringHelper.randomId(),
+            new HashMap<>(),
+            Sort.INDEXORDER);
+    si.addDiagnostics(Map.of("key2", "foo"));
+    assertEquals(Map.of("key1", "value1", "key2", "foo"), si.getDiagnostics());
+
+    dir.close();
+  }
 }