<NME> SearchManager.cpp
<BEF> #include "SearchManager.h"
#include "SearchFactory.h"
#include <mining-manager/MiningManager.h>
#include <bundles/index/IndexBundleConfiguration.h>

#include <glog/logging.h>

using namespace sf1r;


// @brief: here SchemaMap is just for normal search, and
// zambeziSearch perparefilter;
SearchManager::SearchManager(
    const IndexBundleConfiguration& config,
    const SearchFactory& searchFactory)
    : preprocessor_(config.indexSchema_)
    , topKReranker_(preprocessor_)
    , fuzzySearchRanker_(preprocessor_)
    , queryBuilder_(searchFactory.createQueryBuilder(
                        preprocessor_.getSchemaMap()))
    , normalSearch_(searchFactory.createSearchBase(
                        SearchingMode::DefaultSearchingMode,
                        preprocessor_, *queryBuilder_))
    , zambeziSearch_(searchFactory.createSearchBase(
                         SearchingMode::ZAMBEZI,
                         preprocessor_, *queryBuilder_))
{
}

void SearchManager::setMiningManager(
    const boost::shared_ptr<MiningManager>& miningManager)
{
    if (!miningManager)

#include <fstream>
#include <algorithm>
#include <omp.h>
#include <util/cpu_topology.h>

        miningManager->GetProductScorerFactory());

    preprocessor_.setNumericTableBuilder(
        miningManager->GetNumericTableBuilder());

    preprocessor_.setRTypeStringPropTableBuilder(
        miningManager->GetRTypeStringPropTableBuilder());

    topKReranker_.setProductRankerFactory(
        miningManager->GetProductRankerFactory());

    fuzzySearchRanker_.setFuzzyScoreWeight(
        miningManager->getMiningSchema().product_ranking_config);

    fuzzySearchRanker_.setCustomRankManager(
        miningManager->GetCustomRankManager());

    normalSearch_->setMiningManager(miningManager);

    zambeziSearch_->setMiningManager(miningManager);
}
    rankingManagerPtr_->createPropertyRankers(pTextRankingType, indexPropertySize, propertyRankers);
    bool readTermPosition = propertyRankers[0]->requireTermPosition();

    DocumentIterator* pScoreDocIterator = NULL;
    MultiPropertyScorer* pMultiPropertyIterator = NULL;
    WANDDocumentIterator* pWandDocIterator = NULL;

                                        readTermPosition,
                                        termIndexMaps
                                   );
                pScoreDocIterator = pWandDocIterator;
            }
            else
            {
                                             indexPropertyIdList,
                                             readTermPosition,
                                             termIndexMaps );
                pScoreDocIterator = pMultiPropertyIterator;
            }
        }

    }
    catch (std::exception& e)
    {
        if (pScoreDocIterator)
            delete pScoreDocIterator;
        return false;
    }

    boost::shared_ptr<CombinedDocumentIterator> pDocIterator;
    pDocIterator.reset(new CombinedDocumentIterator());
    if (pFilterIdSet)
    {
        ///1. Search Filter
    else
        scoreItemQueue.reset(new ScoreSortedHitQueue(heapSize));

    if (isFilterQuery == false)
    {
        const std::string& query = actionOperation.actionItem_.env_.queryString_;

        pScoreDocIterator = combineCustomDocIterator_(
            query, pSorter, pScoreDocIterator);

        if (pScoreDocIterator == NULL)
        {

<MSG> revise below functions to ensure exception safe:

- QueryBuilder::prepare_dociterator()
- QueryBuilder::prefetch_term_doc_readers_()
- SearchManager::doSearchInThread()

<DFF> @@ -34,6 +34,7 @@
 
 #include <fstream>
 #include <algorithm>
+#include <memory> // auto_ptr
 #include <omp.h>
 #include <util/cpu_topology.h>
 
@@ -470,7 +471,7 @@ bool SearchManager::doSearchInThread(const SearchKeywordOperation& actionOperati
     rankingManagerPtr_->createPropertyRankers(pTextRankingType, indexPropertySize, propertyRankers);
     bool readTermPosition = propertyRankers[0]->requireTermPosition();
 
-    DocumentIterator* pScoreDocIterator = NULL;
+    std::auto_ptr<DocumentIterator> scoreDocIterPtr;
     MultiPropertyScorer* pMultiPropertyIterator = NULL;
     WANDDocumentIterator* pWandDocIterator = NULL;
 
@@ -503,7 +504,7 @@ bool SearchManager::doSearchInThread(const SearchKeywordOperation& actionOperati
                                         readTermPosition,
                                         termIndexMaps
                                    );
-                pScoreDocIterator = pWandDocIterator;
+                scoreDocIterPtr.reset(pWandDocIterator);
             }
             else
             {
@@ -515,20 +516,17 @@ bool SearchManager::doSearchInThread(const SearchKeywordOperation& actionOperati
                                              indexPropertyIdList,
                                              readTermPosition,
                                              termIndexMaps );
-                pScoreDocIterator = pMultiPropertyIterator;
+                scoreDocIterPtr.reset(pMultiPropertyIterator);
             }
         }
-
     }
     catch (std::exception& e)
     {
-        if (pScoreDocIterator)
-            delete pScoreDocIterator;
         return false;
     }
 
-    boost::shared_ptr<CombinedDocumentIterator> pDocIterator;
-    pDocIterator.reset(new CombinedDocumentIterator());
+    boost::shared_ptr<CombinedDocumentIterator> pDocIterator(
+        new CombinedDocumentIterator());
     if (pFilterIdSet)
     {
         ///1. Search Filter
@@ -558,12 +556,13 @@ bool SearchManager::doSearchInThread(const SearchKeywordOperation& actionOperati
     else
         scoreItemQueue.reset(new ScoreSortedHitQueue(heapSize));
 
+    DocumentIterator* pScoreDocIterator = scoreDocIterPtr.get();
     if (isFilterQuery == false)
     {
         const std::string& query = actionOperation.actionItem_.env_.queryString_;
 
         pScoreDocIterator = combineCustomDocIterator_(
-            query, pSorter, pScoreDocIterator);
+            query, pSorter, scoreDocIterPtr.release());
 
         if (pScoreDocIterator == NULL)
         {

{'addition_count': 8, 'commit_subject': 'revise below functions to ensure exception safe:', 'deletion_count': 9, 'file_extension': '.cpp', 'lang': 'cpp', 'license': 'apache-2.0', 'repo_name': 'izenecloud/sf1r-lite'}