LCOV - code coverage report
Current view: top level - include/xapian - keymaker.h (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core 954b5873a738 Lines: 16 16 100.0 %
Date: 2019-06-30 05:20:33 Functions: 10 10 100.0 %
Branches: 7 16 43.8 %

           Branch data     Line data    Source code
       1                 :            : /** @file keymaker.h
       2                 :            :  * @brief Build key strings for MSet ordering or collapsing.
       3                 :            :  */
       4                 :            : /* Copyright (C) 2007,2009,2011,2013,2014,2015,2016 Olly Betts
       5                 :            :  * Copyright (C) 2010 Richard Boulton
       6                 :            :  *
       7                 :            :  * This program is free software; you can redistribute it and/or
       8                 :            :  * modify it under the terms of the GNU General Public License as
       9                 :            :  * published by the Free Software Foundation; either version 2 of the
      10                 :            :  * License, or (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                 :            : #ifndef XAPIAN_INCLUDED_KEYMAKER_H
      23                 :            : #define XAPIAN_INCLUDED_KEYMAKER_H
      24                 :            : 
      25                 :            : #if !defined XAPIAN_IN_XAPIAN_H && !defined XAPIAN_LIB_BUILD
      26                 :            : # error "Never use <xapian/keymaker.h> directly; include <xapian.h> instead."
      27                 :            : #endif
      28                 :            : 
      29                 :            : #include <string>
      30                 :            : #include <vector>
      31                 :            : 
      32                 :            : #include <xapian/intrusive_ptr.h>
      33                 :            : #include <xapian/types.h>
      34                 :            : #include <xapian/visibility.h>
      35                 :            : 
      36                 :            : namespace Xapian {
      37                 :            : 
      38                 :            : class Document;
      39                 :            : 
      40                 :            : /** Virtual base class for key making functors. */
      41                 :            : class XAPIAN_VISIBILITY_DEFAULT KeyMaker
      42                 :            :     : public Xapian::Internal::opt_intrusive_base {
      43                 :            :     /// Don't allow assignment.
      44                 :            :     void operator=(const KeyMaker &) = delete;
      45                 :            : 
      46                 :            :     /// Don't allow copying.
      47                 :            :     KeyMaker(const KeyMaker &) = delete;
      48                 :            : 
      49                 :            :   public:
      50                 :            :     /// Default constructor.
      51                 :        242 :     KeyMaker() { }
      52                 :            : 
      53                 :            :     /** Build a key string for a Document.
      54                 :            :      *
      55                 :            :      *  These keys can be used for sorting or collapsing matching documents.
      56                 :            :      *
      57                 :            :      *  @param doc      Document object to build a key for.
      58                 :            :      */
      59                 :            :     virtual std::string operator()(const Xapian::Document & doc) const = 0;
      60                 :            : 
      61                 :            :     /** Virtual destructor, because we have virtual methods. */
      62                 :            :     virtual ~KeyMaker();
      63                 :            : 
      64                 :            :     /** Start reference counting this object.
      65                 :            :      *
      66                 :            :      *  You can hand ownership of a dynamically allocated KeyMaker
      67                 :            :      *  object to Xapian by calling release() and then passing the object to a
      68                 :            :      *  Xapian method.  Xapian will arrange to delete the object once it is no
      69                 :            :      *  longer required.
      70                 :            :      */
      71                 :         42 :     KeyMaker * release() {
      72                 :         42 :         opt_intrusive_base::release();
      73                 :         42 :         return this;
      74                 :            :     }
      75                 :            : 
      76                 :            :     /** Start reference counting this object.
      77                 :            :      *
      78                 :            :      *  You can hand ownership of a dynamically allocated KeyMaker
      79                 :            :      *  object to Xapian by calling release() and then passing the object to a
      80                 :            :      *  Xapian method.  Xapian will arrange to delete the object once it is no
      81                 :            :      *  longer required.
      82                 :            :      */
      83                 :            :     const KeyMaker * release() const {
      84                 :            :         opt_intrusive_base::release();
      85                 :            :         return this;
      86                 :            :     }
      87                 :            : };
      88                 :            : 
      89                 :            : /** KeyMaker subclass which combines several values.
      90                 :            :  *
      91                 :            :  *  When the result is used for sorting, results are ordered by the first
      92                 :            :  *  value.  In the event of a tie, the second is used.  If this is the same for
      93                 :            :  *  both, the third is used, and so on.  If @a reverse is true for a value,
      94                 :            :  *  then the sort order for that value is reversed.
      95                 :            :  *
      96                 :            :  *  When used for collapsing, the documents will only be considered equal if
      97                 :            :  *  all the values specified match.  If none of the specified values are set
      98                 :            :  *  then the generated key will be empty, so such documents won't be collapsed
      99                 :            :  *  (which is consistent with the behaviour in the "collapse on a value" case).
     100                 :            :  *  If you'd prefer that documents with none of the keys set are collapsed
     101                 :            :  *  together, then you can set @a reverse for at least one of the values.
     102                 :            :  *  Other than this, it isn't useful to set @a reverse for collapsing.
     103                 :            :  */
     104         [ -  + ]:        140 : class XAPIAN_VISIBILITY_DEFAULT MultiValueKeyMaker : public KeyMaker {
     105                 :        844 :     struct KeySpec {
     106                 :            :         Xapian::valueno slot;
     107                 :            :         bool reverse;
     108                 :            :         std::string defvalue;
     109                 :        108 :         KeySpec(Xapian::valueno slot_, bool reverse_,
     110                 :            :                 const std::string & defvalue_)
     111                 :        108 :                 : slot(slot_), reverse(reverse_), defvalue(defvalue_)
     112                 :        108 :         {}
     113                 :            :     };
     114                 :            :     std::vector<KeySpec> slots;
     115                 :            : 
     116                 :            :   public:
     117                 :        118 :     MultiValueKeyMaker() { }
     118                 :            : 
     119                 :            :     /** Construct a MultiValueKeyMaker from a pair of iterators.
     120                 :            :      *
     121                 :            :      *  The iterators must be a begin/end pair returning Xapian::valueno (or
     122                 :            :      *  a compatible type) when dereferenced.
     123                 :            :      */
     124                 :            :     template<class Iterator>
     125                 :         22 :     MultiValueKeyMaker(Iterator begin, Iterator end) {
     126 [ #  # ][ #  # ]:         25 :         while (begin != end) add_value(*begin++);
         [ -  + ][ +  - ]
         [ +  - ][ +  + ]
     127                 :         11 :     }
     128                 :            : 
     129                 :            :     virtual std::string operator()(const Xapian::Document & doc) const;
     130                 :            : 
     131                 :            :     /** Add a value slot to the list to build a key from.
     132                 :            :      *
     133                 :            :      *  @param slot     The value slot to add
     134                 :            :      *  @param reverse  Adjust values from this slot to reverse their sort
     135                 :            :      *                  order (default: false)
     136                 :            :      *  @param defvalue Value to use for documents which don't have a value
     137                 :            :      *                  set in this slot (default: empty).  This can be used
     138                 :            :      *                  to make such documents sort after all others by
     139                 :            :      *                  passing <code>get_value_upper_bound(slot) + "x"</code>
     140                 :            :      *                  - this is guaranteed to be greater than any value in
     141                 :            :      *                  this slot.
     142                 :            :      */
     143                 :        108 :     void add_value(Xapian::valueno slot, bool reverse = false,
     144                 :            :                    const std::string & defvalue = std::string()) {
     145         [ +  - ]:        108 :         slots.push_back(KeySpec(slot, reverse, defvalue));
     146                 :        108 :     }
     147                 :            : };
     148                 :            : 
     149                 :            : }
     150                 :            : 
     151                 :            : #endif // XAPIAN_INCLUDED_KEYMAKER_H

Generated by: LCOV version 1.11