Examine
Examine copied to clipboard
Abstaction of LuceneIndex.cs
Problem: overriding AddDocument
Method
This code is used to access and set the private field _latestGen
in a derived class.
var result = IndexWriter.UpdateDocuments(new Term(ExamineFieldNames.ItemIdFieldName, valueSet.Id), doc);
Type baseType = typeof(LuceneIndex);
FieldInfo privateFieldInfo = baseType.GetField("_latestGen", BindingFlags.Instance | BindingFlags.NonPublic);
if (privateFieldInfo != null)
{
privateFieldInfo.SetValue(this, result);
}
Resolve Solution
Option 1: Making the Private Field Protected Abstract or Virtual
Should _latestGen
be made a protected abstract field?
_latestGen = IndexWriter.UpdateDocument(new Term(ExamineFieldNames.ItemIdFieldName, valueSet.Id), doc);
should be at least:
protected abstract IndexWriter LatestGen { get; set; }
Option 2: Making the Method Virtual
Should the WaitForChanges
method be made virtual?
public void WaitForChanges()
should be at least:
public virtual void WaitForChanges()
{
if(mycustomupdatelatestGen) {
base.WaitForChanges();
return;
}
base.WaitForChanges();
}
I need to understand why this is being done in the first place?
I want to create Block Index (BlockJoin) therefore I must use UpdateDocuments Method and I have to change
IndexWriter.UpdateDocument
to IndexWriter.UpdateDocuments
inside my override void AddDocument
method.
Do you have maybe another solution for this?
Really sorry for the long delay here. I can't fields or properties mutable read/write since that is dangerous and can easily result in folks making mistakes that are hard to debug.
An easy solution is to add a virtual method:
protected virtual long? UpdateLuceneDocument(Term term, Document doc)
Then you can override this, update the document as you wish and return your custom lastestGen value.
I'll get this done and ship a new version shortly.
Will be shipped with 3.2.1 today.
https://github.com/Shazwazza/Examine/releases/tag/v3.2.1