add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / backwards / 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     final int SIZE = atLeast(20);
39     int id = 0;
40     IndexReader r = null;
41     final int numUpdates = (int) (SIZE * (2+random.nextDouble()));
42     for(int docIter=0;docIter<numUpdates;docIter++) {
43       final Document doc = docs.nextDoc();
44       final String myID = ""+id;
45       if (id == SIZE-1) {
46         id = 0;
47       } else {
48         id++;
49       }
50       doc.getField("docid").setValue(myID);
51       w.updateDocument(new Term("docid", myID), doc);
52
53       if (docIter >= SIZE && random.nextInt(50) == 17) {
54         if (r != null) {
55           r.close();
56         }
57         final boolean applyDeletions = random.nextBoolean();
58         r = w.getReader(applyDeletions);
59         assertTrue("applyDeletions=" + applyDeletions + " r.numDocs()=" + r.numDocs() + " vs SIZE=" + SIZE, !applyDeletions || r.numDocs() == SIZE);
60       }
61     }
62
63     if (r != null) {
64       r.close();
65     }
66
67     w.commit();
68     assertEquals(SIZE, w.numDocs());
69
70     w.close();
71     docs.close();
72     
73     dir.close();
74   }
75   
76   
77   public void testUpdateSameDoc() throws Exception {
78     final Directory dir = newDirectory();
79
80     final LineFileDocs docs = new LineFileDocs(random);
81     for (int r = 0; r < 3; r++) {
82       final IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(
83           TEST_VERSION_CURRENT, new MockAnalyzer(random)).setMaxBufferedDocs(2));
84       final int numUpdates = atLeast(20);
85       int numThreads = _TestUtil.nextInt(random, 2, 6);
86       IndexingThread[] threads = new IndexingThread[numThreads];
87       for (int i = 0; i < numThreads; i++) {
88         threads[i] = new IndexingThread(docs, w, numUpdates);
89         threads[i].start();
90       }
91
92       for (int i = 0; i < numThreads; i++) {
93         threads[i].join();
94       }
95
96       w.close();
97     }
98
99     IndexReader open = IndexReader.open(dir);
100     assertEquals(1, open.numDocs());
101     open.close();
102     docs.close();
103     dir.close();
104   }
105   
106   static class IndexingThread extends Thread {
107     final LineFileDocs docs;
108     final IndexWriter writer;
109     final int num;
110     
111     public IndexingThread(LineFileDocs docs, IndexWriter writer, int num) {
112       super();
113       this.docs = docs;
114       this.writer = writer;
115       this.num = num;
116     }
117
118     public void run() {
119       try {
120         IndexReader open = null;
121         for (int i = 0; i < num; i++) {
122           Document doc = new Document();// docs.nextDoc();
123           doc.add(newField("id", "test", Field.Index.NOT_ANALYZED));
124           writer.updateDocument(new Term("id", "test"), doc);
125           if (random.nextInt(3) == 0) {
126             if (open == null) {
127               open = IndexReader.open(writer, true);
128             }
129             IndexReader reader = open.reopen();
130             if (reader != open) {
131               open.close();
132               open = reader;
133             }
134             assertEquals("iter: " + i + " numDocs: "+ open.numDocs() + " del: " + open.numDeletedDocs() + " max: " + open.maxDoc(), 1, open.numDocs());
135           }
136         }
137         if (open != null) {
138           open.close();
139         }
140       } catch (Exception e) {
141         throw new RuntimeException(e);
142       }
143     }
144   }
145 }