DefaultIndexReaderFactory implements LuceneIndexReaderFactory { @Override public List<LuceneIndexReader> createReaders(LuceneIndexDefinition definition, NodeState defnState, String indexPath) throws IOException { if (!mountInfoProvider.hasNonDefaultMounts()) { LuceneIndexReader reader = createReader(definition, defnState, indexPath, FulltextIndexConstants.INDEX_DATA_CHILD_NAME, SUGGEST_DATA_CHILD_NAME); return reader != null ? ImmutableList.of(reader) : Collections.<LuceneIndexReader>emptyList(); } else { return createMountedReaders(definition, defnState, indexPath); } } DefaultIndexReaderFactory(MountInfoProvider mountInfoProvider, @Nullable IndexCopier cloner); @Override List<LuceneIndexReader> createReaders(LuceneIndexDefinition definition, NodeState defnState,
                                                 String indexPath); @Override MountInfoProvider getMountInfoProvider();  }
@Test public void emptyDir() throws Exception{ LuceneIndexReaderFactory factory = new DefaultIndexReaderFactory(mip, null); List<LuceneIndexReader> readers = factory.createReaders(defn, EMPTY_NODE,"/foo"); assertTrue(readers.isEmpty()); }
@Test public void indexDir() throws Exception{ LuceneIndexWriterFactory factory = newDirectoryFactory(); LuceneIndexWriter writer = factory.newInstance(defn, builder, null, true); writer.updateDocument("/content/en", newDoc("/content/en")); writer.close(0); LuceneIndexReaderFactory readerFactory = new DefaultIndexReaderFactory(mip, null); List<LuceneIndexReader> readers = readerFactory.createReaders(defn, builder.getNodeState(),"/foo"); assertEquals(1, readers.size()); LuceneIndexReader reader = readers.get(0); assertNotNull(reader.getReader()); assertNull(reader.getSuggestDirectory()); assertNull(reader.getLookup()); assertEquals(1, reader.getReader().numDocs()); final AtomicBoolean closed = new AtomicBoolean(); reader.getReader().addReaderClosedListener(reader1 -> closed.set(true)); reader.close(); assertTrue(closed.get()); }
@Test public void indexDirWithBlobStore() throws Exception { CachingFileDataStore ds = DataStoreUtils .createCachingFDS(folder.newFolder().getAbsolutePath(), folder.newFolder().getAbsolutePath()); DirectoryFactory directoryFactory = new DefaultDirectoryFactory(null, new DataStoreBlobStore(ds)); LuceneIndexWriterFactory factory = new DefaultIndexWriterFactory(mip, directoryFactory, writerConfig); LuceneIndexWriter writer = factory.newInstance(defn, builder, null, true); writer.updateDocument("/content/en", newDoc("/content/en")); writer.close(0); LuceneIndexReaderFactory readerFactory = new DefaultIndexReaderFactory(mip, null); List<LuceneIndexReader> readers = readerFactory.createReaders(defn, builder.getNodeState(),"/foo"); assertEquals(1, readers.size()); LuceneIndexReader reader = readers.get(0); assertNotNull(reader.getReader()); assertNull(reader.getSuggestDirectory()); assertNull(reader.getLookup()); assertEquals(1, reader.getReader().numDocs()); final AtomicBoolean closed = new AtomicBoolean(); reader.getReader().addReaderClosedListener(reader1 -> closed.set(true)); reader.close(); assertTrue(closed.get()); }
@Test public void suggesterDir() throws Exception{ LuceneIndexWriterFactory factory = newDirectoryFactory(); enabledSuggestorForSomeProp(); defn = new LuceneIndexDefinition(root, builder.getNodeState(), "/foo"); LuceneIndexWriter writer = factory.newInstance(defn, builder, null, true); Document doc = newDoc("/content/en"); doc.add(new StringField(FieldNames.SUGGEST, "test", null)); writer.updateDocument("/content/en", doc); writer.close(0); LuceneIndexReaderFactory readerFactory = new DefaultIndexReaderFactory(mip, null); List<LuceneIndexReader> readers = readerFactory.createReaders(defn, builder.getNodeState(),"/foo"); LuceneIndexReader reader = readers.get(0); assertNotNull(reader.getReader()); assertNotNull(reader.getSuggestDirectory()); assertNotNull(reader.getLookup()); }
@Test public void multipleReaders() throws Exception{ LuceneIndexWriterFactory factory = newDirectoryFactory(); LuceneIndexWriter writer = factory.newInstance(defn, builder, null, true); writer.updateDocument("/content/en", newDoc("/content/en")); writer.updateDocument("/libs/config", newDoc("/libs/config")); writer.close(0); LuceneIndexReaderFactory readerFactory = new DefaultIndexReaderFactory(mip, null); List<LuceneIndexReader> readers = readerFactory.createReaders(defn, builder.getNodeState(),"/foo"); assertEquals(2, readers.size()); }
@Test public void multipleReaders_SingleSuggester() throws Exception{ LuceneIndexWriterFactory factory = newDirectoryFactory(); enabledSuggestorForSomeProp(); defn = new LuceneIndexDefinition(root, builder.getNodeState(), "/foo"); LuceneIndexWriter writer = factory.newInstance(defn, builder, null, true); Document doc = newDoc("/content/en"); doc.add(new StringField(FieldNames.SUGGEST, "test", null)); writer.updateDocument("/content/en", doc); writer.updateDocument("/libs/config", newDoc("/libs/config")); writer.close(0); LuceneIndexReaderFactory readerFactory = new DefaultIndexReaderFactory(mip, null); List<LuceneIndexReader> readers = readerFactory.createReaders(defn, builder.getNodeState(),"/foo"); for (LuceneIndexReader reader : readers){ assertNotNull(reader.getReader()); assertNotNull(reader.getSuggestDirectory()); assertNotNull(reader.getLookup()); } }