public virtual void TestUpdatesAndDeletes()
{
// create an index with a segment with only deletes, a segment with both
// deletes and updates and a segment with only updates
Directory dir = NewDirectory();
IndexWriterConfig conf = NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random()));
conf.SetMaxBufferedDocs(10); // control segment flushing
conf.SetMergePolicy(NoMergePolicy.COMPOUND_FILES); // prevent merges for this test
IndexWriter writer = new IndexWriter(dir, conf);
for (int i = 0; i < 6; i++)
{
writer.AddDocument(Doc(i));
if (i % 2 == 1)
{
writer.Commit(); // create 2-docs segments
}
}
// delete doc-1 and doc-2
writer.DeleteDocuments(new Term("id", "doc-1"), new Term("id", "doc-2")); // 1st and 2nd segments
// update docs 3 and 5
writer.UpdateBinaryDocValue(new Term("id", "doc-3"), "val", ToBytes(17L));
writer.UpdateBinaryDocValue(new Term("id", "doc-5"), "val", ToBytes(17L));
DirectoryReader reader;
if (Random().NextBoolean()) // not NRT
{
writer.Dispose();
reader = DirectoryReader.Open(dir);
} // NRT
else
{
reader = DirectoryReader.Open(writer, true);
writer.Dispose();
}
AtomicReader slow = SlowCompositeReaderWrapper.Wrap(reader);
Bits liveDocs = slow.LiveDocs;
bool[] expectedLiveDocs = new bool[] { true, false, false, true, true, true };
for (int i = 0; i < expectedLiveDocs.Length; i++)
{
Assert.AreEqual(expectedLiveDocs[i], liveDocs.Get(i));
}
long[] expectedValues = new long[] { 1, 2, 3, 17, 5, 17 };
BinaryDocValues bdv = slow.GetBinaryDocValues("val");
BytesRef scratch = new BytesRef();
for (int i = 0; i < expectedValues.Length; i++)
{
Assert.AreEqual(expectedValues[i], GetValue(bdv, i, scratch));
}
reader.Dispose();
dir.Dispose();
}