LCOV - code coverage report
Current view: top level - matcher - andnotpostlist.cc (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core 7028d852e609 Lines: 54 117 46.2 %
Date: 2019-02-17 14:59:59 Functions: 5 8 62.5 %
Branches: 26 106 24.5 %

           Branch data     Line data    Source code
       1                 :            : /** @file andnotpostlist.cc
       2                 :            :  * @brief PostList class implementing Query::OP_AND_NOT
       3                 :            :  */
       4                 :            : /* Copyright 2017 Olly Betts
       5                 :            :  *
       6                 :            :  * This program is free software; you can redistribute it and/or
       7                 :            :  * modify it under the terms of the GNU General Public License as
       8                 :            :  * published by the Free Software Foundation; either version 2 of the
       9                 :            :  * License, or (at your option) any later version.
      10                 :            :  *
      11                 :            :  * This program is distributed in the hope that it will be useful,
      12                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14                 :            :  * GNU General Public License for more details.
      15                 :            :  *
      16                 :            :  * You should have received a copy of the GNU General Public License
      17                 :            :  * along with this program; if not, write to the Free Software
      18                 :            :  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
      19                 :            :  */
      20                 :            : 
      21                 :            : #include <config.h>
      22                 :            : 
      23                 :            : #include "andnotpostlist.h"
      24                 :            : 
      25                 :            : #include <algorithm>
      26                 :            : 
      27                 :            : using namespace std;
      28                 :            : 
      29                 :            : Xapian::doccount
      30                 :        128 : AndNotPostList::get_termfreq_min() const
      31                 :            : {
      32                 :        128 :     Xapian::doccount l_tf_min = pl->get_termfreq_min();
      33                 :        128 :     Xapian::doccount r_tf_max = r->get_termfreq_max();
      34         [ +  + ]:        128 :     if (l_tf_min <= r_tf_max)
      35                 :         66 :         return 0;
      36                 :         62 :     return l_tf_min - r_tf_max;
      37                 :            : }
      38                 :            : 
      39                 :            : Xapian::doccount
      40                 :        144 : AndNotPostList::get_termfreq_max() const
      41                 :            : {
      42                 :            :     // We can't match more documents than our left-side does.
      43         [ +  - ]:        144 :     Xapian::doccount l_tf_max = pl->get_termfreq_max();
      44                 :            :     // We also can't more documents than our right-side *doesn't*.
      45         [ +  - ]:        144 :     Xapian::doccount r_tf_min = r->get_termfreq_min();
      46                 :        144 :     return min(db_size - r_tf_min, l_tf_max);
      47                 :            : }
      48                 :            : 
      49                 :            : Xapian::doccount
      50                 :        136 : AndNotPostList::get_termfreq_est() const
      51                 :            : {
      52         [ +  + ]:        136 :     if (rare(db_size == 0))
      53                 :          8 :         return 0;
      54                 :            :     // We calculate the estimate assuming independence.  With this assumption,
      55                 :            :     // the estimate is the product of the estimates for the sub-postlists
      56                 :            :     // (for the right side this is inverted by subtracting from db_size),
      57                 :            :     // divided by db_size.
      58                 :        128 :     double result = pl->get_termfreq_est();
      59                 :        128 :     result = (result * (db_size - r->get_termfreq_est())) / db_size;
      60                 :        128 :     return static_cast<Xapian::doccount>(result + 0.5);
      61                 :            : }
      62                 :            : 
      63                 :            : TermFreqs
      64                 :         16 : AndNotPostList::get_termfreq_est_using_stats(const Xapian::Weight::Internal & stats) const
      65                 :            : {
      66                 :            :     // We calculate the estimate assuming independence.  With this assumption,
      67                 :            :     // the estimate is the product of the estimates for the sub-postlists
      68                 :            :     // (for the right side this is inverted by subtracting from db_size),
      69                 :            :     // divided by db_size.
      70         [ +  - ]:         16 :     TermFreqs freqs(pl->get_termfreq_est_using_stats(stats));
      71                 :            : 
      72                 :         16 :     double freqest = double(freqs.termfreq);
      73                 :         16 :     double relfreqest = double(freqs.reltermfreq);
      74                 :         16 :     double collfreqest = double(freqs.collfreq);
      75                 :            : 
      76         [ +  - ]:         16 :     freqs = r->get_termfreq_est_using_stats(stats);
      77                 :            : 
      78                 :            :     // Our caller should have ensured this.
      79                 :            :     Assert(stats.collection_size);
      80                 :            : 
      81                 :         16 :     freqs.termfreq = stats.collection_size - freqs.termfreq;
      82                 :         16 :     freqest = (freqest * freqs.termfreq) / stats.collection_size;
      83                 :            : 
      84         [ +  - ]:         16 :     if (stats.total_term_count != 0) {
      85                 :         16 :         freqs.collfreq = stats.total_term_count - freqs.termfreq;
      86                 :         16 :         collfreqest = (collfreqest * freqs.collfreq) / stats.total_term_count;
      87                 :            :     }
      88                 :            : 
      89                 :            :     // If the rset is empty, relfreqest should be 0 already, so leave
      90                 :            :     // it alone.
      91         [ -  + ]:         16 :     if (stats.rset_size != 0) {
      92                 :          0 :         freqs.reltermfreq = stats.rset_size - freqs.reltermfreq;
      93                 :          0 :         relfreqest = (relfreqest * freqs.reltermfreq) / stats.rset_size;
      94                 :            :     }
      95                 :            : 
      96                 :         16 :     return TermFreqs(static_cast<Xapian::doccount>(freqest + 0.5),
      97                 :         16 :                      static_cast<Xapian::doccount>(relfreqest + 0.5),
      98                 :         16 :                      static_cast<Xapian::termcount>(collfreqest + 0.5));
      99                 :            : }
     100                 :            : 
     101                 :            : PostList*
     102                 :       2778 : AndNotPostList::next(double w_min)
     103                 :            : {
     104                 :            :     while (true) {
     105                 :       3317 :         PostList* result = pl->next(w_min);
     106         [ +  + ]:       3317 :         if (result) {
     107         [ +  - ]:          8 :             delete pl;
     108                 :          8 :             pl = result;
     109                 :            :         }
     110         [ +  + ]:       3317 :         if (pl->at_end()) {
     111                 :         47 :             result = pl;
     112                 :         47 :             pl = NULL;
     113                 :         47 :             return result;
     114                 :            :         }
     115                 :       3270 :         Xapian::docid l_did = pl->get_docid();
     116         [ +  + ]:       3270 :         if (l_did > r_did) {
     117                 :            :             bool r_valid;
     118         [ +  - ]:        642 :             result = r->check(l_did, 0, r_valid);
     119         [ -  + ]:        642 :             if (result) {
     120         [ #  # ]:          0 :                 delete r;
     121                 :          0 :                 r = result;
     122                 :            :             }
     123         [ -  + ]:        642 :             if (!r_valid)
     124                 :         26 :                 return NULL;
     125 [ +  - ][ +  + ]:        642 :             if (r->at_end()) {
     126                 :         26 :                 result = pl;
     127                 :         26 :                 pl = NULL;
     128                 :         26 :                 return result;
     129                 :            :             }
     130         [ +  - ]:        616 :             r_did = r->get_docid();
     131                 :            :         }
     132         [ +  + ]:       3244 :         if (l_did < r_did)
     133                 :       2705 :             break;
     134                 :            :     }
     135                 :       3317 :     return NULL;
     136                 :            : }
     137                 :            : 
     138                 :            : PostList*
     139                 :          0 : AndNotPostList::skip_to(Xapian::docid did, double w_min)
     140                 :            : {
     141         [ #  # ]:          0 :     if (did > pl->get_docid()) {
     142                 :          0 :         PostList* result = pl->skip_to(did, w_min);
     143         [ #  # ]:          0 :         if (result) {
     144         [ #  # ]:          0 :             delete pl;
     145                 :          0 :             pl = result;
     146                 :            :         }
     147         [ #  # ]:          0 :         if (pl->at_end()) {
     148                 :          0 :             result = pl;
     149                 :          0 :             pl = NULL;
     150                 :          0 :             return result;
     151                 :            :         }
     152                 :          0 :         Xapian::docid l_did = pl->get_docid();
     153         [ #  # ]:          0 :         if (l_did > r_did) {
     154                 :            :             bool r_valid;
     155         [ #  # ]:          0 :             result = r->check(l_did, 0, r_valid);
     156         [ #  # ]:          0 :             if (result) {
     157         [ #  # ]:          0 :                 delete r;
     158                 :          0 :                 r = result;
     159                 :            :             }
     160         [ #  # ]:          0 :             if (!r_valid)
     161                 :          0 :                 return NULL;
     162 [ #  # ][ #  # ]:          0 :             if (r->at_end()) {
     163                 :          0 :                 result = pl;
     164                 :          0 :                 pl = NULL;
     165                 :          0 :                 return result;
     166                 :            :             }
     167         [ #  # ]:          0 :             r_did = r->get_docid();
     168                 :            :         }
     169         [ #  # ]:          0 :         if (l_did == r_did) {
     170                 :            :             // Advance to the next match.
     171                 :          0 :             return AndNotPostList::next(w_min);
     172                 :            :         }
     173                 :            :     }
     174                 :          0 :     return NULL;
     175                 :            : }
     176                 :            : 
     177                 :            : PostList*
     178                 :          0 : AndNotPostList::check(Xapian::docid did, double w_min, bool& valid)
     179                 :            : {
     180                 :          0 :     PostList* result = pl->check(did, w_min, valid);
     181         [ #  # ]:          0 :     if (result) {
     182         [ #  # ]:          0 :         delete pl;
     183                 :          0 :         pl = result;
     184                 :            :     }
     185         [ #  # ]:          0 :     if (valid) {
     186         [ #  # ]:          0 :         if (pl->at_end()) {
     187                 :          0 :             result = pl;
     188                 :          0 :             pl = NULL;
     189                 :          0 :             return result;
     190                 :            :         }
     191                 :          0 :         Xapian::docid l_did = pl->get_docid();
     192         [ #  # ]:          0 :         if (l_did > r_did) {
     193                 :            :             bool r_valid;
     194         [ #  # ]:          0 :             result = r->check(l_did, 0, r_valid);
     195         [ #  # ]:          0 :             if (result) {
     196         [ #  # ]:          0 :                 delete r;
     197                 :          0 :                 r = result;
     198                 :            :             }
     199         [ #  # ]:          0 :             if (!r_valid)
     200                 :          0 :                 return NULL;
     201 [ #  # ][ #  # ]:          0 :             if (r->at_end()) {
     202                 :          0 :                 result = pl;
     203                 :          0 :                 pl = NULL;
     204                 :          0 :                 return result;
     205                 :            :             }
     206         [ #  # ]:          0 :             r_did = r->get_docid();
     207                 :            :         }
     208         [ #  # ]:          0 :         if (l_did == r_did) {
     209                 :            :             // For check() we can simply indicate !valid.
     210                 :          0 :             valid = false;
     211                 :            :         }
     212                 :            :     }
     213                 :          0 :     return NULL;
     214                 :            : }
     215                 :            : 
     216                 :            : string
     217                 :          0 : AndNotPostList::get_description() const
     218                 :            : {
     219         [ #  # ]:          0 :     string desc = "AndNotPostList(";
     220 [ #  # ][ #  # ]:          0 :     desc += pl->get_description();
     221         [ #  # ]:          0 :     desc += ", ";
     222 [ #  # ][ #  # ]:          0 :     desc += r->get_description();
     223         [ #  # ]:          0 :     desc += ')';
     224                 :          0 :     return desc;
     225                 :            : }

Generated by: LCOV version 1.11