| 1 |
|
package org.apache.lucene; |
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 41 (41) |
Complexity: 8 |
Complexity Density: 0.3 |
|
| 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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 37 |
0
|
private static int random(int i) { ... |
| 38 |
0
|
int r = RANDOM.nextInt(); |
| 39 |
0
|
if (r < 0) r = -r; |
| 40 |
0
|
return r % i; |
| 41 |
|
} |
| 42 |
|
|
|
|
|
| 0% |
Uncovered Elements: 25 (25) |
Complexity: 5 |
Complexity Density: 0.26 |
|
| 43 |
|
private static class IndexerThread extends Thread { |
| 44 |
|
private final int reopenInterval = 30 + random(60); |
| 45 |
|
IndexWriter writer; |
| 46 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 47 |
0
|
public IndexerThread(IndexWriter writer) {... |
| 48 |
0
|
this.writer = writer; |
| 49 |
|
} |
| 50 |
|
|
|
|
|
| 0% |
Uncovered Elements: 22 (22) |
Complexity: 4 |
Complexity Density: 0.22 |
|
| 51 |
0
|
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 |
|
|
| 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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 34 (34) |
Complexity: 9 |
Complexity Density: 0.47 |
|
| 84 |
|
private static class SearcherThread extends Thread { |
| 85 |
|
private IndexSearcher searcher; |
| 86 |
|
private final int reopenInterval = 10 + random(20); |
| 87 |
|
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
| 88 |
0
|
public SearcherThread(boolean useGlobal) throws java.io.IOException {... |
| 89 |
0
|
if (!useGlobal) |
| 90 |
0
|
this.searcher = new IndexSearcher("index"); |
| 91 |
|
} |
| 92 |
|
|
|
|
|
| 0% |
Uncovered Elements: 19 (19) |
Complexity: 5 |
Complexity Density: 0.45 |
|
| 93 |
0
|
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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
| 113 |
0
|
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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 33 (33) |
Complexity: 6 |
Complexity Density: 0.26 |
|
| 126 |
0
|
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 |
|
} |