|
29 | 29 | import org.elasticsearch.xcontent.XContentType;
|
30 | 30 |
|
31 | 31 | import java.io.IOException;
|
| 32 | +import java.util.function.Consumer; |
32 | 33 |
|
33 | 34 | import static org.elasticsearch.test.ESTestCase.TEST_REQUEST_TIMEOUT;
|
| 35 | +import static org.elasticsearch.test.ESTestCase.randomAlphanumericOfLength; |
| 36 | +import static org.elasticsearch.test.ESTestCase.randomFrom; |
34 | 37 | import static org.elasticsearch.test.ESTestCase.safeGet;
|
35 | 38 | import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
|
36 | 39 | import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder;
|
@@ -140,4 +143,86 @@ public static SimulatePipelineRequest jsonSimulatePipelineRequest(String jsonStr
|
140 | 143 | public static SimulatePipelineRequest jsonSimulatePipelineRequest(BytesReference jsonBytes) {
|
141 | 144 | return new SimulatePipelineRequest(ReleasableBytesReference.wrap(jsonBytes), XContentType.JSON);
|
142 | 145 | }
|
| 146 | + |
| 147 | + /** |
| 148 | + * Executes an action against an ingest document using a random access pattern. A synthetic pipeline instance with the provided |
| 149 | + * access pattern is created and executed against the ingest document, thus updating its internal access pattern. |
| 150 | + * @param document The document to operate on |
| 151 | + * @param action A consumer which takes the updated ingest document during execution |
| 152 | + * @throws Exception Any exception thrown from the provided consumer |
| 153 | + */ |
| 154 | + public static void doWithRandomAccessPattern(IngestDocument document, Consumer<IngestDocument> action) throws Exception { |
| 155 | + doWithAccessPattern(randomFrom(IngestPipelineFieldAccessPattern.values()), document, action); |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Executes an action against an ingest document using a random access pattern. A synthetic pipeline instance with the provided |
| 160 | + * access pattern is created and executed against the ingest document, thus updating its internal access pattern. |
| 161 | + * @param accessPattern The access pattern to use when executing the block of code |
| 162 | + * @param document The document to operate on |
| 163 | + * @param action A consumer which takes the updated ingest document during execution |
| 164 | + * @throws Exception Any exception thrown from the provided consumer |
| 165 | + */ |
| 166 | + public static void doWithAccessPattern( |
| 167 | + IngestPipelineFieldAccessPattern accessPattern, |
| 168 | + IngestDocument document, |
| 169 | + Consumer<IngestDocument> action |
| 170 | + ) throws Exception { |
| 171 | + runWithAccessPattern(accessPattern, document, new TestProcessor(action)); |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Executes a processor against an ingest document using a random access pattern. A synthetic pipeline instance with the provided |
| 176 | + * access pattern is created and executed against the ingest document, thus updating its internal access pattern. |
| 177 | + * @param document The document to operate on |
| 178 | + * @param processor A processor which takes the updated ingest document during execution |
| 179 | + * @return the resulting ingest document instance |
| 180 | + * @throws Exception Any exception thrown from the provided consumer |
| 181 | + */ |
| 182 | + public static IngestDocument runWithRandomAccessPattern(IngestDocument document, Processor processor) throws Exception { |
| 183 | + return runWithAccessPattern(randomFrom(IngestPipelineFieldAccessPattern.values()), document, processor); |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Executes a processor against an ingest document using the provided access pattern. A synthetic pipeline instance with the provided |
| 188 | + * access pattern is created and executed against the ingest document, thus updating its internal access pattern. |
| 189 | + * @param accessPattern The access pattern to use when executing the block of code |
| 190 | + * @param document The document to operate on |
| 191 | + * @param processor A processor which takes the updated ingest document during execution |
| 192 | + * @return the resulting ingest document instance |
| 193 | + * @throws Exception Any exception thrown from the provided consumer |
| 194 | + */ |
| 195 | + public static IngestDocument runWithAccessPattern( |
| 196 | + IngestPipelineFieldAccessPattern accessPattern, |
| 197 | + IngestDocument document, |
| 198 | + Processor processor |
| 199 | + ) throws Exception { |
| 200 | + IngestDocument[] ingestDocumentHolder = new IngestDocument[1]; |
| 201 | + Exception[] exceptionHolder = new Exception[1]; |
| 202 | + document.executePipeline( |
| 203 | + new Pipeline( |
| 204 | + randomAlphanumericOfLength(10), |
| 205 | + null, |
| 206 | + null, |
| 207 | + null, |
| 208 | + new CompoundProcessor(processor), |
| 209 | + accessPattern, |
| 210 | + null, |
| 211 | + null, |
| 212 | + null |
| 213 | + ), |
| 214 | + (result, ex) -> { |
| 215 | + ingestDocumentHolder[0] = result; |
| 216 | + exceptionHolder[0] = ex; |
| 217 | + } |
| 218 | + ); |
| 219 | + Exception exception = exceptionHolder[0]; |
| 220 | + if (exception != null) { |
| 221 | + if (exception instanceof IngestProcessorException ingestProcessorException) { |
| 222 | + exception = ((Exception) ingestProcessorException.getCause()); |
| 223 | + } |
| 224 | + throw exception; |
| 225 | + } |
| 226 | + return ingestDocumentHolder[0]; |
| 227 | + } |
143 | 228 | }
|
0 commit comments