LCOV - code coverage report
Current view: top level - matcher - boolorpostlist.h (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core 954b5873a738 Lines: 28 28 100.0 %
Date: 2019-06-30 05:20:33 Functions: 5 7 71.4 %
Branches: 21 56 37.5 %

           Branch data     Line data    Source code
       1                 :            : /** @file boolorpostlist.h
       2                 :            :  * @brief PostList class implementing unweighted Query::OP_OR
       3                 :            :  */
       4                 :            : /* Copyright 2017,2018 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                 :            : #ifndef XAPIAN_INCLUDED_BOOLORPOSTLIST_H
      22                 :            : #define XAPIAN_INCLUDED_BOOLORPOSTLIST_H
      23                 :            : 
      24                 :            : #include "api/postlist.h"
      25                 :            : 
      26                 :            : /// PostList class implementing unweighted Query::OP_OR
      27                 :            : class BoolOrPostList : public PostList {
      28                 :            :     /// Don't allow assignment.
      29                 :            :     void operator=(const BoolOrPostList&) = delete;
      30                 :            : 
      31                 :            :     /// Don't allow copying.
      32                 :            :     BoolOrPostList(const BoolOrPostList&) = delete;
      33                 :            : 
      34                 :            :     /// The current docid, or zero if we haven't started or are at_end.
      35                 :            :     Xapian::docid did;
      36                 :            : 
      37                 :            :     /// The number of sub-postlists.
      38                 :            :     size_t n_kids;
      39                 :            : 
      40                 :            :     struct PostListAndDocID {
      41                 :            :         PostList* pl;
      42                 :            : 
      43                 :            :         Xapian::docid did = 0;
      44                 :            : 
      45                 :       2063 :         PostListAndDocID() : pl(nullptr) { }
      46                 :            : 
      47                 :       2063 :         PostListAndDocID(PostList* pl_) : pl(pl_) { }
      48                 :            : 
      49                 :      16635 :         bool operator>(const PostListAndDocID& o) const {
      50                 :      16635 :             return did > o.did;
      51                 :            :         }
      52                 :            :     };
      53                 :            : 
      54                 :            :     /// Array of pointers to sub-postlists.
      55                 :            :     PostListAndDocID* plist;
      56                 :            : 
      57                 :            :     /** Total number of documents in the database. */
      58                 :            :     Xapian::doccount db_size;
      59                 :            : 
      60                 :            :     /** Helper to apply operation to all postlists matching current docid.
      61                 :            :      *
      62                 :            :      *  This function makes use of the heap structure, descending to any
      63                 :            :      *  children which match the current docid in an effectively recursive way
      64                 :            :      *  which needs O(1) storage, and evaluating func for each of them.
      65                 :            :      *
      66                 :            :      *  There's support for accumulating a value of type Xapian::termcount,
      67                 :            :      *  which is returned (of the three current uses, two want to accumulate a
      68                 :            :      *  value of this type, while the other doesn't need to accumulate a
      69                 :            :      *  value).
      70                 :            :      */
      71                 :            :     template<typename F>
      72                 :            :     Xapian::termcount
      73                 :       7480 :     for_all_matches(F func) const
      74                 :            :     {
      75                 :       7480 :         size_t i = 0;
      76                 :       7480 :         Xapian::termcount result = 0;
      77                 :            :         AssertEq(plist[0].did, did);
      78                 :            :         while (true) {
      79                 :       9704 :             result += func(plist[i].pl);
      80                 :            :             // Children of i are (2 * i + 1) and (2 * i + 2).
      81                 :       9704 :             size_t j = 2 * i + 1;
      82         [ #  # ]:       9704 :             if (j < n_kids && plist[j].did == did) {
           [ #  #  #  # ]
           [ #  #  +  + ]
                 [ +  + ]
      83                 :            :                 // Down left.
      84                 :       2045 :                 i = j;
      85                 :       2045 :                 continue;
      86                 :            :             }
      87 [ #  # ][ #  # ]:       7659 :             if (j + 1 < n_kids && plist[j + 1].did == did) {
         [ #  # ][ #  # ]
         [ +  + ][ +  + ]
      88                 :            :                 // Down right.
      89                 :         17 :                 i = j + 1;
      90                 :         17 :                 continue;
      91                 :            :             }
      92                 :            :     try_right:
      93 [ #  # ][ #  # ]:     356016 :             if ((i & 1) && i + 1 < n_kids && plist[i + 1].did == did) {
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ +  + ][ +  + ]
                 [ +  + ]
      94                 :            :                 // Right.
      95                 :        162 :                 ++i;
      96                 :        162 :                 continue;
      97                 :            :             }
      98                 :            :             // Up.
      99                 :     355854 :             i = (i - 1) / 2;
     100 [ #  # ][ #  # ]:     355854 :             if (i == 0) break;
                 [ +  + ]
     101                 :     348374 :             goto try_right;
     102                 :            :         }
     103                 :       9704 :         return result;
     104                 :            :     }
     105                 :            : 
     106                 :            :   public:
     107                 :            :     /** Construct from 2 random-access iterators to a container of PostList*,
     108                 :            :      *  a pointer to the matcher, and the document collection size.
     109                 :            :      */
     110                 :            :     template<class RandomItor>
     111                 :        825 :     BoolOrPostList(RandomItor pl_begin, RandomItor pl_end,
     112                 :            :                    Xapian::doccount db_size_)
     113                 :        825 :         : did(0), n_kids(pl_end - pl_begin), plist(NULL),
     114                 :        825 :           db_size(db_size_)
     115                 :            :     {
     116 [ +  - ][ +  - ]:       2888 :         plist = new PostListAndDocID[n_kids];
                 [ +  + ]
     117                 :            :         // This initialises all entries to have did 0, so all entries are
     118                 :            :         // equal, which is a valid heap.
     119         [ +  - ]:        825 :         std::copy(pl_begin, pl_end, plist);
     120                 :        825 :     }
     121                 :            : 
     122                 :            :     ~BoolOrPostList();
     123                 :            : 
     124                 :            :     Xapian::doccount get_termfreq_min() const;
     125                 :            : 
     126                 :            :     Xapian::doccount get_termfreq_max() const;
     127                 :            : 
     128                 :            :     Xapian::doccount get_termfreq_est() const;
     129                 :            : 
     130                 :            :     TermFreqs get_termfreq_est_using_stats(
     131                 :            :             const Xapian::Weight::Internal& stats) const;
     132                 :            : 
     133                 :            :     Xapian::docid get_docid() const;
     134                 :            : 
     135                 :            :     double get_weight(Xapian::termcount doclen,
     136                 :            :                       Xapian::termcount unique_terms) const;
     137                 :            : 
     138                 :            :     bool at_end() const;
     139                 :            : 
     140                 :            :     double recalc_maxweight();
     141                 :            : 
     142                 :            :     PostList* next(double w_min);
     143                 :            : 
     144                 :            :     PostList* skip_to(Xapian::docid did, double w_min);
     145                 :            : 
     146                 :            :     std::string get_description() const;
     147                 :            : 
     148                 :            :     Xapian::termcount get_wdf() const;
     149                 :            : 
     150                 :            :     Xapian::termcount count_matching_subqs() const;
     151                 :            : 
     152                 :            :     void gather_position_lists(OrPositionList* orposlist);
     153                 :            : };
     154                 :            : 
     155                 :            : #endif // XAPIAN_INCLUDED_BOOLORPOSTLIST_H

Generated by: LCOV version 1.11