Skip to content

Commit

Permalink
HSEARCH-3998 Avoid loading (and indexing) instances of non-indexed su…
Browse files Browse the repository at this point in the history
…btypes during mass indexing
  • Loading branch information
yrodiere committed Sep 23, 2020
1 parent 7770f31 commit 1f02bec
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
Expand Up @@ -151,7 +151,7 @@ private void doBatchWork() throws InterruptedException {
private <E> BatchIndexingWorkspace<E, ?> createBatchIndexingWorkspace(MassIndexingIndexedTypeGroup<E> typeGroup) {
return new BatchIndexingWorkspace<>(
mappingContext, sessionContext, getNotifier(),
typeGroup.commonSuperType(), typeGroup.idAttribute(),
typeGroup.commonSuperType(), typeGroup.idAttribute(), typeGroup.includedIndexedTypesOrEmpty(),
documentBuilderThreads, cacheMode,
objectLoadingBatchSize,
objectsLimit, idFetchSize, transactionTimeout
Expand Down
Expand Up @@ -9,6 +9,7 @@
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
Expand Down Expand Up @@ -40,6 +41,7 @@ public class BatchIndexingWorkspace<E, I> extends FailureHandledRunnable {

private final HibernateOrmMassIndexingIndexedTypeContext<E> type;
private final SingularAttribute<? super E, I> idAttributeOfType;
private final Set<Class<? extends E>> includedTypesFilter;

private final ProducerConsumerQueue<List<I>> primaryKeyStream;

Expand All @@ -61,6 +63,7 @@ public class BatchIndexingWorkspace<E, I> extends FailureHandledRunnable {
DetachedBackendSessionContext sessionContext,
MassIndexingNotifier notifier,
HibernateOrmMassIndexingIndexedTypeContext<E> type, SingularAttribute<? super E, I> idAttributeOfType,
Set<Class<? extends E>> includedTypesFilter,
int objectLoadingThreads, CacheMode cacheMode, int objectLoadingBatchSize,
long objectsLimit,
int idFetchSize, Integer transactionTimeout) {
Expand All @@ -70,6 +73,7 @@ public class BatchIndexingWorkspace<E, I> extends FailureHandledRunnable {

this.type = type;
this.idAttributeOfType = idAttributeOfType;
this.includedTypesFilter = includedTypesFilter;
this.idFetchSize = idFetchSize;
this.transactionTimeout = transactionTimeout;

Expand Down Expand Up @@ -133,7 +137,7 @@ private void startProducingPrimaryKeys(BatchTransactionalContext transactionalCo
getNotifier(),
primaryKeyStream,
objectLoadingBatchSize,
type, idAttributeOfType,
type, idAttributeOfType, includedTypesFilter,
objectsLimit,
idFetchSize
),
Expand Down
Expand Up @@ -9,6 +9,7 @@
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
Expand Down Expand Up @@ -50,6 +51,7 @@ public class IdentifierProducer<E, I> implements StatelessSessionAwareRunnable {

private final HibernateOrmMassIndexingIndexedTypeContext<E> type;
private final SingularAttribute<? super E, I> idAttributeOfType;
private final Set<Class<? extends E>> includedTypesFilter;

private final ProducerConsumerQueue<List<I>> destination;
private final int batchSize;
Expand All @@ -72,12 +74,14 @@ public class IdentifierProducer<E, I> implements StatelessSessionAwareRunnable {
ProducerConsumerQueue<List<I>> fromIdentifierListToEntities,
int objectLoadingBatchSize,
HibernateOrmMassIndexingIndexedTypeContext<E> type, SingularAttribute<? super E, I> idAttributeOfType,
Set<Class<? extends E>> includedTypesFilter,
long objectsLimit, int idFetchSize) {
this.sessionFactory = sessionFactory;
this.tenantId = tenantId;
this.notifier = notifier;
this.type = type;
this.idAttributeOfType = idAttributeOfType;
this.includedTypesFilter = includedTypesFilter;
this.destination = fromIdentifierListToEntities;
this.batchSize = objectLoadingBatchSize;
this.objectsLimit = objectsLimit;
Expand Down Expand Up @@ -181,6 +185,9 @@ private Query<Long> createTotalCountQuery(StatelessSession session) {

Root<E> root = criteriaQuery.from( type.entityTypeDescriptor() );
criteriaQuery.select( criteriaBuilder.count( root ) );
if ( !includedTypesFilter.isEmpty() ) {
criteriaQuery.where( root.type().in( includedTypesFilter ) );
}

return session.createQuery( criteriaQuery )
.setCacheable( false );
Expand All @@ -193,6 +200,9 @@ private Query<I> createIdentifiersQuery(StatelessSession session) {
Root<E> root = criteriaQuery.from( type.entityTypeDescriptor() );
Path<I> idPath = root.get( idAttributeOfType );
criteriaQuery.select( idPath );
if ( !includedTypesFilter.isEmpty() ) {
criteriaQuery.where( root.type().in( includedTypesFilter ) );
}

return session.createQuery( criteriaQuery )
.setCacheable( false )
Expand Down
Expand Up @@ -85,6 +85,20 @@ public HibernateOrmMassIndexingIndexedTypeContext<E> commonSuperType() {
return typeDescriptor.getId( typeDescriptor.getIdType().getJavaType() );
}

public Set<Class<? extends E>> includedIndexedTypesOrEmpty() {
if ( commonSuperType.entityPersister().getEntityMetamodel().getSubclassEntityNames().size()
== includedTypes.size() ) {
// All types are included, no need to filter.
return Collections.emptySet();
}

Set<Class<? extends E>> classes = new HashSet<>( includedTypes.size() );
for ( HibernateOrmMassIndexingIndexedTypeContext<? extends E> includedType : includedTypes ) {
classes.add( includedType.entityTypeDescriptor().getJavaType() );
}
return classes;
}

/**
* Merge this group with the other group if
* the other group's {@code commonSuperType} represents a supertype or subtype
Expand Down

0 comments on commit 1f02bec

Please sign in to comment.