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
3941import static java .util .stream .Collectors .toUnmodifiableList ;
42+ import static org .elasticsearch .tasks .TaskResultsService .TASK_INDEX ;
4043
4144/**
4245 * This class holds the {@link SystemIndexDescriptor} objects that represent system indices the
4548 */
4649public class SystemIndices {
4750
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+
4855 private final CharacterRunAutomaton runAutomaton ;
4956 private final Collection <SystemIndexDescriptor > systemIndexDescriptors ;
5057
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 ()
5462 .stream ()
5563 .flatMap (Collection ::stream )
5664 .collect (Collectors .toUnmodifiableList ());
@@ -63,7 +71,16 @@ public SystemIndices(Map<String, Collection<SystemIndexDescriptor>> systemIndexD
6371 * @return true if the {@link Index}'s name matches a pattern from a {@link SystemIndexDescriptor}
6472 */
6573 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 );
6784 }
6885
6986 /**
@@ -126,10 +143,10 @@ static void checkForOverlappingPatterns(Map<String, Collection<SystemIndexDescri
126143 .filter (d -> overlaps (descriptorToCheck .v2 (), d .v2 ()))
127144 .collect (Collectors .toUnmodifiableList ());
128145 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 [" +
130147 descriptorToCheck .v1 () + "] overlaps with other system index descriptors: [" +
131148 descriptorsMatchingThisPattern .stream ()
132- .map (descriptor -> descriptor .v2 () + " from plugin [" + descriptor .v1 () + "]" )
149+ .map (descriptor -> descriptor .v2 () + " from [" + descriptor .v1 () + "]" )
133150 .collect (Collectors .joining (", " )));
134151 }
135152 });
@@ -140,4 +157,19 @@ private static boolean overlaps(SystemIndexDescriptor a1, SystemIndexDescriptor
140157 Automaton a2Automaton = Regex .simpleMatchToAutomaton (a2 .getIndexPattern ());
141158 return Operations .isEmpty (Operations .intersection (a1Automaton , a2Automaton )) == false ;
142159 }
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+ }
143175}
0 commit comments