LCOV - code coverage report
Current view: top level - backends/glass - glass_positionlist.h (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core c2b6f1024d3a Lines: 22 22 100.0 %
Date: 2019-05-16 09:13:18 Functions: 11 11 100.0 %
Branches: 10 20 50.0 %

           Branch data     Line data    Source code
       1                 :            : /** @file glass_positionlist.h
       2                 :            :  * @brief A position list in a glass database.
       3                 :            :  */
       4                 :            : /* Copyright (C) 2005,2006,2008,2009,2010,2011,2013,2016,2017,2019 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
      19                 :            :  * USA
      20                 :            :  */
      21                 :            : 
      22                 :            : #ifndef XAPIAN_INCLUDED_GLASS_POSITIONLIST_H
      23                 :            : #define XAPIAN_INCLUDED_GLASS_POSITIONLIST_H
      24                 :            : 
      25                 :            : #include <xapian/types.h>
      26                 :            : 
      27                 :            : #include "bitstream.h"
      28                 :            : #include "glass_cursor.h"
      29                 :            : #include "glass_lazytable.h"
      30                 :            : #include "pack.h"
      31                 :            : #include "backends/positionlist.h"
      32                 :            : 
      33                 :            : #include <string>
      34                 :            : 
      35                 :            : using namespace std;
      36                 :            : 
      37                 :       7940 : class GlassPositionListTable : public GlassLazyTable {
      38                 :            :   public:
      39                 :    1944271 :     static string make_key(Xapian::docid did, const string & term) {
      40                 :    1944271 :         string key;
      41         [ +  - ]:    1944271 :         pack_string_preserving_sort(key, term);
      42         [ +  - ]:    1944271 :         pack_uint_preserving_sort(key, did);
      43                 :    1944271 :         return key;
      44                 :            :     }
      45                 :            : 
      46                 :            :     /** Create a new GlassPositionListTable object.
      47                 :            :      *
      48                 :            :      *  This method does not create or open the table on disk - you
      49                 :            :      *  must call the create() or open() methods respectively!
      50                 :            :      *
      51                 :            :      *  @param dbdir            The directory the glass database is stored in.
      52                 :            :      *  @param readonly         true if we're opening read-only, else false.
      53                 :            :      */
      54                 :       3665 :     GlassPositionListTable(const string & dbdir, bool readonly)
      55         [ +  - ]:       3665 :         : GlassLazyTable("position", dbdir + "/position.", readonly) { }
      56                 :            : 
      57                 :        308 :     GlassPositionListTable(int fd, off_t offset_, bool readonly_)
      58                 :        308 :         : GlassLazyTable("position", fd, offset_, readonly_) { }
      59                 :            : 
      60                 :            :     /** Pack a position list into a string.
      61                 :            :      *
      62                 :            :      *  @param s The string to append the position list data to.
      63                 :            :      */
      64                 :            :     void pack(string & s, const Xapian::VecCOW<Xapian::termpos> & vec) const;
      65                 :            : 
      66                 :            :     /** Set the position list for term tname in document did.
      67                 :            :      */
      68                 :    1814675 :     void set_positionlist(Xapian::docid did, const string & tname,
      69                 :            :                           const string & s) {
      70 [ +  - ][ +  - ]:    1814675 :         add(make_key(did, tname), s);
      71                 :    1814675 :     }
      72                 :            : 
      73                 :            :     /// Delete the position list for term tname in document did.
      74                 :      28675 :     void delete_positionlist(Xapian::docid did, const string & tname) {
      75         [ +  - ]:      28675 :         del(make_key(did, tname));
      76                 :      28675 :     }
      77                 :            : 
      78                 :            :     /// Return the number of entries in specified position list data.
      79                 :            :     Xapian::termcount positionlist_count(const string& data) const;
      80                 :            : 
      81                 :            :     /// Return the number of entries in specified position list.
      82                 :            :     Xapian::termcount positionlist_count(Xapian::docid did,
      83                 :            :                                          const string & term) const;
      84                 :            : };
      85                 :            : 
      86                 :            : /** Base-class for a position list in a glass database. */
      87         [ -  + ]:     150196 : class GlassBasePositionList : public PositionList {
      88                 :            :     /// Copying is not allowed.
      89                 :            :     GlassBasePositionList(const GlassBasePositionList&) = delete;
      90                 :            : 
      91                 :            :     /// Assignment is not allowed.
      92                 :            :     GlassBasePositionList& operator=(const GlassBasePositionList&) = delete;
      93                 :            : 
      94                 :            :   protected:
      95                 :            :     /// Interpolative decoder.
      96                 :            :     BitReader rd;
      97                 :            : 
      98                 :            :     /// Current entry.
      99                 :            :     Xapian::termpos current_pos;
     100                 :            : 
     101                 :            :     /// Last entry.
     102                 :            :     Xapian::termpos last;
     103                 :            : 
     104                 :            :     /// Number of entries.
     105                 :            :     Xapian::termcount size;
     106                 :            : 
     107                 :            :     /// Have we started iterating yet?
     108                 :            :     bool have_started;
     109                 :            : 
     110                 :            :     /** Set positional data and start to decode it.
     111                 :            :      *
     112                 :            :      *  @param data     The positional data.  Must stay valid
     113                 :            :      *                  while this object is using it.
     114                 :            :      */
     115                 :            :     void set_data(const string& data);
     116                 :            : 
     117                 :            :   public:
     118                 :            :     /// Default constructor.
     119                 :     150196 :     GlassBasePositionList() {}
     120                 :            : 
     121                 :            :     /// Returns size of position list.
     122                 :            :     Xapian::termcount get_approx_size() const;
     123                 :            : 
     124                 :            :     Xapian::termpos back() const;
     125                 :            : 
     126                 :            :     /** Returns current position.
     127                 :            :      *
     128                 :            :      *  Either next() or skip_to() must have been called before this
     129                 :            :      *  method can be called.
     130                 :            :      */
     131                 :            :     Xapian::termpos get_position() const;
     132                 :            : 
     133                 :            :     /// Advance to the next term position in the list.
     134                 :            :     bool next();
     135                 :            : 
     136                 :            :     /// Advance to the first term position which is at least termpos.
     137                 :            :     bool skip_to(Xapian::termpos termpos);
     138                 :            : };
     139                 :            : 
     140                 :            : /** A position list in a glass database. */
     141         [ -  + ]:     296692 : class GlassPositionList : public GlassBasePositionList {
     142                 :            :     /// The encoded positional data being read by rd.
     143                 :            :     std::string pos_data;
     144                 :            : 
     145                 :            :     /// Copying is not allowed.
     146                 :            :     GlassPositionList(const GlassPositionList&) = delete;
     147                 :            : 
     148                 :            :     /// Assignment is not allowed.
     149                 :            :     GlassPositionList& operator=(const GlassPositionList&) = delete;
     150                 :            : 
     151                 :            :   public:
     152                 :            :     /// Construct and initialise with data.
     153                 :            :     explicit
     154                 :            :     GlassPositionList(string&& data);
     155                 :            : 
     156                 :            :     /// Construct and initialise with data.
     157                 :            :     GlassPositionList(const GlassTable* table,
     158                 :            :                       Xapian::docid did,
     159                 :            :                       const string& term);
     160                 :            : };
     161                 :            : 
     162                 :            : /** A reusable position list in a glass database. */
     163         [ -  + ]:       3688 : class GlassRePositionList : public GlassBasePositionList {
     164                 :            :     /// Cursor for locating multiple entries efficiently.
     165                 :            :     GlassCursor cursor;
     166                 :            : 
     167                 :            :     /// Copying is not allowed.
     168                 :            :     GlassRePositionList(const GlassRePositionList&) = delete;
     169                 :            : 
     170                 :            :     /// Assignment is not allowed.
     171                 :            :     GlassRePositionList& operator=(const GlassRePositionList&) = delete;
     172                 :            : 
     173                 :            :   public:
     174                 :            :     /// Constructor.
     175                 :            :     explicit
     176                 :        922 :     GlassRePositionList(const GlassTable* table)
     177         [ +  - ]:        922 :         : cursor(table) {}
     178                 :            : 
     179                 :            :     /** Fill list with data, and move the position to the start. */
     180                 :            :     void assign_data(string&& data);
     181                 :            : 
     182                 :            :     /** Fill list with data, and move the position to the start. */
     183                 :            :     void read_data(Xapian::docid did,
     184                 :            :                    const string& term);
     185                 :            : };
     186                 :            : 
     187                 :            : #endif /* XAPIAN_INCLUDED_GLASS_POSITIONLIST_H */

Generated by: LCOV version 1.11