KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > ThreadSafetyTest


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 JavaDoc;
28 import java.io.File JavaDoc;
29
30 class ThreadSafetyTest {
31   private static final Analyzer ANALYZER = new SimpleAnalyzer();
32   private static final Random JavaDoc RANDOM = new Random JavaDoc();
33   private static Searcher SEARCHER;
34
35   private static int ITERATIONS = 1;
36
37   private static int random(int i) { // for JDK 1.1 compatibility
38
int r = RANDOM.nextInt();
39     if (r < 0) r = -r;
40     return r % i;
41   }
42
43   private static class IndexerThread extends Thread JavaDoc {
44     private final int reopenInterval = 30 + random(60);
45     IndexWriter writer;
46
47     public IndexerThread(IndexWriter writer) {
48       this.writer = writer;
49     }
50
51     public void run() {
52       try {
53         boolean useCompoundFiles = false;
54         
55         for (int i = 0; i < 1024*ITERATIONS; i++) {
56           Document d = new Document();
57           int n = RANDOM.nextInt();
58           d.add(Field.Keyword("id", Integer.toString(n)));
59           d.add(Field.UnStored("contents", English.intToEnglish(n)));
60           System.out.println("Adding " + n);
61           
62           // Switch between single and multiple file segments
63
useCompoundFiles = Math.random() < 0.5;
64           writer.setUseCompoundFile(useCompoundFiles);
65           
66           writer.addDocument(d);
67
68           if (i%reopenInterval == 0) {
69             writer.close();
70             writer = new IndexWriter("index", ANALYZER, false);
71           }
72         }
73         
74         writer.close();
75
76       } catch (Exception JavaDoc e) {
77         System.out.println(e.toString());
78         e.printStackTrace();
79         System.exit(0);
80       }
81     }
82   }
83
84   private static class SearcherThread extends Thread JavaDoc {
85     private IndexSearcher searcher;
86     private final int reopenInterval = 10 + random(20);
87
88     public SearcherThread(boolean useGlobal) throws java.io.IOException JavaDoc {
89       if (!useGlobal)
90         this.searcher = new IndexSearcher("index");
91     }
92
93     public void run() {
94       try {
95         for (int i = 0; i < 512*ITERATIONS; i++) {
96           searchFor(RANDOM.nextInt(), (searcher==null)?SEARCHER:searcher);
97           if (i%reopenInterval == 0) {
98             if (searcher == null) {
99               SEARCHER = new IndexSearcher("index");
100             } else {
101               searcher.close();
102               searcher = new IndexSearcher("index");
103             }
104           }
105         }
106       } catch (Exception JavaDoc e) {
107         System.out.println(e.toString());
108         e.printStackTrace();
109         System.exit(0);
110       }
111     }
112
113     private void searchFor(int n, Searcher searcher)
114       throws Exception JavaDoc {
115       System.out.println("Searching for " + n);
116       Hits hits =
117         searcher.search(QueryParser.parse(English.intToEnglish(n), "contents",
118                                           ANALYZER));
119       System.out.println("Search for " + n + ": total=" + hits.length());
120       for (int j = 0; j < Math.min(3, hits.length()); j++) {
121         System.out.println("Hit for " + n + ": " + hits.doc(j).get("id"));
122       }
123     }
124   }
125
126   public static void main(String JavaDoc[] args) throws Exception JavaDoc {
127
128     boolean readOnly = false;
129     boolean add = false;
130
131     for (int i = 0; i < args.length; i++) {
132       if ("-ro".equals(args[i]))
133         readOnly = true;
134       if ("-add".equals(args[i]))
135         add = true;
136     }
137
138     File JavaDoc indexDir = new File JavaDoc("index");
139     if (! indexDir.exists()) indexDir.mkdirs();
140     
141     IndexReader.unlock(FSDirectory.getDirectory(indexDir, false));
142
143     if (!readOnly) {
144       IndexWriter writer = new IndexWriter(indexDir, ANALYZER, !add);
145       
146       Thread JavaDoc indexerThread = new IndexerThread(writer);
147       indexerThread.start();
148       
149       Thread.sleep(1000);
150     }
151       
152     SearcherThread searcherThread1 = new SearcherThread(false);
153     searcherThread1.start();
154
155     SEARCHER = new IndexSearcher(indexDir.toString());
156
157     SearcherThread searcherThread2 = new SearcherThread(true);
158     searcherThread2.start();
159
160     SearcherThread searcherThread3 = new SearcherThread(true);
161     searcherThread3.start();
162   }
163 }
164
Popular Tags