2828import org .elasticsearch .common .collect .Tuple ;
2929import org .elasticsearch .common .regex .Regex ;
3030import org .elasticsearch .index .Index ;
31+ import org .elasticsearch .tasks .TaskResultsService ;
3132
3233import java .util .Collection ;
3334import java .util .Comparator ;
35+ import java .util .HashMap ;
3436import java .util .List ;
3537import java .util .Map ;
3638import java .util .Optional ;
3739import java .util .stream .Collectors ;
3840
41+ import static java .util .Collections .singletonList ;
42+ import static java .util .Collections .singletonMap ;
3943import static java .util .Collections .unmodifiableList ;
44+ import static java .util .Collections .unmodifiableMap ;
45+ import static org .elasticsearch .tasks .TaskResultsService .TASK_INDEX ;
4046
4147/**
4248 * This class holds the {@link SystemIndexDescriptor} objects that represent system indices the
4551 */
4652public class SystemIndices {
4753
54+ private static final Map <String , Collection <SystemIndexDescriptor >> SERVER_SYSTEM_INDEX_DESCRIPTORS = singletonMap (
55+ TaskResultsService .class .getName (), singletonList (new SystemIndexDescriptor (TASK_INDEX + "*" , "Task Result Index" ))
56+ );
57+
4858 private final CharacterRunAutomaton runAutomaton ;
4959 private final Collection <SystemIndexDescriptor > systemIndexDescriptors ;
5060
51- public SystemIndices (Map <String , Collection <SystemIndexDescriptor >> systemIndexDescriptorMap ) {
52- checkForOverlappingPatterns (systemIndexDescriptorMap );
53- this .systemIndexDescriptors = unmodifiableList (systemIndexDescriptorMap .values ()
61+ public SystemIndices (Map <String , Collection <SystemIndexDescriptor >> pluginAndModulesDescriptors ) {
62+ final Map <String , Collection <SystemIndexDescriptor >> descriptorsMap = buildSystemIndexDescriptorMap (pluginAndModulesDescriptors );
63+ checkForOverlappingPatterns (descriptorsMap );
64+ this .systemIndexDescriptors = unmodifiableList (descriptorsMap .values ()
5465 .stream ()
5566 .flatMap (Collection ::stream )
5667 .collect (Collectors .toList ()));
@@ -63,7 +74,16 @@ public SystemIndices(Map<String, Collection<SystemIndexDescriptor>> systemIndexD
6374 * @return true if the {@link Index}'s name matches a pattern from a {@link SystemIndexDescriptor}
6475 */
6576 public boolean isSystemIndex (Index index ) {
66- return runAutomaton .run (index .getName ());
77+ return isSystemIndex (index .getName ());
78+ }
79+
80+ /**
81+ * Determines whether a given index is a system index by comparing its name to the collection of loaded {@link SystemIndexDescriptor}s
82+ * @param indexName the index name to check against loaded {@link SystemIndexDescriptor}s
83+ * @return true if the index name matches a pattern from a {@link SystemIndexDescriptor}
84+ */
85+ public boolean isSystemIndex (String indexName ) {
86+ return runAutomaton .run (indexName );
6787 }
6888
6989 /**
@@ -126,10 +146,10 @@ static void checkForOverlappingPatterns(Map<String, Collection<SystemIndexDescri
126146 .filter (d -> overlaps (descriptorToCheck .v2 (), d .v2 ()))
127147 .collect (Collectors .toList ());
128148 if (descriptorsMatchingThisPattern .isEmpty () == false ) {
129- throw new IllegalStateException ("a system index descriptor [" + descriptorToCheck .v2 () + "] from plugin [" +
149+ throw new IllegalStateException ("a system index descriptor [" + descriptorToCheck .v2 () + "] from [" +
130150 descriptorToCheck .v1 () + "] overlaps with other system index descriptors: [" +
131151 descriptorsMatchingThisPattern .stream ()
132- .map (descriptor -> descriptor .v2 () + " from plugin [" + descriptor .v1 () + "]" )
152+ .map (descriptor -> descriptor .v2 () + " from [" + descriptor .v1 () + "]" )
133153 .collect (Collectors .joining (", " )));
134154 }
135155 });
@@ -140,4 +160,19 @@ private static boolean overlaps(SystemIndexDescriptor a1, SystemIndexDescriptor
140160 Automaton a2Automaton = Regex .simpleMatchToAutomaton (a2 .getIndexPattern ());
141161 return Operations .isEmpty (Operations .intersection (a1Automaton , a2Automaton )) == false ;
142162 }
163+
164+ private static Map <String , Collection <SystemIndexDescriptor >> buildSystemIndexDescriptorMap (
165+ Map <String , Collection <SystemIndexDescriptor >> pluginAndModulesMap ) {
166+ final Map <String , Collection <SystemIndexDescriptor >> map =
167+ new HashMap <>(pluginAndModulesMap .size () + SERVER_SYSTEM_INDEX_DESCRIPTORS .size ());
168+ map .putAll (pluginAndModulesMap );
169+ // put the server items last since we expect less of them
170+ SERVER_SYSTEM_INDEX_DESCRIPTORS .forEach ((source , descriptors ) -> {
171+ if (map .putIfAbsent (source , descriptors ) != null ) {
172+ throw new IllegalArgumentException ("plugin or module attempted to define the same source [" + source +
173+ "] as a built-in system index" );
174+ }
175+ });
176+ return unmodifiableMap (map );
177+ }
143178}
0 commit comments