Skip to content

Commit

Permalink
Optimize API: Add onlyExpungeDeletes, flush and refresh parameters. C…
Browse files Browse the repository at this point in the history
…loses #15.
  • Loading branch information
kimchy committed Feb 15, 2010
1 parent 9633108 commit 66b86a7
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ public class OptimizeRequest extends BroadcastOperationRequest {

private int maxNumSegments = -1;

private boolean onlyExpungeDeletes = false;

private boolean flush = false;

private boolean refresh = false;

/**
* Constructs an optimization request over one or more indices.
*
Expand Down Expand Up @@ -101,15 +107,68 @@ public OptimizeRequest maxNumSegments(int maxNumSegments) {
return this;
}

/**
* Should the optimization only expunge deletes from the index, without full optimization.
* Defaults to full optimization (<tt>false</tt>).
*/
public boolean onlyExpungeDeletes() {
return onlyExpungeDeletes;
}

/**
* Should the optimization only expunge deletes from the index, without full optimization.
* Defaults to full optimization (<tt>false</tt>).
*/
public OptimizeRequest onlyExpungeDeletes(boolean onlyExpungeDeletes) {
this.onlyExpungeDeletes = onlyExpungeDeletes;
return this;
}

/**
* Should flush be performed after the optimization. Defaults to <tt>false</tt>.
*/
public boolean flush() {
return flush;
}

/**
* Should flush be performed after the optimization. Defaults to <tt>false</tt>.
*/
public OptimizeRequest flush(boolean flush) {
this.flush = flush;
return this;
}

/**
* Should refresh be performed after the optimization. Defaults to <tt>false</tt>.
*/
public boolean refresh() {
return refresh;
}

/**
* Should refresh be performed after the optimization. Defaults to <tt>false</tt>.
*/
public OptimizeRequest refresh(boolean refresh) {
this.refresh = refresh;
return this;
}

public void readFrom(DataInput in) throws IOException, ClassNotFoundException {
super.readFrom(in);
waitForMerge = in.readBoolean();
maxNumSegments = in.readInt();
onlyExpungeDeletes = in.readBoolean();
flush = in.readBoolean();
refresh = in.readBoolean();
}

public void writeTo(DataOutput out) throws IOException {
super.writeTo(out);
out.writeBoolean(waitForMerge);
out.writeInt(maxNumSegments);
out.writeBoolean(onlyExpungeDeletes);
out.writeBoolean(flush);
out.writeBoolean(refresh);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,22 @@ public class ShardOptimizeRequest extends BroadcastShardOperationRequest {

private int maxNumSegments = -1;

private boolean onlyExpungeDeletes = false;

private boolean flush = false;

private boolean refresh = false;

ShardOptimizeRequest() {
}

public ShardOptimizeRequest(String index, int shardId, OptimizeRequest request) {
super(index, shardId);
waitForMerge = request.waitForMerge();
maxNumSegments = request.maxNumSegments();
onlyExpungeDeletes = request.onlyExpungeDeletes();
flush = request.flush();
refresh = request.refresh();
}

boolean waitForMerge() {
Expand All @@ -51,15 +60,33 @@ int maxNumSegments() {
return maxNumSegments;
}

public boolean onlyExpungeDeletes() {
return onlyExpungeDeletes;
}

public boolean flush() {
return flush;
}

public boolean refresh() {
return refresh;
}

@Override public void readFrom(DataInput in) throws IOException, ClassNotFoundException {
super.readFrom(in);
waitForMerge = in.readBoolean();
maxNumSegments = in.readInt();
onlyExpungeDeletes = in.readBoolean();
flush = in.readBoolean();
refresh = in.readBoolean();
}

@Override public void writeTo(DataOutput out) throws IOException {
super.writeTo(out);
out.writeBoolean(waitForMerge);
out.writeInt(maxNumSegments);
out.writeBoolean(onlyExpungeDeletes);
out.writeBoolean(flush);
out.writeBoolean(refresh);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,13 @@ public class TransportOptimizeAction extends TransportBroadcastOperationAction<O

@Override protected ShardOptimizeResponse shardOperation(ShardOptimizeRequest request) throws ElasticSearchException {
IndexShard indexShard = indicesService.indexServiceSafe(request.index()).shardSafe(request.shardId());
indexShard.optimize(new Engine.Optimize(request.waitForMerge(), request.maxNumSegments()));
indexShard.optimize(new Engine.Optimize()
.waitForMerge(request.waitForMerge())
.maxNumSegments(request.maxNumSegments())
.onlyExpungeDeletes(request.onlyExpungeDeletes())
.flush(request.flush())
.refresh(request.refresh())
);
return new ShardOptimizeResponse(request.index(), request.shardId());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public class HttpOptimizeAction extends BaseHttpServerHandler {
try {
optimizeRequest.waitForMerge(HttpActions.paramAsBoolean(request.param("waitForMerge"), true));
optimizeRequest.maxNumSegments(HttpActions.paramAsInt(request.param("maxNumSegments"), -1));
optimizeRequest.onlyExpungeDeletes(HttpActions.paramAsBoolean(request.param("onlyExpungeDeletes"), false));
optimizeRequest.flush(HttpActions.paramAsBoolean(request.param("flush"), false));
optimizeRequest.refresh(HttpActions.paramAsBoolean(request.param("refresh"), false));

// we just send back a response, no need to fork a listener
optimizeRequest.listenerThreaded(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,24 +162,62 @@ public Flush refresh(boolean refresh) {
}

static class Optimize {
private final boolean waitForMerge;
private final int maxNumSegments;
private boolean waitForMerge = true;
private int maxNumSegments = -1;
private boolean onlyExpungeDeletes = false;
private boolean flush = false;
private boolean refresh = false;

public Optimize(boolean waitForMerge, int maxNumSegments) {
this.waitForMerge = waitForMerge;
this.maxNumSegments = maxNumSegments;
public Optimize() {
}

public boolean waitForMerge() {
return waitForMerge;
}

public Optimize waitForMerge(boolean waitForMerge) {
this.waitForMerge = waitForMerge;
return this;
}

public int maxNumSegments() {
return maxNumSegments;
}

public Optimize maxNumSegments(int maxNumSegments) {
this.maxNumSegments = maxNumSegments;
return this;
}

public boolean onlyExpungeDeletes() {
return onlyExpungeDeletes;
}

public Optimize onlyExpungeDeletes(boolean onlyExpungeDeletes) {
this.onlyExpungeDeletes = onlyExpungeDeletes;
return this;
}

public boolean flush() {
return flush;
}

public Optimize flush(boolean flush) {
this.flush = flush;
return this;
}

public boolean refresh() {
return refresh;
}

public Optimize refresh(boolean refresh) {
this.refresh = refresh;
return this;
}

@Override public String toString() {
return "waitForMerge[" + waitForMerge + "], maxNumSegments[" + maxNumSegments + "]";
return "waitForMerge[" + waitForMerge + "], maxNumSegments[" + maxNumSegments + "], onlyExpungeDeletes[" + onlyExpungeDeletes + "], flush[" + flush + "], refresh[" + refresh + "]";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,14 +309,24 @@ public class RobinEngine extends AbstractIndexShardComponent implements Engine,
}
}
}
indexWriter.optimize(maxNumberOfSegments, optimize.waitForMerge());
if (optimize.onlyExpungeDeletes()) {
indexWriter.expungeDeletes(optimize.waitForMerge());
} else {
indexWriter.optimize(maxNumberOfSegments, optimize.waitForMerge());
}
} catch (Exception e) {
throw new OptimizeFailedEngineException(shardId, e);
} finally {
rwl.readLock().unlock();
optimizeMutex.set(false);
}
}
if (optimize.flush()) {
flush(new Flush());
}
if (optimize.refresh()) {
refresh(new Refresh(false));
}
}

@Override public void snapshot(SnapshotHandler snapshotHandler) throws EngineException {
Expand Down

0 comments on commit 66b86a7

Please sign in to comment.