28
28
import org .elasticsearch .common .collect .Tuple ;
29
29
import org .elasticsearch .common .regex .Regex ;
30
30
import org .elasticsearch .index .Index ;
31
+ import org .elasticsearch .tasks .TaskResultsService ;
31
32
32
33
import java .util .Collection ;
33
34
import java .util .Comparator ;
35
+ import java .util .HashMap ;
34
36
import java .util .List ;
35
37
import java .util .Map ;
36
38
import java .util .Optional ;
37
39
import java .util .stream .Collectors ;
38
40
39
41
import static java .util .stream .Collectors .toUnmodifiableList ;
42
+ import static org .elasticsearch .tasks .TaskResultsService .TASK_INDEX ;
40
43
41
44
/**
42
45
* This class holds the {@link SystemIndexDescriptor} objects that represent system indices the
45
48
*/
46
49
public class SystemIndices {
47
50
51
+ private static final Map <String , Collection <SystemIndexDescriptor >> SERVER_SYSTEM_INDEX_DESCRIPTORS = Map .of (
52
+ TaskResultsService .class .getName (), List .of (new SystemIndexDescriptor (TASK_INDEX + "*" , "Task Result Index" ))
53
+ );
54
+
48
55
private final CharacterRunAutomaton runAutomaton ;
49
56
private final Collection <SystemIndexDescriptor > systemIndexDescriptors ;
50
57
51
- public SystemIndices (Map <String , Collection <SystemIndexDescriptor >> systemIndexDescriptorMap ) {
52
- checkForOverlappingPatterns (systemIndexDescriptorMap );
53
- this .systemIndexDescriptors = systemIndexDescriptorMap .values ()
58
+ public SystemIndices (Map <String , Collection <SystemIndexDescriptor >> pluginAndModulesDescriptors ) {
59
+ final Map <String , Collection <SystemIndexDescriptor >> descriptorsMap = buildSystemIndexDescriptorMap (pluginAndModulesDescriptors );
60
+ checkForOverlappingPatterns (descriptorsMap );
61
+ this .systemIndexDescriptors = descriptorsMap .values ()
54
62
.stream ()
55
63
.flatMap (Collection ::stream )
56
64
.collect (Collectors .toUnmodifiableList ());
@@ -63,7 +71,16 @@ public SystemIndices(Map<String, Collection<SystemIndexDescriptor>> systemIndexD
63
71
* @return true if the {@link Index}'s name matches a pattern from a {@link SystemIndexDescriptor}
64
72
*/
65
73
public boolean isSystemIndex (Index index ) {
66
- return runAutomaton .run (index .getName ());
74
+ return isSystemIndex (index .getName ());
75
+ }
76
+
77
+ /**
78
+ * Determines whether a given index is a system index by comparing its name to the collection of loaded {@link SystemIndexDescriptor}s
79
+ * @param indexName the index name to check against loaded {@link SystemIndexDescriptor}s
80
+ * @return true if the index name matches a pattern from a {@link SystemIndexDescriptor}
81
+ */
82
+ public boolean isSystemIndex (String indexName ) {
83
+ return runAutomaton .run (indexName );
67
84
}
68
85
69
86
/**
@@ -126,10 +143,10 @@ static void checkForOverlappingPatterns(Map<String, Collection<SystemIndexDescri
126
143
.filter (d -> overlaps (descriptorToCheck .v2 (), d .v2 ()))
127
144
.collect (Collectors .toUnmodifiableList ());
128
145
if (descriptorsMatchingThisPattern .isEmpty () == false ) {
129
- throw new IllegalStateException ("a system index descriptor [" + descriptorToCheck .v2 () + "] from plugin [" +
146
+ throw new IllegalStateException ("a system index descriptor [" + descriptorToCheck .v2 () + "] from [" +
130
147
descriptorToCheck .v1 () + "] overlaps with other system index descriptors: [" +
131
148
descriptorsMatchingThisPattern .stream ()
132
- .map (descriptor -> descriptor .v2 () + " from plugin [" + descriptor .v1 () + "]" )
149
+ .map (descriptor -> descriptor .v2 () + " from [" + descriptor .v1 () + "]" )
133
150
.collect (Collectors .joining (", " )));
134
151
}
135
152
});
@@ -140,4 +157,19 @@ private static boolean overlaps(SystemIndexDescriptor a1, SystemIndexDescriptor
140
157
Automaton a2Automaton = Regex .simpleMatchToAutomaton (a2 .getIndexPattern ());
141
158
return Operations .isEmpty (Operations .intersection (a1Automaton , a2Automaton )) == false ;
142
159
}
160
+
161
+ private static Map <String , Collection <SystemIndexDescriptor >> buildSystemIndexDescriptorMap (
162
+ Map <String , Collection <SystemIndexDescriptor >> pluginAndModulesMap ) {
163
+ final Map <String , Collection <SystemIndexDescriptor >> map =
164
+ new HashMap <>(pluginAndModulesMap .size () + SERVER_SYSTEM_INDEX_DESCRIPTORS .size ());
165
+ map .putAll (pluginAndModulesMap );
166
+ // put the server items last since we expect less of them
167
+ SERVER_SYSTEM_INDEX_DESCRIPTORS .forEach ((source , descriptors ) -> {
168
+ if (map .putIfAbsent (source , descriptors ) != null ) {
169
+ throw new IllegalArgumentException ("plugin or module attempted to define the same source [" + source +
170
+ "] as a built-in system index" );
171
+ }
172
+ });
173
+ return Map .copyOf (map );
174
+ }
143
175
}
0 commit comments