Index: CHANGES.txt =================================================================== --- CHANGES.txt (revision 932055) +++ CHANGES.txt (working copy) @@ -343,6 +343,10 @@ and iterated as byte[] (wrapped in a BytesRef) by IndexReader for searching. +* LUCENE-2385: Moved NoDeletionPolicy from benchmark to core. NoDeletionPolicy + can be used to prevent commits from ever getting deleted from the index. + (Shai Erera) + Optimizations * LUCENE-2075: Terms dict cache is now shared across threads instead Index: contrib/benchmark/sortBench.py =================================================================== --- contrib/benchmark/sortBench.py (revision 932055) +++ contrib/benchmark/sortBench.py (working copy) @@ -83,7 +83,7 @@ analyzer=org.apache.lucene.analysis.standard.StandardAnalyzer $OTHER$ -deletion.policy = org.apache.lucene.benchmark.utils.NoDeletionPolicy +deletion.policy = org.apache.lucene.index.NoDeletionPolicy doc.tokenized = false doc.body.tokenized = true doc.stored = true Index: contrib/benchmark/conf/deletepercent.alg =================================================================== --- contrib/benchmark/conf/deletepercent.alg (revision 932055) +++ contrib/benchmark/conf/deletepercent.alg (working copy) @@ -35,7 +35,7 @@ #query.maker=org.apache.lucene.benchmark.byTask.feeds.SimpleQueryMaker query.maker=org.apache.lucene.benchmark.byTask.feeds.ReutersQueryMaker -deletion.policy=org.apache.lucene.benchmark.utils.NoDeletionPolicy +deletion.policy=org.apache.lucene.index.NoDeletionPolicy # task at this depth or less would print when they start task.max.depth.log=2 Index: contrib/benchmark/src/java/org/apache/lucene/benchmark/byTask/tasks/CreateIndexTask.java =================================================================== --- contrib/benchmark/src/java/org/apache/lucene/benchmark/byTask/tasks/CreateIndexTask.java (revision 932055) +++ contrib/benchmark/src/java/org/apache/lucene/benchmark/byTask/tasks/CreateIndexTask.java (working copy) @@ -25,6 +25,7 @@ import org.apache.lucene.index.MergeScheduler; import org.apache.lucene.index.ConcurrentMergeScheduler; import org.apache.lucene.index.MergePolicy; +import org.apache.lucene.index.NoDeletionPolicy; import org.apache.lucene.index.NoMergePolicy; import org.apache.lucene.index.NoMergeScheduler; import org.apache.lucene.index.IndexWriterConfig.OpenMode; @@ -135,23 +136,15 @@ public static IndexDeletionPolicy getIndexDeletionPolicy(Config config) { String deletionPolicyName = config.get("deletion.policy", "org.apache.lucene.index.KeepOnlyLastCommitDeletionPolicy"); - IndexDeletionPolicy indexDeletionPolicy = null; - RuntimeException err = null; - try { - indexDeletionPolicy = Class.forName(deletionPolicyName).asSubclass(IndexDeletionPolicy.class).newInstance(); - } catch (IllegalAccessException iae) { - err = new RuntimeException("unable to instantiate class '" + deletionPolicyName + "' as IndexDeletionPolicy"); - err.initCause(iae); - } catch (InstantiationException ie) { - err = new RuntimeException("unable to instantiate class '" + deletionPolicyName + "' as IndexDeletionPolicy"); - err.initCause(ie); - } catch (ClassNotFoundException cnfe) { - err = new RuntimeException("unable to load class '" + deletionPolicyName + "' as IndexDeletionPolicy"); - err.initCause(cnfe); + if (deletionPolicyName.equals(NoDeletionPolicy.class.getName())) { + return NoDeletionPolicy.INSTANCE; + } else { + try { + return Class.forName(deletionPolicyName).asSubclass(IndexDeletionPolicy.class).newInstance(); + } catch (Exception e) { + throw new RuntimeException("unable to instantiate class '" + deletionPolicyName + "' as IndexDeletionPolicy", e); + } } - if (err != null) - throw err; - return indexDeletionPolicy; } @Override Index: contrib/benchmark/src/java/org/apache/lucene/benchmark/utils/NoDeletionPolicy.java =================================================================== --- contrib/benchmark/src/java/org/apache/lucene/benchmark/utils/NoDeletionPolicy.java (revision 932055) +++ contrib/benchmark/src/java/org/apache/lucene/benchmark/utils/NoDeletionPolicy.java (working copy) @@ -1,37 +0,0 @@ -package org.apache.lucene.benchmark.utils; - -/** - * 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.List; - -import org.apache.lucene.index.IndexCommit; -import org.apache.lucene.index.IndexDeletionPolicy; - -public class NoDeletionPolicy implements IndexDeletionPolicy { - - public void onCommit(List commits) throws IOException { - // TODO Auto-generated method stub - - } - - public void onInit(List commits) throws IOException { - // TODO Auto-generated method stub - - } -} Index: contrib/benchmark/src/test/org/apache/lucene/benchmark/byTask/tasks/CreateIndexTaskTest.java =================================================================== --- contrib/benchmark/src/test/org/apache/lucene/benchmark/byTask/tasks/CreateIndexTaskTest.java (revision 932055) +++ contrib/benchmark/src/test/org/apache/lucene/benchmark/byTask/tasks/CreateIndexTaskTest.java (working copy) @@ -25,6 +25,7 @@ import org.apache.lucene.benchmark.BenchmarkTestCase; import org.apache.lucene.benchmark.byTask.PerfRunData; import org.apache.lucene.benchmark.byTask.utils.Config; +import org.apache.lucene.index.NoDeletionPolicy; import org.apache.lucene.index.NoMergePolicy; import org.apache.lucene.index.NoMergeScheduler; @@ -94,5 +95,12 @@ new CreateIndexTask(runData).doLogic(); new CloseIndexTask(runData).doLogic(); } + + public void testNoDeletionPolicy() throws Exception { + PerfRunData runData = createPerfRunData(null); + runData.getConfig().set("deletion.policy", NoDeletionPolicy.class.getName()); + new CreateIndexTask(runData).doLogic(); + new CloseIndexTask(runData).doLogic(); + } } Index: src/java/org/apache/lucene/index/NoDeletionPolicy.java =================================================================== --- src/java/org/apache/lucene/index/NoDeletionPolicy.java (revision 932055) +++ src/java/org/apache/lucene/index/NoDeletionPolicy.java (working copy) @@ -1,4 +1,4 @@ -package org.apache.lucene.benchmark.utils; +package org.apache.lucene.index; /** * Licensed to the Apache Software Foundation (ASF) under one or more @@ -20,18 +20,22 @@ import java.io.IOException; import java.util.List; -import org.apache.lucene.index.IndexCommit; -import org.apache.lucene.index.IndexDeletionPolicy; +/** + * An {@link IndexDeletionPolicy} which keeps all index commits around, never + * deleting them. This class is a singleton and can be accessed by referencing + * {@link #INSTANCE}. + */ +public final class NoDeletionPolicy implements IndexDeletionPolicy { -public class NoDeletionPolicy implements IndexDeletionPolicy { - - public void onCommit(List commits) throws IOException { - // TODO Auto-generated method stub - + /** The single instance of this class. */ + public static final IndexDeletionPolicy INSTANCE = new NoDeletionPolicy(); + + private NoDeletionPolicy() { + // keep private to avoid instantiation } + + public void onCommit(List commits) throws IOException {} - public void onInit(List commits) throws IOException { - // TODO Auto-generated method stub - - } + public void onInit(List commits) throws IOException {} + } Index: src/test/org/apache/lucene/index/TestNoDeletionPolicy.java =================================================================== --- src/test/org/apache/lucene/index/TestNoDeletionPolicy.java (revision 0) +++ src/test/org/apache/lucene/index/TestNoDeletionPolicy.java (revision 0) @@ -0,0 +1,93 @@ +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 static org.junit.Assert.*; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.Arrays; + +import org.apache.lucene.analysis.WhitespaceAnalyzer; +import org.apache.lucene.document.Document; +import org.apache.lucene.document.Field; +import org.apache.lucene.document.Field.Index; +import org.apache.lucene.document.Field.Store; +import org.apache.lucene.store.Directory; +import org.apache.lucene.store.RAMDirectory; +import org.apache.lucene.util.LuceneTestCaseJ4; +import org.junit.Test; + +public class TestNoDeletionPolicy extends LuceneTestCaseJ4 { + + @Test + public void testNoDeletionPolicy() throws Exception { + IndexDeletionPolicy idp = NoDeletionPolicy.INSTANCE; + idp.onInit(null); + idp.onCommit(null); + } + + @Test + public void testFinalSingleton() throws Exception { + assertTrue(Modifier.isFinal(NoDeletionPolicy.class.getModifiers())); + Constructor[] ctors = NoDeletionPolicy.class.getDeclaredConstructors(); + assertEquals("expected 1 private ctor only: " + Arrays.toString(ctors), 1, ctors.length); + assertTrue("that 1 should be private: " + ctors[0], Modifier.isPrivate(ctors[0].getModifiers())); + } + + @Test + public void testMethodsOverridden() throws Exception { + // Ensures that all methods of IndexDeletionPolicy are + // overridden/implemented. That's important to ensure that NoDeletionPolicy + // overrides everything, so that no unexpected behavior/error occurs. + // NOTE: even though IndexDeletionPolicy is an interface today, and so all + // methods must be implemented by NoDeletionPolicy, this test is important + // in case one day IDP becomes an abstract class. + for (Method m : NoDeletionPolicy.class.getMethods()) { + // getDeclaredMethods() returns just those methods that are declared on + // NoDeletionPolicy. getMethods() returns those that are visible in that + // context, including ones from Object. So just filter out Object. If in + // the future IndexDeletionPolicy will become a class that extends a + // different class than Object, this will need to change. + if (m.getDeclaringClass() != Object.class) { + assertTrue(m + " is not overridden !", m.getDeclaringClass() == NoDeletionPolicy.class); + } + } + } + + @Test + public void testAllCommitsRemain() throws Exception { + Directory dir = new RAMDirectory(); + IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig( + TEST_VERSION_CURRENT, new WhitespaceAnalyzer(TEST_VERSION_CURRENT)) + .setIndexDeletionPolicy(NoDeletionPolicy.INSTANCE)); + for (int i = 0; i < 10; i++) { + Document doc = new Document(); + doc.add(new Field("c", "a" + i, Store.YES, Index.ANALYZED)); + writer.addDocument(doc); + writer.commit(); + // the reason to expect i + 2 commits is because when IndexWriter is + // created it creates a first commit. If this ever changes, then the + // expected should be i + 1 (and this comment removed). + assertEquals("wrong number of commits !", i + 2, IndexReader.listCommits(dir).size()); + } + writer.close(); + } + +} Property changes on: src\test\org\apache\lucene\index\TestNoDeletionPolicy.java ___________________________________________________________________ Added: svn:keywords + Date Author Id Revision HeadURL Added: svn:eol-style + native Index: src/test/org/apache/lucene/index/TestNoMergeScheduler.java =================================================================== --- src/test/org/apache/lucene/index/TestNoMergeScheduler.java (revision 932055) +++ src/test/org/apache/lucene/index/TestNoMergeScheduler.java (working copy) @@ -46,9 +46,9 @@ @Test public void testMethodsOverridden() throws Exception { - // Ensures that all methods of MergePolicy are overridden. That's important - // to ensure that NoMergePolicy overrides everything, so that no unexpected - // behavior/error occurs + // Ensures that all methods of MergeScheduler are overridden. That's + // important to ensure that NoMergeScheduler overrides everything, so that + // no unexpected behavior/error occurs for (Method m : NoMergeScheduler.class.getMethods()) { // getDeclaredMethods() returns just those methods that are declared on // NoMergeScheduler. getMethods() returns those that are visible in that