Skip to content

Commit

Permalink
Create Mapping API: Automatically create indices. Closes #12.
Browse files Browse the repository at this point in the history
  • Loading branch information
kimchy committed Feb 15, 2010
1 parent b5f3fc9 commit 9a9ce99
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,25 @@
package org.elasticsearch.action.admin.indices.mapping.create;

import com.google.inject.Inject;
import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.Actions;
import org.elasticsearch.action.TransportActions;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.create.TransportCreateIndexAction;
import org.elasticsearch.action.support.BaseAction;
import org.elasticsearch.cluster.ClusterService;
import org.elasticsearch.cluster.metadata.MetaDataService;
import org.elasticsearch.indices.IndexAlreadyExistsException;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.*;
import org.elasticsearch.util.io.VoidStreamable;
import org.elasticsearch.util.settings.Settings;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
* @author kimchy (Shay Banon)
Expand All @@ -44,21 +51,54 @@ public class TransportCreateMappingAction extends BaseAction<CreateMappingReques

private final MetaDataService metaDataService;

private final TransportCreateIndexAction createIndexAction;

private final ThreadPool threadPool;

private final boolean autoCreateIndex;

@Inject public TransportCreateMappingAction(Settings settings, TransportService transportService, ClusterService clusterService,
ThreadPool threadPool, MetaDataService metaDataService) {
ThreadPool threadPool, MetaDataService metaDataService, TransportCreateIndexAction createIndexAction) {
super(settings);
this.transportService = transportService;
this.clusterService = clusterService;
this.threadPool = threadPool;
this.metaDataService = metaDataService;
this.createIndexAction = createIndexAction;
this.autoCreateIndex = settings.getAsBoolean("action.autoCreateIndex", true);

transportService.registerHandler(TransportActions.Admin.Indices.Mapping.CREATE, new TransportHandler());
}

@Override protected void doExecute(final CreateMappingRequest request, final ActionListener<CreateMappingResponse> listener) {
final String[] indices = Actions.processIndices(clusterService.state(), request.indices());
if (autoCreateIndex) {
final CountDownLatch latch = new CountDownLatch(indices.length);
for (String index : indices) {
if (!clusterService.state().metaData().hasIndex(index)) {
createIndexAction.execute(new CreateIndexRequest(index), new ActionListener<CreateIndexResponse>() {
@Override public void onResponse(CreateIndexResponse result) {
latch.countDown();
}

@Override public void onFailure(Throwable e) {
if (ExceptionsHelper.unwrapCause(e) instanceof IndexAlreadyExistsException) {
latch.countDown();
} else {
listener.onFailure(e);
}
}
});
} else {
latch.countDown();
}
}
try {
latch.await(10, TimeUnit.SECONDS);
} catch (InterruptedException e) {
// ignore
}
}
if (clusterService.state().nodes().localNodeMaster()) {
threadPool.execute(new Runnable() {
@Override public void run() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class TransportIndexAction extends TransportShardReplicationOperationActi
TransportCreateIndexAction createIndexAction) {
super(settings, transportService, clusterService, indicesService, threadPool, shardStateAction);
this.createIndexAction = createIndexAction;
this.autoCreateIndex = componentSettings.getAsBoolean("autoCreateIndex", true);
this.autoCreateIndex = settings.getAsBoolean("action.autoCreateIndex", true);
this.allowIdGeneration = componentSettings.getAsBoolean("allowIdGeneration", true);
}

Expand Down

0 comments on commit 9a9ce99

Please sign in to comment.