private IndexReader Build(Random random, TestIndex index)
{
/* build an index */
Document doc = new Document();
Field idField = NewStringField(random, "id", "", Field.Store.YES);
Field randField = NewStringField(random, "rand", "", Field.Store.YES);
Field bodyField = NewStringField(random, "body", "", Field.Store.NO);
doc.Add(idField);
doc.Add(randField);
doc.Add(bodyField);
RandomIndexWriter writer = new RandomIndexWriter(random, index.Index, NewIndexWriterConfig(random, TEST_VERSION_CURRENT, new MockAnalyzer(random)).SetOpenMode(OpenMode.CREATE).SetMaxBufferedDocs(TestUtil.NextInt(random, 50, 1000)).SetMergePolicy(NewLogMergePolicy()));
TestUtil.ReduceOpenFiles(writer.w);
while (true)
{
int minCount = 0;
int maxCount = 0;
for (int d = MinId; d <= MaxId; d++)
{
idField.StringValue = Pad(d);
int r = index.AllowNegativeRandomInts ? random.Next() : random.Next(int.MaxValue);
if (index.MaxR < r)
{
index.MaxR = r;
maxCount = 1;
}
else if (index.MaxR == r)
{
maxCount++;
}
if (r < index.MinR)
{
index.MinR = r;
minCount = 1;
}
else if (r == index.MinR)
{
minCount++;
}
randField.StringValue = Pad(r);
bodyField.StringValue = "body";
writer.AddDocument(doc);
}
if (minCount == 1 && maxCount == 1)
{
// our subclasses rely on only 1 doc having the min or
// max, so, we loop until we satisfy that. it should be
// exceedingly rare (Yonik calculates 1 in ~429,000)
// times) that this loop requires more than one try:
IndexReader ir = writer.Reader;
writer.Dispose();
return ir;
}
// try again
writer.DeleteAll();
}
}