Skip to content

Commit e5cd594

Browse files
committed
Boot-time Mapping Definitions, closes #86.
1 parent 8f32467 commit e5cd594

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

modules/elasticsearch/src/main/java/org/elasticsearch/cluster/metadata/MetaDataService.java

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.elasticsearch.cluster.metadata;
2121

22+
import com.google.common.collect.Maps;
2223
import com.google.inject.Inject;
2324
import org.elasticsearch.ElasticSearchException;
2425
import org.elasticsearch.cluster.ClusterService;
@@ -30,6 +31,7 @@
3031
import org.elasticsearch.cluster.routing.IndexRoutingTable;
3132
import org.elasticsearch.cluster.routing.RoutingTable;
3233
import org.elasticsearch.cluster.routing.strategy.ShardsRoutingStrategy;
34+
import org.elasticsearch.env.Environment;
3335
import org.elasticsearch.index.Index;
3436
import org.elasticsearch.index.mapper.DocumentMapper;
3537
import org.elasticsearch.index.mapper.InvalidTypeNameException;
@@ -44,9 +46,13 @@
4446
import org.elasticsearch.util.TimeValue;
4547
import org.elasticsearch.util.Tuple;
4648
import org.elasticsearch.util.component.AbstractComponent;
49+
import org.elasticsearch.util.io.Streams;
4750
import org.elasticsearch.util.settings.ImmutableSettings;
4851
import org.elasticsearch.util.settings.Settings;
4952

53+
import java.io.File;
54+
import java.io.FileReader;
55+
import java.io.IOException;
5056
import java.util.List;
5157
import java.util.Map;
5258
import java.util.Set;
@@ -66,6 +72,7 @@
6672
*/
6773
public class MetaDataService extends AbstractComponent {
6874

75+
private final Environment environment;
6976

7077
private final ClusterService clusterService;
7178

@@ -79,10 +86,11 @@ public class MetaDataService extends AbstractComponent {
7986

8087
private final NodeMappingCreatedAction nodeMappingCreatedAction;
8188

82-
@Inject public MetaDataService(Settings settings, ClusterService clusterService, IndicesService indicesService, ShardsRoutingStrategy shardsRoutingStrategy,
89+
@Inject public MetaDataService(Settings settings, Environment environment, ClusterService clusterService, IndicesService indicesService, ShardsRoutingStrategy shardsRoutingStrategy,
8390
NodeIndexCreatedAction nodeIndexCreatedAction, NodeIndexDeletedAction nodeIndexDeletedAction,
8491
NodeMappingCreatedAction nodeMappingCreatedAction) {
8592
super(settings);
93+
this.environment = environment;
8694
this.clusterService = clusterService;
8795
this.indicesService = indicesService;
8896
this.shardsRoutingStrategy = shardsRoutingStrategy;
@@ -130,7 +138,7 @@ public synchronized IndicesAliasesResult indicesAliases(final List<AliasAction>
130138
return new IndicesAliasesResult();
131139
}
132140

133-
public synchronized CreateIndexResult createIndex(final String index, final Settings indexSettings, final Map<String, String> mappings, TimeValue timeout) throws IndexAlreadyExistsException {
141+
public synchronized CreateIndexResult createIndex(final String index, final Settings indexSettings, Map<String, String> mappings, TimeValue timeout) throws IndexAlreadyExistsException {
134142
ClusterState clusterState = clusterService.state();
135143

136144
if (clusterState.routingTable().hasIndex(index)) {
@@ -161,6 +169,30 @@ public synchronized CreateIndexResult createIndex(final String index, final Sett
161169
throw new InvalidIndexNameException(new Index(index), index, "an alias with the same name already exists");
162170
}
163171

172+
// add to the mappings files that exists within the config/mappings location
173+
if (mappings == null) {
174+
mappings = Maps.newHashMap();
175+
} else {
176+
mappings = Maps.newHashMap(mappings);
177+
}
178+
File mappingsDir = new File(environment.configFile(), "mappings");
179+
if (mappingsDir.exists() && mappingsDir.isDirectory()) {
180+
File[] mappingsFiles = mappingsDir.listFiles();
181+
for (File mappingFile : mappingsFiles) {
182+
String fileNameNoSuffix = mappingFile.getName().substring(0, mappingFile.getName().lastIndexOf('.'));
183+
if (mappings.containsKey(fileNameNoSuffix)) {
184+
// if we have the mapping defined, ignore it
185+
continue;
186+
}
187+
try {
188+
mappings.put(fileNameNoSuffix, Streams.copyToString(new FileReader(mappingFile)));
189+
} catch (IOException e) {
190+
logger.warn("Failed to read mapping [" + fileNameNoSuffix + "] from location [" + mappingFile + "], ignoring...", e);
191+
}
192+
}
193+
}
194+
final Map<String, String> fMappings = mappings;
195+
164196
final CountDownLatch latch = new CountDownLatch(clusterService.state().nodes().size());
165197
NodeIndexCreatedAction.Listener nodeCreatedListener = new NodeIndexCreatedAction.Listener() {
166198
@Override public void onNodeIndexCreated(String mIndex, String nodeId) {
@@ -186,10 +218,8 @@ public synchronized CreateIndexResult createIndex(final String index, final Sett
186218
Settings actualIndexSettings = indexSettingsBuilder.build();
187219

188220
IndexMetaData.Builder indexMetaData = newIndexMetaDataBuilder(index).settings(actualIndexSettings);
189-
if (mappings != null) {
190-
for (Map.Entry<String, String> entry : mappings.entrySet()) {
191-
indexMetaData.putMapping(entry.getKey(), entry.getValue());
192-
}
221+
for (Map.Entry<String, String> entry : fMappings.entrySet()) {
222+
indexMetaData.putMapping(entry.getKey(), entry.getValue());
193223
}
194224
MetaData newMetaData = newMetaDataBuilder()
195225
.metaData(currentState.metaData())
@@ -200,7 +230,7 @@ public synchronized CreateIndexResult createIndex(final String index, final Sett
200230
.initializeEmpty(newMetaData.index(index));
201231
routingTableBuilder.add(indexRoutingBuilder);
202232

203-
logger.info("Creating Index [{}], shards [{}]/[{}]", new Object[]{index, indexMetaData.numberOfShards(), indexMetaData.numberOfReplicas()});
233+
logger.info("Creating Index [{}], shards [{}]/[{}], mappings {}", new Object[]{index, indexMetaData.numberOfShards(), indexMetaData.numberOfReplicas(), fMappings.keySet()});
204234
RoutingTable newRoutingTable = shardsRoutingStrategy.reroute(newClusterStateBuilder().state(currentState).routingTable(routingTableBuilder).metaData(newMetaData).build());
205235
return newClusterStateBuilder().state(currentState).routingTable(newRoutingTable).metaData(newMetaData).build();
206236
}

modules/elasticsearch/src/main/java/org/elasticsearch/env/Environment.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ public File workFile() {
8787
return workFile;
8888
}
8989

90+
public File configFile() {
91+
return configFile;
92+
}
93+
9094
public File workWithClusterFile() {
9195
return workWithClusterFile;
9296
}

0 commit comments

Comments
 (0)