public virtual void TestUpdateSegmentWithNoDocValues()
{
Directory dir = NewDirectory();
IndexWriterConfig conf = NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random()));
// prevent merges, otherwise by the time updates are applied
// (writer.Dispose()), the segments might have merged and that update becomes
// legit.
conf.SetMergePolicy(NoMergePolicy.COMPOUND_FILES);
IndexWriter writer = new IndexWriter(dir, conf);
// first segment with BDV
Document doc = new Document();
doc.Add(new StringField("id", "doc0", Store.NO));
doc.Add(new BinaryDocValuesField("bdv", ToBytes(3L)));
writer.AddDocument(doc);
doc = new Document();
doc.Add(new StringField("id", "doc4", Store.NO)); // document without 'bdv' field
writer.AddDocument(doc);
writer.Commit();
// second segment with no BDV
doc = new Document();
doc.Add(new StringField("id", "doc1", Store.NO));
writer.AddDocument(doc);
doc = new Document();
doc.Add(new StringField("id", "doc2", Store.NO)); // document that isn't updated
writer.AddDocument(doc);
writer.Commit();
// update document in the first segment - should not affect docsWithField of
// the document without BDV field
writer.UpdateBinaryDocValue(new Term("id", "doc0"), "bdv", ToBytes(5L));
// update document in the second segment - field should be added and we should
// be able to handle the other document correctly (e.g. no NPE)
writer.UpdateBinaryDocValue(new Term("id", "doc1"), "bdv", ToBytes(5L));
writer.Dispose();
DirectoryReader reader = DirectoryReader.Open(dir);
BytesRef scratch = new BytesRef();
foreach (AtomicReaderContext context in reader.Leaves)
{
AtomicReader r = context.AtomicReader;
BinaryDocValues bdv = r.GetBinaryDocValues("bdv");
Bits docsWithField = r.GetDocsWithField("bdv");
Assert.IsNotNull(docsWithField);
Assert.IsTrue(docsWithField.Get(0));
Assert.AreEqual(5L, GetValue(bdv, 0, scratch));
Assert.IsFalse(docsWithField.Get(1));
bdv.Get(1, scratch);
Assert.AreEqual(0, scratch.Length);
}
reader.Dispose();
dir.Dispose();
}