pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / test / org / apache / lucene / index / TestRollingUpdates.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 org.apache.lucene.analysis.MockAnalyzer;
21 import org.apache.lucene.document.*;
22 import org.apache.lucene.store.*;
23 import org.apache.lucene.util.*;
24 import org.junit.Test;
25
26 public class TestRollingUpdates extends LuceneTestCase {
27
28   // Just updates the same set of N docs over and over, to
29   // stress out deletions
30
31   @Test
32   public void testRollingUpdates() throws Exception {
33     final Directory dir = newDirectory();
34
35     final LineFileDocs docs = new LineFileDocs(random);
36
37     final IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)));
38     w.setInfoStream(VERBOSE ? System.out : null);
39     final int SIZE = atLeast(TEST_NIGHTLY ? 100 : 20);
40     int id = 0;
41     IndexReader r = null;
42     final int numUpdates = (int) (SIZE * (2+random.nextDouble()));
43     for(int docIter=0;docIter<numUpdates;docIter++) {
44       final Document doc = docs.nextDoc();
45       final String myID = ""+id;
46       if (id == SIZE-1) {
47         id = 0;
48       } else {
49         id++;
50       }
51       doc.getField("docid").setValue(myID);
52       w.updateDocument(new Term("docid", myID), doc);
53
54       if (docIter >= SIZE && random.nextInt(50) == 17) {
55         if (r != null) {
56           r.close();
57         }
58         final boolean applyDeletions = random.nextBoolean();
59         r = w.getReader(applyDeletions);
60         assertTrue("applyDeletions=" + applyDeletions + " r.numDocs()=" + r.numDocs() + " vs SIZE=" + SIZE, !applyDeletions || r.numDocs() == SIZE);
61       }
62     }
63
64     if (r != null) {
65       r.close();
66     }
67
68     w.commit();
69     assertEquals(SIZE, w.numDocs());
70
71     w.close();
72     docs.close();
73     
74     dir.close();
75   }
76   
77   
78   public void testUpdateSameDoc() throws Exception {
79     final Directory dir = newDirectory();
80
81     final LineFileDocs docs = new LineFileDocs(random);
82     for (int r = 0; r < 3; r++) {
83       final IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(
84           TEST_VERSION_CURRENT, new MockAnalyzer(random)).setMaxBufferedDocs(2));
85       final int numUpdates = atLeast(20);
86       int numThreads = _TestUtil.nextInt(random, 2, 6);
87       IndexingThread[] threads = new IndexingThread[numThreads];
88       for (int i = 0; i < numThreads; i++) {
89         threads[i] = new IndexingThread(docs, w, numUpdates);
90         threads[i].start();
91       }
92
93       for (int i = 0; i < numThreads; i++) {
94         threads[i].join();
95       }
96
97       w.close();
98     }
99
100     IndexReader open = IndexReader.open(dir);
101     assertEquals(1, open.numDocs());
102     open.close();
103     docs.close();
104     dir.close();
105   }
106   
107   static class IndexingThread extends Thread {
108     final LineFileDocs docs;
109     final IndexWriter writer;
110     final int num;
111     
112     public IndexingThread(LineFileDocs docs, IndexWriter writer, int num) {
113       super();
114       this.docs = docs;
115       this.writer = writer;
116       this.num = num;
117     }
118
119     public void run() {
120       try {
121         IndexReader open = null;
122         for (int i = 0; i < num; i++) {
123           Document doc = new Document();// docs.nextDoc();
124           doc.add(newField("id", "test", Field.Index.NOT_ANALYZED));
125           writer.updateDocument(new Term("id", "test"), doc);
126           if (random.nextInt(3) == 0) {
127             if (open == null) {
128               open = IndexReader.open(writer, true);
129             }
130             IndexReader reader = IndexReader.openIfChanged(open);
131             if (reader != null) {
132               open.close();
133               open = reader;
134             }
135             assertEquals("iter: " + i + " numDocs: "+ open.numDocs() + " del: " + open.numDeletedDocs() + " max: " + open.maxDoc(), 1, open.numDocs());
136           }
137         }
138         if (open != null) {
139           open.close();
140         }
141       } catch (Exception e) {
142         throw new RuntimeException(e);
143       }
144     }
145   }
146 }