forked from hibernate/hibernate-search
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
us-address/src/main/java/io/github/mincongh/batch/EntityPartitionMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package io.github.mincongh.batch; | ||
|
||
import java.util.HashSet; | ||
import java.util.Properties; | ||
import java.util.Set; | ||
|
||
import javax.batch.api.BatchProperty; | ||
import javax.batch.api.partition.PartitionMapper; | ||
import javax.batch.api.partition.PartitionPlan; | ||
import javax.batch.api.partition.PartitionPlanImpl; | ||
import javax.inject.Inject; | ||
import javax.inject.Named; | ||
|
||
@Named | ||
public class EntityPartitionMapper implements PartitionMapper { | ||
|
||
@Inject @BatchProperty private String rootEntitiesStr; | ||
|
||
@Override | ||
public PartitionPlan mapPartitions() throws Exception { | ||
|
||
String[] rootEntities = parse(rootEntitiesStr); | ||
|
||
return new PartitionPlanImpl() { | ||
|
||
@Override | ||
public int getPartitions() { | ||
System.out.printf("#mapPartitions(): %d partitions.%n", rootEntities.length); | ||
return rootEntities.length; | ||
} | ||
|
||
@Override | ||
public int getThreads() { | ||
System.out.printf("#getThreads(): %d threads.%n", getPartitions()); | ||
return getPartitions(); | ||
} | ||
|
||
@Override | ||
public Properties[] getPartitionProperties() { | ||
Properties[] props = new Properties[getPartitions()]; | ||
for (int i = 0; i < props.length; i++) { | ||
props[i] = new Properties(); | ||
props[i].setProperty("entityType", rootEntities[i]); | ||
} | ||
return props; | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* Parse a set of entities in string into a set of entity-types. | ||
* | ||
* @param raw a set of entities concatenated in string, with "[]" on both | ||
* ends, separated by ",". | ||
* @return a set of entity-types | ||
* @throws NullPointerException thrown if the entity-token is not found. | ||
*/ | ||
private String[] parse(String raw) throws NullPointerException { | ||
|
||
if (raw == null) { | ||
throw new NullPointerException("Not any target entity to index"); | ||
} | ||
return raw.substring(1, raw.length() - 1).split(", "); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters