Skip to content

Commit

Permalink
HSEARCH-3920 Remove the now useless class StubMappingIndexManager
Browse files Browse the repository at this point in the history
  • Loading branch information
yrodiere committed May 19, 2020
1 parent becb520 commit 5df4906
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 236 deletions.
Expand Up @@ -35,7 +35,6 @@
import org.hibernate.search.util.impl.integrationtest.common.TestConfigurationProvider;
import org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappedIndex;
import org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMapping;
import org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappingIndexManager;
import org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappingInitiator;
import org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappingKey;
import org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappingSchemaManagementStrategy;
Expand Down Expand Up @@ -245,32 +244,6 @@ public SearchIntegration setup() {

}

public static class StubTypeMappingConfigurationContext {

private String typeName;
private String backendName;

public StubTypeMappingConfigurationContext() {
}

public StubTypeMappingConfigurationContext mappedType(String typeName) {
this.typeName = typeName;
return this;
}

public StubTypeMappingConfigurationContext backend(String backendName) {
this.backendName = backendName;
return this;
}

}

public interface IndexSetupListener {

void onSetup(StubMappingIndexManager indexMapping);

}

public interface PartialSetup {

SearchIntegration doSecondPhase();
Expand Down
Expand Up @@ -20,7 +20,7 @@
import org.hibernate.search.util.common.logging.impl.Log;
import org.hibernate.search.util.common.logging.impl.LoggerFactory;
import org.hibernate.search.util.impl.integrationtest.mapper.stub.SimpleMappedIndex;
import org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappingIndexManager;
import org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappedIndex;
import org.hibernate.search.util.impl.test.FutureAssert;

import org.junit.Rule;
Expand Down Expand Up @@ -89,19 +89,19 @@ public void failure() {

protected abstract void ensureOperationsFail(TckBackendAccessor accessor, String indexName);

protected void beforeInitData(StubMappingIndexManager index) {
protected void beforeInitData(StubMappedIndex index) {
// Nothing to do by default.
}

protected void afterInitData(StubMappingIndexManager index) {
protected void afterInitData(StubMappedIndex index) {
// Nothing to do by default.
}

protected abstract void assertPreconditions(StubMappingIndexManager index);
protected abstract void assertPreconditions(StubMappedIndex index);

protected abstract CompletableFuture<?> executeAsync(IndexWorkspace workspace);

protected abstract void assertSuccess(StubMappingIndexManager index);
protected abstract void assertSuccess(StubMappedIndex index);

private void setup() {
setupHelper.start().withIndex( index ).setup();
Expand Down
Expand Up @@ -10,7 +10,7 @@

import org.hibernate.search.engine.backend.work.execution.spi.IndexWorkspace;
import org.hibernate.search.integrationtest.backend.tck.testsupport.util.TckBackendAccessor;
import org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappingIndexManager;
import org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappedIndex;

public class IndexWorkspaceFlushIT extends AbstractIndexWorkspaceSimpleOperationIT {

Expand All @@ -25,12 +25,12 @@ protected CompletableFuture<?> executeAsync(IndexWorkspace workspace) {
}

@Override
protected void assertPreconditions(StubMappingIndexManager index) {
protected void assertPreconditions(StubMappedIndex index) {
// No measurable effect: don't assert anything.
}

@Override
protected void assertSuccess(StubMappingIndexManager index) {
protected void assertSuccess(StubMappedIndex index) {
// No measurable effect: don't assert anything.
}
}
Expand Up @@ -10,7 +10,7 @@

import org.hibernate.search.engine.backend.work.execution.spi.IndexWorkspace;
import org.hibernate.search.integrationtest.backend.tck.testsupport.util.TckBackendAccessor;
import org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappingIndexManager;
import org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappedIndex;

public class IndexWorkspaceMergeSegmentsIT extends AbstractIndexWorkspaceSimpleOperationIT {

Expand All @@ -25,12 +25,12 @@ protected CompletableFuture<?> executeAsync(IndexWorkspace workspace) {
}

@Override
protected void assertPreconditions(StubMappingIndexManager index) {
protected void assertPreconditions(StubMappedIndex index) {
// No measurable effect: don't assert anything.
}

@Override
protected void assertSuccess(StubMappingIndexManager index) {
protected void assertSuccess(StubMappedIndex index) {
// No measurable effect: don't assert anything.
}
}
Expand Up @@ -13,7 +13,7 @@

import org.hibernate.search.engine.backend.work.execution.spi.IndexWorkspace;
import org.hibernate.search.integrationtest.backend.tck.testsupport.util.TckBackendAccessor;
import org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappingIndexManager;
import org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappedIndex;

public class IndexWorkspacePurgeIT extends AbstractIndexWorkspaceSimpleOperationIT {

Expand All @@ -28,22 +28,22 @@ protected CompletableFuture<?> executeAsync(IndexWorkspace workspace) {
}

@Override
protected void afterInitData(StubMappingIndexManager index) {
protected void afterInitData(StubMappedIndex index) {
// Make sure to flush the index, otherwise the test won't fail as expected with Lucene,
// probably because the index writer optimizes purges when changes are not committed yet.
index.createWorkspace().flush();
}

@Override
protected void assertPreconditions(StubMappingIndexManager index) {
protected void assertPreconditions(StubMappedIndex index) {
index.createWorkspace().refresh().join();
long count = index.createScope().query().where( f -> f.matchAll() )
.fetchTotalHitCount();
assertThat( count ).isGreaterThan( 0 );
}

@Override
protected void assertSuccess(StubMappingIndexManager index) {
protected void assertSuccess(StubMappedIndex index) {
index.createWorkspace().refresh().join();
long count = index.createScope().query().where( f -> f.matchAll() )
.fetchTotalHitCount();
Expand Down
Expand Up @@ -13,7 +13,7 @@
import org.hibernate.search.engine.backend.work.execution.spi.IndexWorkspace;
import org.hibernate.search.integrationtest.backend.tck.testsupport.util.TckBackendAccessor;
import org.hibernate.search.integrationtest.backend.tck.testsupport.util.rule.SearchSetupHelper;
import org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappingIndexManager;
import org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappedIndex;

public class IndexWorkspaceRefreshIT extends AbstractIndexWorkspaceSimpleOperationIT {

Expand All @@ -34,22 +34,22 @@ protected CompletableFuture<?> executeAsync(IndexWorkspace workspace) {
}

@Override
protected void beforeInitData(StubMappingIndexManager index) {
protected void beforeInitData(StubMappedIndex index) {
// Make sure index readers are initialized before writing,
// otherwise the preconditions won't be met.
index.createScope().query().where( f -> f.matchAll() ).fetchTotalHitCount();
}

@Override
protected void assertPreconditions(StubMappingIndexManager index) {
protected void assertPreconditions(StubMappedIndex index) {
long count = index.createScope().query().where( f -> f.matchAll() )
.fetchTotalHitCount();
// Indexes haven't been refreshed yet, so documents added so far are missing.
assertThat( count ).isEqualTo( 0 );
}

@Override
protected void assertSuccess(StubMappingIndexManager index) {
protected void assertSuccess(StubMappedIndex index) {
long count = index.createScope().query().where( f -> f.matchAll() )
.fetchTotalHitCount();
// After a refresh, documents are visible
Expand Down
Expand Up @@ -6,16 +6,36 @@
*/
package org.hibernate.search.util.impl.integrationtest.mapper.stub;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.function.IntFunction;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import org.hibernate.search.engine.backend.common.DocumentReference;
import org.hibernate.search.engine.backend.document.model.dsl.IndexSchemaElement;
import org.hibernate.search.engine.backend.index.IndexManager;
import org.hibernate.search.engine.backend.schema.management.spi.IndexSchemaManager;
import org.hibernate.search.engine.backend.session.spi.DetachedBackendSessionContext;
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.IndexIndexer;
import org.hibernate.search.engine.backend.work.execution.spi.IndexIndexingPlan;
import org.hibernate.search.engine.backend.work.execution.spi.IndexWorkspace;
import org.hibernate.search.engine.mapper.mapping.building.spi.IndexedEntityBindingContext;
import org.hibernate.search.engine.mapper.mapping.spi.MappedIndexManager;
import org.hibernate.search.engine.mapper.scope.spi.MappedIndexScopeBuilder;

// TODO This extends StubMappingIndexManager for backward compatibility; ideally we'll move everything to this class, eventually.
public abstract class StubMappedIndex extends StubMappingIndexManager {
/**
* A wrapper around {@link MappedIndexManager} providing some syntactic sugar,
* such as methods that do not force to provide a session context.
*/
public abstract class StubMappedIndex {

private static final int INIT_BATCH_SIZE = 500;

public static StubMappedIndex withoutFields() {
return ofAdvancedNonRetrievable( ignored -> { } );
Expand Down Expand Up @@ -44,7 +64,6 @@ public StubMappedIndex() {
this.typeName = null;
}

@Override
public final String name() {
return indexName;
}
Expand Down Expand Up @@ -76,13 +95,156 @@ public IndexManager toApi() {
return manager.toAPI();
}

public <T> T unwrapForTests(Class<T> clazz) {
return clazz.cast( delegate().toAPI() );
}

public IndexSchemaManager getSchemaManager() {
return delegate().getSchemaManager();
}

public CompletableFuture<?> initAsync(StubDocumentProvider ... documentProviders) {
return initAsync( Arrays.asList( documentProviders ) );
}

public CompletableFuture<?> initAsync(int documentCount,
IntFunction<StubDocumentProvider> documentProviderGenerator) {
return initAsync( documentCount, documentProviderGenerator, true );
}

public CompletableFuture<?> initAsync(int documentCount,
IntFunction<StubDocumentProvider> documentProviderGenerator, boolean refresh) {
return initAsync( new StubBackendSessionContext(), documentCount, documentProviderGenerator, refresh );
}

public CompletableFuture<?> initAsync(StubBackendSessionContext sessionContext, int documentCount,
IntFunction<StubDocumentProvider> documentProviderGenerator, boolean refresh) {
return initAsync(
sessionContext,
IntStream.range( 0, documentCount )
.mapToObj( documentProviderGenerator )
.collect( Collectors.toList() ),
refresh
);
}

public CompletableFuture<?> initAsync(List<StubDocumentProvider> documentProviders) {
return initAsync( documentProviders, true );
}

public CompletableFuture<?> initAsync(List<StubDocumentProvider> documentProviders, boolean refresh) {
return initAsync( new StubBackendSessionContext(), documentProviders, refresh );
}

public CompletableFuture<?> initAsync(StubBackendSessionContext sessionContext,
List<StubDocumentProvider> documentProviders, boolean refresh) {
if ( documentProviders.isEmpty() ) {
return CompletableFuture.completedFuture( null );
}
IndexIndexer indexer = createIndexer( sessionContext );
CompletableFuture<?> future = CompletableFuture.completedFuture( null );
for ( int i = 0; i < documentProviders.size(); i += INIT_BATCH_SIZE ) {
int batchStart = i;
int batchSize = Math.min( batchStart + INIT_BATCH_SIZE, documentProviders.size() ) - batchStart;
future = future.thenCompose( ignored -> {
CompletableFuture<?>[] batchFutures = new CompletableFuture[batchSize];
for ( int j = 0; j < batchSize; j++ ) {
StubDocumentProvider documentProvider = documentProviders.get( batchStart + j );
batchFutures[j] = indexer.add(
documentProvider.getReferenceProvider(), documentProvider.getContributor(),
DocumentCommitStrategy.NONE, DocumentRefreshStrategy.NONE
);
}
return CompletableFuture.allOf( batchFutures );
} );
}
if ( refresh ) {
IndexWorkspace workspace = createWorkspace( sessionContext );
return future.thenCompose( ignored -> workspace.refresh() );
}
else {
return future;
}
}

public IndexIndexingPlan<StubEntityReference> createIndexingPlan() {
return createIndexingPlan( new StubBackendSessionContext() );
}

public IndexIndexingPlan<StubEntityReference> createIndexingPlan(StubBackendSessionContext sessionContext) {
/*
* Use the same defaults as in the ORM mapper for the commit strategy,
* but force refreshes because it's more convenient for tests.
*/
return delegate().createIndexingPlan( sessionContext, StubEntityReference.FACTORY,
DocumentCommitStrategy.FORCE, DocumentRefreshStrategy.FORCE );
}

public IndexIndexingPlan<StubEntityReference> createIndexingPlan(StubBackendSessionContext sessionContext,
DocumentCommitStrategy commitStrategy, DocumentRefreshStrategy refreshStrategy) {
return delegate().createIndexingPlan( sessionContext, StubEntityReference.FACTORY,
commitStrategy, refreshStrategy );
}

public IndexIndexer createIndexer() {
return createIndexer( new StubBackendSessionContext() );
}

public IndexIndexer createIndexer(StubBackendSessionContext sessionContext) {
return delegate().createIndexer( sessionContext );
}

public IndexWorkspace createWorkspace() {
return createWorkspace( new StubBackendSessionContext() );
}

public IndexWorkspace createWorkspace(StubBackendSessionContext sessionContext) {
return createWorkspace( DetachedBackendSessionContext.of( sessionContext ) );
}

public IndexWorkspace createWorkspace(DetachedBackendSessionContext sessionContext) {
return delegate().createWorkspace( sessionContext );
}

/**
* @return A scope containing this index only.
*/
public StubMappingScope createScope() {
MappedIndexScopeBuilder<DocumentReference, DocumentReference> builder =
delegate().createScopeBuilder( new StubBackendMappingContext() );
return new StubMappingScope( builder.build() );
}

/**
* @return A scope containing this index and the given other indexes.
*/
public StubMappingScope createScope(StubMappedIndex... others) {
MappedIndexScopeBuilder<DocumentReference, DocumentReference> builder =
delegate().createScopeBuilder( new StubBackendMappingContext() );
for ( StubMappedIndex other : others ) {
other.delegate().addTo( builder );
}
return new StubMappingScope( builder.build() );
}

/**
* @return A scope containing this index and the given other indexes.
*/
public <R, E> GenericStubMappingScope<R, E> createGenericScope(StubMappedIndex... others) {
MappedIndexScopeBuilder<R, E> builder =
delegate().createScopeBuilder( new StubBackendMappingContext() );
for ( StubMappedIndex other : others ) {
other.delegate().addTo( builder );
}
return new GenericStubMappingScope<>( builder.build() );
}

protected abstract void bind(IndexedEntityBindingContext context);

protected void onIndexManagerCreated(MappedIndexManager manager) {
this.manager = manager;
}

@Override
protected MappedIndexManager delegate() {
return manager;
}
Expand Down

0 comments on commit 5df4906

Please sign in to comment.