Skip to content

Commit

Permalink
HSEARCH-4153 Add a reference to the root indexing plan in type indexi…
Browse files Browse the repository at this point in the history
…ng plans

This is not very useful now, but it will be when we add loading plans.

Signed-off-by: Yoann Rodière <yoann@hibernate.org>
  • Loading branch information
yrodiere authored and fax4ever committed Feb 10, 2021
1 parent a0aa933 commit dc381d9
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 44 deletions.
Expand Up @@ -11,6 +11,7 @@
import org.hibernate.search.mapper.pojo.model.spi.PojoRawTypeIdentifier;
import org.hibernate.search.mapper.pojo.scope.impl.PojoScopeContainedTypeContext;
import org.hibernate.search.mapper.pojo.work.impl.PojoContainedTypeIndexingPlan;
import org.hibernate.search.mapper.pojo.work.impl.PojoIndexingPlanImpl;
import org.hibernate.search.mapper.pojo.work.impl.PojoWorkContainedTypeContext;
import org.hibernate.search.mapper.pojo.work.spi.PojoWorkSessionContext;

Expand All @@ -26,10 +27,9 @@ public PojoContainedTypeManager(String entityName, PojoRawTypeIdentifier<E> type
}

@Override
public PojoContainedTypeIndexingPlan<E> createIndexingPlan(PojoWorkSessionContext<?> sessionContext) {
return new PojoContainedTypeIndexingPlan<>(
this, sessionContext
);
public PojoContainedTypeIndexingPlan<E> createIndexingPlan(PojoWorkSessionContext<?> sessionContext,
PojoIndexingPlanImpl<?> root) {
return new PojoContainedTypeIndexingPlan<>( this, sessionContext, root );
}

}
Expand Up @@ -27,6 +27,7 @@
import org.hibernate.search.mapper.pojo.scope.impl.PojoScopeIndexedTypeContext;
import org.hibernate.search.mapper.pojo.work.impl.PojoDocumentContributor;
import org.hibernate.search.mapper.pojo.work.impl.PojoIndexedTypeIndexingPlan;
import org.hibernate.search.mapper.pojo.work.impl.PojoIndexingPlanImpl;
import org.hibernate.search.mapper.pojo.work.impl.PojoTypeIndexer;
import org.hibernate.search.mapper.pojo.work.impl.PojoWorkIndexedTypeContext;
import org.hibernate.search.mapper.pojo.work.impl.PojoWorkRouter;
Expand Down Expand Up @@ -133,9 +134,10 @@ public IndexWorkspace createWorkspace(DetachedBackendSessionContext sessionConte

@Override
public <R> PojoIndexedTypeIndexingPlan<I, E, R> createIndexingPlan(PojoWorkSessionContext<R> sessionContext,
PojoIndexingPlanImpl<?> root,
DocumentCommitStrategy commitStrategy, DocumentRefreshStrategy refreshStrategy) {
return new PojoIndexedTypeIndexingPlan<>(
this, sessionContext,
this, sessionContext, root,
indexManager.createIndexingPlan(
sessionContext, sessionContext.entityReferenceFactory(),
commitStrategy, refreshStrategy
Expand Down
Expand Up @@ -13,7 +13,6 @@

import org.hibernate.search.mapper.pojo.automaticindexing.impl.PojoImplicitReindexingResolverRootContext;
import org.hibernate.search.mapper.pojo.automaticindexing.spi.PojoImplicitReindexingResolverSessionContext;
import org.hibernate.search.mapper.pojo.automaticindexing.impl.PojoReindexingCollector;
import org.hibernate.search.mapper.pojo.work.spi.PojoWorkSessionContext;

/**
Expand All @@ -24,12 +23,14 @@
abstract class AbstractPojoTypeIndexingPlan<I, E, S extends AbstractPojoTypeIndexingPlan<I, E, S>.AbstractEntityState> {

final PojoWorkSessionContext<?> sessionContext;
final PojoIndexingPlanImpl<?> root;

// Use a LinkedHashMap for deterministic iteration
final Map<I, S> statesPerId = new LinkedHashMap<>();

AbstractPojoTypeIndexingPlan(PojoWorkSessionContext<?> sessionContext) {
AbstractPojoTypeIndexingPlan(PojoWorkSessionContext<?> sessionContext, PojoIndexingPlanImpl<?> root) {
this.sessionContext = sessionContext;
this.root = root;
}

void add(Object providedId, String providedRoutingKey, Object entity) {
Expand All @@ -45,7 +46,7 @@ void addOrUpdate(Object providedId, String providedRoutingKey, Object entity) {
}

void addOrUpdate(Object providedId, String providedRoutingKey, Object entity, BitSet dirtyPaths) {
Supplier<E> entitySupplier = typeContext().toEntitySupplier( sessionContext, entity );
Supplier<E> entitySupplier = entity == null ? null : typeContext().toEntitySupplier( sessionContext, entity );
I identifier = toIdentifier( providedId, entitySupplier );
getState( identifier ).addOrUpdate( entitySupplier, providedRoutingKey, dirtyPaths );
}
Expand All @@ -56,9 +57,9 @@ void delete(Object providedId, String providedRoutingKey, Object entity) {
getState( identifier ).delete( entitySupplier, providedRoutingKey );
}

void resolveDirty(PojoReindexingCollector containingEntityCollector) {
void resolveDirty() {
for ( S state : statesPerId.values() ) {
state.resolveDirty( containingEntityCollector );
state.resolveDirty();
}
}

Expand Down Expand Up @@ -150,10 +151,10 @@ void delete(Supplier<E> entitySupplier, String providedRoutingKey) {
dirtyPaths = null;
}

void resolveDirty(PojoReindexingCollector containingEntityCollector) {
void resolveDirty() {
if ( shouldResolveToReindex ) {
shouldResolveToReindex = false; // Avoid infinite looping
typeContext().resolveEntitiesToReindex( containingEntityCollector, sessionContext, identifier,
typeContext().resolveEntitiesToReindex( root, sessionContext, identifier,
entitySupplier, this );
}
}
Expand Down
Expand Up @@ -19,8 +19,8 @@ public class PojoContainedTypeIndexingPlan<E>
private final PojoWorkContainedTypeContext<E> typeContext;

public PojoContainedTypeIndexingPlan(PojoWorkContainedTypeContext<E> typeContext,
PojoWorkSessionContext<?> sessionContext) {
super( sessionContext );
PojoWorkSessionContext<?> sessionContext, PojoIndexingPlanImpl<?> root) {
super( sessionContext, root );
this.typeContext = typeContext;
}

Expand Down
Expand Up @@ -14,7 +14,6 @@
import org.hibernate.search.engine.backend.work.execution.spi.DocumentReferenceProvider;
import org.hibernate.search.engine.backend.work.execution.spi.IndexIndexingPlan;
import org.hibernate.search.engine.backend.work.execution.spi.IndexIndexingPlanExecutionReport;
import org.hibernate.search.mapper.pojo.automaticindexing.impl.PojoReindexingCollector;
import org.hibernate.search.mapper.pojo.route.impl.DocumentRouteImpl;
import org.hibernate.search.mapper.pojo.work.spi.PojoWorkSessionContext;

Expand All @@ -30,9 +29,9 @@ public class PojoIndexedTypeIndexingPlan<I, E, R>
private final IndexIndexingPlan<R> delegate;

public PojoIndexedTypeIndexingPlan(PojoWorkIndexedTypeContext<I, E> typeContext,
PojoWorkSessionContext<?> sessionContext,
PojoWorkSessionContext<?> sessionContext, PojoIndexingPlanImpl<?> root,
IndexIndexingPlan<R> delegate) {
super( sessionContext );
super( sessionContext, root );
this.typeContext = typeContext;
this.delegate = delegate;
}
Expand All @@ -44,11 +43,11 @@ void updateBecauseOfContained(Object entity) {
}

@Override
void resolveDirty(PojoReindexingCollector containingEntityCollector) {
void resolveDirty() {
// We need to iterate on a "frozen snapshot" of the states because of HSEARCH-3857
List<IndexedEntityState> frozenIndexingPlansPerId = new ArrayList<>( statesPerId.values() );
for ( IndexedEntityState plan : frozenIndexingPlansPerId ) {
plan.resolveDirty( containingEntityCollector );
plan.resolveDirty();
}
}

Expand Down
Expand Up @@ -18,6 +18,7 @@
import org.hibernate.search.engine.backend.work.execution.DocumentCommitStrategy;
import org.hibernate.search.engine.backend.work.execution.DocumentRefreshStrategy;
import org.hibernate.search.engine.backend.work.execution.spi.IndexIndexingPlanExecutionReport;
import org.hibernate.search.mapper.pojo.automaticindexing.impl.PojoReindexingCollector;
import org.hibernate.search.mapper.pojo.logging.impl.Log;
import org.hibernate.search.mapper.pojo.model.spi.PojoRawTypeIdentifier;
import org.hibernate.search.mapper.pojo.model.spi.PojoRuntimeIntrospector;
Expand All @@ -26,7 +27,7 @@
import org.hibernate.search.util.common.AssertionFailure;
import org.hibernate.search.util.common.logging.impl.LoggerFactory;

public class PojoIndexingPlanImpl<R> implements PojoIndexingPlan<R> {
public class PojoIndexingPlanImpl<R> implements PojoIndexingPlan<R>, PojoReindexingCollector {

private static final Log log = LoggerFactory.make( Log.class, MethodHandles.lookup() );

Expand Down Expand Up @@ -90,12 +91,12 @@ public void process() {
isProcessing = true;
try {
for ( PojoContainedTypeIndexingPlan<?> delegate : containedTypeDelegates.values() ) {
delegate.resolveDirty( this::updateBecauseOfContained );
delegate.resolveDirty();
}
// We need to iterate on a "frozen snapshot" of the indexedTypeDelegates values because of HSEARCH-3857
List<PojoIndexedTypeIndexingPlan<?, ?, ?>> frozenIndexedTypeDelegates = new ArrayList<>( indexedTypeDelegates.values() );
for ( PojoIndexedTypeIndexingPlan<?, ?, ?> delegate : frozenIndexedTypeDelegates ) {
delegate.resolveDirty( this::updateBecauseOfContained );
delegate.resolveDirty();
}
for ( PojoIndexedTypeIndexingPlan<?, ?, ?> delegate : indexedTypeDelegates.values() ) {
delegate.process();
Expand Down Expand Up @@ -140,6 +141,24 @@ public void discardNotProcessed() {
}
}

@Override
public void markForReindexing(Object containingEntity) {
// Note this method won't work when using provided identifiers
// Fortunately, all platforms relying on provided identifiers (Infinispan)
// also disable reindexing of other entities on updates,
// so they won't ever call this method.

PojoRawTypeIdentifier<?> typeIdentifier = getIntrospector().detectEntityType( containingEntity );
if ( typeIdentifier == null ) {
throw new AssertionFailure(
"Attempt to reindex entity " + containingEntity + " because a contained entity was modified,"
+ " but this entity type is not indexed directly."
);
}
PojoIndexedTypeIndexingPlan<?, ?, ?> delegate = getOrCreateIndexedDelegateForContainedUpdate( typeIdentifier );
delegate.updateBecauseOfContained( containingEntity );
}

private PojoRuntimeIntrospector getIntrospector() {
return introspector;
}
Expand All @@ -160,7 +179,7 @@ private PojoRuntimeIntrospector getIntrospector() {
indexedTypeContextProvider.getByExactType( typeIdentifier );
if ( indexedTypeContextOptional.isPresent() ) {
PojoIndexedTypeIndexingPlan<?, ?, R> delegate = indexedTypeContextOptional.get()
.createIndexingPlan( sessionContext, commitStrategy, refreshStrategy );
.createIndexingPlan( sessionContext, this, commitStrategy, refreshStrategy );
indexedTypeDelegates.put( typeIdentifier, delegate );
return delegate;
}
Expand All @@ -169,7 +188,7 @@ private PojoRuntimeIntrospector getIntrospector() {
containedTypeContextProvider.getByExactType( typeIdentifier );
if ( containedTypeContextOptional.isPresent() ) {
PojoContainedTypeIndexingPlan<?> delegate = containedTypeContextOptional.get()
.createIndexingPlan( sessionContext );
.createIndexingPlan( sessionContext, this );
containedTypeDelegates.put( typeIdentifier, delegate );
return delegate;
}
Expand All @@ -187,7 +206,7 @@ private PojoRuntimeIntrospector getIntrospector() {
indexedTypeContextProvider.getByExactType( typeIdentifier );
if ( indexedTypeManagerOptional.isPresent() ) {
delegate = indexedTypeManagerOptional.get()
.createIndexingPlan( sessionContext, commitStrategy, refreshStrategy );
.createIndexingPlan( sessionContext, this, commitStrategy, refreshStrategy );
indexedTypeDelegates.put( typeIdentifier, delegate );
return delegate;
}
Expand All @@ -197,22 +216,4 @@ private PojoRuntimeIntrospector getIntrospector() {
+ " but this entity type is not indexed directly."
);
}

private void updateBecauseOfContained(Object containingEntity) {
// Note this method won't work when using provided identifiers
// Fortunately, all platforms relying on provided identifiers (Infinispan)
// also disable reindexing of other entities on updates,
// so they won't ever call this method.

PojoRawTypeIdentifier<?> typeIdentifier = getIntrospector().detectEntityType( containingEntity );
if ( typeIdentifier == null ) {
throw new AssertionFailure(
"Attempt to reindex entity " + containingEntity + " because a contained entity was modified,"
+ " but this entity type is not indexed directly."
);
}
PojoIndexedTypeIndexingPlan<?, ?, ?> delegate = getOrCreateIndexedDelegateForContainedUpdate( typeIdentifier );
delegate.updateBecauseOfContained( containingEntity );
}

}
Expand Up @@ -13,6 +13,7 @@
*/
public interface PojoWorkContainedTypeContext<E> extends PojoWorkTypeContext<E> {

PojoContainedTypeIndexingPlan<E> createIndexingPlan(PojoWorkSessionContext<?> sessionContext);
PojoContainedTypeIndexingPlan<E> createIndexingPlan(PojoWorkSessionContext<?> sessionContext,
PojoIndexingPlanImpl<?> root);

}
Expand Up @@ -34,6 +34,7 @@ PojoDocumentContributor<E> toDocumentContributor(PojoWorkSessionContext<?> sessi
PojoPathFilter dirtySelfFilter();

<R> PojoIndexedTypeIndexingPlan<I, E, R> createIndexingPlan(PojoWorkSessionContext<R> sessionContext,
PojoIndexingPlanImpl<?> root,
DocumentCommitStrategy commitStrategy, DocumentRefreshStrategy refreshStrategy);

PojoTypeIndexer<I, E> createIndexer(PojoWorkSessionContext<?> sessionContext);
Expand Down

0 comments on commit dc381d9

Please sign in to comment.