LCOV - code coverage report
Current view: top level - backends/multi - multi_alltermslist.cc (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core c2b6f1024d3a Lines: 71 76 93.4 %
Date: 2019-05-16 09:13:18 Functions: 8 9 88.9 %
Branches: 57 80 71.2 %

           Branch data     Line data    Source code
       1                 :            : /** @file multi_alltermslist.cc
       2                 :            :  * @brief Class for merging AllTermsList objects from subdatabases.
       3                 :            :  */
       4                 :            : /* Copyright (C) 2007,2008,2009,2011,2017,2018 Olly Betts
       5                 :            :  *
       6                 :            :  * This program is free software; you can redistribute it and/or modify
       7                 :            :  * it under the terms of the GNU General Public License as published by
       8                 :            :  * the Free Software Foundation; either version 2 of the License, or
       9                 :            :  * (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 "multi_alltermslist.h"
      24                 :            : 
      25                 :            : #include <xapian/database.h>
      26                 :            : 
      27                 :            : #include "backends/databaseinternal.h"
      28                 :            : #include "heap.h"
      29                 :            : #include "omassert.h"
      30                 :            : 
      31                 :            : using namespace std;
      32                 :            : 
      33                 :            : /// Comparison functor which orders TermList* by ascending term name.
      34                 :            : struct CompareTermListsByTerm {
      35                 :            :     /// Order by ascending term name.
      36                 :        293 :     bool operator()(const TermList *a, const TermList *b) const {
      37 [ +  - ][ +  - ]:        293 :         return a->get_termname() > b->get_termname();
      38                 :            :     }
      39                 :            : };
      40                 :            : 
      41                 :         79 : MultiAllTermsList::MultiAllTermsList(size_t count_, TermList** termlists_)
      42         [ +  - ]:         79 :     : count(count_), termlists(termlists_)
      43                 :            : {
      44                 :         79 : }
      45                 :            : 
      46                 :        237 : MultiAllTermsList::~MultiAllTermsList()
      47                 :            : {
      48         [ +  + ]:         93 :     while (count)
      49         [ +  - ]:         14 :         delete termlists[--count];
      50         [ +  - ]:         79 :     delete [] termlists;
      51         [ -  + ]:        158 : }
      52                 :            : 
      53                 :            : Xapian::termcount
      54                 :          0 : MultiAllTermsList::get_approx_size() const
      55                 :            : {
      56                 :            :     // This should never get called.
      57                 :            :     Assert(false);
      58                 :          0 :     return 0;
      59                 :            : }
      60                 :            : 
      61                 :            : string
      62                 :        266 : MultiAllTermsList::get_termname() const
      63                 :            : {
      64                 :        266 :     return current_term;
      65                 :            : }
      66                 :            : 
      67                 :            : Xapian::doccount
      68                 :        231 : MultiAllTermsList::get_termfreq() const
      69                 :            : {
      70 [ +  - ][ +  - ]:        231 :     if (current_termfreq == 0 && count != 0) {
      71                 :            :         while (true) {
      72                 :        505 :             TermList * tl = termlists[0];
      73 [ +  - ][ +  + ]:        505 :             if (tl->get_termname() != current_term)
      74                 :        228 :                 break;
      75                 :        277 :             current_termfreq += tl->get_termfreq();
      76                 :        277 :             tl->next();
      77         [ +  + ]:        277 :             if (tl->at_end()) {
      78                 :            :                 Heap::pop(termlists, termlists + count,
      79         [ +  - ]:         19 :                           CompareTermListsByTerm());
      80         [ +  - ]:         19 :                 delete tl;
      81         [ +  + ]:         19 :                 if (--count == 0)
      82                 :          3 :                     break;
      83                 :            :             } else {
      84                 :            :                 Heap::replace(termlists, termlists + count,
      85         [ +  - ]:        258 :                               CompareTermListsByTerm());
      86                 :            :             }
      87                 :        505 :         }
      88                 :            :     }
      89                 :        231 :     return current_termfreq;
      90                 :            : }
      91                 :            : 
      92                 :            : TermList *
      93                 :        308 : MultiAllTermsList::next()
      94                 :            : {
      95         [ +  + ]:        308 :     if (current_term.empty()) {
      96                 :            :         // Make termlists into a heap so that the one (or one of the ones) with
      97                 :            :         // earliest sorting term is at the top of the heap.
      98                 :         79 :         size_t j = 0;
      99         [ +  + ]:        245 :         for (size_t i = 0; i != count; ++i) {
     100                 :        168 :             TermList* tl = termlists[i];
     101                 :        168 :             tl->next();
     102         [ +  + ]:        166 :             if (!tl->at_end()) {
     103         [ +  + ]:         89 :                 if (i != j)
     104                 :         16 :                     swap(termlists[i], termlists[j]);
     105                 :         89 :                 ++j;
     106                 :            :             }
     107                 :            :         }
     108         [ +  + ]:        154 :         while (count > j)
     109         [ +  - ]:         77 :             delete termlists[--count];
     110                 :            :         Heap::make(termlists, termlists + count,
     111         [ +  - ]:         77 :                    CompareTermListsByTerm());
     112                 :            :     } else {
     113                 :            :         // Skip over current_term if we haven't already.
     114         [ +  + ]:        229 :         if (current_termfreq == 0)
     115                 :        195 :             (void)get_termfreq();
     116                 :            :     }
     117                 :            : 
     118                 :        306 :     current_termfreq = 0;
     119                 :            : 
     120         [ +  + ]:        306 :     if (count <= 1) {
     121         [ +  + ]:         70 :         if (count == 0) {
     122         [ +  - ]:         14 :             current_term = std::string();
     123                 :         14 :             return NULL;
     124                 :            :         }
     125                 :         56 :         count = 0;
     126                 :         56 :         return termlists[0];
     127                 :            :     }
     128                 :            : 
     129         [ +  - ]:        236 :     current_term = termlists[0]->get_termname();
     130                 :        306 :     return NULL;
     131                 :            : }
     132                 :            : 
     133                 :            : TermList *
     134                 :          5 : MultiAllTermsList::skip_to(const std::string &term)
     135                 :            : {
     136                 :            :     // Assume the skip is likely to be a long distance, and rebuild the heap
     137                 :            :     // from scratch.  FIXME: It would be useful to profile this against an
     138                 :            :     // approach more like that next() uses if this ever gets heavy use.
     139                 :          5 :     size_t j = 0;
     140         [ +  + ]:         15 :     for (size_t i = 0; i != count; ++i) {
     141                 :         10 :         TermList* tl = termlists[i];
     142                 :         10 :         tl->skip_to(term);
     143         [ +  + ]:         10 :         if (!tl->at_end()) {
     144         [ -  + ]:          6 :             if (i != j)
     145                 :          0 :                 swap(termlists[i], termlists[j]);
     146                 :          6 :             ++j;
     147                 :            :         }
     148                 :            :     }
     149         [ +  + ]:          9 :     while (count > j)
     150         [ +  - ]:          4 :         delete termlists[--count];
     151                 :            : 
     152                 :          5 :     current_termfreq = 0;
     153                 :            : 
     154         [ +  + ]:          5 :     if (count <= 1) {
     155         [ +  - ]:          2 :         if (count == 0) {
     156         [ +  - ]:          2 :             current_term = std::string();
     157                 :          2 :             return NULL;
     158                 :            :         }
     159                 :          0 :         count = 0;
     160                 :          0 :         return termlists[0];
     161                 :            :     }
     162                 :            : 
     163         [ +  - ]:          3 :     Heap::make(termlists, termlists + count, CompareTermListsByTerm());
     164                 :            : 
     165         [ +  - ]:          3 :     current_term = termlists[0]->get_termname();
     166                 :          5 :     return NULL;
     167                 :            : }
     168                 :            : 
     169                 :            : bool
     170                 :        255 : MultiAllTermsList::at_end() const
     171                 :            : {
     172 [ +  + ][ +  - ]:        255 :     return count == 0 && current_term.empty();
     173                 :            : }

Generated by: LCOV version 1.11