Bobo源代码分析(BoboIndexReader和BoboSubBrowser)

BoboIndexReader继承于FilterIndexReader,这个类是对IndexReader的进一步包装,
首先看构造函数

protected BoboIndexReader(IndexReader reader,
                            Collection<FacetHandler<?>> facetHandlers,
                            Collection<RuntimeFacetHandlerFactory<?,?>> facetHandlerFactories,
                            WorkArea workArea,
                            boolean useSubReaders) throws IOException
  {
    super(useSubReaders ? new MultiReader(createSubReaders(reader, workArea), false) : reader);
    if(useSubReaders)//是否使用多IndexReader
    {
      _dir = reader.directory();
      BoboIndexReader[] subReaders = (BoboIndexReader[])in.getSequentialSubReaders();
      if(subReaders != null && subReaders.length > 0)
      {
        _subReaders = subReaders;
        
        int maxDoc = 0;
        _starts = new int[_subReaders.length + 1];
        for (int i = 0; i < _subReaders.length; i++)
        {
          _subReaders[i]._dir = _dir;
          if(facetHandlers != null) _subReaders[i].setFacetHandlers(facetHandlers);
          _starts[i] = maxDoc;
          maxDoc += _subReaders[i].maxDoc();
        }
        _starts[_subReaders.length] = maxDoc;
      }
    }
    _runtimeFacetHandlerFactories = facetHandlerFactories;
    _runtimeFacetHandlerFactoryMap = new HashMap<String,RuntimeFacetHandlerFactory<?,?>>();
    if (_runtimeFacetHandlerFactories!=null)
    {
      for(RuntimeFacetHandlerFactory<?,?> factory : _runtimeFacetHandlerFactories)
      {
        _runtimeFacetHandlerFactoryMap.put(factory.getName(), factory);
      }
    }
    _facetHandlers = facetHandlers;
    _workArea = workArea;
  }


BoboIndexReader在多IndexReader的情况下,将所有的IndexReader的doc[]虚拟成一个,后一个Index的doc的id号在前面所有的indexReader的doc总数的基础上相加
_starts 是一个int[ ]类型的数组,保存了每一个IndexReader的第一个doc在这个虚拟的大的doc[]中的位置

 

 

搜索的时候最主要的函数是browse( ),多个BoboIndexReader的情况下,最终是调用的BoboSubBrowser中的

browse()函数:

 

public BrowseResult browse(BrowseRequest req) throws BrowseException
  {
    if (_reader == null)
      return new BrowseResult();

    final BrowseResult result = new BrowseResult();

    long start = System.currentTimeMillis();
    //创建排序收集器,这里返回得到的是SortCollectorImpl的实例
    SortCollector collector = getSortCollector(req.getSort(),req.getQuery(), req.getOffset(), req.getCount(), req.isFetchStoredFields(),false);
    
    Map<String, FacetAccessible> facetCollectors = new HashMap<String, FacetAccessible>();
    //搜索过程
    browse(req, collector, facetCollectors);
    BrowseHit[] hits = null;

    try
    {
        //收集命中结果
        hits = collector.topDocs();
    }
    catch (IOException e)
    {
      logger.error(e.getMessage(), e);
      hits = new BrowseHit[0];
    }
    
    Query q = req.getQuery();
    if (q == null){
    	q = new MatchAllDocsQuery();
    }
    if (req.isShowExplanation()){
    	for (BrowseHit hit : hits){
    		try {
				Explanation expl = explain(q, hit.getDocid());
				hit.setExplanation(expl);
			} catch (IOException e) {
				logger.error(e.getMessage(),e);
			}
    	}
    }
    result.setHits(hits);
    result.setNumHits(collector.getTotalHits());
    result.setTotalDocs(_reader.numDocs());
    result.addAll(facetCollectors);
    long end = System.currentTimeMillis();
    result.setTime(end - start);
    return result;
  }
 

 

你可能感兴趣的:(browser)