LCOV - code coverage report
Current view: top level - backends/glass - glass_alltermslist.cc (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core 954b5873a738 Lines: 55 64 85.9 %
Date: 2019-06-30 05:20:33 Functions: 7 9 77.8 %
Branches: 43 82 52.4 %

           Branch data     Line data    Source code
       1                 :            : /** @file glass_alltermslist.cc
       2                 :            :  * @brief A termlist containing all terms in a glass database.
       3                 :            :  */
       4                 :            : /* Copyright (C) 2005,2007,2008,2009,2010,2017 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                 :            : #include <config.h>
      23                 :            : 
      24                 :            : #include "glass_alltermslist.h"
      25                 :            : #include "glass_postlist.h"
      26                 :            : 
      27                 :            : #include "debuglog.h"
      28                 :            : #include "pack.h"
      29                 :            : #include "stringutils.h"
      30                 :            : 
      31                 :            : void
      32                 :       3515 : GlassAllTermsList::read_termfreq_and_collfreq() const
      33                 :            : {
      34                 :            :     LOGCALL_VOID(DB, "GlassAllTermsList::read_termfreq_and_collfreq", NO_ARGS);
      35                 :            :     Assert(!current_term.empty());
      36                 :            :     Assert(!at_end());
      37                 :            : 
      38                 :            :     // Unpack the termfreq and collfreq from the tag.  Only do this if
      39                 :            :     // one or other is actually read.
      40         [ +  - ]:       3515 :     cursor->read_tag();
      41                 :       3515 :     const char *p = cursor->current_tag.data();
      42                 :       3515 :     const char *pend = p + cursor->current_tag.size();
      43         [ +  - ]:       3515 :     GlassPostList::read_number_of_entries(&p, pend, &termfreq, &collfreq);
      44                 :       3515 : }
      45                 :            : 
      46                 :       1986 : GlassAllTermsList::~GlassAllTermsList()
      47                 :            : {
      48                 :            :     LOGCALL_DTOR(DB, "GlassAllTermsList");
      49         [ +  + ]:        662 :     delete cursor;
      50         [ -  + ]:       1324 : }
      51                 :            : 
      52                 :            : Xapian::termcount
      53                 :          0 : GlassAllTermsList::get_approx_size() const
      54                 :            : {
      55                 :            :     // This is an over-estimate and not entirely proportional between shards,
      56                 :            :     // but we only use this value to build a balanced or-tree, and it'll at
      57                 :            :     // least tend to distinguish large databases from small ones.
      58                 :          0 :     return database->postlist_table.get_entry_count();
      59                 :            : }
      60                 :            : 
      61                 :            : string
      62                 :      49109 : GlassAllTermsList::get_termname() const
      63                 :            : {
      64                 :            :     LOGCALL(DB, string, "GlassAllTermsList::get_termname", NO_ARGS);
      65                 :            :     Assert(!current_term.empty());
      66                 :            :     Assert(!at_end());
      67                 :      49109 :     RETURN(current_term);
      68                 :            : }
      69                 :            : 
      70                 :            : Xapian::doccount
      71                 :       3515 : GlassAllTermsList::get_termfreq() const
      72                 :            : {
      73                 :            :     LOGCALL(DB, Xapian::doccount, "GlassAllTermsList::get_termfreq", NO_ARGS);
      74                 :            :     Assert(!current_term.empty());
      75                 :            :     Assert(!at_end());
      76         [ +  - ]:       3515 :     if (termfreq == 0) read_termfreq_and_collfreq();
      77                 :       3515 :     RETURN(termfreq);
      78                 :            : }
      79                 :            : 
      80                 :            : Xapian::termcount
      81                 :          0 : GlassAllTermsList::get_collection_freq() const
      82                 :            : {
      83                 :            :     LOGCALL(DB, Xapian::termcount, "GlassAllTermsList::get_collection_freq", NO_ARGS);
      84                 :            :     Assert(!current_term.empty());
      85                 :            :     Assert(!at_end());
      86         [ #  # ]:          0 :     if (termfreq == 0) read_termfreq_and_collfreq();
      87                 :          0 :     RETURN(collfreq);
      88                 :            : }
      89                 :            : 
      90                 :            : TermList *
      91                 :       8498 : GlassAllTermsList::next()
      92                 :            : {
      93                 :            :     LOGCALL(DB, TermList *, "GlassAllTermsList::next", NO_ARGS);
      94                 :            :     Assert(!at_end());
      95                 :            :     // Set termfreq to 0 to indicate no termfreq/collfreq have been read for
      96                 :            :     // the current term.
      97                 :       8498 :     termfreq = 0;
      98                 :            : 
      99         [ +  + ]:       8498 :     if (rare(!cursor)) {
     100                 :        660 :         cursor = database->postlist_table.cursor_get();
     101                 :            :         Assert(cursor); // The postlist table isn't optional.
     102                 :            : 
     103         [ +  + ]:        654 :         if (prefix.empty()) {
     104 [ +  - ][ +  - ]:        353 :             (void)cursor->find_entry_ge(string("\x00\xff", 2));
     105                 :            :         } else {
     106                 :        301 :             const string & key = pack_glass_postlist_key(prefix);
     107 [ +  - ][ +  + ]:        301 :             if (cursor->find_entry_ge(key)) {
     108                 :            :                 // The exact term we asked for is there, so just copy it rather
     109                 :            :                 // than wasting effort unpacking it from the key.
     110         [ +  - ]:         34 :                 current_term = prefix;
     111         [ +  + ]:        301 :                 RETURN(NULL);
     112                 :        267 :             }
     113                 :            :         }
     114                 :        620 :         goto first_time;
     115                 :            :     }
     116                 :            : 
     117                 :            :     while (true) {
     118         [ +  - ]:       8609 :         cursor->next();
     119                 :            : first_time:
     120         [ +  + ]:       9229 :         if (cursor->after_end()) {
     121         [ +  - ]:        380 :             current_term.resize(0);
     122                 :        380 :             RETURN(NULL);
     123                 :            :         }
     124                 :            : 
     125                 :       8849 :         const char *p = cursor->current_key.data();
     126                 :       8849 :         const char *pend = p + cursor->current_key.size();
     127 [ +  - ][ -  + ]:       8849 :         if (!unpack_string_preserving_sort(&p, pend, current_term)) {
     128 [ #  # ][ #  # ]:          0 :             throw Xapian::DatabaseCorruptError("PostList table key has unexpected format");
                 [ #  # ]
     129                 :            :         }
     130                 :            : 
     131                 :            :         // If this key is for the first chunk of a postlist, we're done.
     132                 :            :         // Otherwise we need to skip past continuation chunks until we find the
     133                 :            :         // first chunk of the next postlist.
     134         [ +  + ]:       8849 :         if (p == pend) break;
     135                 :            :     }
     136                 :            : 
     137         [ +  + ]:       8078 :     if (!startswith(current_term, prefix)) {
     138                 :            :         // We've reached the end of the prefixed terms.
     139                 :        193 :         cursor->to_end();
     140                 :        193 :         current_term.resize(0);
     141                 :            :     }
     142                 :            : 
     143                 :       9263 :     RETURN(NULL);
     144                 :            : }
     145                 :            : 
     146                 :            : TermList *
     147                 :         36 : GlassAllTermsList::skip_to(const string &term)
     148                 :            : {
     149                 :            :     LOGCALL(DB, TermList *, "GlassAllTermsList::skip_to", term);
     150                 :            :     Assert(!at_end());
     151                 :            :     // Set termfreq to 0 to indicate no termfreq/collfreq have been read for
     152                 :            :     // the current term.
     153                 :         36 :     termfreq = 0;
     154                 :            : 
     155         [ -  + ]:         36 :     if (rare(!cursor)) {
     156         [ #  # ]:          0 :         cursor = database->postlist_table.cursor_get();
     157                 :            :         Assert(cursor); // The postlist table isn't optional.
     158                 :            :     }
     159                 :            : 
     160         [ +  - ]:         36 :     string key = pack_glass_postlist_key(term);
     161 [ +  - ][ +  + ]:         36 :     if (cursor->find_entry_ge(key)) {
     162                 :            :         // The exact term we asked for is there, so just copy it rather than
     163                 :            :         // wasting effort unpacking it from the key.
     164         [ +  - ]:         15 :         current_term = term;
     165                 :            :     } else {
     166         [ +  + ]:         21 :         if (cursor->after_end()) {
     167         [ +  - ]:         10 :             current_term.resize(0);
     168                 :         10 :             RETURN(NULL);
     169                 :            :         }
     170                 :            : 
     171                 :         11 :         const char *p = cursor->current_key.data();
     172                 :         11 :         const char *pend = p + cursor->current_key.size();
     173 [ +  - ][ -  + ]:         11 :         if (!unpack_string_preserving_sort(&p, pend, current_term)) {
     174 [ #  # ][ #  # ]:         11 :             throw Xapian::DatabaseCorruptError("PostList table key has unexpected format");
                 [ #  # ]
     175                 :            :         }
     176                 :            :     }
     177                 :            : 
     178         [ -  + ]:         26 :     if (!startswith(current_term, prefix)) {
     179                 :            :         // We've reached the end of the prefixed terms.
     180                 :          0 :         cursor->to_end();
     181         [ #  # ]:          0 :         current_term.resize(0);
     182                 :            :     }
     183                 :            : 
     184                 :         36 :     RETURN(NULL);
     185                 :            : }
     186                 :            : 
     187                 :            : bool
     188                 :       8564 : GlassAllTermsList::at_end() const
     189                 :            : {
     190                 :            :     LOGCALL(DB, bool, "GlassAllTermsList::at_end", NO_ARGS);
     191 [ +  - ][ +  + ]:       8564 :     RETURN(cursor && cursor->after_end());
     192                 :            : }

Generated by: LCOV version 1.11