log.debug("deleting indexed documents of entity: " + entityClass.getName());
userTx = (UserTransaction)org.jboss.seam.Component.getInstance("org.jboss.seam.transaction.transaction");
userTx.begin();
EntityManager em = (EntityManager) Component.getInstance("entityManager");
FullTextSession ftSession = (FullTextSession)em.getDelegate();
// Delete all documents with "_hibernate_class" term of the selected entity
DirectoryProvider dirProvider = ftSession.getSearchFactory().getDirectoryProviders(entityClass)[0];
IndexReader reader = IndexReader.open(dirProvider.getDirectory());
// TODO: This is using an internal term of HSearch
reader.deleteDocuments(new Term("_hibernate_class", entityClass.getName()));
reader.close();
// Optimize index
progress.setStatus("Optimizing index");
log.debug("optimizing index (merging segments)");
ftSession.getSearchFactory().optimize(entityClass);
userTx.commit();
progress.setStatus("Building index");
log.debug("indexing documents in batches of: " + batchSize);
// Now re-index with HSearch
em = (EntityManager) Component.getInstance("entityManager");
ftSession = (FullTextSession)em.getDelegate();
// TODO: Let's run this in auto-commit mode, assuming we have READ COMMITTED isolation anyway and non-repeatable reads
//userTx.setTransactionTimeout(3600);
//userTx.begin();
// Use HQL instead of Criteria to eager fetch lazy properties
String query = "select o from " + entityClass.getName() + " o fetch all properties";
if (WikiNode.class.isAssignableFrom(entityClass)) {
// If it's a WikiNode, fetch the associated User instances, avoiding N+1 selects
query = "select o from " + entityClass.getName() + " o inner join fetch o.createdBy left join fetch o.lastModifiedBy fetch all properties";
}
ScrollableResults cursor = ftSession.createQuery(query).scroll();
cursor.last();
int count = cursor.getRowNumber() + 1;
log.debug("total documents in database: " + count);
if (count > 0) {
cursor.first(); // Reset to first result row
int i = 0;
while (true) {
i++;
Object o = cursor.get(0);
log.debug("indexing instance " + i + ": " + o);
ftSession.index(o);
if (i % batchSize == 0) {
log.debug("ending batch, beginning new batch");
ftSession.clear(); // Clear persistence context for each batch
}
progress.setPercentComplete( new Double(100d/count*i).intValue() );
log.debug(progress);
if (cursor.isLast())