-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
WriteableSetting.java
443 lines (422 loc) · 17.4 KB
/
WriteableSetting.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.common.settings;
import org.opensearch.Version;
import org.opensearch.common.Nullable;
import org.opensearch.common.settings.Setting.ByteSizeValueParser;
import org.opensearch.common.settings.Setting.DoubleParser;
import org.opensearch.common.settings.Setting.FloatParser;
import org.opensearch.common.settings.Setting.IntegerParser;
import org.opensearch.common.settings.Setting.LongParser;
import org.opensearch.common.settings.Setting.MemorySizeValueParser;
import org.opensearch.common.settings.Setting.MinMaxTimeValueParser;
import org.opensearch.common.settings.Setting.MinTimeValueParser;
import org.opensearch.common.settings.Setting.Property;
import org.opensearch.common.settings.Setting.RegexValidator;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.core.common.io.stream.Writeable;
import org.opensearch.core.common.unit.ByteSizeValue;
import java.io.IOException;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.concurrent.TimeUnit;
/**
* Wrapper for {@link Setting} with {@link #writeTo(StreamOutput)} implementation dependent on the setting type.
*
* @opensearch.internal
*/
public class WriteableSetting implements Writeable {
/**
* The Generic Types which this class can serialize.
*/
public enum SettingType {
Boolean,
Integer,
Long,
Float,
Double,
String,
TimeValue, // long + TimeUnit
ByteSizeValue, // long + ByteSizeUnit
Version
}
private Setting<?> setting;
private SettingType type;
/**
* Wrap a {@link Setting}. The generic type is determined from the type of the default value.
*
* @param setting The setting to wrap. The default value must not be null.
* @throws IllegalArgumentException if the setting has a null default value.
*/
public WriteableSetting(Setting<?> setting) {
this(setting, getGenericTypeFromDefault(setting));
}
/**
* Wrap a {@link Setting} with a specified generic type.
*
* @param setting The setting to wrap.
* @param type The Generic type of the setting.
*/
public WriteableSetting(Setting<?> setting, SettingType type) {
this.setting = setting;
this.type = type;
}
/**
* Wrap a {@link Setting} read from a stream.
*
* @param in Input to read the value from.
* @throws IOException if there is an error reading the values
*/
public WriteableSetting(StreamInput in) throws IOException {
// Read the type
this.type = in.readEnum(SettingType.class);
// Read the key
String key = in.readString();
// Read the default value
Object defaultValue = readDefaultValue(in);
// Read a boolean specifying whether the fallback settings is null
WriteableSetting fallback = null;
boolean hasFallback = in.readBoolean();
if (hasFallback) {
fallback = new WriteableSetting(in);
}
// Read the parser
Object parser = null;
boolean isParserWriteable = in.readBoolean();
if (isParserWriteable) {
parser = readParser(in, parser);
}
// Read the Validator
Object validator = new Object();
boolean isValidatorWriteable = in.readBoolean();
if (isValidatorWriteable) {
validator = readValidator(in);
}
// Read properties
EnumSet<Property> propSet = in.readEnumSet(Property.class);
// Put it all in a setting object
this.setting = createSetting(type, key, defaultValue, parser, validator, fallback, propSet.toArray(Property[]::new));
}
/**
* Due to type erasure, it is impossible to determine the generic type of a Setting at runtime.
* All settings have a non-null default, however, so the type of the default can be used to determine the setting type.
*
* @param setting The setting with a generic type.
* @return The corresponding {@link SettingType} for the default value.
*/
private static SettingType getGenericTypeFromDefault(Setting<?> setting) {
String typeStr = setting.getDefault(Settings.EMPTY).getClass().getSimpleName();
try {
// This throws IAE if not in enum
return SettingType.valueOf(typeStr);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException(
"This class is not yet set up to handle the generic type: "
+ typeStr
+ ". Supported types are "
+ Arrays.toString(SettingType.values())
);
}
}
/**
* Gets the wrapped setting. Use {@link #getType()} to determine its generic type.
*
* @return The wrapped setting.
*/
public Setting<?> getSetting() {
return this.setting;
}
/**
* Gets the generic type of the wrapped setting.
*
* @return The wrapped setting's generic type.
*/
public SettingType getType() {
return this.type;
}
@SuppressWarnings("unchecked")
private Setting<?> createSetting(
SettingType type,
String key,
Object defaultValue,
@Nullable Object parser,
Object validator,
WriteableSetting fallback,
Property[] propertyArray
) {
switch (type) {
case Boolean:
return fallback == null
? Setting.boolSetting(key, (boolean) defaultValue, propertyArray)
: Setting.boolSetting(key, (Setting<Boolean>) fallback.getSetting(), propertyArray);
case Integer:
if (fallback == null && parser instanceof Writeable) {
return Setting.intSetting(
key,
(int) defaultValue,
((IntegerParser) parser).getMin(),
((IntegerParser) parser).getMax(),
propertyArray
);
} else if (fallback == null) {
return Setting.intSetting(key, (int) defaultValue, propertyArray);
}
return Setting.intSetting(key, (Setting<Integer>) fallback.getSetting(), propertyArray);
case Long:
if (fallback == null && parser instanceof Writeable) {
return Setting.longSetting(
key,
(long) defaultValue,
((LongParser) parser).getMin(),
((LongParser) parser).getMax(),
propertyArray
);
} else if (fallback == null) {
return Setting.longSetting(key, (long) defaultValue, propertyArray);
}
return Setting.longSetting(key, (Setting<Long>) fallback.getSetting(), propertyArray);
case Float:
if (fallback == null && parser instanceof Writeable) {
return Setting.floatSetting(
key,
(float) defaultValue,
((FloatParser) parser).getMin(),
((FloatParser) parser).getMax(),
propertyArray
);
} else if (fallback == null) {
return Setting.floatSetting(key, (float) defaultValue, propertyArray);
}
return Setting.floatSetting(key, (Setting<Float>) fallback.getSetting(), propertyArray);
case Double:
if (fallback == null && parser instanceof Writeable) {
return Setting.doubleSetting(
key,
(double) defaultValue,
((DoubleParser) parser).getMin(),
((DoubleParser) parser).getMax(),
propertyArray
);
} else if (fallback == null) {
return Setting.doubleSetting(key, (double) defaultValue, propertyArray);
}
return Setting.doubleSetting(key, (Setting<Double>) fallback.getSetting(), propertyArray);
case String:
return fallback == null
? Setting.simpleString(key, (String) defaultValue, propertyArray)
: Setting.simpleString(key, (Setting<String>) fallback.getSetting(), propertyArray);
case TimeValue:
if (fallback == null && parser instanceof Writeable) {
if (parser instanceof MinMaxTimeValueParser) {
return Setting.timeSetting(
key,
(TimeValue) defaultValue,
((MinMaxTimeValueParser) parser).getMin(),
((MinMaxTimeValueParser) parser).getMax(),
propertyArray
);
} else {
return Setting.timeSetting(key, (TimeValue) defaultValue, ((MinTimeValueParser) parser).getMin(), propertyArray);
}
} else if (fallback == null) {
return Setting.timeSetting(key, (TimeValue) defaultValue, propertyArray);
}
return Setting.timeSetting(key, (Setting<TimeValue>) fallback.getSetting(), propertyArray);
case ByteSizeValue:
if (fallback == null && parser instanceof Writeable) {
if (parser instanceof MemorySizeValueParser) {
return Setting.memorySizeSetting(key, (ByteSizeValue) defaultValue, propertyArray);
} else {
ByteSizeValueParser byteSizeValueParser = (ByteSizeValueParser) parser;
return Setting.byteSizeSetting(
key,
(ByteSizeValue) defaultValue,
byteSizeValueParser.getMin(),
byteSizeValueParser.getMax(),
propertyArray
);
}
} else if (fallback == null) {
return Setting.byteSizeSetting(key, (ByteSizeValue) defaultValue, propertyArray);
}
return Setting.byteSizeSetting(key, (Setting<ByteSizeValue>) fallback.getSetting(), propertyArray);
case Version:
// No fallback option on this method
return Setting.versionSetting(key, (Version) defaultValue, propertyArray);
default:
// This Should Never Happen (TM)
throw new IllegalArgumentException("A SettingType has been added to the enum and not handled here.");
}
}
@Override
public void writeTo(StreamOutput out) throws IOException {
// Write the type
out.writeEnum(type);
// Write the key
out.writeString(setting.getKey());
// Write the default value
writeDefaultValue(out, setting.getDefault(Settings.EMPTY));
// Write a boolean specifying whether the fallback settings is null
boolean hasFallback = setting.fallbackSetting != null;
out.writeBoolean(hasFallback);
if (hasFallback) {
new WriteableSetting(setting.fallbackSetting, type).writeTo(out);
}
// Write a boolean specifying whether the parser is an instanceof writeable
boolean isParserWriteable = setting.parser instanceof Writeable;
out.writeBoolean(isParserWriteable);
if (isParserWriteable) {
writeParser(out, setting.parser);
}
// Write the validator
boolean isValidatorWriteable = setting.validator instanceof Writeable;
out.writeBoolean(isValidatorWriteable);
if (isValidatorWriteable) {
writeValidator(out, setting.validator);
}
// Write properties
out.writeEnumSet(setting.getProperties());
}
private void writeParser(StreamOutput out, Object parser) throws IOException {
switch (type) {
case Integer:
((IntegerParser) parser).writeTo(out);
break;
case Long:
((LongParser) parser).writeTo(out);
break;
case Float:
((FloatParser) parser).writeTo(out);
break;
case Double:
((DoubleParser) parser).writeTo(out);
break;
case TimeValue:
if (parser instanceof MinMaxTimeValueParser) {
out.writeBoolean(true);
((MinMaxTimeValueParser) parser).writeTo(out);
} else if (parser instanceof MinTimeValueParser) {
out.writeBoolean(false);
((MinTimeValueParser) parser).writeTo(out);
}
break;
case ByteSizeValue:
if (parser instanceof ByteSizeValueParser) {
out.writeBoolean(true);
((ByteSizeValueParser) parser).writeTo(out);
} else if (parser instanceof MemorySizeValueParser) {
out.writeBoolean(false);
((MemorySizeValueParser) parser).writeTo(out);
}
break;
default:
throw new IllegalArgumentException("A SettingType has been added to the enum and not handled here.");
}
}
private void writeValidator(StreamOutput out, Object validator) throws IOException {
((Writeable) validator).writeTo(out);
}
private void writeDefaultValue(StreamOutput out, Object defaultValue) throws IOException {
switch (type) {
case Boolean:
out.writeBoolean((boolean) defaultValue);
break;
case Integer:
out.writeInt((int) defaultValue);
break;
case Long:
out.writeLong((long) defaultValue);
break;
case Float:
out.writeFloat((float) defaultValue);
break;
case Double:
out.writeDouble((double) defaultValue);
break;
case String:
out.writeString((String) defaultValue);
break;
case TimeValue:
TimeValue tv = (TimeValue) defaultValue;
out.writeLong(tv.duration());
out.writeString(tv.timeUnit().name());
break;
case ByteSizeValue:
((ByteSizeValue) defaultValue).writeTo(out);
break;
case Version:
out.writeVersion((Version) defaultValue);
break;
default:
// This Should Never Happen (TM)
throw new IllegalArgumentException("A SettingType has been added to the enum and not handled here.");
}
}
private Object readParser(StreamInput in, Object parser) throws IOException {
switch (type) {
case Integer:
return new IntegerParser(in);
case Long:
return new LongParser(in);
case Float:
return new FloatParser(in);
case Double:
return new DoubleParser(in);
case TimeValue:
if (in.readBoolean()) {
return new MinMaxTimeValueParser(in);
} else {
return new MinTimeValueParser(in);
}
case ByteSizeValue:
if (in.readBoolean()) {
return new ByteSizeValueParser(in);
} else {
return new MemorySizeValueParser(in);
}
default:
throw new IllegalArgumentException("A SettingType has been added to the enum and not handled here.");
}
}
private Object readValidator(StreamInput in) throws IOException {
return new RegexValidator(in);
}
private Object readDefaultValue(StreamInput in) throws IOException {
switch (type) {
case Boolean:
return in.readBoolean();
case Integer:
return in.readInt();
case Long:
return in.readLong();
case Float:
return in.readFloat();
case Double:
return in.readDouble();
case String:
return in.readString();
case TimeValue:
long duration = in.readLong();
TimeUnit unit = TimeUnit.valueOf(in.readString());
return new TimeValue(duration, unit);
case ByteSizeValue:
return new ByteSizeValue(in);
case Version:
return in.readVersion();
default:
// This Should Never Happen (TM)
throw new IllegalArgumentException("A SettingType has been added to the enum and not handled here.");
}
}
@Override
public String toString() {
return "WriteableSettings{type=Setting<" + type + ">, setting=" + setting + "}";
}
}