LCOV - code coverage report
Current view: top level - backends/honey - honey_postlisttable.cc (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core 954b5873a738 Lines: 45 54 83.3 %
Date: 2019-06-30 05:20:33 Functions: 4 4 100.0 %
Branches: 53 116 45.7 %

           Branch data     Line data    Source code
       1                 :            : /** @file honey_postlisttable.cc
       2                 :            :  * @brief Subclass of HoneyTable which holds postlists.
       3                 :            :  */
       4                 :            : /* Copyright (C) 2007,2008,2009,2010,2013,2014,2015,2016,2017,2018 Olly Betts
       5                 :            :  *
       6                 :            :  * This program is free software; you can redistribute it and/or modify
       7                 :            :  * it under the terms of the GNU General Public License as published by
       8                 :            :  * the Free Software Foundation; either version 2 of the License, or
       9                 :            :  * (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                 :            : #include <config.h>
      22                 :            : 
      23                 :            : #include "honey_postlisttable.h"
      24                 :            : 
      25                 :            : #include "honey_alldocspostlist.h"
      26                 :            : #include "honey_cursor.h"
      27                 :            : #include "honey_database.h"
      28                 :            : #include "honey_defs.h"
      29                 :            : #include "honey_postlist.h"
      30                 :            : #include "honey_postlist_encodings.h"
      31                 :            : 
      32                 :            : #include <memory>
      33                 :            : 
      34                 :            : using namespace Honey;
      35                 :            : using namespace std;
      36                 :            : 
      37                 :            : HoneyPostList*
      38                 :      27099 : HoneyPostListTable::open_post_list(const HoneyDatabase* db,
      39                 :            :                                    const std::string& term,
      40                 :            :                                    bool need_read_pos) const
      41                 :            : {
      42                 :            :     Assert(!term.empty());
      43                 :            :     // Try to position cursor first so we avoid creating HoneyPostList objects
      44                 :            :     // for terms which don't exist.
      45         [ +  + ]:      27099 :     unique_ptr<HoneyCursor> cursor(cursor_get());
      46 [ +  - ][ +  - ]:      27097 :     if (!cursor->find_exact(Honey::make_postingchunk_key(term))) {
                 [ +  + ]
      47                 :            :         // FIXME: Return NULL here and handle that in Query::Internal
      48                 :            :         // postlist() methods as we build the PostList tree.
      49                 :            :         // return NULL;
      50 [ +  - ][ +  - ]:        115 :         return new HoneyPostList(db, term, NULL);
      51                 :            :     }
      52                 :            : 
      53         [ +  + ]:      26982 :     if (need_read_pos)
      54 [ +  - ][ +  - ]:         89 :         return new HoneyPosPostList(db, term, cursor.release());
      55 [ +  - ][ +  - ]:      27097 :     return new HoneyPostList(db, term, cursor.release());
      56                 :            : }
      57                 :            : 
      58                 :            : void
      59                 :     100115 : HoneyPostListTable::get_freqs(const std::string& term,
      60                 :            :                               Xapian::doccount* termfreq_ptr,
      61                 :            :                               Xapian::termcount* collfreq_ptr) const
      62                 :            : {
      63         [ +  - ]:     100115 :     string chunk;
      64 [ +  - ][ +  + ]:     100115 :     if (!get_exact_entry(Honey::make_postingchunk_key(term), chunk)) {
                 [ +  + ]
      65         [ +  + ]:        389 :         if (termfreq_ptr) *termfreq_ptr = 0;
      66         [ +  + ]:        389 :         if (collfreq_ptr) *collfreq_ptr = 0;
      67                 :     100500 :         return;
      68                 :            :     }
      69                 :            : 
      70                 :      99722 :     const char* p = chunk.data();
      71                 :      99722 :     const char* pend = p + chunk.size();
      72                 :            :     Xapian::doccount tf;
      73                 :            :     Xapian::termcount cf;
      74 [ +  - ][ -  + ]:      99722 :     if (!decode_initial_chunk_header_freqs(&p, pend, tf, cf))
      75 [ #  # ][ #  # ]:          0 :         throw Xapian::DatabaseCorruptError("Postlist initial chunk header");
                 [ #  # ]
      76         [ +  + ]:      99722 :     if (termfreq_ptr) *termfreq_ptr = tf;
      77 [ +  + ][ +  + ]:     100115 :     if (collfreq_ptr) *collfreq_ptr = cf;
      78                 :            : }
      79                 :            : 
      80                 :            : void
      81                 :          2 : HoneyPostListTable::get_used_docid_range(Xapian::doccount doccount,
      82                 :            :                                          Xapian::docid& first,
      83                 :            :                                          Xapian::docid& last) const
      84                 :            : {
      85         [ +  - ]:          2 :     unique_ptr<HoneyCursor> cursor(cursor_get());
      86                 :            :     Assert(cursor.get());
      87                 :            : 
      88                 :            :     static const char doclen_key_prefix[2] = {
      89                 :            :         0, char(Honey::KEY_DOCLEN_CHUNK)
      90                 :            :     };
      91 [ +  - ][ +  - ]:          2 :     if (cursor->find_entry_ge(string(doclen_key_prefix, 2))) {
                 [ -  + ]
      92                 :          0 :         first = 1;
      93                 :            :     } else {
      94                 :            :         // doccount == 0 should be handled by our caller.
      95                 :            :         Assert(!cursor->after_end());
      96                 :          2 :         Xapian::docid last_in_first_chunk = docid_from_key(cursor->current_key);
      97         [ -  + ]:          2 :         if (last_in_first_chunk == 0) {
      98                 :            :             // Note that our caller checks for doccount == 0 and handles that.
      99 [ #  # ][ #  # ]:          0 :             throw Xapian::DatabaseCorruptError("Bad first doclen chunk key");
                 [ #  # ]
     100                 :            :         }
     101         [ +  - ]:          2 :         cursor->read_tag();
     102                 :            :         Xapian::docid delta;
     103                 :          2 :         const char* p = cursor->current_tag.data();
     104                 :          2 :         const char* pend = p + cursor->current_tag.size();
     105         [ -  + ]:          2 :         if (!unpack_uint(&p, pend, &delta)) {
     106 [ #  # ][ #  # ]:          0 :             throw Xapian::DatabaseCorruptError("Bad first doclen chunk delta");
                 [ #  # ]
     107                 :            :         }
     108                 :          2 :         first = last_in_first_chunk - delta;
     109                 :            :     }
     110                 :            : 
     111                 :            :     // We know the last docid is at least first - 1 + doccount, so seek
     112                 :            :     // to there and then scan forwards.  If we match exactly, then that
     113                 :            :     // is exactly the last docid (our caller handles this case when
     114                 :            :     // first == 1, but not otherwise).
     115                 :          2 :     last = first - 1 + doccount;
     116 [ +  - ][ +  - ]:          2 :     if (cursor->find_entry_ge(make_doclenchunk_key(last)))
                 [ -  + ]
     117                 :          0 :         return;
     118                 :            : 
     119         [ -  + ]:          2 :     if (cursor->after_end())
     120 [ #  # ][ #  # ]:          0 :         throw Xapian::DatabaseCorruptError("Missing doclen chunk");
                 [ #  # ]
     121                 :            : 
     122 [ #  # ][ -  + ]:          2 :     do {
     123                 :          2 :         Xapian::docid new_last = docid_from_key(cursor->current_key);
     124         [ +  - ]:          2 :         if (new_last == 0) {
     125                 :            :             // We've hit a non-doclen item.
     126                 :          2 :             return;
     127                 :            :         }
     128                 :          0 :         last = new_last;
     129         [ #  # ]:          0 :         cursor->next();
     130                 :          2 :     } while (!cursor->after_end());
     131                 :            : 
     132                 :            :     // We've reached the end of the table (only possible if there are no terms
     133                 :            :     // at all!)
     134                 :            : }
     135                 :            : 
     136                 :            : Xapian::termcount
     137                 :      50614 : HoneyPostListTable::get_wdf_upper_bound(const std::string& term) const
     138                 :            : {
     139         [ +  - ]:      50614 :     string chunk;
     140 [ +  - ][ +  + ]:      50614 :     if (!get_exact_entry(Honey::make_postingchunk_key(term), chunk)) {
                 [ +  + ]
     141                 :            :         // Term not present.
     142                 :        159 :         return 0;
     143                 :            :     }
     144                 :            : 
     145                 :      50454 :     const char* p = chunk.data();
     146                 :      50454 :     const char* pend = p + chunk.size();
     147                 :            :     Xapian::doccount tf;
     148                 :            :     Xapian::termcount cf;
     149                 :            :     Xapian::docid first;
     150                 :            :     Xapian::docid last;
     151                 :            :     Xapian::docid chunk_last;
     152                 :            :     Xapian::termcount first_wdf;
     153                 :            :     Xapian::termcount wdf_max;
     154         [ -  + ]:      50454 :     if (!decode_initial_chunk_header(&p, pend, tf, cf, first, last, chunk_last,
     155         [ +  - ]:      50454 :                                      first_wdf, wdf_max))
     156 [ #  # ][ #  # ]:          0 :         throw Xapian::DatabaseCorruptError("Postlist initial chunk header");
                 [ #  # ]
     157                 :      50614 :     return wdf_max;
     158                 :            : }

Generated by: LCOV version 1.11