Apache Lucene open-source search software
Support addition of diagnostics by custom merge policies (#329)
Chris Hegarty3e568b911fba
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex adf4c142f74..0e337f33402 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 customquery 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.javaindex 6652c0cb9b9..2d9717ad0d5 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.javaindex e09cc3afc87..de87d73fa10 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.javaindex 7c939e3f0f4..2e1f2ebad07 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 {@Overridepublic 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.javaindex 91e537a8a8c..99b772effdd 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();+ }}