lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cpoersc...@apache.org
Subject [04/50] [abbrv] lucene-solr:jira/solr-6203: LUCENE-7688: Add OneMergeWrappingMergePolicy class. (Keith Laban, Christine Poerschke)
Date Fri, 24 Feb 2017 21:19:47 GMT
LUCENE-7688: Add OneMergeWrappingMergePolicy class. (Keith Laban, Christine Poerschke)


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

Branch: refs/heads/jira/solr-6203
Commit: 6f9acb51549f8edd5164f8db26d72f83448d0fc1
Parents: cf63ad9
Author: Christine Poerschke <cpoerschke@apache.org>
Authored: Mon Feb 20 17:19:54 2017 +0000
Committer: Christine Poerschke <cpoerschke@apache.org>
Committed: Mon Feb 20 17:38:38 2017 +0000

----------------------------------------------------------------------
 lucene/CHANGES.txt                              |   3 +
 .../index/OneMergeWrappingMergePolicy.java      |  72 +++++++++
 .../index/TestOneMergeWrappingMergePolicy.java  | 146 +++++++++++++++++++
 3 files changed, 221 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/6f9acb51/lucene/CHANGES.txt
----------------------------------------------------------------------
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 47939c7..46a3e8d 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -116,6 +116,9 @@ New Features
   implementation for analysis/tokenization (Clinton Gormley, Mike
   McCandless)
 
+* LUCENE-7688: Add OneMergeWrappingMergePolicy class.
+  (Keith Laban, Christine Poerschke)
+
 Bug Fixes
 
 * LUCENE-7630: Fix (Edge)NGramTokenFilter to no longer drop payloads

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/6f9acb51/lucene/core/src/java/org/apache/lucene/index/OneMergeWrappingMergePolicy.java
----------------------------------------------------------------------
diff --git a/lucene/core/src/java/org/apache/lucene/index/OneMergeWrappingMergePolicy.java
b/lucene/core/src/java/org/apache/lucene/index/OneMergeWrappingMergePolicy.java
new file mode 100644
index 0000000..982e8c2
--- /dev/null
+++ b/lucene/core/src/java/org/apache/lucene/index/OneMergeWrappingMergePolicy.java
@@ -0,0 +1,72 @@
+/*
+ * 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.index;
+
+import java.io.IOException;
+import java.util.Map;
+import java.util.function.UnaryOperator;
+
+/**
+ * A wrapping merge policy that wraps the {@link org.apache.lucene.index.MergePolicy.OneMerge}
+ * objects returned by the wrapped merge policy.
+ *
+ * @lucene.experimental
+ */
+public class OneMergeWrappingMergePolicy extends MergePolicyWrapper {
+
+  private final UnaryOperator<OneMerge> wrapOneMerge;
+
+  /**
+   * Constructor
+   *
+   * @param in - the wrapped merge policy
+   * @param wrapOneMerge - operator for wrapping OneMerge objects
+   */
+  public OneMergeWrappingMergePolicy(MergePolicy in, UnaryOperator<OneMerge> wrapOneMerge)
{
+    super(in);
+    this.wrapOneMerge = wrapOneMerge;
+  }
+
+  @Override
+  public MergeSpecification findMerges(MergeTrigger mergeTrigger, SegmentInfos segmentInfos,
IndexWriter writer)
+      throws IOException {
+    return wrapSpec(in.findMerges(mergeTrigger, segmentInfos, writer));
+  }
+
+  @Override
+  public MergeSpecification findForcedMerges(SegmentInfos segmentInfos, int maxSegmentCount,
+      Map<SegmentCommitInfo,Boolean> segmentsToMerge, IndexWriter writer) throws IOException
{
+    return wrapSpec(in.findForcedMerges(segmentInfos, maxSegmentCount, segmentsToMerge, writer));
+  }
+
+  @Override
+  public MergeSpecification findForcedDeletesMerges(SegmentInfos segmentInfos, IndexWriter
writer)
+    throws IOException {
+    return wrapSpec(in.findForcedDeletesMerges(segmentInfos, writer));
+  }
+
+  private MergeSpecification wrapSpec(MergeSpecification spec) {
+    MergeSpecification wrapped = spec == null ? null : new MergeSpecification();
+    if (wrapped != null) {
+      for (OneMerge merge : spec.merges) {
+        wrapped.add(wrapOneMerge.apply(merge));
+      }
+    }
+    return wrapped;
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/6f9acb51/lucene/core/src/test/org/apache/lucene/index/TestOneMergeWrappingMergePolicy.java
----------------------------------------------------------------------
diff --git a/lucene/core/src/test/org/apache/lucene/index/TestOneMergeWrappingMergePolicy.java
b/lucene/core/src/test/org/apache/lucene/index/TestOneMergeWrappingMergePolicy.java
new file mode 100644
index 0000000..f27437f
--- /dev/null
+++ b/lucene/core/src/test/org/apache/lucene/index/TestOneMergeWrappingMergePolicy.java
@@ -0,0 +1,146 @@
+/*
+ * 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.index;
+
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.util.LuceneTestCase;
+import org.apache.lucene.util.StringHelper;
+import org.apache.lucene.util.TestUtil;
+import org.apache.lucene.util.Version;
+import org.junit.Test;
+
+public class TestOneMergeWrappingMergePolicy extends LuceneTestCase {
+
+  private static class PredeterminedMergePolicy extends MergePolicy {
+
+    final private MergePolicy.MergeSpecification merges;
+    final private MergePolicy.MergeSpecification forcedMerges;
+    final private MergePolicy.MergeSpecification forcedDeletesMerges;
+
+    public PredeterminedMergePolicy(
+        MergePolicy.MergeSpecification merges,
+        MergePolicy.MergeSpecification forcedMerges,
+        MergePolicy.MergeSpecification forcedDeletesMerges) {
+      this.merges = merges;
+      this.forcedMerges = forcedMerges;
+      this.forcedDeletesMerges = forcedDeletesMerges;
+    }
+
+    @Override
+    public MergePolicy.MergeSpecification findMerges(MergeTrigger mergeTrigger, SegmentInfos
segmentInfos, IndexWriter writer)
+        throws IOException {
+      return merges;
+    }
+
+    @Override
+    public MergePolicy.MergeSpecification findForcedMerges(SegmentInfos segmentInfos, int
maxSegmentCount,
+        Map<SegmentCommitInfo,Boolean> segmentsToMerge, IndexWriter writer) throws
IOException {
+      return forcedMerges;
+    }
+
+    @Override
+    public MergePolicy.MergeSpecification findForcedDeletesMerges(SegmentInfos segmentInfos,
IndexWriter writer)
+        throws IOException {
+      return forcedDeletesMerges;
+    }
+
+  }
+
+  private static class WrappedOneMerge extends MergePolicy.OneMerge {
+
+    final MergePolicy.OneMerge original;
+
+    public WrappedOneMerge(MergePolicy.OneMerge original) {
+      super(original.segments);
+      this.original = original;
+    }
+
+  }
+
+  @Test
+  public void testSegmentsAreWrapped() throws IOException {
+    try (final Directory dir = newDirectory()) {
+      // first create random merge specs
+      final MergePolicy.MergeSpecification msM = createRandomMergeSpecification(dir);
+      final MergePolicy.MergeSpecification msF = createRandomMergeSpecification(dir);
+      final MergePolicy.MergeSpecification msD = createRandomMergeSpecification(dir);
+      // secondly, pass them to the predetermined merge policy constructor
+      final MergePolicy originalMP = new PredeterminedMergePolicy(msM, msF, msD);
+      // thirdly wrap the predetermined merge policy
+      final MergePolicy oneMergeWrappingMP = new OneMergeWrappingMergePolicy(
+          originalMP,
+          merge -> new WrappedOneMerge(merge));
+      // finally, ask for merges and check what we got
+      implTestSegmentsAreWrapped(msM, oneMergeWrappingMP.findMerges(null, null, null));
+      implTestSegmentsAreWrapped(msF, oneMergeWrappingMP.findForcedMerges(null, 0, null,
null));
+      implTestSegmentsAreWrapped(msD, oneMergeWrappingMP.findForcedDeletesMerges(null, null));
+    }
+  }
+
+  private static void implTestSegmentsAreWrapped(MergePolicy.MergeSpecification originalMS,
MergePolicy.MergeSpecification testMS) {
+    // wrapping does not add or remove merge specs
+    assertEquals((originalMS == null), (testMS == null));
+    if (originalMS == null) return;
+    assertEquals(originalMS.merges.size(), testMS.merges.size());
+    // wrapping does not re-order merge specs
+    for (int ii = 0; ii < originalMS.merges.size(); ++ii) {
+        final MergePolicy.OneMerge originalOM = originalMS.merges.get(ii);
+        final MergePolicy.OneMerge testOM = testMS.merges.get(ii);
+        // wrapping wraps
+        assertTrue(testOM instanceof WrappedOneMerge);
+        final WrappedOneMerge wrappedOM = (WrappedOneMerge)testOM;
+        // and what is wrapped is what was originally passed in
+        assertEquals(originalOM, wrappedOM.original);
+    }
+  }
+
+  private static MergePolicy.MergeSpecification createRandomMergeSpecification(Directory
dir) {
+    MergePolicy.MergeSpecification ms;
+    if (0 < random().nextInt(10)) { // ~ 1 in 10 times return null
+      ms = new MergePolicy.MergeSpecification();
+      // append up to 10 (random non-sensical) one merge objects
+      for (int ii = 0; ii < random().nextInt(10); ++ii) {
+        final SegmentInfo si = new SegmentInfo(
+            dir, // dir
+            Version.LATEST, // version
+            TestUtil.randomSimpleString(random()), // name
+            random().nextInt(), // maxDoc
+            random().nextBoolean(), // isCompoundFile
+            null, // codec
+            Collections.emptyMap(), // diagnostics
+            TestUtil.randomSimpleString(// id
+                random(),
+                StringHelper.ID_LENGTH,
+                StringHelper.ID_LENGTH).getBytes(StandardCharsets.US_ASCII),
+            Collections.emptyMap(), // attributes
+            null /* indexSort */);
+        final List<SegmentCommitInfo> segments = new LinkedList<SegmentCommitInfo>();
+        segments.add(new SegmentCommitInfo(si, 0, 0, 0, 0));
+        ms.add(new MergePolicy.OneMerge(segments));
+      }
+    }
+    return null;
+  }
+
+}


Mime
View raw message