final LineFileDocs docs = new LineFileDocs(random);
final int RUN_TIME_MSEC = atLeast(500);
final IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).setMaxBufferedDocs(-1).setRAMBufferSizeMB(64);
final File tempDir = _TestUtil.getTempDir("fstlines");
final MockDirectoryWrapper dir = new MockDirectoryWrapper(random, FSDirectory.open(tempDir));
final IndexWriter writer = new IndexWriter(dir, conf);
writer.setInfoStream(VERBOSE ? System.out : null);
final long stopTime = System.currentTimeMillis() + RUN_TIME_MSEC;
Document doc;
int docCount = 0;
while((doc = docs.nextDoc()) != null && System.currentTimeMillis() < stopTime) {
writer.addDocument(doc);
docCount++;
}
IndexReader r = IndexReader.open(writer, true);
writer.close();
final PositiveIntOutputs outputs = PositiveIntOutputs.getSingleton(random.nextBoolean());
Builder<Long> builder = new Builder<Long>(FST.INPUT_TYPE.BYTE2, 0, 0, true, outputs);
boolean storeOrd = false;
if (VERBOSE) {
if (storeOrd) {
System.out.println("FST stores ord");
} else {
System.out.println("FST stores docFreq");
}
}
TermEnum termEnum = r.terms(new Term("body", ""));
if (VERBOSE) {
System.out.println("TEST: got termEnum=" + termEnum);
}
int ord = 0;
while(true) {
final Term term = termEnum.term();
if (term == null || !"body".equals(term.field())) {
break;
}
// No ord in 3.x:
/*
if (ord == 0) {
try {
termsEnum.ord();
} catch (UnsupportedOperationException uoe) {
if (VERBOSE) {
System.out.println("TEST: codec doesn't support ord; FST stores docFreq");
}
storeOrd = false;
}
}
*/
final int output;
if (storeOrd) {
output = ord;
} else {
output = termEnum.docFreq();
}
//System.out.println("ADD: " + term.text() + " ch[0]=" + (term.text().length() == 0 ? -1 : term.text().charAt(0)));
builder.add(toIntsRef(term.text()), outputs.get(output));
ord++;
if (ord % 100000 == 0 && LuceneTestCase.TEST_NIGHTLY) {
System.out.println(ord + " terms...");
}
termEnum.next();
}
final FST<Long> fst = builder.finish();
if (VERBOSE) {
System.out.println("FST: " + docCount + " docs; " + ord + " terms; " + fst.getNodeCount() + " nodes; " + fst.getArcCount() + " arcs;" + " " + fst.sizeInBytes() + " bytes");
}
if (ord > 0) {
// Now confirm BytesRefFSTEnum and TermEnum act the
// same:
final IntsRefFSTEnum<Long> fstEnum = new IntsRefFSTEnum<Long>(fst);
int num = atLeast(1000);
for(int iter=0;iter<num;iter++) {
final String randomTerm = getRandomString();
if (VERBOSE) {
System.out.println("TEST: seek " + randomTerm + " ch[0]=" + (randomTerm.length() == 0 ? -1 : randomTerm.charAt(0)));
}
termEnum = r.terms(new Term("body", randomTerm));
final IntsRefFSTEnum.InputOutput fstSeekResult = fstEnum.seekCeil(toIntsRef(randomTerm));
if (termEnum.term() == null || !"body".equals(termEnum.term().field())) {
assertNull("got " + (fstSeekResult == null ? "null" : toString(fstSeekResult.input) + " but expected null"), fstSeekResult);
} else {
assertSame(termEnum, fstEnum, storeOrd);
for(int nextIter=0;nextIter<10;nextIter++) {
if (VERBOSE) {
System.out.println("TEST: next");
//if (storeOrd) {
//System.out.println(" ord=" + termEnum.ord());
//}
}
termEnum.next();
if (termEnum.term() != null && "body".equals(termEnum.term().field())) {
if (VERBOSE) {
System.out.println(" term=" + termEnum.term());
}
assertNotNull(fstEnum.next());
assertSame(termEnum, fstEnum, storeOrd);
} else {
if (VERBOSE) {
System.out.println(" end!");
}
IntsRefFSTEnum.InputOutput<Long> nextResult = fstEnum.next();
if (nextResult != null) {
System.out.println("expected null but got: input=" + toString(nextResult.input) + " output=" + outputs.outputToString(nextResult.output));
fail();
}
break;
}
}
}
}
}
r.close();
dir.close();
}