List of usage examples for org.apache.lucene.index IndexReader decRef
@SuppressWarnings("try") public final void decRef() throws IOException
From source file:com.bugull.mongo.lucene.backend.IndexReopenTask.java
License:Apache License
@Override public void run() { BuguIndex index = BuguIndex.getInstance(); if (index.isReopening()) { return;/*from w w w . ja v a 2 s .c om*/ } index.setReopening(true); IndexSearcherCache searcherCache = IndexSearcherCache.getInstance(); Map<String, IndexSearcher> map = searcherCache.getAll(); for (Entry<String, IndexSearcher> entry : map.entrySet()) { IndexSearcher searcher = entry.getValue(); IndexReader reader = searcher.getIndexReader(); IndexReader newReader = null; try { newReader = IndexReader.openIfChanged(reader, true); } catch (IOException ex) { logger.error("Something is wrong when reopen the Lucene IndexReader", ex); } if (newReader != null && newReader != reader) { try { reader.decRef(); } catch (IOException ex) { logger.error("Something is wrong when decrease the reference of the lucene IndexReader", ex); } IndexSearcher newSearcher = new IndexSearcher(newReader); searcherCache.put(entry.getKey(), newSearcher); } } index.setReopening(false); }
From source file:com.google.gerrit.lucene.WrappableSearcherManager.java
License:Apache License
/** Expert: creates a searcher from the provided {@link * IndexReader} using the provided {@link * SearcherFactory}. NOTE: this decRefs incoming reader * on throwing an exception. */// w w w.ja va2 s .co m @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; }
From source file:com.nearinfinity.blur.manager.IndexManager.java
License:Apache License
public void fetchRow(String table, Selector selector, FetchResult fetchResult) throws BlurException { validSelector(selector);//from www . ja v a 2 s. co m BlurIndex index; try { if (selector.getLocationId() == null) { populateSelector(table, selector); } String locationId = selector.getLocationId(); if (locationId.equals(NOT_FOUND)) { fetchResult.setDeleted(false); fetchResult.setExists(false); return; } String shard = getShard(locationId); Map<String, BlurIndex> blurIndexes = _indexServer.getIndexes(table); if (blurIndexes == null) { LOG.error("Table [{0}] not found", table); throw new BlurException("Table [" + table + "] not found", null); } index = blurIndexes.get(shard); if (index == null) { if (index == null) { LOG.error("Shard [{0}] not found in table [{1}]", shard, table); throw new BlurException("Shard [" + shard + "] not found in table [" + table + "]", null); } } } catch (BlurException e) { throw e; } catch (Exception e) { LOG.error("Unknown error while trying to get the correct index reader for selector [{0}].", e, selector); throw new BException(e.getMessage(), e); } IndexReader reader = null; try { reader = index.getIndexReader(); fetchRow(reader, table, selector, fetchResult); if (_blurMetrics != null) { if (fetchResult.rowResult != null) { if (fetchResult.rowResult.row != null && fetchResult.rowResult.row.records != null) { _blurMetrics.recordReads.addAndGet(fetchResult.rowResult.row.records.size()); } _blurMetrics.rowReads.incrementAndGet(); } else if (fetchResult.recordResult != null) { _blurMetrics.recordReads.incrementAndGet(); } } } catch (Exception e) { LOG.error("Unknown error while trying to fetch row.", e); throw new BException(e.getMessage(), e); } finally { if (reader != null) { // this will allow for closing of index try { reader.decRef(); } catch (IOException e) { LOG.error("Unknown error trying to call decRef on reader [{0}]", e, reader); } } } }
From source file:com.nearinfinity.blur.manager.IndexManager.java
License:Apache License
private void populateSelector(String table, Selector selector) throws IOException, BlurException { String rowId = selector.rowId; String recordId = selector.recordId; String shardName = MutationHelper.getShardName(table, rowId, getNumberOfShards(table), _blurPartitioner); Map<String, BlurIndex> indexes = _indexServer.getIndexes(table); BlurIndex blurIndex = indexes.get(shardName); if (blurIndex == null) { throw new BlurException("Shard [" + shardName + "] is not being servered by this shardserver.", null); }/*from ww w .j a va 2s.co m*/ IndexReader reader = blurIndex.getIndexReader(); try { IndexSearcher searcher = new IndexSearcher(reader); BooleanQuery query = new BooleanQuery(); if (selector.recordOnly) { query.add(new TermQuery(new Term(RECORD_ID, recordId)), Occur.MUST); query.add(new TermQuery(new Term(ROW_ID, rowId)), Occur.MUST); } else { query.add(new TermQuery(new Term(ROW_ID, rowId)), Occur.MUST); query.add(new TermQuery(BlurConstants.PRIME_DOC_TERM), Occur.MUST); } TopDocs topDocs = searcher.search(query, 1); if (topDocs.totalHits > 1) { if (selector.recordOnly) { LOG.warn("Rowid [" + rowId + "], recordId [" + recordId + "] has more than one prime doc that is not deleted."); } else { LOG.warn("Rowid [" + rowId + "] has more than one prime doc that is not deleted."); } } if (topDocs.totalHits == 1) { selector.setLocationId(shardName + "/" + topDocs.scoreDocs[0].doc); } else { selector.setLocationId(NOT_FOUND); } } finally { // this will allow for closing of index reader.decRef(); } }
From source file:com.nearinfinity.blur.manager.IndexManager.java
License:Apache License
public long recordFrequency(final String table, final String columnFamily, final String columnName, final String value) throws Exception { Map<String, BlurIndex> blurIndexes; try {//from w ww .j a va2 s . c o m blurIndexes = _indexServer.getIndexes(table); } catch (IOException e) { LOG.error("Unknown error while trying to fetch index readers.", e); throw new BException(e.getMessage(), e); } return ForkJoin .execute(_executor, blurIndexes.entrySet(), new ParallelCall<Entry<String, BlurIndex>, Long>() { @Override public Long call(Entry<String, BlurIndex> input) throws Exception { BlurIndex index = input.getValue(); IndexReader reader = index.getIndexReader(); try { return recordFrequency(reader, columnFamily, columnName, value); } finally { // this will allow for closing of index reader.decRef(); } } }).merge(new Merger<Long>() { @Override public Long merge(BlurExecutorCompletionService<Long> service) throws BlurException { long total = 0; while (service.getRemainingCount() > 0) { Future<Long> future = service.poll(_defaultParallelCallTimeout, TimeUnit.MILLISECONDS, true, table, columnFamily, columnName, value); total += service.getResultThrowException(future, table, columnFamily, columnName, value); } return total; } }); }
From source file:com.nearinfinity.blur.manager.IndexManager.java
License:Apache License
public List<String> terms(final String table, final String columnFamily, final String columnName, final String startWith, final short size) throws Exception { Map<String, BlurIndex> blurIndexes; try {/*from w w w. j av a 2s . co m*/ blurIndexes = _indexServer.getIndexes(table); } catch (IOException e) { LOG.error("Unknown error while trying to fetch index readers.", e); throw new BException(e.getMessage(), e); } return ForkJoin.execute(_executor, blurIndexes.entrySet(), new ParallelCall<Entry<String, BlurIndex>, List<String>>() { @Override public List<String> call(Entry<String, BlurIndex> input) throws Exception { BlurIndex index = input.getValue(); IndexReader reader = index.getIndexReader(); try { return terms(reader, columnFamily, columnName, startWith, size); } finally { // this will allow for closing of index reader.decRef(); } } }).merge(new Merger<List<String>>() { @Override public List<String> merge(BlurExecutorCompletionService<List<String>> service) throws BlurException { TreeSet<String> terms = new TreeSet<String>(); while (service.getRemainingCount() > 0) { Future<List<String>> future = service.poll(_defaultParallelCallTimeout, TimeUnit.MILLISECONDS, true, table, columnFamily, columnName, startWith, size); terms.addAll(service.getResultThrowException(future, table, columnFamily, columnName, startWith, size)); } return new ArrayList<String>(terms).subList(0, Math.min(size, terms.size())); } }); }
From source file:com.nearinfinity.blur.manager.IndexManager.java
License:Apache License
public Schema schema(String table) throws IOException { Schema schema = new Schema().setTable(table); schema.columnFamilies = new TreeMap<String, Set<String>>(); Map<String, BlurIndex> blurIndexes = _indexServer.getIndexes(table); for (BlurIndex blurIndex : blurIndexes.values()) { IndexReader reader = blurIndex.getIndexReader(); try {//w ww . jav a 2s.co m FieldInfos mergedFieldInfos = ReaderUtil.getMergedFieldInfos(reader); for (FieldInfo fieldInfo : mergedFieldInfos) { String fieldName = fieldInfo.name; int index = fieldName.indexOf('.'); if (index > 0) { String columnFamily = fieldName.substring(0, index); String column = fieldName.substring(index + 1); Set<String> set = schema.columnFamilies.get(columnFamily); if (set == null) { set = new TreeSet<String>(); schema.columnFamilies.put(columnFamily, set); } set.add(column); } } } finally { // this will allow for closing of index reader.decRef(); } } return schema; }
From source file:net.ymate.platform.module.search.support.IndexHelper.java
License:Apache License
public IndexHelper(ISearchConfig config) { __isBuildWorkingSet = Collections.synchronizedSet(new HashSet<String>()); // ??30//from ww w .java2 s.com long _period = config.getScheduledPeriod() * 1000L; if (_period <= 0) { _period = 30L * 1000L; } // commit?Reopen __scheduler = Executors.newSingleThreadScheduledExecutor(); // ?? __scheduler.scheduleAtFixedRate(new Runnable() { public void run() { if (__isWorking) { return; } __isWorking = true; try { _LOG.debug("Start Reopen Working..."); for (Map.Entry<String, IndexSearcher> entry : Searchs.__SEARCH_CACHES.entrySet()) { IndexReader _reader = entry.getValue().getIndexReader(); try { IndexReader _reOpenedReader = DirectoryReader.openIfChanged((DirectoryReader) _reader); if (_reOpenedReader != null && _reOpenedReader != _reader) { _reader.decRef(); Searchs.__SEARCH_CACHES.put(entry.getKey(), new IndexSearcher(_reOpenedReader)); } } catch (IOException ex) { _LOG.error("Reopen And DecRef IndexReader Error:", ex); } } } finally { _LOG.debug("End Reopen Working..."); __isWorking = false; } } }, _period, _period, TimeUnit.MILLISECONDS); }
From source file:org.alfresco.repo.search.impl.lucene.index.IndexInfo.java
License:Open Source License
/** * Get the main index reader augmented with the specified TX data As above but we add the TX data * // ww w. j a v a2 s . c o m * @param id String * @param deleteOnlyNodes boolean * @return IndexReader * @throws IOException */ public IndexReader getMainIndexReferenceCountingReadOnlyIndexReader(String id, Set<String> deletions, Set<String> containerDeletions, boolean deleteOnlyNodes) throws IOException { if (id == null) { throw new IndexerException("\"null\" is not a valid identifier for a transaction"); } getReadLock(); try { if (indexIsShared && !checkVersion()) { releaseReadLock(); getWriteLock(); try { if (mainIndexReader != null) { ((ReferenceCounting) mainIndexReader).setInvalidForReuse(); } mainIndexReader = null; } finally { getReadLock(); releaseWriteLock(); } } if (mainIndexReader == null) { releaseReadLock(); getWriteLock(); try { if (mainIndexReader == null) { // Sync with disk image if required doWithFileLock(new LockWork<Object>() { public Object doWork() throws Exception { return null; } public boolean canRetry() { return true; } }); mainIndexReader = createMainIndexReader(); } } finally { getReadLock(); releaseWriteLock(); } } // Combine the index delta with the main index // Make sure the index is written to disk // TODO: Should use the in memory index but we often end up forcing // to disk anyway. // Is it worth it? // luceneIndexer.flushPending(); IndexReader deltaReader = buildAndRegisterDeltaReader(id); IndexReader reader = null; if ((deletions == null || deletions.size() == 0) && (containerDeletions == null || containerDeletions.size() == 0)) { reader = new MultiReader(new IndexReader[] { mainIndexReader, deltaReader }, false); } else { IndexReader filterReader = new FilterIndexReaderByStringId("main+id", mainIndexReader, deletions, containerDeletions, deleteOnlyNodes); reader = new MultiReader(new IndexReader[] { filterReader, deltaReader }, false); // Cancel out extra incRef made by MultiReader filterReader.decRef(); } // The reference count would have been incremented automatically by MultiReader / // FilterIndexReaderByStringId deltaReader.decRef(); if (s_logger.isDebugEnabled()) { s_logger.debug("Main index reader references = " + ((ReferenceCounting) mainIndexReader).getReferenceCount()); } reader = ReferenceCountingReadOnlyIndexReaderFactory.createReader(MAIN_READER + id, reader, false, config); ReferenceCounting refCounting = (ReferenceCounting) reader; reader.incRef(); refCounting.setInvalidForReuse(); return reader; } finally { releaseReadLock(); } }
From source file:org.alfresco.repo.search.impl.lucene.index.IndexInfo.java
License:Open Source License
private IndexReader createMainIndexReader() throws IOException { IndexReader reader = null;//from www. jav a 2 s . c o m IndexReader oldReader = null; for (String id : indexEntries.keySet()) { IndexEntry entry = indexEntries.get(id); if (entry.getStatus().isCommitted()) { IndexReader subReader = getReferenceCountingIndexReader(id); if (reader == null) { reader = subReader; } else { boolean oldReaderIsSubReader = oldReader == null; oldReader = reader; reader = mainIndexReaders.get(id); if (reader == null) { if (entry.getType() == IndexType.INDEX) { reader = new MultiReader(new IndexReader[] { oldReader, subReader }, false); } else if (entry.getType() == IndexType.DELTA) { try { IndexReader filterReader = new FilterIndexReaderByStringId(id, oldReader, getDeletions(entry.getName(), INDEX_INFO_DELETIONS), getDeletions(entry.getName(), INDEX_INFO_CONTAINER_DELETIONS), entry.isDeletOnlyNodes()); reader = new MultiReader(new IndexReader[] { filterReader, subReader }, false); // Cancel out the incRef on the filter reader filterReader.decRef(); } catch (IOException ioe) { s_logger.error("Failed building filter reader beneath " + entry.getName(), ioe); throw ioe; } } reader = ReferenceCountingReadOnlyIndexReaderFactory.createReader(id + "multi", reader, true, config); mainIndexReaders.put(id, reader); } } } } if (reader == null) { reader = IndexReader.open(emptyIndex); } else { // Keep this reader open whilst it is referenced by mainIndexReaders / referenceCountingReadOnlyIndexReaders reader.incRef(); } reader = ReferenceCountingReadOnlyIndexReaderFactory.createReader(MAIN_READER, reader, false, config); return reader; }