Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
../../../img/srcFileCovDistChart0.png 86% of files have more coverage
65   163   22   9.29
28   116   0.34   2.33
7     3.14  
3    
 
  ThreadSafetyTest       Line # 30 27 8 0% 0.0
  ThreadSafetyTest.IndexerThread       Line # 43 19 5 0% 0.0
  ThreadSafetyTest.SearcherThread       Line # 84 19 9 0% 0.0
 
No Tests
 
1    package org.apache.lucene;
2   
3    /**
4    * Copyright 2004 The Apache Software Foundation
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10    * http://www.apache.org/licenses/LICENSE-2.0
11    *
12    * Unless required by applicable law or agreed to in writing, software
13    * distributed under the License is distributed on an "AS IS" BASIS,
14    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15    * See the License for the specific language governing permissions and
16    * limitations under the License.
17    */
18   
19    import org.apache.lucene.util.*;
20    import org.apache.lucene.store.*;
21    import org.apache.lucene.document.*;
22    import org.apache.lucene.analysis.*;
23    import org.apache.lucene.index.*;
24    import org.apache.lucene.search.*;
25    import org.apache.lucene.queryParser.*;
26   
27    import java.util.Random;
28    import java.io.File;
29   
 
30    class ThreadSafetyTest {
31    private static final Analyzer ANALYZER = new SimpleAnalyzer();
32    private static final Random RANDOM = new Random();
33    private static Searcher SEARCHER;
34   
35    private static int ITERATIONS = 1;
36   
 
37  0 toggle private static int random(int i) { // for JDK 1.1 compatibility
38  0 int r = RANDOM.nextInt();
39  0 if (r < 0) r = -r;
40  0 return r % i;
41    }
42   
 
43    private static class IndexerThread extends Thread {
44    private final int reopenInterval = 30 + random(60);
45    IndexWriter writer;
46   
 
47  0 toggle public IndexerThread(IndexWriter writer) {
48  0 this.writer = writer;
49    }
50   
 
51  0 toggle public void run() {
52  0 try {
53  0 boolean useCompoundFiles = false;
54   
55  0 for (int i = 0; i < 1024*ITERATIONS; i++) {
56  0 Document d = new Document();
57  0 int n = RANDOM.nextInt();
58  0 d.add(new Field("id", Integer.toString(n), Field.Store.YES, Field.Index.UN_TOKENIZED));
59  0 d.add(new Field("contents", English.intToEnglish(n), Field.Store.NO, Field.Index.TOKENIZED));
60  0 System.out.println("Adding " + n);
61   
62    // Switch between single and multiple file segments
63  0 useCompoundFiles = Math.random() < 0.5;
64  0 writer.setUseCompoundFile(useCompoundFiles);
65   
66  0 writer.addDocument(d);
67   
68  0 if (i%reopenInterval == 0) {
69  0 writer.close();
70  0 writer = new IndexWriter("index", ANALYZER, false);
71    }
72    }
73   
74  0 writer.close();
75   
76    } catch (Exception e) {
77  0 System.out.println(e.toString());
78  0 e.printStackTrace();
79  0 System.exit(0);
80    }
81    }
82    }
83   
 
84    private static class SearcherThread extends Thread {
85    private IndexSearcher searcher;
86    private final int reopenInterval = 10 + random(20);
87   
 
88  0 toggle public SearcherThread(boolean useGlobal) throws java.io.IOException {
89  0 if (!useGlobal)
90  0 this.searcher = new IndexSearcher("index");
91    }
92   
 
93  0 toggle public void run() {
94  0 try {
95  0 for (int i = 0; i < 512*ITERATIONS; i++) {
96  0 searchFor(RANDOM.nextInt(), (searcher==null)?SEARCHER:searcher);
97  0 if (i%reopenInterval == 0) {
98  0 if (searcher == null) {
99  0 SEARCHER = new IndexSearcher("index");
100    } else {
101  0 searcher.close();
102  0 searcher = new IndexSearcher("index");
103    }
104    }
105    }
106    } catch (Exception e) {
107  0 System.out.println(e.toString());
108  0 e.printStackTrace();
109  0 System.exit(0);
110    }
111    }
112   
 
113  0 toggle private void searchFor(int n, Searcher searcher)
114    throws Exception {
115  0 System.out.println("Searching for " + n);
116  0 QueryParser parser = new QueryParser("contents", ANALYZER);
117  0 Hits hits =
118    searcher.search(parser.parse(English.intToEnglish(n)));
119  0 System.out.println("Search for " + n + ": total=" + hits.length());
120  0 for (int j = 0; j < Math.min(3, hits.length()); j++) {
121  0 System.out.println("Hit for " + n + ": " + hits.doc(j).get("id"));
122    }
123    }
124    }
125   
 
126  0 toggle public static void main(String[] args) throws Exception {
127   
128  0 boolean readOnly = false;
129  0 boolean add = false;
130   
131  0 for (int i = 0; i < args.length; i++) {
132  0 if ("-ro".equals(args[i]))
133  0 readOnly = true;
134  0 if ("-add".equals(args[i]))
135  0 add = true;
136    }
137   
138  0 File indexDir = new File("index");
139  0 if (! indexDir.exists()) indexDir.mkdirs();
140   
141  0 IndexReader.unlock(FSDirectory.getDirectory(indexDir, false));
142   
143  0 if (!readOnly) {
144  0 IndexWriter writer = new IndexWriter(indexDir, ANALYZER, !add);
145   
146  0 Thread indexerThread = new IndexerThread(writer);
147  0 indexerThread.start();
148   
149  0 Thread.sleep(1000);
150    }
151   
152  0 SearcherThread searcherThread1 = new SearcherThread(false);
153  0 searcherThread1.start();
154   
155  0 SEARCHER = new IndexSearcher(indexDir.toString());
156   
157  0 SearcherThread searcherThread2 = new SearcherThread(true);
158  0 searcherThread2.start();
159   
160  0 SearcherThread searcherThread3 = new SearcherThread(true);
161  0 searcherThread3.start();
162    }
163    }