boolean any = false;
// silly starting guess:
final AtomicInteger totTermCount = new AtomicInteger(100);
final ExecutorService es = Executors.newCachedThreadPool();
while(System.currentTimeMillis() < stopTime && !failed.get()) {
if (random.nextBoolean()) {
if (VERBOSE) {
System.out.println("TEST: now reopen r=" + r);
}
final IndexReader r2 = r.reopen();
if (r != r2) {
r.close();
r = r2;
}
} else {
if (VERBOSE) {
System.out.println("TEST: now close reader=" + r);
}
r.close();
writer.commit();
final Set<String> openDeletedFiles = dir.getOpenDeletedFiles();
if (openDeletedFiles.size() > 0) {
System.out.println("OBD files: " + openDeletedFiles);
}
any |= openDeletedFiles.size() > 0;
//assertEquals("open but deleted: " + openDeletedFiles, 0, openDeletedFiles.size());
if (VERBOSE) {
System.out.println("TEST: now open");
}
r = IndexReader.open(writer, true);
}
if (VERBOSE) {
System.out.println("TEST: got new reader=" + r);
}
//System.out.println("numDocs=" + r.numDocs() + "
//openDelFileCount=" + dir.openDeleteFileCount());
smokeTestReader(r);
if (r.numDocs() > 0) {
final IndexSearcher s = new IndexSearcher(r, es);
// run search threads
final long searchStopTime = System.currentTimeMillis() + 500;
final Thread[] searchThreads = new Thread[NUM_SEARCH_THREADS];
final AtomicInteger totHits = new AtomicInteger();
for(int thread=0;thread<NUM_SEARCH_THREADS;thread++) {
searchThreads[thread] = new Thread() {
@Override
public void run() {
try {
TermEnum termEnum = s.getIndexReader().terms(new Term("body", ""));
int seenTermCount = 0;
int shift;
int trigger;
if (totTermCount.get() < 10) {
shift = 0;
trigger = 1;
} else {
trigger = totTermCount.get()/10;
shift = random.nextInt(trigger);
}
while(System.currentTimeMillis() < searchStopTime) {
Term term = termEnum.term();
if (term == null) {
if (seenTermCount < 10) {
break;
}
totTermCount.set(seenTermCount);
seenTermCount = 0;
trigger = totTermCount.get()/10;
//System.out.println("trigger " + trigger);
shift = random.nextInt(trigger);
termEnum = s.getIndexReader().terms(new Term("body", ""));
continue;
}
seenTermCount++;
// search 10 terms
if (trigger == 0) {
trigger = 1;
}
if ((seenTermCount + shift) % trigger == 0) {
//if (VERBOSE) {
//System.out.println(Thread.currentThread().getName() + " now search body:" + term.utf8ToString());
//}
totHits.addAndGet(runQuery(s, new TermQuery(term)));
}
termEnum.next();
}
if (VERBOSE) {
System.out.println(Thread.currentThread().getName() + ": search done");
}
} catch (Throwable t) {
System.out.println(Thread.currentThread().getName() + ": hit exc");
failed.set(true);
t.printStackTrace(System.out);
throw new RuntimeException(t);
}
}
};
searchThreads[thread].setDaemon(true);
searchThreads[thread].start();
}
for(int thread=0;thread<NUM_SEARCH_THREADS;thread++) {
searchThreads[thread].join();
}
if (VERBOSE) {
System.out.println("TEST: DONE search: totHits=" + totHits);
}
} else {
Thread.sleep(100);
}
}
es.shutdown();
es.awaitTermination(1, TimeUnit.SECONDS);
if (VERBOSE) {
System.out.println("TEST: all searching done [" + (System.currentTimeMillis()-t0) + " ms]");
}