LCOV - code coverage report
Current view: top level - api - decvalwtsource.cc (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core c2b6f1024d3a Lines: 61 66 92.4 %
Date: 2019-05-16 09:13:18 Functions: 12 12 100.0 %
Branches: 45 74 60.8 %

           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 "net/length.h"
      27                 :            : #include "serialise-double.h"
      28                 :            : #include <cmath>
      29                 :            : 
      30                 :            : using namespace Xapian;
      31                 :            : 
      32                 :       1664 : 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                 :       1664 :           range_end(range_end_)
      39                 :            : {
      40                 :       1664 : }
      41                 :            : 
      42                 :            : double
      43                 :        199 : DecreasingValueWeightPostingSource::get_weight() const {
      44                 :        199 :     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                 :       1469 : DecreasingValueWeightPostingSource::name() const {
      55         [ +  - ]:       1469 :     return "Xapian::DecreasingValueWeightPostingSource";
      56                 :            : }
      57                 :            : 
      58                 :            : std::string
      59                 :         22 : DecreasingValueWeightPostingSource::serialise() const {
      60                 :         22 :     std::string result;
      61 [ +  - ][ +  - ]:         22 :     result += encode_length(get_slot());
      62 [ +  - ][ +  - ]:         22 :     result += encode_length(range_start);
      63 [ +  - ][ +  - ]:         22 :     result += encode_length(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         [ +  - ]:         22 :     decode_length(&pos, end, new_slot);
      74         [ +  - ]:         22 :     decode_length(&pos, end, new_range_start);
      75         [ +  - ]:         22 :     decode_length(&pos, end, new_range_end);
      76         [ -  + ]:         22 :     if (pos != end)
      77                 :            :         throw Xapian::NetworkError("Junk at end of serialised "
      78 [ #  # ][ #  # ]:          0 :                                    "DecreasingValueWeightPostingSource");
                 [ #  # ]
      79                 :            :     return new DecreasingValueWeightPostingSource(new_slot, new_range_start,
      80 [ +  - ][ +  - ]:         22 :                                                   new_range_end);
      81                 :            : }
      82                 :            : 
      83                 :            : void
      84                 :        129 : DecreasingValueWeightPostingSource::init(const Xapian::Database & db_) {
      85                 :        129 :     Xapian::ValueWeightPostingSource::init(db_);
      86 [ +  + ][ +  - ]:        129 :     if (range_end == 0 || get_database().get_doccount() <= range_end)
         [ +  - ][ +  + ]
                 [ +  + ]
           [ +  +  #  # ]
      87                 :         75 :         items_at_end = false;
      88                 :            :     else
      89                 :         54 :         items_at_end = true;
      90                 :        129 : }
      91                 :            : 
      92                 :            : void
      93                 :        460 : DecreasingValueWeightPostingSource::skip_if_in_range(double min_wt)
      94                 :            : {
      95         [ +  + ]:        460 :     if (ValueWeightPostingSource::at_end()) return;
      96                 :        364 :     curr_weight = Xapian::ValueWeightPostingSource::get_weight();
      97                 :        364 :     Xapian::docid docid = Xapian::ValueWeightPostingSource::get_docid();
      98 [ +  + ][ +  + ]:        364 :     if (docid >= range_start && (range_end == 0 || docid <= range_end)) {
                 [ +  + ]
      99         [ +  + ]:        290 :         if (items_at_end) {
     100         [ +  + ]:        148 :             if (curr_weight < min_wt) {
     101                 :            :                 // skip to end of range.
     102                 :         48 :                 ValueWeightPostingSource::skip_to(range_end + 1, min_wt);
     103         [ +  - ]:         48 :                 if (!ValueWeightPostingSource::at_end())
     104                 :         48 :                     curr_weight = Xapian::ValueWeightPostingSource::get_weight();
     105                 :            :             }
     106                 :            :         } else {
     107         [ +  + ]:        142 :             if (curr_weight < min_wt) {
     108                 :            :                 // terminate early.
     109                 :         20 :                 done();
     110                 :            :             } else {
     111                 :            :                 // Update max_weight.
     112                 :        122 :                 set_maxweight(curr_weight);
     113                 :            :             }
     114                 :            :         }
     115                 :            :     }
     116                 :            : }
     117                 :            : 
     118                 :            : void
     119                 :        436 : DecreasingValueWeightPostingSource::next(double min_wt) {
     120         [ +  + ]:        436 :     if (get_maxweight() < min_wt) {
     121                 :          6 :         done();
     122                 :          6 :         return;
     123                 :            :     }
     124                 :        430 :     Xapian::ValueWeightPostingSource::next(min_wt);
     125                 :        430 :     skip_if_in_range(min_wt);
     126                 :            : }
     127                 :            : 
     128                 :            : void
     129                 :         10 : DecreasingValueWeightPostingSource::skip_to(Xapian::docid min_docid,
     130                 :            :                                             double min_wt) {
     131         [ -  + ]:         10 :     if (get_maxweight() < min_wt) {
     132                 :          0 :         done();
     133                 :          0 :         return;
     134                 :            :     }
     135                 :         10 :     Xapian::ValueWeightPostingSource::skip_to(min_docid, min_wt);
     136                 :         10 :     skip_if_in_range(min_wt);
     137                 :            : }
     138                 :            : 
     139                 :            : bool
     140                 :         20 : DecreasingValueWeightPostingSource::check(Xapian::docid min_docid,
     141                 :            :                                           double min_wt) {
     142         [ -  + ]:         20 :     if (get_maxweight() < min_wt) {
     143                 :          0 :         done();
     144                 :          0 :         return true;
     145                 :            :     }
     146                 :         20 :     bool valid = Xapian::ValueWeightPostingSource::check(min_docid, min_wt);
     147         [ +  - ]:         20 :     if (valid) {
     148                 :         20 :         skip_if_in_range(min_wt);
     149                 :            :     }
     150                 :         20 :     return valid;
     151                 :            : }
     152                 :            : 
     153                 :            : std::string
     154                 :          1 : DecreasingValueWeightPostingSource::get_description() const {
     155         [ +  - ]:          1 :     return "DecreasingValueWeightPostingSource()";
     156                 :            : }

Generated by: LCOV version 1.11