LCOV - code coverage report
Current view: top level - api - decvalwtsource.cc (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core 954b5873a738 Lines: 60 65 92.3 %
Date: 2019-06-30 05:20:33 Functions: 12 12 100.0 %
Branches: 42 62 67.7 %

           Branch data     Line data    Source code
       1                 :            : /** @file decvalwtsource.cc
       2                 :            :  * @brief A posting source which returns decreasing weights from a value.
       3                 :            :  */
       4                 :            : /* Copyright (C) 2009 Lemur Consulting Ltd
       5                 :            :  * Copyright (C) 2011,2012,2015,2016 Olly Betts
       6                 :            :  *
       7                 :            :  * This program is free software; you can redistribute it and/or modify
       8                 :            :  * it under the terms of the GNU General Public License as published by
       9                 :            :  * the Free Software Foundation; either version 2 of the License, or
      10                 :            :  * (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 "xapian/postingsource.h"
      25                 :            : #include "xapian/error.h"
      26                 :            : #include "pack.h"
      27                 :            : #include "serialise-double.h"
      28                 :            : #include <cmath>
      29                 :            : 
      30                 :            : using namespace Xapian;
      31                 :            : 
      32                 :       1779 : DecreasingValueWeightPostingSource::DecreasingValueWeightPostingSource(
      33                 :            :     Xapian::valueno slot_,
      34                 :            :     Xapian::docid range_start_,
      35                 :            :     Xapian::docid range_end_)
      36                 :            :         : Xapian::ValueWeightPostingSource(slot_),
      37                 :            :           range_start(range_start_),
      38                 :       1779 :           range_end(range_end_)
      39                 :            : {
      40                 :       1779 : }
      41                 :            : 
      42                 :            : double
      43                 :        201 : DecreasingValueWeightPostingSource::get_weight() const {
      44                 :        201 :     return curr_weight;
      45                 :            : }
      46                 :            : 
      47                 :            : Xapian::DecreasingValueWeightPostingSource *
      48                 :         92 : DecreasingValueWeightPostingSource::clone() const {
      49                 :         92 :     return new DecreasingValueWeightPostingSource(get_slot(), range_start,
      50         [ +  - ]:         92 :                                                   range_end);
      51                 :            : }
      52                 :            : 
      53                 :            : std::string
      54                 :       1581 : DecreasingValueWeightPostingSource::name() const {
      55         [ +  - ]:       1581 :     return "Xapian::DecreasingValueWeightPostingSource";
      56                 :            : }
      57                 :            : 
      58                 :            : std::string
      59                 :         22 : DecreasingValueWeightPostingSource::serialise() const {
      60                 :         22 :     std::string result;
      61         [ +  - ]:         22 :     pack_uint(result, get_slot());
      62         [ +  - ]:         22 :     pack_uint(result, range_start);
      63         [ +  - ]:         22 :     pack_uint_last(result, range_end);
      64                 :         22 :     return result;
      65                 :            : }
      66                 :            : 
      67                 :            : Xapian::DecreasingValueWeightPostingSource *
      68                 :         22 : DecreasingValueWeightPostingSource::unserialise(const std::string &s) const {
      69                 :         22 :     const char * pos = s.data();
      70                 :         22 :     const char * end = pos + s.size();
      71                 :            :     Xapian::valueno new_slot;
      72                 :            :     Xapian::docid new_range_start, new_range_end;
      73 [ +  - ][ -  + ]:         66 :     if (!unpack_uint(&pos, end, &new_slot) ||
      74 [ +  - ][ -  + ]:         44 :         !unpack_uint(&pos, end, &new_range_start) ||
      75                 :         22 :         !unpack_uint_last(&pos, end, &new_range_end)) {
      76                 :          0 :         unpack_throw_serialisation_error(pos);
      77                 :            :     }
      78                 :            :     return new DecreasingValueWeightPostingSource(new_slot, new_range_start,
      79 [ +  - ][ +  - ]:         22 :                                                   new_range_end);
      80                 :            : }
      81                 :            : 
      82                 :            : void
      83                 :        131 : DecreasingValueWeightPostingSource::init(const Xapian::Database & db_) {
      84                 :        131 :     Xapian::ValueWeightPostingSource::init(db_);
      85 [ +  + ][ +  - ]:        131 :     if (range_end == 0 || get_database().get_doccount() <= range_end)
         [ +  - ][ +  + ]
                 [ +  + ]
           [ +  +  #  # ]
      86                 :         77 :         items_at_end = false;
      87                 :            :     else
      88                 :         54 :         items_at_end = true;
      89                 :        131 : }
      90                 :            : 
      91                 :            : void
      92                 :        462 : DecreasingValueWeightPostingSource::skip_if_in_range(double min_wt)
      93                 :            : {
      94         [ +  + ]:        462 :     if (ValueWeightPostingSource::at_end()) return;
      95                 :        366 :     curr_weight = Xapian::ValueWeightPostingSource::get_weight();
      96                 :        366 :     Xapian::docid docid = Xapian::ValueWeightPostingSource::get_docid();
      97 [ +  + ][ +  + ]:        366 :     if (docid >= range_start && (range_end == 0 || docid <= range_end)) {
                 [ +  + ]
      98         [ +  + ]:        292 :         if (items_at_end) {
      99         [ +  + ]:        148 :             if (curr_weight < min_wt) {
     100                 :            :                 // skip to end of range.
     101                 :         48 :                 ValueWeightPostingSource::skip_to(range_end + 1, min_wt);
     102         [ +  - ]:         48 :                 if (!ValueWeightPostingSource::at_end())
     103                 :         48 :                     curr_weight = Xapian::ValueWeightPostingSource::get_weight();
     104                 :            :             }
     105                 :            :         } else {
     106         [ +  + ]:        144 :             if (curr_weight < min_wt) {
     107                 :            :                 // terminate early.
     108                 :         20 :                 done();
     109                 :            :             } else {
     110                 :            :                 // Update max_weight.
     111                 :        124 :                 set_maxweight(curr_weight);
     112                 :            :             }
     113                 :            :         }
     114                 :            :     }
     115                 :            : }
     116                 :            : 
     117                 :            : void
     118                 :        438 : DecreasingValueWeightPostingSource::next(double min_wt) {
     119         [ +  + ]:        438 :     if (get_maxweight() < min_wt) {
     120                 :          6 :         done();
     121                 :          6 :         return;
     122                 :            :     }
     123                 :        432 :     Xapian::ValueWeightPostingSource::next(min_wt);
     124                 :        432 :     skip_if_in_range(min_wt);
     125                 :            : }
     126                 :            : 
     127                 :            : void
     128                 :         10 : DecreasingValueWeightPostingSource::skip_to(Xapian::docid min_docid,
     129                 :            :                                             double min_wt) {
     130         [ -  + ]:         10 :     if (get_maxweight() < min_wt) {
     131                 :          0 :         done();
     132                 :          0 :         return;
     133                 :            :     }
     134                 :         10 :     Xapian::ValueWeightPostingSource::skip_to(min_docid, min_wt);
     135                 :         10 :     skip_if_in_range(min_wt);
     136                 :            : }
     137                 :            : 
     138                 :            : bool
     139                 :         20 : DecreasingValueWeightPostingSource::check(Xapian::docid min_docid,
     140                 :            :                                           double min_wt) {
     141         [ -  + ]:         20 :     if (get_maxweight() < min_wt) {
     142                 :          0 :         done();
     143                 :          0 :         return true;
     144                 :            :     }
     145                 :         20 :     bool valid = Xapian::ValueWeightPostingSource::check(min_docid, min_wt);
     146         [ +  - ]:         20 :     if (valid) {
     147                 :         20 :         skip_if_in_range(min_wt);
     148                 :            :     }
     149                 :         20 :     return valid;
     150                 :            : }
     151                 :            : 
     152                 :            : std::string
     153                 :          1 : DecreasingValueWeightPostingSource::get_description() const {
     154         [ +  - ]:          1 :     return "DecreasingValueWeightPostingSource()";
     155                 :            : }

Generated by: LCOV version 1.11