20
20
package org .elasticsearch .cluster .metadata ;
21
21
22
22
import com .google .common .collect .ImmutableMap ;
23
+ import org .codehaus .jackson .JsonParser ;
24
+ import org .codehaus .jackson .JsonToken ;
23
25
import org .elasticsearch .util .MapBuilder ;
26
+ import org .elasticsearch .util .Nullable ;
24
27
import org .elasticsearch .util .Preconditions ;
25
28
import org .elasticsearch .util .concurrent .Immutable ;
29
+ import org .elasticsearch .util .json .JsonBuilder ;
30
+ import org .elasticsearch .util .json .ToJson ;
26
31
import org .elasticsearch .util .settings .ImmutableSettings ;
27
32
import org .elasticsearch .util .settings .Settings ;
28
33
@@ -52,8 +57,8 @@ public class IndexMetaData {
52
57
private transient final int totalNumberOfShards ;
53
58
54
59
private IndexMetaData (String index , Settings settings , ImmutableMap <String , String > mappings ) {
55
- Preconditions .checkArgument (settings .getAsInt (SETTING_NUMBER_OF_SHARDS , -1 ) != -1 , "must specify numberOfShards" );
56
- Preconditions .checkArgument (settings .getAsInt (SETTING_NUMBER_OF_REPLICAS , -1 ) != -1 , "must specify numberOfReplicas" );
60
+ Preconditions .checkArgument (settings .getAsInt (SETTING_NUMBER_OF_SHARDS , -1 ) != -1 , "must specify numberOfShards for index [" + index + "] " );
61
+ Preconditions .checkArgument (settings .getAsInt (SETTING_NUMBER_OF_REPLICAS , -1 ) != -1 , "must specify numberOfReplicas for index [" + index + "] " );
57
62
this .index = index ;
58
63
this .settings = settings ;
59
64
this .mappings = mappings ;
@@ -115,7 +120,7 @@ public String index() {
115
120
}
116
121
117
122
public Builder numberOfShards (int numberOfShards ) {
118
- settings = ImmutableSettings . settingsBuilder ().putAll (settings ).putInt (SETTING_NUMBER_OF_SHARDS , numberOfShards ).build ();
123
+ settings = settingsBuilder ().putAll (settings ).putInt (SETTING_NUMBER_OF_SHARDS , numberOfShards ).build ();
119
124
return this ;
120
125
}
121
126
@@ -124,14 +129,19 @@ public int numberOfShards() {
124
129
}
125
130
126
131
public Builder numberOfReplicas (int numberOfReplicas ) {
127
- settings = ImmutableSettings . settingsBuilder ().putAll (settings ).putInt (SETTING_NUMBER_OF_REPLICAS , numberOfReplicas ).build ();
132
+ settings = settingsBuilder ().putAll (settings ).putInt (SETTING_NUMBER_OF_REPLICAS , numberOfReplicas ).build ();
128
133
return this ;
129
134
}
130
135
131
136
public int numberOfReplicas () {
132
137
return settings .getAsInt (SETTING_NUMBER_OF_REPLICAS , -1 );
133
138
}
134
139
140
+ public Builder settings (Settings .Builder settings ) {
141
+ this .settings = settings .build ();
142
+ return this ;
143
+ }
144
+
135
145
public Builder settings (Settings settings ) {
136
146
this .settings = settings ;
137
147
return this ;
@@ -151,6 +161,68 @@ public IndexMetaData build() {
151
161
return new IndexMetaData (index , settings , mappings .immutableMap ());
152
162
}
153
163
164
+ public static void toJson (IndexMetaData indexMetaData , JsonBuilder builder , ToJson .Params params ) throws IOException {
165
+ builder .startObject (indexMetaData .index ());
166
+
167
+ builder .startObject ("settings" );
168
+ for (Map .Entry <String , String > entry : indexMetaData .settings ().getAsMap ().entrySet ()) {
169
+ builder .field (entry .getKey (), entry .getValue ());
170
+ }
171
+ builder .endObject ();
172
+
173
+ builder .startObject ("mappings" );
174
+ for (Map .Entry <String , String > entry : indexMetaData .mappings ().entrySet ()) {
175
+ builder .startObject (entry .getKey ());
176
+ builder .field ("source" , entry .getValue ());
177
+ builder .endObject ();
178
+ }
179
+ builder .endObject ();
180
+
181
+ builder .endObject ();
182
+ }
183
+
184
+ public static IndexMetaData fromJson (JsonParser jp , @ Nullable Settings globalSettings ) throws IOException {
185
+ Builder builder = new Builder (jp .getCurrentName ());
186
+
187
+ String currentFieldName = null ;
188
+ JsonToken token = jp .nextToken ();
189
+ while ((token = jp .nextToken ()) != JsonToken .END_OBJECT ) {
190
+ if (token == JsonToken .FIELD_NAME ) {
191
+ currentFieldName = jp .getCurrentName ();
192
+ } else if (token == JsonToken .START_OBJECT ) {
193
+ if ("settings" .equals (currentFieldName )) {
194
+ ImmutableSettings .Builder settingsBuilder = settingsBuilder ().globalSettings (globalSettings );
195
+ while ((token = jp .nextToken ()) != JsonToken .END_OBJECT ) {
196
+ String key = jp .getCurrentName ();
197
+ token = jp .nextToken ();
198
+ String value = jp .getText ();
199
+ settingsBuilder .put (key , value );
200
+ }
201
+ builder .settings (settingsBuilder .build ());
202
+ } else if ("mappings" .equals (currentFieldName )) {
203
+ while ((token = jp .nextToken ()) != JsonToken .END_OBJECT ) {
204
+ String mappingType = jp .getCurrentName ();
205
+ String mappingSource = null ;
206
+ while ((token = jp .nextToken ()) != JsonToken .END_OBJECT ) {
207
+ if (token == JsonToken .FIELD_NAME ) {
208
+ if ("source" .equals (jp .getCurrentName ())) {
209
+ jp .nextToken ();
210
+ mappingSource = jp .getText ();
211
+ }
212
+ }
213
+ }
214
+ if (mappingSource == null ) {
215
+ // crap, no mapping source, warn?
216
+ } else {
217
+ builder .putMapping (mappingType , mappingSource );
218
+ }
219
+ }
220
+ }
221
+ }
222
+ }
223
+ return builder .build ();
224
+ }
225
+
154
226
public static IndexMetaData readFrom (DataInput in , Settings globalSettings ) throws ClassNotFoundException , IOException {
155
227
Builder builder = new Builder (in .readUTF ());
156
228
builder .settings (readSettingsFromStream (in , globalSettings ));
0 commit comments