LCOV - code coverage report
Current view: top level - backends/glass - glass_postlist.h (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core 954b5873a738 Lines: 22 22 100.0 %
Date: 2019-06-30 05:20:33 Functions: 11 11 100.0 %
Branches: 3 4 75.0 %

           Branch data     Line data    Source code
       1                 :            : /** @file glass_postlist.h
       2                 :            :  * @brief Postlists in glass databases
       3                 :            :  */
       4                 :            : /* Copyright 1999,2000,2001 BrightStation PLC
       5                 :            :  * Copyright 2002 Ananova Ltd
       6                 :            :  * Copyright 2002,2003,2004,2005,2007,2008,2009,2011,2013,2014,2015,2017 Olly Betts
       7                 :            :  * Copyright 2007,2009 Lemur Consulting Ltd
       8                 :            :  *
       9                 :            :  * This program is free software; you can redistribute it and/or
      10                 :            :  * modify it under the terms of the GNU General Public License as
      11                 :            :  * published by the Free Software Foundation; either version 2 of the
      12                 :            :  * License, or (at your option) any later version.
      13                 :            :  *
      14                 :            :  * This program is distributed in the hope that it will be useful,
      15                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      16                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17                 :            :  * GNU General Public License for more details.
      18                 :            :  *
      19                 :            :  * You should have received a copy of the GNU General Public License
      20                 :            :  * along with this program; if not, write to the Free Software
      21                 :            :  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
      22                 :            :  * USA
      23                 :            :  */
      24                 :            : 
      25                 :            : #ifndef XAPIAN_INCLUDED_GLASS_POSTLIST_H
      26                 :            : #define XAPIAN_INCLUDED_GLASS_POSTLIST_H
      27                 :            : 
      28                 :            : #include <xapian/database.h>
      29                 :            : 
      30                 :            : #include "glass_defs.h"
      31                 :            : #include "glass_inverter.h"
      32                 :            : #include "glass_positionlist.h"
      33                 :            : #include "api/leafpostlist.h"
      34                 :            : #include "omassert.h"
      35                 :            : 
      36                 :            : #include <memory>
      37                 :            : #include <map>
      38                 :            : #include <string>
      39                 :            : 
      40                 :            : using namespace std;
      41                 :            : 
      42                 :            : class GlassCursor;
      43                 :            : class GlassDatabase;
      44                 :            : 
      45                 :            : namespace Glass {
      46                 :            :     class PostlistChunkReader;
      47                 :            :     class PostlistChunkWriter;
      48                 :            :     class RootInfo;
      49                 :            : }
      50                 :            : 
      51                 :            : using Glass::RootInfo;
      52                 :            : 
      53                 :            : class GlassPostList;
      54                 :            : 
      55                 :       8256 : class GlassPostListTable : public GlassTable {
      56                 :            :     /// PostList for looking up document lengths.
      57                 :            :     mutable unique_ptr<GlassPostList> doclen_pl;
      58                 :            : 
      59                 :            :   public:
      60                 :            :     /** Create a new table object.
      61                 :            :      *
      62                 :            :      *  This does not create the table on disk - the create() method must
      63                 :            :      *  be called before the table is created on disk
      64                 :            :      *
      65                 :            :      *  This also does not open the table - the open() method must be
      66                 :            :      *  called before use is made of the table.
      67                 :            :      *
      68                 :            :      *  @param path_          - Path at which the table is stored.
      69                 :            :      *  @param readonly_      - whether to open the table for read only
      70                 :            :      *                          access.
      71                 :            :      */
      72                 :       3819 :     GlassPostListTable(const string & path_, bool readonly_)
      73                 :       7638 :         : GlassTable("postlist", path_ + "/postlist.", readonly_),
      74         [ +  - ]:       7638 :           doclen_pl()
      75                 :       3819 :     { }
      76                 :            : 
      77                 :        309 :     GlassPostListTable(int fd, off_t offset_, bool readonly_)
      78                 :            :         : GlassTable("postlist", fd, offset_, readonly_),
      79                 :        309 :           doclen_pl()
      80                 :        309 :     { }
      81                 :            : 
      82                 :       3161 :     void open(int flags_, const RootInfo & root_info,
      83                 :            :               glass_revision_number_t rev) {
      84                 :       3161 :         doclen_pl.reset(0);
      85                 :       3161 :         GlassTable::open(flags_, root_info, rev);
      86                 :       3161 :     }
      87                 :            : 
      88                 :            :     /// Merge changes for a term.
      89                 :            :     void merge_changes(const string& term,
      90                 :            :                        const Inverter::PostingChanges& changes);
      91                 :            : 
      92                 :            :     /// Merge document length changes.
      93                 :            :     void merge_doclen_changes(const map<Xapian::docid,
      94                 :            :                                         Xapian::termcount>& doclens);
      95                 :            : 
      96                 :            :     Xapian::docid get_chunk(const string& tname,
      97                 :            :                             Xapian::docid did, bool adding,
      98                 :            :                             Glass::PostlistChunkReader** from,
      99                 :            :                             Glass::PostlistChunkWriter** to);
     100                 :            : 
     101                 :            :     /// Compose a key from a termname and docid.
     102                 :     490719 :     static string make_key(const string& term, Xapian::docid did) {
     103                 :     490719 :         return pack_glass_postlist_key(term, did);
     104                 :            :     }
     105                 :            : 
     106                 :            :     /// Compose a key from a termname.
     107                 :    1773714 :     static string make_key(const string & term) {
     108                 :    1773714 :         return pack_glass_postlist_key(term);
     109                 :            :     }
     110                 :            : 
     111                 :       2491 :     bool term_exists(const string & term) const {
     112         [ +  + ]:       2491 :         return key_exists(make_key(term));
     113                 :            :     }
     114                 :            : 
     115                 :            :     /** Returns frequencies for a term.
     116                 :            :      *
     117                 :            :      *  @param term             The term to get frequencies for
     118                 :            :      *  @param termfreq_ptr     Point to return number of docs indexed by @a
     119                 :            :      *                  term (or NULL not to return)
     120                 :            :      *  @param collfreq_ptr     Point to return number of occurrences of @a
     121                 :            :      *                  term in the database (or NULL not to return)
     122                 :            :      *  @param wdfub_ptr        Point to return an upper bound on the wdf
     123                 :            :      *                  of @a term in the database (or NULL not to
     124                 :            :      *                  return)
     125                 :            :      */
     126                 :            :     void get_freqs(const std::string & term,
     127                 :            :                    Xapian::doccount * termfreq_ptr,
     128                 :            :                    Xapian::termcount * collfreq_ptr,
     129                 :            :                    Xapian::termcount * wdfub_ptr = NULL) const;
     130                 :            : 
     131                 :            :     /** Returns the length of document @a did. */
     132                 :            :     Xapian::termcount get_doclength(Xapian::docid did,
     133                 :            :                                     Xapian::Internal::intrusive_ptr<const GlassDatabase> db) const;
     134                 :            : 
     135                 :            :     /** Check if document @a did exists. */
     136                 :            :     bool document_exists(Xapian::docid did,
     137                 :            :                          Xapian::Internal::intrusive_ptr<const GlassDatabase> db) const;
     138                 :            : 
     139                 :            :     void get_used_docid_range(Xapian::docid & first,
     140                 :            :                               Xapian::docid & last) const;
     141                 :            : };
     142                 :            : 
     143                 :            : /** A postlist in a glass database.
     144                 :            :  */
     145                 :            : class GlassPostList : public LeafPostList {
     146                 :            :     /** The database we are searching.  This pointer is held so that the
     147                 :            :      *  database doesn't get deleted before us, and also to give us access
     148                 :            :      *  to the position_table.
     149                 :            :      */
     150                 :            :     Xapian::Internal::intrusive_ptr<const GlassDatabase> this_db;
     151                 :            : 
     152                 :            :     /// The position list object for this posting list.
     153                 :            :     GlassRePositionList* positionlist = NULL;
     154                 :            : 
     155                 :            :     /// Whether we've started reading the list yet.
     156                 :            :     bool have_started;
     157                 :            : 
     158                 :            :     /// True if this is the last chunk.
     159                 :            :     bool is_last_chunk;
     160                 :            : 
     161                 :            :     /// Whether we've run off the end of the list yet.
     162                 :            :     bool is_at_end;
     163                 :            : 
     164                 :            :     /// Cursor pointing to current chunk of postlist.
     165                 :            :     unique_ptr<GlassCursor> cursor;
     166                 :            : 
     167                 :            :     /// The first document id in this chunk.
     168                 :            :     Xapian::docid first_did_in_chunk;
     169                 :            : 
     170                 :            :     /// The last document id in this chunk.
     171                 :            :     Xapian::docid last_did_in_chunk;
     172                 :            : 
     173                 :            :     /// Position of iteration through current chunk.
     174                 :            :     const char * pos;
     175                 :            : 
     176                 :            :     /// Pointer to byte after end of current chunk.
     177                 :            :     const char * end;
     178                 :            : 
     179                 :            :     /// Document id we're currently at.
     180                 :            :     Xapian::docid did;
     181                 :            : 
     182                 :            :     /// The wdf of the current document.
     183                 :            :     Xapian::termcount wdf;
     184                 :            : 
     185                 :            :     /// The number of entries in the posting list.
     186                 :            :     Xapian::doccount number_of_entries;
     187                 :            : 
     188                 :            :     /// Copying is not allowed.
     189                 :            :     GlassPostList(const GlassPostList &);
     190                 :            : 
     191                 :            :     /// Assignment is not allowed.
     192                 :            :     void operator=(const GlassPostList &);
     193                 :            : 
     194                 :            :     /** Move to the next item in the chunk, if possible.
     195                 :            :      *  If already at the end of the chunk, returns false.
     196                 :            :      */
     197                 :            :     bool next_in_chunk();
     198                 :            : 
     199                 :            :     /** Move to the next chunk.
     200                 :            :      *
     201                 :            :      *  If there are no more chunks in this postlist, this will set
     202                 :            :      *  is_at_end to true.
     203                 :            :      */
     204                 :            :     void next_chunk();
     205                 :            : 
     206                 :            :     /** Return true if the given document ID lies in the range covered
     207                 :            :      *  by the current chunk.  This does not say whether the document ID
     208                 :            :      *  is actually present.  It will return false if the document ID
     209                 :            :      *  is greater than the last document ID in the chunk, even if it is
     210                 :            :      *  less than the first document ID in the next chunk: it is possible
     211                 :            :      *  for no chunk to contain a particular document ID.
     212                 :            :      */
     213                 :            :     bool current_chunk_contains(Xapian::docid desired_did);
     214                 :            : 
     215                 :            :     /** Move to chunk containing the specified document ID.
     216                 :            :      *
     217                 :            :      *  This moves to the chunk whose starting document ID is
     218                 :            :      *  <= desired_did, but such that the next chunk's starting
     219                 :            :      *  document ID is > desired_did.
     220                 :            :      *
     221                 :            :      *  It is thus possible that current_chunk_contains(desired_did)
     222                 :            :      *  will return false after this call, since the document ID
     223                 :            :      *  might lie after the end of this chunk, but before the start
     224                 :            :      *  of the next chunk.
     225                 :            :      */
     226                 :            :     void move_to_chunk_containing(Xapian::docid desired_did);
     227                 :            : 
     228                 :            :     /** Scan forward in the current chunk for the specified document ID.
     229                 :            :      *
     230                 :            :      *  This is particularly efficient if the desired document ID is
     231                 :            :      *  greater than the last in the chunk - it then skips straight
     232                 :            :      *  to the end.
     233                 :            :      *
     234                 :            :      *  @return true if we moved to a valid document,
     235                 :            :      *      false if we reached the end of the chunk.
     236                 :            :      */
     237                 :            :     bool move_forward_in_chunk_to_at_least(Xapian::docid desired_did);
     238                 :            : 
     239                 :            :     GlassPostList(Xapian::Internal::intrusive_ptr<const GlassDatabase> this_db_,
     240                 :            :                   const string & term,
     241                 :            :                   GlassCursor * cursor_);
     242                 :            : 
     243                 :            :     void init();
     244                 :            : 
     245                 :            :   public:
     246                 :            :     /// Default constructor.
     247                 :            :     GlassPostList(Xapian::Internal::intrusive_ptr<const GlassDatabase> this_db_,
     248                 :            :                   const string & term,
     249                 :            :                   bool keep_reference);
     250                 :            : 
     251                 :            :     /// Destructor.
     252                 :            :     ~GlassPostList();
     253                 :            : 
     254                 :            :     LeafPostList * open_nearby_postlist(const std::string & term_,
     255                 :            :                                         bool need_read_pos) const;
     256                 :            : 
     257                 :            :     /** Used for looking up doclens.
     258                 :            :      *
     259                 :            :      *  @return true if docid @a desired_did has a document length.
     260                 :            :      */
     261                 :            :     bool jump_to(Xapian::docid desired_did);
     262                 :            : 
     263                 :            :     /** Returns number of docs indexed by this term.
     264                 :            :      *
     265                 :            :      *  This is the length of the postlist.
     266                 :            :      */
     267                 :    1652156 :     Xapian::doccount get_termfreq() const { return number_of_entries; }
     268                 :            : 
     269                 :            :     /// Returns the current docid.
     270                 :   66720118 :     Xapian::docid get_docid() const { Assert(have_started); return did; }
     271                 :            : 
     272                 :            :     /** Returns the Within Document Frequency of the term in the current
     273                 :            :      *  document.
     274                 :            :      */
     275                 :  115186034 :     Xapian::termcount get_wdf() const { Assert(have_started); return wdf; }
     276                 :            : 
     277                 :            :     /** Get the list of positions of the term in the current document.
     278                 :            :      */
     279                 :            :     PositionList *read_position_list();
     280                 :            : 
     281                 :            :     /** Get the list of positions of the term in the current document.
     282                 :            :      */
     283                 :            :     PositionList * open_position_list() const;
     284                 :            : 
     285                 :            :     /// Move to the next document.
     286                 :            :     PostList * next(double w_min);
     287                 :            : 
     288                 :            :     /// Skip to next document with docid >= docid.
     289                 :            :     PostList * skip_to(Xapian::docid desired_did, double w_min);
     290                 :            : 
     291                 :            :     /// Return true if and only if we're off the end of the list.
     292                 :   57996576 :     bool at_end() const { return is_at_end; }
     293                 :            : 
     294                 :            :     /// Get a description of the document.
     295                 :            :     std::string get_description() const;
     296                 :            : 
     297                 :            :     /// Read the number of entries and the collection frequency.
     298                 :            :     static void read_number_of_entries(const char ** posptr,
     299                 :            :                                        const char * end,
     300                 :            :                                        Xapian::doccount * number_of_entries_ptr,
     301                 :            :                                        Xapian::termcount * collection_freq_ptr);
     302                 :            : };
     303                 :            : 
     304                 :            : #ifdef DISABLE_GPL_LIBXAPIAN
     305                 :            : # error GPL source we cannot relicense included in libxapian
     306                 :            : #endif
     307                 :            : 
     308                 :            : #endif /* XAPIAN_INCLUDED_GLASS_POSTLIST_H */

Generated by: LCOV version 1.11