pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / test / org / apache / lucene / index / TestNeverDelete.java
1 package org.apache.lucene.index;
2
3 /**
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 import java.io.File;
21 import java.util.HashSet;
22 import java.util.Set;
23
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;
30
31 // Make sure if you use NoDeletionPolicy that no file
32 // referenced by a commit point is ever deleted
33
34 public class TestNeverDelete extends LuceneTestCase {
35
36   public void testIndexing() throws Exception {
37     final File tmpDir = _TestUtil.getTempDir("TestNeverDelete");
38     final MockDirectoryWrapper d = newFSDirectory(tmpDir);
39
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,
45                                                       d,
46                                                       newIndexWriterConfig(TEST_VERSION_CURRENT,
47                                                                            new MockAnalyzer(random))
48                                                       .setIndexDeletionPolicy(NoDeletionPolicy.INSTANCE));
49     w.w.getConfig().setMaxBufferedDocs(_TestUtil.nextInt(random, 5, 30));
50
51     w.w.setInfoStream(VERBOSE ? System.out : null);
52     w.commit();
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() {
57           @Override
58           public void run() {
59             try {
60               int docCount = 0;
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));
65                 w.addDocument(doc);
66
67                 if (docCount % 13 == 0) {
68                   w.commit();
69                 }
70                 docCount++;
71               }
72             } catch (Exception e) {
73               throw new RuntimeException(e);
74             }
75           }
76         };
77       indexThreads[x].setName("Thread " + x);
78       indexThreads[x].start();
79     }
80
81     final Set<String> allFiles = new HashSet<String>();
82
83     IndexReader r = IndexReader.open(d);
84     while(System.currentTimeMillis() < stopTime) {
85       final IndexCommit ic = r.getIndexCommit();
86       if (VERBOSE) {
87         System.out.println("TEST: check files: " + ic.getFileNames());
88       }
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));
93       }
94       IndexReader r2 = IndexReader.openIfChanged(r);
95       if (r2 != null) {
96         r.close();
97         r = r2;
98       }
99       Thread.sleep(1);
100     }
101     r.close();
102
103     for(Thread t : indexThreads) {
104       t.join();
105     }
106     w.close();
107     d.close();
108
109     _TestUtil.rmDir(tmpDir);
110   }
111 }