Skip to content

Commit

Permalink
HSEARCH-4843 Avoid EntityReferenceFactory and its entity type lookup …
Browse files Browse the repository at this point in the history
…where possible

It turns out that in most cases, we already have some representation of
the entity type, which allows us to simply use a
PojoEntityReferenceFactoryDelegate (which is a simple constructor call).
  • Loading branch information
yrodiere committed May 1, 2023
1 parent db77c8e commit 789bffb
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.util.Optional;
import java.util.function.Supplier;

import org.hibernate.search.engine.backend.common.spi.EntityReferenceFactory;
import org.hibernate.search.engine.common.EntityReference;
import org.hibernate.search.mapper.pojo.automaticindexing.impl.PojoImplicitReindexingResolver;
import org.hibernate.search.mapper.pojo.automaticindexing.impl.PojoImplicitReindexingResolverRootContext;
Expand Down Expand Up @@ -159,9 +158,8 @@ public final void resolveEntitiesToReindex(PojoReindexingCollector collector, Po
reindexingResolver.resolveEntitiesToReindex( collector, entitySupplier.get(), context );
}
catch (RuntimeException e) {
EntityReferenceFactory entityReferenceFactory = sessionContext.mappingContext().entityReferenceFactory();
EntityReference entityReference = EntityReferenceFactory.safeCreateEntityReference(
entityReferenceFactory, entityName, identifier, e::addSuppressed );
EntityReference entityReference = sessionContext.mappingContext().entityReferenceFactoryDelegate()
.create( typeIdentifier, entityName, identifier );
throw log.errorResolvingEntitiesToReindex( entityReference, e.getMessage(), e );
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public DocumentRouter<? super E> router() {
public PojoDocumentContributor<E> toDocumentContributor(PojoWorkSessionContext sessionContext,
PojoIndexingProcessorRootContext processorContext,
I identifier, Supplier<E> entitySupplier) {
return new PojoDocumentContributor<>( entityName, processor, sessionContext, processorContext,
return new PojoDocumentContributor<>( typeIdentifier, entityName, processor, sessionContext, processorContext,
identifier, entitySupplier );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,12 @@ public EntityReference extractReference(PojoMassIndexingSessionContext sessionCo
PojoMassIndexingIndexedTypeContext<?> typeContext = typeContextProvider.indexedForExactType( targetType );
String entityName = typeContext.entityName();
Object identifier = extractIdentifier( typeContext, sessionContext, entity );
return mappingContext.entityReferenceFactory().createEntityReference( entityName, identifier );
return mappingContext.entityReferenceFactoryDelegate().create( targetType, entityName, identifier );
}

public EntityReference makeSuperTypeReference(Object identifier) {
return mappingContext.entityReferenceFactory().createEntityReference(
commonSuperType.entityName(),
identifier
);
return mappingContext.entityReferenceFactoryDelegate().create( commonSuperType.typeIdentifier(),
commonSuperType.entityName(), identifier );
}

public <E2> Object extractIdentifier(PojoMassIndexingIndexedTypeContext<E2> typeContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.hibernate.search.engine.backend.mapping.spi.BackendMappingContext;
import org.hibernate.search.engine.reporting.FailureHandler;
import org.hibernate.search.engine.environment.thread.spi.ThreadPoolProvider;
import org.hibernate.search.mapper.pojo.common.spi.PojoEntityReferenceFactoryDelegate;

/**
* Contextual information about a search mapping.
Expand All @@ -34,4 +35,9 @@ public interface PojoMassIndexingMappingContext extends BackendMappingContext {
*/
PojoMassIndexerAgent createMassIndexerAgent(PojoMassIndexerAgentCreateContext context);

/**
* @return A {@link PojoEntityReferenceFactoryDelegate}.
*/
PojoEntityReferenceFactoryDelegate entityReferenceFactoryDelegate();

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;

import org.hibernate.search.engine.backend.common.spi.EntityReferenceFactory;
import org.hibernate.search.engine.backend.common.spi.MultiEntityOperationExecutionReport;
import org.hibernate.search.engine.backend.work.execution.OperationSubmitter;
import org.hibernate.search.engine.common.EntityReference;
Expand Down Expand Up @@ -360,9 +359,8 @@ void resolveDirty(PojoLoadingPlanProvider loadingPlanProvider, PojoReindexingCol
typeContext().reindexingResolver().resolveEntitiesToReindex( collector, entitySupplier.get(), this );
}
catch (RuntimeException e) {
EntityReferenceFactory entityReferenceFactory = sessionContext.mappingContext().entityReferenceFactory();
EntityReference entityReference = EntityReferenceFactory.safeCreateEntityReference(
entityReferenceFactory, typeContext().entityName(), identifier, e::addSuppressed );
EntityReference entityReference = sessionContext.mappingContext().entityReferenceFactoryDelegate()
.create( typeContext().typeIdentifier(), typeContext().entityName(), identifier );
throw log.errorResolvingEntitiesToReindex( entityReference, e.getMessage(), e );
}
typeContext().resolveEntitiesToReindex( collector, sessionContext, identifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
import java.lang.invoke.MethodHandles;
import java.util.function.Supplier;

import org.hibernate.search.engine.backend.common.spi.EntityReferenceFactory;
import org.hibernate.search.engine.backend.document.DocumentElement;
import org.hibernate.search.engine.backend.work.execution.spi.DocumentContributor;
import org.hibernate.search.engine.common.EntityReference;
import org.hibernate.search.mapper.pojo.common.spi.PojoEntityReferenceFactoryDelegate;
import org.hibernate.search.mapper.pojo.logging.impl.Log;
import org.hibernate.search.mapper.pojo.model.spi.PojoRawTypeIdentifier;
import org.hibernate.search.mapper.pojo.processing.impl.PojoIndexingProcessor;
import org.hibernate.search.mapper.pojo.processing.spi.PojoIndexingProcessorRootContext;
import org.hibernate.search.mapper.pojo.work.spi.PojoWorkSessionContext;
Expand All @@ -25,6 +26,7 @@
public final class PojoDocumentContributor<E> implements DocumentContributor {
private static final Log log = LoggerFactory.make( Log.class, MethodHandles.lookup() );

private final PojoRawTypeIdentifier<E> typeIdentifier;
private final String entityName;
private final PojoIndexingProcessor<E> processor;

Expand All @@ -34,9 +36,11 @@ public final class PojoDocumentContributor<E> implements DocumentContributor {
private final Object identifier;
private final Supplier<E> entitySupplier;

public PojoDocumentContributor(String entityName, PojoIndexingProcessor<E> processor,
public PojoDocumentContributor(PojoRawTypeIdentifier<E> typeIdentifier, String entityName,
PojoIndexingProcessor<E> processor,
PojoWorkSessionContext sessionContext, PojoIndexingProcessorRootContext processorContext,
Object identifier, Supplier<E> entitySupplier) {
this.typeIdentifier = typeIdentifier;
this.entityName = entityName;
this.processor = processor;
this.sessionContext = sessionContext;
Expand All @@ -51,9 +55,10 @@ public void contribute(DocumentElement state) {
processor.process( state, entitySupplier.get(), processorContext );
}
catch (RuntimeException e) {
EntityReferenceFactory entityReferenceFactory = sessionContext.mappingContext().entityReferenceFactory();
EntityReference entityReference = EntityReferenceFactory.safeCreateEntityReference(
entityReferenceFactory, entityName, identifier, e::addSuppressed );
PojoEntityReferenceFactoryDelegate entityReferenceFactoryDelegate =
sessionContext.mappingContext().entityReferenceFactoryDelegate();
EntityReference entityReference = entityReferenceFactoryDelegate.create( typeIdentifier, entityName,
identifier );
throw log.errorBuildingDocument( entityReference, e.getMessage(), e );
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@

import org.hibernate.search.engine.backend.mapping.spi.BackendMappingContext;
import org.hibernate.search.mapper.pojo.bridge.runtime.spi.BridgeMappingContext;
import org.hibernate.search.mapper.pojo.common.spi.PojoEntityReferenceFactoryDelegate;

/**
* Mapping-scoped information and operations for use in POJO work execution.
*/
public interface PojoWorkMappingContext extends BackendMappingContext, BridgeMappingContext {

/**
* @return A {@link PojoEntityReferenceFactoryDelegate}.
*/
PojoEntityReferenceFactoryDelegate entityReferenceFactoryDelegate();

}

0 comments on commit 789bffb

Please sign in to comment.