-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Processor.java
182 lines (157 loc) · 5.9 KB
/
Processor.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
/*
* 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.
*/
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
package org.opensearch.ingest;
import org.opensearch.client.Client;
import org.opensearch.common.util.concurrent.ThreadContext;
import org.opensearch.env.Environment;
import org.opensearch.index.analysis.AnalysisRegistry;
import org.opensearch.script.ScriptService;
import org.opensearch.threadpool.Scheduler;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.LongSupplier;
/**
* A processor implementation may modify the data belonging to a document.
* Whether changes are made and what exactly is modified is up to the implementation.
*
* Processors may get called concurrently and thus need to be thread-safe.
*
* @opensearch.internal
*/
public interface Processor {
/**
* Introspect and potentially modify the incoming data.
*
* Expert method: only override this method if a processor implementation needs to make an asynchronous call,
* otherwise just overwrite {@link #execute(IngestDocument)}.
*/
default void execute(IngestDocument ingestDocument, BiConsumer<IngestDocument, Exception> handler) {
final IngestDocument result;
try {
result = execute(ingestDocument);
} catch (Exception e) {
handler.accept(null, e);
return;
}
handler.accept(result, null);
}
/**
* Introspect and potentially modify the incoming data.
*
* @return If <code>null</code> is returned then the current document will be dropped and not be indexed,
* otherwise this document will be kept and indexed
*/
IngestDocument execute(IngestDocument ingestDocument) throws Exception;
/**
* Gets the type of a processor
*/
String getType();
/**
* Gets the tag of a processor.
*/
String getTag();
/**
* Gets the description of a processor.
*/
String getDescription();
/**
* A factory that knows how to construct a processor based on a map of maps.
*/
interface Factory {
/**
* Creates a processor based on the specified map of maps config.
* @param processorFactories Other processors which may be created inside this processor
* @param tag The tag for the processor
* @param description A short description of what this processor does
* @param config The configuration for the processor
*
* <b>Note:</b> Implementations are responsible for removing the used configuration keys, so that after
*/
Processor create(Map<String, Factory> processorFactories, String tag, String description, Map<String, Object> config)
throws Exception;
}
/**
* Infrastructure class that holds services that can be used by processor factories to create processor instances
* and that gets passed around to all {@link org.opensearch.plugins.IngestPlugin}s.
*/
class Parameters {
/**
* Useful to provide access to the node's environment like config directory to processor factories.
*/
public final Environment env;
/**
* Provides processors script support.
*/
public final ScriptService scriptService;
/**
* Provide analyzer support
*/
public final AnalysisRegistry analysisRegistry;
/**
* Allows processors to read headers set by {@link org.opensearch.action.support.ActionFilter}
* instances that have run prior to in ingest.
*/
public final ThreadContext threadContext;
public final LongSupplier relativeTimeSupplier;
public final IngestService ingestService;
public final Consumer<Runnable> genericExecutor;
/**
* Provides scheduler support
*/
public final BiFunction<Long, Runnable, Scheduler.ScheduledCancellable> scheduler;
/**
* Provides access to the node client
*/
public final Client client;
public Parameters(
Environment env,
ScriptService scriptService,
AnalysisRegistry analysisRegistry,
ThreadContext threadContext,
LongSupplier relativeTimeSupplier,
BiFunction<Long, Runnable, Scheduler.ScheduledCancellable> scheduler,
IngestService ingestService,
Client client,
Consumer<Runnable> genericExecutor
) {
this.env = env;
this.scriptService = scriptService;
this.threadContext = threadContext;
this.analysisRegistry = analysisRegistry;
this.relativeTimeSupplier = relativeTimeSupplier;
this.scheduler = scheduler;
this.ingestService = ingestService;
this.client = client;
this.genericExecutor = genericExecutor;
}
}
}