OptimizerStrategy optimizerStrategy = indexManager.getOptimizerStrategy();
Assert.assertTrue( "Unexpected optimizer strategy", optimizerStrategy instanceof IncrementalOptimizerStrategy );
// let's start the actual test
IncrementalOptimizerStrategy strategy = (IncrementalOptimizerStrategy) optimizerStrategy;
assertEquals( "Initially no optimisation should have been performed", 0, strategy.getOptimizationsPerformed() );
Session session = openSession();
//check that optimization is triggered periodically as configured
long optimizationsPerformed = 0l;
for ( int i = 0; i < 20; i++ ) {
Clock c = new Clock( i, "hwd" + i );
Transaction transaction = session.beginTransaction();
session.persist( c );
transaction.commit();
session.clear();
optimizationsPerformed = strategy.getOptimizationsPerformed();
assertEquals(
"Optimization should be triggered every three inserts",
( i + 1 ) / 3,
optimizationsPerformed
);
}
session.close();
session = openSession();
FullTextSession fullTextSession = Search.getFullTextSession( session );
FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery( new MatchAllDocsQuery(), Clock.class );
int resultSize = fullTextQuery.getResultSize();
assertEquals( "Wrong number of indexed entities", 20, resultSize );
//an explicit invocation of #optimize() should trigger it as well
assertEquals(
"Optimization should not have changed",
optimizationsPerformed,
strategy.getOptimizationsPerformed()
);
fullTextSession.getSearchFactory().optimize( Clock.class );
assertEquals(
"Optimize should have been incremented",
optimizationsPerformed + 1,
strategy.getOptimizationsPerformed()
);
//the massIndexer should optimize only before and after (not during the process)
fullTextSession.createIndexer( Clock.class )
.optimizeAfterPurge( true )
.optimizeOnFinish( true )
.startAndWait();
assertEquals(
"The mass indexer should trigger optimize as well ",
optimizationsPerformed + 3,
strategy.getOptimizationsPerformed()
);
session.close();
}