1 package org.apache.lucene.index;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 import java.util.HashSet;
24 import org.apache.lucene.analysis.MockAnalyzer;
25 import org.apache.lucene.document.Document;
26 import org.apache.lucene.document.Field;
27 import org.apache.lucene.store.MockDirectoryWrapper;
28 import org.apache.lucene.util.LuceneTestCase;
29 import org.apache.lucene.util._TestUtil;
31 // Make sure if you use NoDeletionPolicy that no file
32 // referenced by a commit point is ever deleted
34 public class TestNeverDelete extends LuceneTestCase {
36 public void testIndexing() throws Exception {
37 final File tmpDir = _TestUtil.getTempDir("TestNeverDelete");
38 final MockDirectoryWrapper d = newFSDirectory(tmpDir);
40 // We want to "see" files removed if Lucene removed
41 // them. This is still worth running on Windows since
42 // some files the IR opens and closes.
43 d.setNoDeleteOpenFile(false);
44 final RandomIndexWriter w = new RandomIndexWriter(random,
46 newIndexWriterConfig(TEST_VERSION_CURRENT,
47 new MockAnalyzer(random))
48 .setIndexDeletionPolicy(NoDeletionPolicy.INSTANCE));
49 w.w.getConfig().setMaxBufferedDocs(_TestUtil.nextInt(random, 5, 30));
51 w.w.setInfoStream(VERBOSE ? System.out : null);
53 Thread[] indexThreads = new Thread[random.nextInt(4)];
54 final long stopTime = System.currentTimeMillis() + atLeast(1000);
55 for (int x=0; x < indexThreads.length; x++) {
56 indexThreads[x] = new Thread() {
61 while (System.currentTimeMillis() < stopTime) {
62 final Document doc = new Document();
63 doc.add(newField("dc", ""+docCount, Field.Store.YES, Field.Index.NOT_ANALYZED));
64 doc.add(newField("field", "here is some text", Field.Store.YES, Field.Index.ANALYZED));
67 if (docCount % 13 == 0) {
72 } catch (Exception e) {
73 throw new RuntimeException(e);
77 indexThreads[x].setName("Thread " + x);
78 indexThreads[x].start();
81 final Set<String> allFiles = new HashSet<String>();
83 IndexReader r = IndexReader.open(d);
84 while(System.currentTimeMillis() < stopTime) {
85 final IndexCommit ic = r.getIndexCommit();
87 System.out.println("TEST: check files: " + ic.getFileNames());
89 allFiles.addAll(ic.getFileNames());
90 // Make sure no old files were removed
91 for(String fileName : allFiles) {
92 assertTrue("file " + fileName + " does not exist", d.fileExists(fileName));
94 IndexReader r2 = IndexReader.openIfChanged(r);
103 for(Thread t : indexThreads) {
109 _TestUtil.rmDir(tmpDir);