LCOV - code coverage report
Current view: top level - expand - ortermlist.cc (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core 954b5873a738 Lines: 63 99 63.6 %
Date: 2019-06-30 05:20:33 Functions: 10 14 71.4 %
Branches: 37 66 56.1 %

           Branch data     Line data    Source code
       1                 :            : /** @file ortermlist.cc
       2                 :            :  * @brief Merge two TermList objects using an OR operation.
       3                 :            :  */
       4                 :            : /* Copyright (C) 2007,2010 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 "ortermlist.h"
      24                 :            : 
      25                 :            : #include "debuglog.h"
      26                 :            : #include "omassert.h"
      27                 :            : 
      28                 :            : #include <xapian/positioniterator.h>
      29                 :            : 
      30                 :            : using namespace std;
      31                 :            : 
      32                 :            : void
      33                 :      22878 : OrTermList::check_started() const
      34                 :            : {
      35                 :            :     Assert(!left_current.empty());
      36                 :            :     Assert(!right_current.empty());
      37                 :      22878 : }
      38                 :            : 
      39                 :      32321 : OrTermList::~OrTermList()
      40                 :            : {
      41         [ +  + ]:      10774 :     delete left;
      42         [ +  + ]:      10774 :     delete right;
      43         [ -  + ]:      21547 : }
      44                 :            : 
      45                 :            : Xapian::termcount
      46                 :        973 : OrTermList::get_approx_size() const
      47                 :            : {
      48                 :            :     LOGCALL(EXPAND, Xapian::termcount, "OrTermList::get_approx_size", NO_ARGS);
      49                 :            :     // This is actually the upper bound, but we only use this to order the
      50                 :            :     // binary tree of OrTermList objects so it's probably not worth trying
      51                 :            :     // to be more sophisticated.
      52                 :        973 :     RETURN(left->get_approx_size() + right->get_approx_size());
      53                 :            : }
      54                 :            : 
      55                 :            : void
      56                 :       6710 : OrTermList::accumulate_stats(Xapian::Internal::ExpandStats & stats) const
      57                 :            : {
      58                 :            :     LOGCALL_VOID(EXPAND, "OrTermList::accumulate_stats", stats);
      59                 :       6710 :     check_started();
      60         [ +  + ]:       6710 :     if (left_current <= right_current) left->accumulate_stats(stats);
      61         [ +  + ]:       6710 :     if (left_current >= right_current) right->accumulate_stats(stats);
      62                 :       6710 : }
      63                 :            : 
      64                 :            : string
      65                 :       7883 : OrTermList::get_termname() const
      66                 :            : {
      67                 :            :     LOGCALL(EXPAND, string, "OrTermList::get_termname", NO_ARGS);
      68                 :       7883 :     check_started();
      69         [ +  + ]:       7883 :     if (left_current < right_current) RETURN(left_current);
      70                 :       3108 :     RETURN(right_current);
      71                 :            : }
      72                 :            : 
      73                 :            : Xapian::termcount
      74                 :        399 : OrTermList::get_wdf() const
      75                 :            : {
      76                 :            :     LOGCALL(EXPAND, Xapian::termcount, "OrTermList::get_wdf", NO_ARGS);
      77                 :        399 :     check_started();
      78         [ +  + ]:        399 :     if (left_current < right_current) RETURN(left->get_wdf());
      79         [ +  + ]:        377 :     if (left_current > right_current) RETURN(right->get_wdf());
      80                 :        365 :     RETURN(left->get_wdf() + right->get_wdf());
      81                 :            : }
      82                 :            : 
      83                 :            : Xapian::doccount
      84                 :          0 : OrTermList::get_termfreq() const
      85                 :            : {
      86                 :            :     LOGCALL(EXPAND, Xapian::doccount, "OrTermList::get_termfreq", NO_ARGS);
      87                 :          0 :     check_started();
      88         [ #  # ]:          0 :     if (left_current < right_current) RETURN(left->get_termfreq());
      89                 :            :     Assert(left_current > right_current || left->get_termfreq() == right->get_termfreq());
      90                 :          0 :     RETURN(right->get_termfreq());
      91                 :            : }
      92                 :            : 
      93                 :            : // Helper function.
      94                 :            : static inline void
      95                 :      30949 : handle_prune(TermList *& old, TermList * result)
      96                 :            : {
      97         [ +  + ]:      30949 :     if (result) {
      98         [ +  - ]:        256 :         delete old;
      99                 :        256 :         old = result;
     100                 :            :     }
     101                 :      30949 : }
     102                 :            : 
     103                 :            : TermList *
     104                 :      18657 : OrTermList::next()
     105                 :            : {
     106                 :            :     LOGCALL(EXPAND, TermList *, "OrTermList::next", NO_ARGS);
     107                 :            :     // If we've not started yet, both left_current and right_current will be
     108                 :            :     // empty, so we'll take the third case below which is what we want to do to
     109                 :            :     // get started.
     110         [ +  + ]:      18657 :     if (left_current < right_current) {
     111                 :       4775 :         handle_prune(left, left->next());
     112         [ +  + ]:       4775 :         if (left->at_end()) {
     113                 :          3 :             TermList *ret = right;
     114                 :          3 :             right = NULL;
     115                 :          3 :             RETURN(ret);
     116                 :            :         }
     117         [ +  - ]:       4772 :         left_current = left->get_termname();
     118         [ +  + ]:      13882 :     } else if (left_current > right_current) {
     119                 :       1590 :         handle_prune(right, right->next());
     120         [ +  + ]:       1590 :         if (right->at_end()) {
     121                 :        116 :             TermList *ret = left;
     122                 :        116 :             left = NULL;
     123                 :        116 :             RETURN(ret);
     124                 :            :         }
     125         [ +  - ]:       1474 :         right_current = right->get_termname();
     126                 :            :     } else {
     127                 :            :         AssertEq(left_current, right_current);
     128                 :      12292 :         handle_prune(left, left->next());
     129                 :      12292 :         handle_prune(right, right->next());
     130         [ +  + ]:      12292 :         if (left->at_end()) {
     131                 :            :             // right->at_end() may also be true, but our parent will deal with
     132                 :            :             // that.
     133                 :       5329 :             TermList *ret = right;
     134                 :       5329 :             right = NULL;
     135                 :       5329 :             RETURN(ret);
     136                 :            :         }
     137         [ +  + ]:       6963 :         if (right->at_end()) {
     138                 :       5326 :             TermList *ret = left;
     139                 :       5326 :             left = NULL;
     140                 :       5326 :             RETURN(ret);
     141                 :            :         }
     142         [ +  - ]:       1637 :         left_current = left->get_termname();
     143         [ +  - ]:       1637 :         right_current = right->get_termname();
     144                 :            :     }
     145                 :      18657 :     RETURN(NULL);
     146                 :            : }
     147                 :            : 
     148                 :            : TermList *
     149                 :          0 : OrTermList::skip_to(const string & term)
     150                 :            : {
     151                 :            :     LOGCALL(EXPAND, TermList *, "OrTermList::skip_to", term);
     152                 :            :     // If we've not started yet, both left_current and right_current will be
     153                 :            :     // empty, so we'll take the third case below which is what we want to do to
     154                 :            :     // get started.
     155         [ #  # ]:          0 :     if (left_current < right_current) {
     156                 :          0 :         handle_prune(left, left->skip_to(term));
     157         [ #  # ]:          0 :         if (left->at_end()) {
     158                 :          0 :             TermList *ret = right;
     159                 :          0 :             right = NULL;
     160                 :          0 :             RETURN(ret);
     161                 :            :         }
     162         [ #  # ]:          0 :         left_current = left->get_termname();
     163         [ #  # ]:          0 :     } else if (left_current > right_current) {
     164                 :          0 :         handle_prune(right, right->skip_to(term));
     165         [ #  # ]:          0 :         if (right->at_end()) {
     166                 :          0 :             TermList *ret = left;
     167                 :          0 :             left = NULL;
     168                 :          0 :             RETURN(ret);
     169                 :            :         }
     170         [ #  # ]:          0 :         right_current = right->get_termname();
     171                 :            :     } else {
     172                 :            :         AssertEq(left_current, right_current);
     173                 :          0 :         handle_prune(left, left->skip_to(term));
     174                 :          0 :         handle_prune(right, right->skip_to(term));
     175         [ #  # ]:          0 :         if (left->at_end()) {
     176                 :            :             // right->at_end() may also be true, but our parent will deal with
     177                 :            :             // that.
     178                 :          0 :             TermList *ret = right;
     179                 :          0 :             right = NULL;
     180                 :          0 :             RETURN(ret);
     181                 :            :         }
     182         [ #  # ]:          0 :         if (right->at_end()) {
     183                 :          0 :             TermList *ret = left;
     184                 :          0 :             left = NULL;
     185                 :          0 :             RETURN(ret);
     186                 :            :         }
     187         [ #  # ]:          0 :         left_current = left->get_termname();
     188         [ #  # ]:          0 :         right_current = right->get_termname();
     189                 :            :     }
     190                 :          0 :     RETURN(NULL);
     191                 :            : }
     192                 :            : 
     193                 :            : bool
     194                 :       7884 : OrTermList::at_end() const
     195                 :            : {
     196                 :            :     LOGCALL(EXPAND, bool, "OrTermList::at_end", NO_ARGS);
     197                 :       7884 :     check_started();
     198                 :            :     // next() should have pruned if either child is at_end().
     199                 :            :     Assert(!left->at_end());
     200                 :            :     Assert(!right->at_end());
     201                 :       7884 :     RETURN(false);
     202                 :            : }
     203                 :            : 
     204                 :            : Xapian::termcount
     205                 :          0 : OrTermList::positionlist_count() const
     206                 :            : {
     207                 :            :     Assert(false);
     208                 :          0 :     return 0;
     209                 :            : }
     210                 :            : 
     211                 :            : PositionList*
     212                 :          0 : OrTermList::positionlist_begin() const
     213                 :            : {
     214                 :            :     Assert(false);
     215                 :          0 :     return NULL;
     216                 :            : }
     217                 :            : 
     218                 :            : 
     219                 :            : Xapian::doccount
     220                 :          2 : FreqAdderOrTermList::get_termfreq() const
     221                 :            : {
     222                 :            :     LOGCALL(EXPAND, Xapian::doccount, "FreqAdderOrTermList::get_termfreq", NO_ARGS);
     223                 :          2 :     check_started();
     224         [ +  + ]:          2 :     if (left_current < right_current) RETURN(left->get_termfreq());
     225         [ -  + ]:          1 :     if (left_current > right_current) RETURN(right->get_termfreq());
     226                 :          1 :     RETURN(left->get_termfreq() + right->get_termfreq());
     227                 :            : }

Generated by: LCOV version 1.11