void disposeSearcher() throws IOException { if ( !searcherIsClosed ) { searcher.getIndexReader().close(); searcherIsClosed = true; } }
public LucenePartitionAllDocumentsReader( PartitionSearcher partitionSearcher ) { this.partitionSearcher = partitionSearcher; this.searcher = partitionSearcher.getIndexSearcher(); this.reader = searcher.getIndexReader(); }
private static void closeSearcher(IndexSearcher searcher) { IndexReader indexReader = searcher.getIndexReader(); try { searcher.close(); } catch(IOException ignore) {/**/} try { indexReader.close(); } catch(IOException ignore) {/**/} } }
private int maxDoc() throws IOException { return searcher.getIndexReader().maxDoc(); } }
private int maxDoc() throws IOException { return searcher.getIndexReader().maxDoc(); } }
@Override public synchronized void close() throws IOException { searcher.getIndexReader().decRef(); } }
/** Release a searcher previously obtained from {@link * #acquire}. * * <p><b>NOTE</b>: it's fine to call this after close. */ public void release(IndexSearcher s) throws IOException { s.getIndexReader().decRef(); }
public SearcherTracker(IndexSearcher searcher) { this.searcher = searcher; version = ((DirectoryReader) searcher.getIndexReader()).getVersion(); searcher.getIndexReader().incRef(); // Use nanoTime not currentTimeMillis since it [in // theory] reduces risk from clock shift recordTimeSec = System.nanoTime() / NANOS_PER_SEC; }
@Override int size(){ setIndexReaderSearcher(); return searcher.getIndexReader().numDocs(); }
@BeforeEach void setUp() { when( searcher.getIndexReader() ).thenReturn( reader ); }
@BeforeEach void setUp() { when( searcher.getIndexReader() ).thenReturn( reader ); }
private ResourceIterator<File> listReadOnlyStoreFiles() throws IOException { // In read-only mode we don't need to take a snapshot, because the index will not be modified. final Collection<File> files = new ArrayList<>(); makeSureAllIndexesAreInstantiated(); for ( IndexReference index : getAllIndexes() ) { File indexDirectory = getFileDirectory( baseStorePath, index.getIdentifier() ); IndexSearcher searcher = index.getSearcher(); try ( IndexReader indexReader = searcher.getIndexReader() ) { DirectoryReader directoryReader = (DirectoryReader) indexReader; IndexCommit commit = directoryReader.getIndexCommit(); for ( String fileName : commit.getFileNames() ) { files.add( new File( indexDirectory, fileName ) ); } } } return Iterators.asResourceIterator( files.iterator() ); }
private static PartitionSearcher createPartitionSearcher( int maxDoc, int partition, int maxSize ) throws IOException { PartitionSearcher partitionSearcher = mock( PartitionSearcher.class ); IndexSearcher indexSearcher = mock( IndexSearcher.class ); IndexReader indexReader = mock( IndexReader.class ); when(partitionSearcher.getIndexSearcher()).thenReturn( indexSearcher ); when( indexSearcher.getIndexReader() ).thenReturn( indexReader ); when( indexReader.maxDoc() ).thenReturn( maxDoc ); when( indexSearcher.doc( 0 ) ).thenReturn( createDocument( uniqueDocValue( 1, partition, maxSize ) ) ); when( indexSearcher.doc( 1 ) ).thenReturn( createDocument( uniqueDocValue( 2, partition, maxSize ) ) ); when( indexSearcher.doc( 2 ) ).thenReturn( createDocument( uniqueDocValue( 3, partition, maxSize ) ) ); return partitionSearcher; }
@Override protected IndexSample performSampling() throws IndexNotFoundKernelException { UniqueIndexSampler sampler = new UniqueIndexSampler(); sampler.increment( indexSearcher.getIndexReader().numDocs() ); checkCancellation(); return sampler.result(); } }
@Override IndexReference refresh( IndexReference indexReference ) throws ExplicitIndexNotFoundKernelException { try { DirectoryReader reader = (DirectoryReader) indexReference.getSearcher().getIndexReader(); IndexWriter writer = indexReference.getWriter(); IndexReader reopened = DirectoryReader.openIfChanged( reader, writer ); if ( reopened != null ) { IndexSearcher newSearcher = newIndexSearcher( indexReference.getIdentifier(), reopened ); indexReference.detachOrClose(); return new WritableIndexReference( indexReference.getIdentifier(), newSearcher, writer ); } return indexReference; } catch ( IOException e ) { throw new UncheckedIOException( e ); } }
@Test void uniqueSamplingUseDocumentsNumber() throws IndexNotFoundKernelException { when( indexSearcher.getIndexReader().numDocs() ).thenReturn( 17 ); UniqueLuceneIndexSampler sampler = new UniqueLuceneIndexSampler( indexSearcher, taskControl.newInstance() ); IndexSample sample = sampler.sampleIndex(); assertEquals( 17, sample.indexSize() ); }
@Test void uniqueSamplingCancel() { when( indexSearcher.getIndexReader().numDocs() ).thenAnswer( invocation -> { taskControl.cancel(); return 17; } ); UniqueLuceneIndexSampler sampler = new UniqueLuceneIndexSampler( indexSearcher, taskControl.newInstance() ); IndexNotFoundKernelException notFoundKernelException = assertThrows( IndexNotFoundKernelException.class, sampler::sampleIndex ); assertEquals( notFoundKernelException.getMessage(), "Index dropped while sampling." ); }
@Test void createWritablePartition() throws Exception { try ( AbstractIndexPartition indexPartition = new WritableIndexPartitionFactory( IndexWriterConfigs::standard ) .createPartition( testDirectory.directory(), directory ) ) { try ( IndexWriter indexWriter = indexPartition.getIndexWriter() ) { indexWriter.addDocument( new Document() ); indexWriter.commit(); indexPartition.maybeRefreshBlocking(); try ( PartitionSearcher searcher = indexPartition.acquireSearcher() ) { assertEquals( 1, searcher.getIndexSearcher().getIndexReader().numDocs(), "We should be able to see newly added document " ); } } } }
@Test void nonUniqueSamplingCancel() throws IOException { Terms terms = getTerms( "test", 1 ); Map<String,Terms> fieldTermsMap = MapUtil.genericMap( "0string", terms, "id", terms, "0string", terms ); IndexReaderStub indexReader = new IndexReaderStub( new SamplingFields( fieldTermsMap ) ); when( indexSearcher.getIndexReader() ).thenReturn( indexReader ); NonUniqueLuceneIndexSampler luceneIndexSampler = createSampler(); taskControl.cancel(); IndexNotFoundKernelException notFoundKernelException = assertThrows( IndexNotFoundKernelException.class, luceneIndexSampler::sampleIndex ); assertEquals( notFoundKernelException.getMessage(), "Index dropped while sampling." ); }
@Test void nonUniqueIndexSampling() throws Exception { Terms aTerms = getTerms( "a", 1 ); Terms idTerms = getTerms( "id", 2 ); Terms bTerms = getTerms( "b", 3 ); Map<String,Terms> fieldTermsMap = MapUtil.genericMap( "0string", aTerms, "id", idTerms, "0array", bTerms ); IndexReaderStub indexReader = new IndexReaderStub( new SamplingFields( fieldTermsMap ) ); indexReader.setElements( new String[4] ); when( indexSearcher.getIndexReader() ).thenReturn( indexReader ); assertEquals( new IndexSample( 4, 2, 4 ), createSampler().sampleIndex() ); }