addToTotalCount() deprecated

I’m trying to understand how to get rid of deprected method addToTotalCount() in my Mass Indexing Monitor but with no success.

package com.thevegcat.app.search;

import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Consumer;

import org.hibernate.search.mapper.pojo.massindexing.MassIndexingMonitor;
import org.hibernate.search.mapper.pojo.massindexing.MassIndexingTypeGroupMonitor;
import org.hibernate.search.mapper.pojo.massindexing.MassIndexingTypeGroupMonitorCreateContext;
import org.hibernate.search.mapper.pojo.massindexing.impl.LegacyDelegatingMassIndexingTypeGroupMonitor;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class CustomMassIndexingMonitor implements MassIndexingMonitor {

    @NoArgsConstructor @Getter
    public class ReindexingSummary {
    	private AtomicLong documentsAdded  = new AtomicLong(0L);
        private AtomicLong documentsBuilt  = new AtomicLong(0L);
        private AtomicLong entitiesLoaded  = new AtomicLong(0L);
        private AtomicLong addToTotalCount = new AtomicLong(0L);
    }

    final Consumer<ReindexingSummary> reindexingDoneCallback;
    final ReindexingSummary           reindexingSummary;

	public CustomMassIndexingMonitor(final Consumer<ReindexingSummary> reindexingDoneCallback) {
		this.reindexingDoneCallback = reindexingDoneCallback;
		this.reindexingSummary      = new ReindexingSummary();
	}
	
    @Override public void documentsAdded(long increment)  { this.reindexingSummary.getDocumentsAdded().addAndGet(increment);  log.trace("documentsAdded: {}",  Long.valueOf(this.reindexingSummary.getDocumentsAdded().get()));  }
    @Override public void documentsBuilt(long increment)  { this.reindexingSummary.getDocumentsBuilt().addAndGet(increment);  log.trace("documentsBuilt: {}",  Long.valueOf(this.reindexingSummary.getDocumentsBuilt().get()));  }
    @Override public void entitiesLoaded(long increment)  { this.reindexingSummary.getEntitiesLoaded().addAndGet(increment);  log.trace("entitiesLoaded: {}",  Long.valueOf(this.reindexingSummary.getEntitiesLoaded().get()));  }
    @Override public void addToTotalCount(long increment) { this.reindexingSummary.getAddToTotalCount().addAndGet(increment); log.trace("addToTotalCount: {}", Long.valueOf(this.reindexingSummary.getAddToTotalCount().get())); }

	@Override
	public void indexingCompleted() {
        this.reindexingDoneCallback.accept(this.reindexingSummary);
	}

}

JavaDoc has a hint but my understanding of it didn’t happen:

Use MassIndexingTypeGroupMonitor.indexingStarted(MassIndexingTypeGroupMonitorContext) and get the total count, if available, from the MassIndexingTypeGroupMonitorContext.totalCount(). Alternatively, use the typeGroupMonitor(MassIndexingTypeGroupMonitorCreateContext) and obtain the count from MassIndexingTypeGroupMonitorCreateContext.totalCount() if a count is needed before any indexing processes are started.


Is it possible to fix my monitor to keep the same functionalities but without deprecated methods?

Thanks!

Hi @horvoje

This one is from Search code itself:

So, depending on what you want to achieve… you should start with removing addToTotalCount and then add:

@Override
public MassIndexingTypeGroupMonitor typeGroupMonitor(MassIndexingTypeGroupMonitorCreateContext context) {
	// do this if you want "addToTotalCount" to be called before any indexing has started:
	OptionalLong count = context.totalCount();
	if ( count.isPresent() ) {
		this.reindexingSummary.getAddToTotalCount().addAndGet( increment );
		log.trace( "addToTotalCount: {}", Long.valueOf( CustomMassIndexingMonitor.this.reindexingSummary.getAddToTotalCount().get() ) );
	}
	return DoNothingMassIndexingTypeGroupMonitor.INSTANCE; // this is not a real class, you'd need to create one...

	// or do this instead, if you want the "addToTotalCount" to be called when indexing of a particular type group starts:
	return new MyMassIndexingTypeGroupMonitor();
}

private class MyMassIndexingTypeGroupMonitor implements MassIndexingTypeGroupMonitor {

	@Override
	public void documentsIndexed(long increment) {

	}

	@Override
	public void indexingStarted(MassIndexingTypeGroupMonitorContext context) {
		OptionalLong count = context.totalCount();
		if ( count.isPresent() ) {
			CustomMassIndexingMonitor.this.reindexingSummary.getAddToTotalCount().addAndGet( increment );
			CustomMassIndexingMonitor.this.log.trace( "addToTotalCount: {}", Long.valueOf( CustomMassIndexingMonitor.this.reindexingSummary.getAddToTotalCount().get() ) );
		}
	}

	@Override
	public void indexingCompleted(MassIndexingTypeGroupMonitorContext context) {

	}
}

From your impl it doesn’t seem like there would be much difference for you whether you increment the total count before the indexing of when the type group indexing is started, so both approaches ^ should produce similar results.

There is no more addToTotalCount call in Hibernate Search. One of the reasons this was changed was to account for the fact that sometimes you (or your app :smiley: e.g. when you have some sort of a data stream instead of a database, where you would need to read it all to just get a count) may not know the total number of entities your massindexer will be indexing, hence Search wouldn’t be able to provide the total to addToTotalCount.