Skip to content

Commit

Permalink
hibernate#48 separate interface and implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
mincong-h committed Jun 19, 2016
1 parent 38c4f54 commit 522f178
Show file tree
Hide file tree
Showing 2 changed files with 207 additions and 110 deletions.
136 changes: 26 additions & 110 deletions src/main/java/org/hibernate/search/jsr352/MassIndexer.java
@@ -1,115 +1,31 @@
package org.hibernate.search.jsr352;

import java.util.Properties;
import java.util.Set;

import javax.batch.operations.JobOperator;
import javax.batch.runtime.BatchRuntime;

// TODO: separate this class into interface and implementation
public class MassIndexer {

private boolean optimizeAfterPurge = false;
private boolean optimizeAtEnd = false;
private boolean purgeAtStart = false;

private int arrayCapacity = 1000;
private int fetchSize = 200000;
private int maxResults = 1000000;
private int partitionCapacity = 250;
private int partitions = 4;
private int threads = 2;

private final String JOB_NAME = "mass-index";

private Set<Class<?>> rootEntities;

MassIndexer() {

}

public long start() {
Properties jobParams = new Properties();
jobParams.setProperty("fetchSize", String.valueOf(fetchSize));
jobParams.setProperty("arrayCapacity", String.valueOf(arrayCapacity));
jobParams.setProperty("maxResults", String.valueOf(maxResults));
jobParams.setProperty("partitionCapacity", String.valueOf(partitionCapacity));
jobParams.setProperty("partitions", String.valueOf(partitions));
jobParams.setProperty("threads", String.valueOf(threads));
jobParams.setProperty("purgeAtStart", String.valueOf(purgeAtStart));
jobParams.setProperty("optimizeAfterPurge", String.valueOf(optimizeAfterPurge));
jobParams.setProperty("optimizeAtEnd", String.valueOf(optimizeAtEnd));
jobParams.setProperty("rootEntities", String.valueOf(rootEntities));
JobOperator jobOperator = BatchRuntime.getJobOperator();
Long executionId = jobOperator.start(JOB_NAME, jobParams);
return executionId;
}

public void stop(long executionId) {
JobOperator jobOperator = BatchRuntime.getJobOperator();
jobOperator.stop(executionId);
}

public MassIndexer arrayCapacity(int arrayCapacity) {
if (arrayCapacity < 1) {
throw new IllegalArgumentException("arrayCapacity must be at least 1");
}
this.arrayCapacity = arrayCapacity;
return this;
}

public MassIndexer fetchSize(int fetchSize) {
if (fetchSize < 1) {
throw new IllegalArgumentException("fetchSize must be at least 1");
}
this.fetchSize = fetchSize;
return this;
}

public MassIndexer maxResults(int maxResults) {
if (maxResults < 1) {
throw new IllegalArgumentException("maxResults must be at least 1");
}
this.maxResults = maxResults;
return this;
}

public MassIndexer optimizeAfterPurge(boolean optimizeAfterPurge) {
this.optimizeAfterPurge = optimizeAfterPurge;
return this;
}

public MassIndexer optimizeAtEnd(boolean optimizeAtEnd) {
this.optimizeAtEnd = optimizeAtEnd;
return this;
}

public MassIndexer partitionCapacity(int partitionCapacity) {
if (partitionCapacity < 1) {
throw new IllegalArgumentException("partitionCapacity must be at least 1");
}
this.partitionCapacity = partitionCapacity;
return this;
}

public MassIndexer partitions(int partitions) {
if (partitions < 1) {
throw new IllegalArgumentException("partitions must be at least 1");
}
this.partitions = partitions;
return this;
}

public MassIndexer purgeAtStart(boolean purgeAtStart) {
this.purgeAtStart = purgeAtStart;
return this;
}

public MassIndexer threads(int threads) {
if (threads < 1) {
throw new IllegalArgumentException("threads must be at least 1");
}
this.threads = threads;
return this;
}
public interface MassIndexer {

public long start();
public void stop(long executionId);

public MassIndexer arrayCapacity(int arrayCapacity);
public MassIndexer fetchSize(int fetchSize);
public MassIndexer maxResults(int maxResults);
public MassIndexer optimizeAfterPurge(boolean optimizeAfterPurge);
public MassIndexer optimizeAtEnd(boolean optimizeAtEnd);
public MassIndexer partitionCapacity(int partitionCapacity);
public MassIndexer partitions(int partitions);
public MassIndexer purgeAtStart(boolean purgeAtStart);
public MassIndexer rootEntities(Set<Class<?>> rootEntities);
public MassIndexer threads(int threads);

public int getArrayCapacity();
public int getFetchSize();
public int getMaxResults();
public boolean isOptimizeAfterPurge();
public boolean isOptimizeAtEnd();
public int getPartitionCapacity();
public int getPartitions();
public boolean isPurgeAtStart();
public Set<Class<?>> getRootEntities();
public int getThreads();
}
181 changes: 181 additions & 0 deletions src/main/java/org/hibernate/search/jsr352/MassIndexerImpl.java
@@ -0,0 +1,181 @@
package org.hibernate.search.jsr352;

import java.util.Properties;
import java.util.Set;

import javax.batch.operations.JobOperator;
import javax.batch.runtime.BatchRuntime;

public class MassIndexerImpl implements MassIndexer {

private boolean optimizeAfterPurge = false;
private boolean optimizeAtEnd = false;
private boolean purgeAtStart = false;

private int arrayCapacity = 1000;
private int fetchSize = 200000;
private int maxResults = 1000000;
private int partitionCapacity = 250;
private int partitions = 4;
private int threads = 2;

private final String JOB_NAME = "mass-index";

private Set<Class<?>> rootEntities;

MassIndexerImpl() {

}

@Override
public long start() {
Properties jobParams = new Properties();
jobParams.setProperty("fetchSize", String.valueOf(fetchSize));
jobParams.setProperty("arrayCapacity", String.valueOf(arrayCapacity));
jobParams.setProperty("maxResults", String.valueOf(maxResults));
jobParams.setProperty("partitionCapacity", String.valueOf(partitionCapacity));
jobParams.setProperty("partitions", String.valueOf(partitions));
jobParams.setProperty("threads", String.valueOf(threads));
jobParams.setProperty("purgeAtStart", String.valueOf(purgeAtStart));
jobParams.setProperty("optimizeAfterPurge", String.valueOf(optimizeAfterPurge));
jobParams.setProperty("optimizeAtEnd", String.valueOf(optimizeAtEnd));
jobParams.setProperty("rootEntities", String.valueOf(rootEntities));
JobOperator jobOperator = BatchRuntime.getJobOperator();
Long executionId = jobOperator.start(JOB_NAME, jobParams);
return executionId;
}

@Override
public void stop(long executionId) {
JobOperator jobOperator = BatchRuntime.getJobOperator();
jobOperator.stop(executionId);
}

@Override
public MassIndexer arrayCapacity(int arrayCapacity) {
if (arrayCapacity < 1) {
throw new IllegalArgumentException("arrayCapacity must be at least 1");
}
this.arrayCapacity = arrayCapacity;
return this;
}

@Override
public MassIndexer fetchSize(int fetchSize) {
if (fetchSize < 1) {
throw new IllegalArgumentException("fetchSize must be at least 1");
}
this.fetchSize = fetchSize;
return this;
}

@Override
public MassIndexer maxResults(int maxResults) {
if (maxResults < 1) {
throw new IllegalArgumentException("maxResults must be at least 1");
}
this.maxResults = maxResults;
return this;
}

@Override
public MassIndexer optimizeAfterPurge(boolean optimizeAfterPurge) {
this.optimizeAfterPurge = optimizeAfterPurge;
return this;
}

@Override
public MassIndexer optimizeAtEnd(boolean optimizeAtEnd) {
this.optimizeAtEnd = optimizeAtEnd;
return this;
}

@Override
public MassIndexer partitionCapacity(int partitionCapacity) {
if (partitionCapacity < 1) {
throw new IllegalArgumentException("partitionCapacity must be at least 1");
}
this.partitionCapacity = partitionCapacity;
return this;
}

@Override
public MassIndexer partitions(int partitions) {
if (partitions < 1) {
throw new IllegalArgumentException("partitions must be at least 1");
}
this.partitions = partitions;
return this;
}

@Override
public MassIndexer purgeAtStart(boolean purgeAtStart) {
this.purgeAtStart = purgeAtStart;
return this;
}

@Override
public MassIndexer threads(int threads) {
if (threads < 1) {
throw new IllegalArgumentException("threads must be at least 1.");
}
this.threads = threads;
return this;
}

@Override
public MassIndexer rootEntities(Set<Class<?>> rootEntities) {
if (rootEntities == null) {
throw new NullPointerException("rootEntities cannot be NULL.");
} else if (rootEntities.isEmpty()) {
throw new NullPointerException("rootEntities must have at least 1 element.");
}
this.rootEntities = rootEntities;
return this;
}

@Override
public boolean isOptimizeAfterPurge() {
return optimizeAfterPurge;
}

public boolean isOptimizeAtEnd() {
return optimizeAtEnd;
}

public boolean isPurgeAtStart() {
return purgeAtStart;
}

public int getArrayCapacity() {
return arrayCapacity;
}

public int getFetchSize() {
return fetchSize;
}

public int getMaxResults() {
return maxResults;
}

public int getPartitionCapacity() {
return partitionCapacity;
}

public int getPartitions() {
return partitions;
}

public int getThreads() {
return threads;
}

public String getJOB_NAME() {
return JOB_NAME;
}

public Set<Class<?>> getRootEntities() {
return rootEntities;
}
}

0 comments on commit 522f178

Please sign in to comment.