public MockEngineSupport(EngineConfig config, Class<? extends FilterDirectoryReader> wrapper) { Settings settings = config.getIndexSettings().getSettings(); shardId = config.getShardId(); filterCache = config.getQueryCache(); filterCachingPolicy = config.getQueryCachingPolicy(); final long seed = config.getIndexSettings().getValue(ESIntegTestCase.INDEX_TEST_SEED_SETTING); Random random = new Random(seed); final double ratio = WRAP_READER_RATIO.get(settings); boolean wrapReader = random.nextDouble() < ratio; if (logger.isTraceEnabled()) { logger.trace("Using [{}] for shard [{}] seed: [{}] wrapReader: [{}]", this.getClass().getName(), shardId, seed, wrapReader); } mockContext = new MockContext(random, wrapReader, wrapper, settings); this.searcherCloseable = new SearcherCloseable(); LuceneTestCase.closeAfterSuite(searcherCloseable); // only one suite closeable per Engine this.disableFlushOnClose = DISABLE_FLUSH_ON_CLOSE.get(settings); }
public FieldMaskingReader(String field, DirectoryReader in) throws IOException { super(in, new FilterDirectoryReader.SubReaderWrapper() { @Override public LeafReader wrap(LeafReader reader) { return new FieldFilterLeafReader(reader, Collections.singleton(field), true); } }); this.field = field; }
/** * Tries to unwrap the given reader until the first {@link ElasticsearchDirectoryReader} instance is found or <code>null</code> if no instance is found; */ public static ElasticsearchDirectoryReader getElasticsearchDirectoryReader(DirectoryReader reader) { if (reader instanceof FilterDirectoryReader) { if (reader instanceof ElasticsearchDirectoryReader) { return (ElasticsearchDirectoryReader) reader; } else { // We need to use FilterDirectoryReader#getDelegate and not FilterDirectoryReader#unwrap, because // If there are multiple levels of filtered leaf readers then with the unwrap() method it immediately // returns the most inner leaf reader and thus skipping of over any other filtered leaf reader that // may be instance of ElasticsearchLeafReader. This can cause us to miss the shardId. return getElasticsearchDirectoryReader(((FilterDirectoryReader) reader).getDelegate()); } } return null; }
/** * Expert: creates a searcher from the provided {@link IndexReader} using the provided {@link * SearcherFactory}. NOTE: this decRefs incoming reader on throwing an exception. */ @SuppressWarnings("resource") public static IndexSearcher getSearcher(SearcherFactory searcherFactory, IndexReader reader) throws IOException { boolean success = false; final IndexSearcher searcher; try { searcher = searcherFactory.newSearcher(reader, null); // Modification for Gerrit: Allow searcherFactory to transitively wrap the // provided reader. IndexReader unwrapped = searcher.getIndexReader(); while (true) { if (unwrapped == reader) { break; } else if (unwrapped instanceof FilterDirectoryReader) { unwrapped = ((FilterDirectoryReader) unwrapped).getDelegate(); } else if (unwrapped instanceof FilterLeafReader) { unwrapped = ((FilterLeafReader) unwrapped).getDelegate(); } else { break; } } if (unwrapped != reader) { throw new IllegalStateException( "SearcherFactory must wrap the provided reader (got " + searcher.getIndexReader() + " but expected " + reader + ")"); } success = true; } finally { if (!success) { reader.decRef(); } } return searcher; }
protected Class<? extends FilterDirectoryReader> getReaderWrapperClass() { return AssertingDirectoryReader.class; }
MockInternalEngine(EngineConfig config, Class<? extends FilterDirectoryReader> wrapper) throws EngineException { super(config); randomizeFlushOnClose = config.getIndexSettings().isOnSharedFilesystem() == false; wrapperClass = wrapper; }
MockShadowEngine(EngineConfig config, Class<? extends FilterDirectoryReader> wrapper) { super(config); this.support = new MockEngineSupport(config, wrapper); }
public MockEngineFactory(Class<? extends FilterDirectoryReader> wrapper) { this.wrapper = wrapper; }
public MockContext(Random random, boolean wrapReader, Class<? extends FilterDirectoryReader> wrapper, Settings indexSettings) { this.random = random; this.wrapReader = wrapReader; this.wrapper = wrapper; this.indexSettings = indexSettings; }
private ElasticsearchDirectoryReader(DirectoryReader in, FilterDirectoryReader.SubReaderWrapper wrapper, ShardId shardId) throws IOException { super(in, wrapper); this.wrapper = wrapper; this.shardId = shardId; }
@Override protected Class<? extends FilterDirectoryReader> getReaderWrapperClass() { return RandomExceptionDirectoryReaderWrapper.class; }