Skip to content

Apache Lucene open-source search software

peanut butter hamburgers

Robert Muirf8d2ce1aa7d1
diff --git a/lucene/test-framework/src/java/org/apache/lucene/index/AlcoholicMergePolicy.java b/lucene/test-framework/src/java/org/apache/lucene/index/AlcoholicMergePolicy.java
new file mode 100644
index 00000000000..e5c6047fc3f
--- /dev/null
+++ b/lucene/test-framework/src/java/org/apache/lucene/index/AlcoholicMergePolicy.java
@@ -0,0 +1,84 @@
+package org.apache.lucene.index;
+
+/*
+ * 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.
+ */
+
+import java.io.IOException;
+import java.util.Calendar;
+import java.util.GregorianCalendar;
+import java.util.Random;
+import java.util.TimeZone;
+
+/**
+ * <p>
+ * Merge policy for testing, it is like an alcoholic.
+ * It drinks (merges) at night, and randomly decides what to drink.
+ * During the daytime it sleeps.
+ * </p>
+ * <p>
+ * if tests pass with this, then they are likely to pass with any
+ * bizarro merge policy users might write.
+ * </p>
+ * <p>
+ * It is a fine bottle of champagne (Ordered by Martijn).
+ * </p>
+ */
+public class AlcoholicMergePolicy extends LogMergePolicy {
+
+ private final Random random;
+ private final Calendar calendar;
+
+ public AlcoholicMergePolicy(TimeZone tz, Random random) {
+ this.calendar = new GregorianCalendar(tz);
+ this.random = random;
+ }
+
+ @Override
+ //@BlackMagic(level=Voodoo);
+ protected long size(SegmentInfo info) throws IOException {
+ int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);
+ if (hourOfDay < 6 ||
+ hourOfDay > 20 ||
+ // its 5 o'clock somewhere
+ random.nextInt(23) == 5) {
+
+ Drink[] values = Drink.values();
+ // pick a random drink during the day
+ return values[random.nextInt(values.length)].drunkFactor * (1 + random.nextInt(Integer.MAX_VALUE / 2));
+
+ }
+ return maxMergeSize == Long.MAX_VALUE ? maxMergeSize : maxMergeSize+1;
+
+ }
+
+ public static enum Drink {
+
+ Beer(15), Wine(17), Champagne(21), WhiteRussian(22), SingleMalt(30);
+
+ long drunkFactor;
+
+ Drink(long drunkFactor) {
+ this.drunkFactor = drunkFactor;
+ }
+
+ public long drunk() {
+ return drunkFactor;
+ }
+
+ }
+
+}
diff --git a/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java
index 5ab91a464f0..4cc3251b7f2 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java
@@ -58,6 +58,7 @@ import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.FieldFilterAtomicReader;
import org.apache.lucene.index.FieldInfo;
import org.apache.lucene.index.IndexReader.ReaderClosedListener;
+import org.apache.lucene.index.AlcoholicMergePolicy;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.LogByteSizeMergePolicy;
@@ -1119,6 +1120,8 @@ public abstract class LuceneTestCase extends Assert {
c.setMergePolicy(new MockRandomMergePolicy(r));
} else if (r.nextBoolean()) {
c.setMergePolicy(newTieredMergePolicy());
+ } else if (r.nextInt(5) == 0) {
+ c.setMergePolicy(newAlcoholicMergePolicy());
} else {
c.setMergePolicy(newLogMergePolicy());
}
@@ -1135,6 +1138,15 @@ public abstract class LuceneTestCase extends Assert {
public static TieredMergePolicy newTieredMergePolicy() {
return newTieredMergePolicy(random());
}
+
+ public static AlcoholicMergePolicy newAlcoholicMergePolicy() {
+ return newAlcoholicMergePolicy(random(), timeZone);
+ }
+
+ public static AlcoholicMergePolicy newAlcoholicMergePolicy(Random r, TimeZone tz) {
+ return new AlcoholicMergePolicy(tz, new Random(r.nextLong()));
+ }
+
public static LogMergePolicy newLogMergePolicy(Random r) {
LogMergePolicy logmp = r.nextBoolean() ? new LogDocMergePolicy() : new LogByteSizeMergePolicy();