Skip to content

Commit

Permalink
hibernate#41 update javadoc and enhance code
Browse files Browse the repository at this point in the history
  • Loading branch information
mincong-h committed Jun 13, 2016
1 parent 1fb8ae6 commit fa1e12c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 23 deletions.
Expand Up @@ -34,7 +34,11 @@ public void add(Serializable[] clazzIDs, Class<?> clazz) {
}

public Serializable[] poll(Class<?> clazz) {
return idQueues.get(clazz).poll();
// TODO: this method is really slow
Serializable[] IDs = idQueues.get(clazz).poll();
String len = (IDs == null) ? "null" : String.valueOf(IDs.length);
System.out.printf("Polling %s IDs for %s.%n", len, clazz.getName());
return IDs;
}

public int sizeOf(Class<?> clazz) {
Expand Down
Expand Up @@ -43,44 +43,47 @@ public class LucenePartitionMapper implements PartitionMapper {
@Override
public PartitionPlan mapPartitions() throws Exception {

String[] rootEntities = parse(rootEntitiesStr);
Class<?>[] rootEntities = parse(rootEntitiesStr);
Queue<String> classQueue = new LinkedList<>();

int partSum = 0;
for (String rootEntity: rootEntities) {
int queueSize = indexingContext.sizeOf(Class.forName(rootEntity));
// TODO: handle queueSize is 0
int partCount = queueSize / partitionCapacity;
int totalPartitions = 0;
for (Class<?> rootEntity: rootEntities) {

int queueSize = indexingContext.sizeOf(rootEntity);
int classPartitions = queueSize / partitionCapacity; // TODO: handle queueSize is 0
if (queueSize % partitionCapacity != 0) {
partCount++;
classPartitions++;
}
partSum += partCount;

// enqueue entity type into classQueue, as much as the number of
// the partitions
for (int i = 0; i < partCount; i++) {
classQueue.add(rootEntity);
// the class partitions
for (int i = 0; i < classPartitions; i++) {
classQueue.add(rootEntity.getName());
}
System.out.printf("%d partitions added to root entity \"%s\".%n",
classPartitions, rootEntity);

totalPartitions += classPartitions;
}
final int partCountFinal = partSum;
final int TOTAL_PARTITIONS = totalPartitions;

return new PartitionPlanImpl() {

@Override
public int getPartitions() {
System.out.printf("#mapPartitions(): %d partitions.%n", partCountFinal);
return partCountFinal;
System.out.printf("#mapPartitions(): %d partitions.%n", TOTAL_PARTITIONS);
return TOTAL_PARTITIONS;
}

@Override
public int getThreads() {
System.out.printf("#getThreads(): %d threads.%n", Math.min(partCountFinal, threads));
return Math.min(partCountFinal, threads);
System.out.printf("#getThreads(): %d threads.%n", Math.min(TOTAL_PARTITIONS, threads));
return Math.min(TOTAL_PARTITIONS, threads);
}

@Override
public Properties[] getPartitionProperties() {

Properties[] props = new Properties[getPartitions()];
Properties[] props = new Properties[TOTAL_PARTITIONS];
for (int i = 0; i < props.length; i++) {
String entityType = classQueue.poll();
props[i] = new Properties();
Expand All @@ -98,14 +101,21 @@ public Properties[] getPartitionProperties() {
* and surrounded by "[]", e.g. "[com.xx.foo, com.xx.bar]".
* @return a set of entity-types
* @throws NullPointerException thrown if the entity-token is not found.
* @throws ClassNotFoundException thrown if the target string name is not
* a valid class name.
*/
private String[] parse(String raw) throws NullPointerException {
private Class<?>[] parse(String raw) throws NullPointerException,
ClassNotFoundException {
if (raw == null) {
throw new NullPointerException("Not any target entity to index");
}
String[] rootEntities = raw
.substring(1, raw.length() - 1)
String[] names = raw
.substring(1, raw.length() - 1) // removes '[' and ']'
.split(", ");
return rootEntities;
Class<?>[] classes = new Class<?>[names.length];
for (int i = 0; i < names.length; i++) {
classes[i] = Class.forName(names[i]);
}
return classes;
}
}

0 comments on commit fa1e12c

Please sign in to comment.