Delete IndexAPI: Allow to delete more than one index or _all indices, closes #791.

This commit is contained in:
kimchy 2011-03-18 20:44:59 +02:00
parent 09fbc919b8
commit bd3f490d60
6 changed files with 56 additions and 27 deletions

View File

@ -37,7 +37,7 @@ import static org.elasticsearch.common.unit.TimeValue.*;
*/
public class DeleteIndexRequest extends MasterNodeOperationRequest {
private String index;
private String[] indices;
private TimeValue timeout = timeValueSeconds(10);
@ -48,22 +48,31 @@ public class DeleteIndexRequest extends MasterNodeOperationRequest {
* Constructs a new delete index request for the specified index.
*/
public DeleteIndexRequest(String index) {
this.index = index;
this.indices = new String[]{index};
}
public DeleteIndexRequest(String... indices) {
this.indices = indices;
}
@Override public ActionRequestValidationException validate() {
ActionRequestValidationException validationException = null;
if (index == null) {
validationException = addValidationError("index is missing", validationException);
if (indices == null) {
validationException = addValidationError("index / indices is missing", validationException);
}
return validationException;
}
public DeleteIndexRequest indices(String... indices) {
this.indices = indices;
return this;
}
/**
* The index to delete.
*/
String index() {
return index;
String[] indices() {
return indices;
}
/**
@ -93,13 +102,23 @@ public class DeleteIndexRequest extends MasterNodeOperationRequest {
@Override public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
index = in.readUTF();
indices = new String[in.readVInt()];
for (int i = 0; i < indices.length; i++) {
indices[i] = in.readUTF();
}
timeout = readTimeValue(in);
}
@Override public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeUTF(index);
if (indices == null) {
out.writeVInt(0);
} else {
out.writeVInt(indices.length);
for (String index : indices) {
out.writeUTF(index);
}
}
timeout.writeTo(out);
}
}

View File

@ -20,6 +20,7 @@
package org.elasticsearch.action.admin.indices.delete;
import org.elasticsearch.ElasticSearchException;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.TransportActions;
import org.elasticsearch.action.support.master.TransportMasterNodeOperationAction;
import org.elasticsearch.cluster.ClusterService;
@ -66,25 +67,32 @@ public class TransportDeleteIndexAction extends TransportMasterNodeOperationActi
return new DeleteIndexResponse();
}
@Override protected void doExecute(DeleteIndexRequest request, ActionListener<DeleteIndexResponse> listener) {
request.indices(clusterService.state().metaData().concreteIndices(request.indices()));
super.doExecute(request, listener);
}
@Override protected ClusterBlockException checkBlock(DeleteIndexRequest request, ClusterState state) {
return state.blocks().indexBlockedException(ClusterBlockLevel.METADATA, request.index());
return state.blocks().indicesBlockedException(ClusterBlockLevel.METADATA, request.indices());
}
@Override protected DeleteIndexResponse masterOperation(DeleteIndexRequest request, ClusterState state) throws ElasticSearchException {
final AtomicReference<DeleteIndexResponse> responseRef = new AtomicReference<DeleteIndexResponse>();
final AtomicReference<Throwable> failureRef = new AtomicReference<Throwable>();
final CountDownLatch latch = new CountDownLatch(1);
deleteIndexService.deleteIndex(new MetaDataDeleteIndexService.Request(request.index()).timeout(request.timeout()), new MetaDataDeleteIndexService.Listener() {
@Override public void onResponse(MetaDataDeleteIndexService.Response response) {
responseRef.set(new DeleteIndexResponse(response.acknowledged()));
latch.countDown();
}
final CountDownLatch latch = new CountDownLatch(request.indices().length);
for (String index : request.indices()) {
deleteIndexService.deleteIndex(new MetaDataDeleteIndexService.Request(index).timeout(request.timeout()), new MetaDataDeleteIndexService.Listener() {
@Override public void onResponse(MetaDataDeleteIndexService.Response response) {
responseRef.set(new DeleteIndexResponse(response.acknowledged()));
latch.countDown();
}
@Override public void onFailure(Throwable t) {
failureRef.set(t);
latch.countDown();
}
});
@Override public void onFailure(Throwable t) {
failureRef.set(t);
latch.countDown();
}
});
}
try {
latch.await();

View File

@ -150,9 +150,9 @@ public interface IndicesAdminClient {
/**
* Deletes an index based on the index name.
*
* @param index The index name to delete
* @param indices The indices to delete. Empty array to delete all indices.
*/
DeleteIndexRequestBuilder prepareDelete(String index);
DeleteIndexRequestBuilder prepareDelete(String... indices);
/**
* Closes an index based on the index name.

View File

@ -31,8 +31,8 @@ import org.elasticsearch.common.unit.TimeValue;
*/
public class DeleteIndexRequestBuilder extends BaseIndicesRequestBuilder<DeleteIndexRequest, DeleteIndexResponse> {
public DeleteIndexRequestBuilder(IndicesAdminClient indicesClient, String index) {
super(indicesClient, new DeleteIndexRequest(index));
public DeleteIndexRequestBuilder(IndicesAdminClient indicesClient, String... indices) {
super(indicesClient, new DeleteIndexRequest(indices));
}
/**

View File

@ -55,8 +55,8 @@ public abstract class AbstractIndicesAdminClient implements InternalIndicesAdmin
return new CreateIndexRequestBuilder(this, index);
}
@Override public DeleteIndexRequestBuilder prepareDelete(String index) {
return new DeleteIndexRequestBuilder(this, index);
@Override public DeleteIndexRequestBuilder prepareDelete(String... indices) {
return new DeleteIndexRequestBuilder(this, indices);
}
@Override public CloseIndexRequestBuilder prepareClose(String index) {

View File

@ -34,6 +34,7 @@ import java.io.IOException;
import static org.elasticsearch.common.unit.TimeValue.*;
import static org.elasticsearch.rest.RestStatus.*;
import static org.elasticsearch.rest.action.support.RestActions.*;
/**
* @author kimchy (shay.banon)
@ -42,11 +43,12 @@ public class RestDeleteIndexAction extends BaseRestHandler {
@Inject public RestDeleteIndexAction(Settings settings, Client client, RestController controller) {
super(settings, client);
controller.registerHandler(RestRequest.Method.DELETE, "/", this);
controller.registerHandler(RestRequest.Method.DELETE, "/{index}", this);
}
@Override public void handleRequest(final RestRequest request, final RestChannel channel) {
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(request.param("index"));
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(splitIndices(request.param("index")));
deleteIndexRequest.timeout(request.paramAsTime("timeout", timeValueSeconds(10)));
client.admin().indices().delete(deleteIndexRequest, new ActionListener<DeleteIndexResponse>() {
@Override public void onResponse(DeleteIndexResponse response) {