|
9 | 9 |
|
10 | 10 | package org.elasticsearch.action.support;
|
11 | 11 |
|
| 12 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 13 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 14 | +import org.elasticsearch.common.io.stream.Writeable; |
| 15 | +import org.elasticsearch.core.Nullable; |
| 16 | + |
| 17 | +import java.io.IOException; |
12 | 18 | import java.util.Collections;
|
13 | 19 | import java.util.HashMap;
|
14 | 20 | import java.util.Map;
|
|
17 | 23 | * We define as index components the two different sets of indices a data stream could consist of:
|
18 | 24 | * - DATA: represents the backing indices
|
19 | 25 | * - FAILURES: represent the failing indices
|
| 26 | + * - ALL: represents all available in this expression components, meaning if it's a data stream both backing and failure indices and if it's |
| 27 | + * an index only the index itself. |
20 | 28 | * Note: An index is its own DATA component, but it cannot have a FAILURE component.
|
21 | 29 | */
|
22 |
| -public enum IndexComponentSelector { |
23 |
| - DATA("data"), |
24 |
| - FAILURES("failures"); |
| 30 | +public enum IndexComponentSelector implements Writeable { |
| 31 | + DATA("data", (byte) 0), |
| 32 | + FAILURES("failures", (byte) 1), |
| 33 | + ALL_APPLICABLE("*", (byte) 2); |
25 | 34 |
|
26 | 35 | private final String key;
|
| 36 | + private final byte id; |
27 | 37 |
|
28 |
| - IndexComponentSelector(String key) { |
| 38 | + IndexComponentSelector(String key, byte id) { |
29 | 39 | this.key = key;
|
| 40 | + this.id = id; |
30 | 41 | }
|
31 | 42 |
|
32 | 43 | public String getKey() {
|
33 | 44 | return key;
|
34 | 45 | }
|
35 | 46 |
|
36 |
| - private static final Map<String, IndexComponentSelector> REGISTRY; |
| 47 | + public byte getId() { |
| 48 | + return id; |
| 49 | + } |
| 50 | + |
| 51 | + private static final Map<String, IndexComponentSelector> KEY_REGISTRY; |
| 52 | + private static final Map<Byte, IndexComponentSelector> ID_REGISTRY; |
37 | 53 |
|
38 | 54 | static {
|
39 |
| - Map<String, IndexComponentSelector> registry = new HashMap<>(IndexComponentSelector.values().length); |
| 55 | + Map<String, IndexComponentSelector> keyRegistry = new HashMap<>(IndexComponentSelector.values().length); |
| 56 | + for (IndexComponentSelector value : IndexComponentSelector.values()) { |
| 57 | + keyRegistry.put(value.getKey(), value); |
| 58 | + } |
| 59 | + KEY_REGISTRY = Collections.unmodifiableMap(keyRegistry); |
| 60 | + Map<Byte, IndexComponentSelector> idRegistry = new HashMap<>(IndexComponentSelector.values().length); |
40 | 61 | for (IndexComponentSelector value : IndexComponentSelector.values()) {
|
41 |
| - registry.put(value.getKey(), value); |
| 62 | + idRegistry.put(value.getId(), value); |
42 | 63 | }
|
43 |
| - REGISTRY = Collections.unmodifiableMap(registry); |
| 64 | + ID_REGISTRY = Collections.unmodifiableMap(idRegistry); |
44 | 65 | }
|
45 | 66 |
|
| 67 | + /** |
| 68 | + * Retrieves the respective selector when the suffix key is recognised |
| 69 | + * @param key the suffix key, probably parsed from an expression |
| 70 | + * @return the selector or null if the key was not recognised. |
| 71 | + */ |
| 72 | + @Nullable |
46 | 73 | public static IndexComponentSelector getByKey(String key) {
|
47 |
| - return REGISTRY.get(key); |
| 74 | + return KEY_REGISTRY.get(key); |
| 75 | + } |
| 76 | + |
| 77 | + public static IndexComponentSelector read(StreamInput in) throws IOException { |
| 78 | + return getById(in.readByte()); |
| 79 | + } |
| 80 | + |
| 81 | + // Visible for testing |
| 82 | + static IndexComponentSelector getById(byte id) { |
| 83 | + IndexComponentSelector indexComponentSelector = ID_REGISTRY.get(id); |
| 84 | + if (indexComponentSelector == null) { |
| 85 | + throw new IllegalArgumentException( |
| 86 | + "Unknown id of index component selector [" + id + "], available options are: " + ID_REGISTRY |
| 87 | + ); |
| 88 | + } |
| 89 | + return indexComponentSelector; |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public void writeTo(StreamOutput out) throws IOException { |
| 94 | + out.writeByte(id); |
| 95 | + } |
| 96 | + |
| 97 | + public boolean shouldIncludeData() { |
| 98 | + return this == ALL_APPLICABLE || this == DATA; |
| 99 | + } |
| 100 | + |
| 101 | + public boolean shouldIncludeFailures() { |
| 102 | + return this == ALL_APPLICABLE || this == FAILURES; |
48 | 103 | }
|
49 | 104 | }
|
0 commit comments