ReadInterceptor interceptor = tableContext.getReadInterceptor();
Filter filter = interceptor.getFilter();
BlurIndex index = null;
String shard = null;
Tracer trace = Trace.trace("manager fetch", Trace.param("table", table));
IndexSearcherClosable searcher = null;
try {
if (selector.getLocationId() == null) {
// Not looking up by location id so we should resetSearchers.
ShardServerContext.resetSearchers();
shard = MutationHelper.getShardName(table, selector.rowId, getNumberOfShards(table), _blurPartitioner);
index = getBlurIndex(table, shard);
searcher = index.getIndexSearcher();
populateSelector(searcher, shard, table, selector);
}
String locationId = selector.getLocationId();
if (locationId.equals(NOT_FOUND)) {
fetchResult.setDeleted(false);
fetchResult.setExists(false);
return;
}
if (shard == null) {
shard = getShard(locationId);
}
if (index == null) {
index = getBlurIndex(table, shard);
}
} 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);
}
TimerContext timerContext = _fetchTimer.time();
boolean usedCache = true;
try {
ShardServerContext shardServerContext = ShardServerContext.getShardServerContext();
if (shardServerContext != null) {
searcher = shardServerContext.getIndexSearcherClosable(table, shard);
}
if (searcher == null) {
// Was not pulled from cache, get a fresh one from the index.
searcher = index.getIndexSearcher();
usedCache = false;
}
FieldManager fieldManager = tableContext.getFieldManager();
Query highlightQuery = getHighlightQuery(selector, table, fieldManager);
fetchRow(searcher.getIndexReader(), table, shard, selector, fetchResult, highlightQuery, fieldManager,
_maxHeapPerRowFetch, tableContext, filter);
} catch (Exception e) {
LOG.error("Unknown error while trying to fetch row.", e);
throw new BException(e.getMessage(), e);
} finally {
trace.done();
timerContext.stop();
if (!usedCache && searcher != null) {
// if the cached search was not used, close the searcher.
// this will allow for closing of index
try {
searcher.close();
} catch (IOException e) {
LOG.error("Unknown error trying to call close on searcher [{0}]", e, searcher);
}
}
}