1 package org.apache.lucene; 2 3 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 private static int random(int i) { int r = RANDOM.nextInt(); 39 if (r < 0) r = -r; 40 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 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 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 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 { 85 private IndexSearcher searcher; 86 private final int reopenInterval = 10 + random(20); 87 88 public SearcherThread(boolean useGlobal) throws java.io.IOException { 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 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 { 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 [] args) throws Exception { 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 indexDir = new File ("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 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 |