Skip to content

Commit

Permalink
HSEARCH-2613 Use enum IndexScope
Browse files Browse the repository at this point in the history
which allows to clarify the implementation of different workflows.

In particular, it clarifies that the parameter `maxResultsPerEntity`
is not only used in customized index scopes (CRITERIA, HQL), but
also used in the full index scope (FULL_ENTITY).
  • Loading branch information
mincong-h authored and Sanne committed Oct 25, 2017
1 parent 48c3fae commit 7380a58
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -223,28 +223,28 @@ public void open(Serializable checkpointId) throws Exception {
session = PersistenceUtil.openSession( emf, tenantId );
sessionFactory = emf.unwrap( SessionFactory.class );

PartitionContextData partitionData = null;
// HQL approach
// In this approach, the checkpoint mechanism is disabled, because we
// don't know if the selection is ordered by ID ascendingly in the query.
if ( customQueryHql != null && !customQueryHql.isEmpty() ) {
// TODO should I worry about the Lucene AddWork? If this is a
// restart, will it create duplicate index for the same entity,
// since there's no purge?
scroll = buildScrollUsingHQL( ss, customQueryHql );
partitionData = new PartitionContextData( partitionId, entityName );
}
// Criteria approach
else {
scroll = buildScrollUsingCriteria( ss, bound, checkpointId, jobData );
if ( checkpointId == null ) {
PartitionContextData partitionData;
switch ( PersistenceUtil.getIndexScope( customQueryHql, jobData.getCustomQueryCriteria() ) ) {
case HQL:
scroll = buildScrollUsingHQL( ss, customQueryHql );
partitionData = new PartitionContextData( partitionId, entityName );
}
else {
partitionData = (PartitionContextData) stepContext.getPersistentUserData();
}
break;

case CRITERIA:
case FULL_ENTITY:
scroll = buildScrollUsingCriteria( ss, bound, checkpointId, jobData );
if ( checkpointId == null ) {
partitionData = new PartitionContextData( partitionId, entityName );
}
else {
partitionData = (PartitionContextData) stepContext.getPersistentUserData();
}
break;

default:
// This should never happen.
throw new IllegalStateException( "Unknown value from enum: " + IndexScope.class );
}

stepContext.setTransientUserData( partitionData );
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Hibernate Search, full-text search for your domain model
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.search.jsr352.massindexing.impl.steps.lucene;

import org.hibernate.Criteria;

/**
* The index scope of a given entity type.
*
* @author Mincong Huang
*/
public enum IndexScope {
/**
* Index entities restricted by the HQL / JPQL given by user.
*/
HQL,
/**
* Index entities restricted the {@link Criteria} given by user.
*/
CRITERIA,
/**
* Index all the entities found.
*/
FULL_ENTITY
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ public class PartitionMapper implements javax.batch.api.partition.PartitionMappe

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

private enum Type {
HQL, CRITERIA, FULL_ENTITY
}

@Inject
private JobContext jobContext;

Expand Down Expand Up @@ -136,7 +132,7 @@ public PartitionPlan mapPartitions() throws Exception {
List<PartitionBound> partitionBounds = new ArrayList<>();
Class<?> entityType;

switch ( typeOfSelection( customQueryHql, jobData.getCustomQueryCriteria() ) ) {
switch ( PersistenceUtil.getIndexScope( customQueryHql, jobData.getCustomQueryCriteria() ) ) {
case HQL:
entityType = entityTypes.get( 0 );
partitionBounds.add( new PartitionBound( entityType, null, null ) );
Expand Down Expand Up @@ -206,18 +202,6 @@ public PartitionPlan mapPartitions() throws Exception {
}
}

private Type typeOfSelection(String hql, Set<Criterion> criterions) {
if ( hql != null && !hql.isEmpty() ) {
return Type.HQL;
}
else if ( criterions != null && criterions.size() > 0 ) {
return Type.CRITERIA;
}
else {
return Type.FULL_ENTITY;
}
}

private List<PartitionBound> buildPartitionUnitsFrom(ScrollableResults scroll, Class<?> clazz) {
List<PartitionBound> partitionUnits = new ArrayList<>();
int rowsPerPartition = SerializationUtil.parseIntegerParameter( ROWS_PER_PARTITION, serializedRowsPerPartition );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
*/
package org.hibernate.search.jsr352.massindexing.impl.util;

import java.util.Set;
import javax.persistence.EntityManagerFactory;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.StatelessSession;
import org.hibernate.criterion.Criterion;
import org.hibernate.search.jsr352.massindexing.impl.steps.lucene.IndexScope;
import org.hibernate.search.util.StringHelper;

/**
Expand Down Expand Up @@ -71,4 +74,21 @@ public static StatelessSession openStatelessSession(EntityManagerFactory entityM
return statelessSession;
}

/**
* Determines the index scope using the input parameters.
*
* @see IndexScope
*/
public static IndexScope getIndexScope(String hql, Set<Criterion> criterionSet) {
if ( StringHelper.isNotEmpty( hql ) ) {
return IndexScope.HQL;
}
else if ( criterionSet != null && criterionSet.size() > 0 ) {
return IndexScope.CRITERIA;
}
else {
return IndexScope.FULL_ENTITY;
}
}

}

0 comments on commit 7380a58

Please sign in to comment.