LCOV - code coverage report
Current view: top level - weight - weightinternal.cc (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core r Lines: 37 53 69.8 %
Date: 2011-08-21 Functions: 4 6 66.7 %
Branches: 15 16 93.8 %

           Branch data     Line data    Source code
       1                 :            : /** @file weightinternal.cc
       2                 :            :  * @brief Xapian::Weight::Internal class, holding database and term statistics.
       3                 :            :  */
       4                 :            : /* Copyright (C) 2007 Lemur Consulting Ltd
       5                 :            :  * Copyright (C) 2009,2010 Olly Betts
       6                 :            :  *
       7                 :            :  * This program is free software; you can redistribute it and/or
       8                 :            :  * modify it under the terms of the GNU General Public License as
       9                 :            :  * published by the Free Software Foundation; either version 2 of the
      10                 :            :  * License, or (at your option) any later version.
      11                 :            :  *
      12                 :            :  * This program is distributed in the hope that it will be useful
      13                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
      15                 :            :  * GNU General Public License for more details.
      16                 :            :  *
      17                 :            :  * You should have received a copy of the GNU General Public License
      18                 :            :  * along with this program; if not, write to the Free Software
      19                 :            :  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
      20                 :            :  */
      21                 :            : 
      22                 :            : #include <config.h>
      23                 :            : 
      24                 :            : #include "weightinternal.h"
      25                 :            : 
      26                 :            : #include "xapian/enquire.h"
      27                 :            : 
      28                 :            : #include "omassert.h"
      29                 :            : #include "omenquireinternal.h"
      30                 :            : #include "str.h"
      31                 :            : #include "termlist.h"
      32                 :            : 
      33                 :            : #include "autoptr.h"
      34                 :            : #include <set>
      35                 :            : 
      36                 :            : using namespace std;
      37                 :            : 
      38                 :            : string
      39                 :          0 : TermFreqs::get_description() const {
      40                 :          0 :     string desc("TermFreqs(");
      41                 :          0 :     desc += str(termfreq);
      42                 :          0 :     desc += ", ";
      43                 :          0 :     desc += str(reltermfreq);
      44                 :          0 :     desc += ")";
      45                 :          0 :     return desc;
      46                 :            : }
      47                 :            : 
      48                 :            : namespace Xapian {
      49                 :            : 
      50                 :            : Weight::Internal &
      51                 :       4404 : Weight::Internal::operator +=(const Weight::Internal & inc)
      52                 :            : {
      53                 :       4404 :     total_length += inc.total_length;
      54                 :       4404 :     collection_size += inc.collection_size;
      55                 :       4404 :     rset_size += inc.rset_size;
      56                 :            : 
      57                 :            :     // Add termfreqs and reltermfreqs
      58                 :       4404 :     map<string, TermFreqs>::const_iterator i;
      59         [ +  + ]:       9954 :     for (i = inc.termfreqs.begin(); i != inc.termfreqs.end(); ++i) {
      60                 :       5550 :         termfreqs[i->first] += i->second;
      61                 :            :     }
      62                 :       4404 :     return *this;
      63                 :            : }
      64                 :            : 
      65                 :            : Xapian::doccount
      66                 :     807243 : Weight::Internal::get_termfreq(const string & term) const
      67                 :            : {
      68                 :            :     // We pass an empty std::string for term when calculating the extra weight.
      69         [ +  + ]:     807243 :     if (term.empty()) return 0;
      70                 :            : 
      71                 :     807190 :     map<string, TermFreqs>::const_iterator tfreq = termfreqs.find(term);
      72                 :            :     Assert(tfreq != termfreqs.end());
      73                 :     807243 :     return tfreq->second.termfreq;
      74                 :            : }
      75                 :            : 
      76                 :            : void
      77                 :     246355 : Weight::Internal::accumulate_stats(const Xapian::Database::Internal &subdb,
      78                 :            :                                    const Xapian::RSet &rset)
      79                 :            : {
      80                 :     246355 :     total_length += subdb.get_total_length();
      81                 :     246355 :     collection_size += subdb.get_doccount();
      82                 :     246355 :     rset_size += rset.size();
      83                 :            : 
      84                 :     246355 :     map<string, TermFreqs>::iterator t;
      85         [ +  + ]:     721899 :     for (t = termfreqs.begin(); t != termfreqs.end(); ++t) {
      86                 :     475547 :         const string & term = t->first;
      87                 :     475547 :         t->second.termfreq += subdb.get_termfreq(term);
      88                 :            :     }
      89                 :            : 
      90                 :     246352 :     const set<Xapian::docid> & items(rset.internal->get_items());
      91                 :     246352 :     set<Xapian::docid>::const_iterator d;
      92         [ +  + ]:     246466 :     for (d = items.begin(); d != items.end(); ++d) {
      93                 :        114 :         Xapian::docid did = *d;
      94                 :            :         Assert(did);
      95                 :            :         // The query is likely to far fewer terms than the documents, and we
      96                 :            :         // can skip the document's termlist, so look for each query term in the
      97                 :            :         // document.
      98                 :        114 :         AutoPtr<TermList> tl(subdb.open_term_list(did));
      99         [ +  + ]:        274 :         for (t = termfreqs.begin(); t != termfreqs.end(); ++t) {
     100                 :        208 :             const string & term = t->first;
     101                 :        208 :             TermList * ret = tl->skip_to(term);
     102                 :            :             AssertEq(ret, NULL);
     103                 :            :             (void)ret;
     104         [ +  + ]:        208 :             if (tl->at_end())
     105                 :         48 :                 break;
     106         [ +  + ]:        160 :             if (term == tl->get_termname())
     107                 :        127 :                 ++t->second.reltermfreq;
     108                 :            :         }
     109                 :            :     }
     110                 :     246352 : }
     111                 :            : 
     112                 :            : Xapian::doccount
     113                 :     471948 : Weight::Internal::get_reltermfreq(const string & term) const
     114                 :            : {
     115                 :            :     // We pass an empty string for term when calculating the extra weight.
     116         [ -  + ]:     471948 :     if (term.empty()) return 0;
     117                 :            : 
     118                 :     471948 :     map<string, TermFreqs>::const_iterator tfreq = termfreqs.find(term);
     119                 :            :     Assert(tfreq != termfreqs.end());
     120                 :     471948 :     return tfreq->second.reltermfreq;
     121                 :            : }
     122                 :            : 
     123                 :            : string
     124                 :          0 : Weight::Internal::get_description() const
     125                 :            : {
     126                 :          0 :     string desc = "Weight::Internal(totlen=";
     127                 :          0 :     desc += str(total_length);
     128                 :          0 :     desc += ", collection_size=";
     129                 :          0 :     desc += str(collection_size);
     130                 :          0 :     desc += ", rset_size=";
     131                 :          0 :     desc += str(rset_size);
     132                 :          0 :     desc += ')';
     133                 :          0 :     return desc;
     134                 :            : }
     135                 :            : 
     136                 :            : }

Generated by: LCOV version 1.8