Examine icon indicating copy to clipboard operation
Examine copied to clipboard

Abstaction of LuceneIndex.cs

Open croban opened this issue 1 year ago • 2 comments

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();
}

croban avatar Oct 18 '23 19:10 croban

I need to understand why this is being done in the first place?

Shazwazza avatar Oct 18 '23 21:10 Shazwazza

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?

croban avatar Oct 19 '23 08:10 croban

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.

Shazwazza avatar Jun 14 '24 15:06 Shazwazza

Will be shipped with 3.2.1 today.

Shazwazza avatar Jun 14 '24 15:06 Shazwazza

https://github.com/Shazwazza/Examine/releases/tag/v3.2.1

Shazwazza avatar Jun 14 '24 19:06 Shazwazza