Java Code Examples for org.apache.lucene.search.SearcherManager#release()

The following examples show how to use org.apache.lucene.search.SearcherManager#release() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Engine.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
/**
 * Read the last segments info from the commit pointed to by the searcher manager
 */
protected static SegmentInfos readLastCommittedSegmentInfos(final SearcherManager sm, final Store store) throws IOException {
    IndexSearcher searcher = sm.acquire();
    try {
        IndexCommit latestCommit = ((DirectoryReader) searcher.getIndexReader()).getIndexCommit();
        return Lucene.readSegmentInfos(latestCommit);
    } catch (IOException e) {
        // Fall back to reading from the store if reading from the commit fails
        try {
            return store. readLastCommittedSegmentsInfo();
        } catch (IOException e2) {
            e2.addSuppressed(e);
            throw e2;
        }
    } finally {
        sm.release(searcher);
    }
}
 
Example 2
Source File: AnalyzingInfixSuggester.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public long getCount() throws IOException {
  if (searcherMgr == null) {
    return 0;
  }
  SearcherManager mgr;
  IndexSearcher searcher;
  synchronized (searcherMgrLock) {
    mgr = searcherMgr; // acquire & release on same SearcherManager, via local reference
    searcher = mgr.acquire();
  }
  try {
    return searcher.getIndexReader().numDocs();
  } finally {
    mgr.release(searcher);
  }
}
 
Example 3
Source File: AnalyzingInfixSuggester.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public long ramBytesUsed() {
  long mem = RamUsageEstimator.shallowSizeOf(this);
  try {
    if (searcherMgr != null) {
      SearcherManager mgr;
      IndexSearcher searcher;
      synchronized (searcherMgrLock) {
        mgr = searcherMgr; // acquire & release on same SearcherManager, via local reference
        searcher = mgr.acquire();
      }
      try {
        for (LeafReaderContext context : searcher.getIndexReader().leaves()) {
          LeafReader reader = FilterLeafReader.unwrap(context.reader());
          if (reader instanceof SegmentReader) {
            mem += ((SegmentReader) context.reader()).ramBytesUsed();
          }
        }
      } finally {
        mgr.release(searcher);
      }
    }
    return mem;
  } catch (IOException ioe) {
    throw new RuntimeException(ioe);
  }
}
 
Example 4
Source File: AnalyzingInfixSuggester.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<Accountable> getChildResources() {
  List<Accountable> resources = new ArrayList<>();
  try {
    if (searcherMgr != null) {
      SearcherManager mgr;
      IndexSearcher searcher;
      synchronized (searcherMgrLock) {
        mgr = searcherMgr; // acquire & release on same SearcherManager, via local reference
        searcher = mgr.acquire();
      }
      try {
        for (LeafReaderContext context : searcher.getIndexReader().leaves()) {
          LeafReader reader = FilterLeafReader.unwrap(context.reader());
          if (reader instanceof SegmentReader) {
            resources.add(Accountables.namedAccountable("segment", (SegmentReader)reader));
          }
        }
      } finally {
        mgr.release(searcher);
      }
    }
    return Collections.unmodifiableList(resources);
  } catch (IOException ioe) {
    throw new RuntimeException(ioe);
  }
}
 
Example 5
Source File: DefaultSearchManager.java    From onedev with MIT License 4 votes vote down vote up
@Override
public List<QueryHit> search(Project project, ObjectId commit, final BlobQuery query) 
		throws InterruptedException {
	List<QueryHit> hits = new ArrayList<>();

	SearcherManager searcherManager = getSearcherManager(project.getForkRoot());
	if (searcherManager != null) {
		try {
			final IndexSearcher searcher = searcherManager.acquire();
			try {
				try (RevWalk revWalk = new RevWalk(project.getRepository())){
					final RevTree revTree = revWalk.parseCommit(commit).getTree();
					final Set<String> checkedBlobPaths = new HashSet<>();
					
					searcher.search(query.asLuceneQuery(), new SimpleCollector() {

						private BinaryDocValues blobPathValues;
						
						@Override
						public void collect(int doc) throws IOException {
							if (hits.size() < query.getCount() && !Thread.currentThread().isInterrupted()) {
								Preconditions.checkState(blobPathValues.advanceExact(doc));
								String blobPath = blobPathValues.binaryValue().utf8ToString();
								
								if (!checkedBlobPaths.contains(blobPath)) {
									TreeWalk treeWalk = TreeWalk.forPath(project.getRepository(), blobPath, revTree);									
									if (treeWalk != null)
										query.collect(searcher, treeWalk, hits);
									checkedBlobPaths.add(blobPath);
								}
							}
						}

						@Override
						protected void doSetNextReader(LeafReaderContext context) throws IOException {
							blobPathValues  = context.reader().getBinaryDocValues(FieldConstants.BLOB_PATH.name());
						}

						@Override
						public boolean needsScores() {
							return false;
						}

					});
				}
			} finally {
				searcherManager.release(searcher);
			}
		} catch (IOException e) {
			throw ExceptionUtils.unchecked(e);
		}
	}
	if (Thread.interrupted())
		throw new InterruptedException();

	return hits;
}