LCOV - code coverage report
Current view: top level - backends/glass - glass_dbcheck.cc (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core 954b5873a738 Lines: 284 619 45.9 %
Date: 2019-06-30 05:20:33 Functions: 4 4 100.0 %
Branches: 269 1130 23.8 %

           Branch data     Line data    Source code
       1                 :            : /** @file glass_dbcheck.cc
       2                 :            :  * @brief Check consistency of a glass table.
       3                 :            :  */
       4                 :            : /* Copyright 1999,2000,2001 BrightStation PLC
       5                 :            :  * Copyright 2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018 Olly Betts
       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
      20                 :            :  * USA
      21                 :            :  */
      22                 :            : 
      23                 :            : #include <config.h>
      24                 :            : 
      25                 :            : #include "glass_dbcheck.h"
      26                 :            : 
      27                 :            : #include "bitstream.h"
      28                 :            : 
      29                 :            : #include "internaltypes.h"
      30                 :            : 
      31                 :            : #include "glass_check.h"
      32                 :            : #include "glass_cursor.h"
      33                 :            : #include "glass_defs.h"
      34                 :            : #include "glass_table.h"
      35                 :            : #include "glass_version.h"
      36                 :            : #include "pack.h"
      37                 :            : #include "backends/valuestats.h"
      38                 :            : 
      39                 :            : #include <xapian.h>
      40                 :            : 
      41                 :            : #include "filetests.h"
      42                 :            : #include <memory>
      43                 :            : #include <ostream>
      44                 :            : #include <vector>
      45                 :            : 
      46                 :            : using namespace std;
      47                 :            : 
      48                 :            : static inline bool
      49                 :       1323 : is_user_metadata_key(const string & key)
      50                 :            : {
      51 [ +  + ][ +  + ]:       1323 :     return key.size() > 1 && key[0] == '\0' && key[1] == '\xc0';
                 [ +  + ]
      52                 :            : }
      53                 :            : 
      54                 :        224 : struct VStats : public ValueStats {
      55                 :            :     Xapian::doccount freq_real;
      56                 :            : 
      57                 :        224 :     VStats() : ValueStats(), freq_real(0) {}
      58                 :            : };
      59                 :            : 
      60                 :            : size_t
      61                 :         87 : check_glass_table(const char * tablename, const string &db_dir, int fd,
      62                 :            :                   off_t offset_,
      63                 :            :                   const GlassVersion & version_file, int opts,
      64                 :            :                   vector<Xapian::termcount> & doclens, ostream * out)
      65                 :            : {
      66                 :         87 :     Xapian::docid db_last_docid = version_file.get_last_docid();
      67         [ +  + ]:         87 :     if (out)
      68 [ +  - ][ +  - ]:         57 :         *out << tablename << ":\n";
      69         [ +  + ]:         87 :     if (fd < 0) {
      70         [ +  + ]:         57 :         if (strcmp(tablename, "postlist") != 0) {
      71                 :            :             // Other filenames are created lazily, so may not exist.
      72         [ +  - ]:         47 :             string filename(db_dir);
      73         [ +  - ]:         47 :             filename += '/';
      74         [ +  - ]:         47 :             filename += tablename;
      75         [ +  - ]:         47 :             filename += "." GLASS_TABLE_EXTENSION;
      76         [ +  + ]:         47 :             if (!file_exists(filename)) {
      77         [ +  + ]:         28 :                 if (out) {
      78         [ -  + ]:         18 :                     if (strcmp(tablename, "termlist") == 0) {
      79         [ #  # ]:          0 :                         *out << "Not present.\n";
      80                 :            :                     } else {
      81         [ +  - ]:         18 :                         *out << "Lazily created, and not yet used.\n";
      82                 :            :                     }
      83         [ +  - ]:         18 :                     *out << endl;
      84                 :            :                 }
      85         [ +  + ]:         47 :                 return 0;
      86                 :         57 :             }
      87                 :            :         }
      88                 :            :     }
      89                 :            : 
      90                 :            :     // Check the btree structure.
      91                 :            :     unique_ptr<GlassTable> table(
      92                 :            :             GlassTableCheck::check(tablename, db_dir, fd, offset_,
      93         [ +  - ]:         59 :                                    version_file, opts, out));
      94                 :            : 
      95                 :            :     // Now check the glass structures inside the btree.
      96         [ +  - ]:        118 :     unique_ptr<GlassCursor> cursor(table->cursor_get());
      97                 :            : 
      98                 :         59 :     size_t errors = 0;
      99                 :            : 
     100         [ +  - ]:         59 :     cursor->rewind();
     101         [ +  - ]:         59 :     cursor->next(); // Skip the empty entry.
     102                 :            : 
     103         [ +  + ]:         59 :     if (strcmp(tablename, "postlist") == 0) {
     104                 :            :         // Now check the structure of each postlist in the table.
     105         [ +  - ]:         15 :         map<Xapian::valueno, VStats> valuestats;
     106         [ +  - ]:         30 :         string current_term;
     107                 :         15 :         Xapian::docid lastdid = 0;
     108                 :         15 :         Xapian::termcount termfreq = 0, collfreq = 0;
     109                 :         15 :         Xapian::termcount tf = 0, cf = 0;
     110                 :         15 :         Xapian::doccount num_doclens = 0;
     111                 :            : 
     112 [ +  - ][ +  + ]:       1338 :         for ( ; !cursor->after_end(); cursor->next()) {
     113                 :       1323 :             string & key = cursor->current_key;
     114                 :            : 
     115         [ +  + ]:       1323 :             if (is_user_metadata_key(key)) {
     116                 :            :                 // User metadata can be anything, so we can't do any particular
     117                 :            :                 // checks on it other than to check that the tag isn't empty.
     118         [ +  - ]:          1 :                 cursor->read_tag();
     119         [ -  + ]:          1 :                 if (cursor->current_tag.empty()) {
     120         [ #  # ]:          0 :                     if (out)
     121 [ #  # ][ #  # ]:          0 :                         *out << "User metadata item is empty" << endl;
     122                 :          0 :                     ++errors;
     123                 :            :                 }
     124                 :        295 :                 continue;
     125                 :            :             }
     126                 :            : 
     127 [ +  + ][ +  - ]:       1322 :             if (key.size() >= 2 && key[0] == '\0' && key[1] == '\xe0') {
         [ +  + ][ +  - ]
         [ +  + ][ +  + ]
     128                 :            :                 // doclen chunk
     129                 :            :                 const char * pos, * end;
     130                 :         19 :                 Xapian::docid did = 1;
     131         [ +  + ]:         19 :                 if (key.size() > 2) {
     132                 :            :                     // Non-initial chunk.
     133                 :          5 :                     pos = key.data();
     134                 :          5 :                     end = pos + key.size();
     135                 :          5 :                     pos += 2;
     136         [ -  + ]:          5 :                     if (!unpack_uint_preserving_sort(&pos, end, &did)) {
     137         [ #  # ]:          0 :                         if (out)
     138 [ #  # ][ #  # ]:          0 :                             *out << "Error unpacking docid from doclen key" << endl;
     139                 :          0 :                         ++errors;
     140                 :          0 :                         continue;
     141                 :            :                     }
     142         [ -  + ]:          5 :                     if (did <= lastdid) {
     143         [ #  # ]:          0 :                         if (out)
     144                 :            :                             *out << "First did in this doclen chunk is <= last in "
     145 [ #  # ][ #  # ]:          0 :                                     "prev chunk" << endl;
     146                 :          0 :                         ++errors;
     147                 :            :                     }
     148                 :            :                 }
     149                 :            : 
     150         [ +  - ]:         19 :                 cursor->read_tag();
     151                 :         19 :                 pos = cursor->current_tag.data();
     152                 :         19 :                 end = pos + cursor->current_tag.size();
     153         [ +  + ]:         19 :                 if (key.size() == 2) {
     154                 :            :                     // Initial chunk.
     155 [ +  - ][ +  - ]:         14 :                     if (end - pos < 2 || pos[0] || pos[1]) {
                 [ -  + ]
     156         [ #  # ]:          0 :                         if (out)
     157 [ #  # ][ #  # ]:          0 :                             *out << "Initial doclen chunk has nonzero dummy fields" << endl;
     158                 :          0 :                         ++errors;
     159                 :          0 :                         continue;
     160                 :            :                     }
     161                 :         14 :                     pos += 2;
     162         [ -  + ]:         14 :                     if (!unpack_uint(&pos, end, &did)) {
     163         [ #  # ]:          0 :                         if (out)
     164 [ #  # ][ #  # ]:          0 :                             *out << "Failed to unpack firstdid for doclen" << endl;
     165                 :          0 :                         ++errors;
     166                 :          0 :                         continue;
     167                 :            :                     }
     168                 :         14 :                     ++did;
     169                 :            :                 }
     170                 :            : 
     171                 :            :                 bool is_last_chunk;
     172         [ -  + ]:         19 :                 if (!unpack_bool(&pos, end, &is_last_chunk)) {
     173         [ #  # ]:          0 :                     if (out)
     174 [ #  # ][ #  # ]:          0 :                         *out << "Failed to unpack last chunk flag for doclen" << endl;
     175                 :          0 :                     ++errors;
     176                 :          0 :                     continue;
     177                 :            :                 }
     178                 :            :                 // Read what the final document ID in this chunk is.
     179         [ -  + ]:         19 :                 if (!unpack_uint(&pos, end, &lastdid)) {
     180         [ #  # ]:          0 :                     if (out)
     181 [ #  # ][ #  # ]:          0 :                         *out << "Failed to unpack increase to last" << endl;
     182                 :          0 :                     ++errors;
     183                 :          0 :                     continue;
     184                 :            :                 }
     185                 :         19 :                 lastdid += did;
     186                 :         19 :                 bool bad = false;
     187                 :            :                 while (true) {
     188                 :            :                     Xapian::termcount doclen;
     189         [ -  + ]:         74 :                     if (!unpack_uint(&pos, end, &doclen)) {
     190         [ #  # ]:          0 :                         if (out)
     191 [ #  # ][ #  # ]:          0 :                             *out << "Failed to unpack doclen" << endl;
     192                 :          0 :                         ++errors;
     193                 :          0 :                         bad = true;
     194                 :          0 :                         break;
     195                 :            :                     }
     196                 :            : 
     197                 :         74 :                     ++num_doclens;
     198                 :            : 
     199         [ -  + ]:         74 :                     if (did > db_last_docid) {
     200         [ #  # ]:          0 :                         if (out)
     201 [ #  # ][ #  # ]:          0 :                             *out << "document id " << did << " in doclen "
     202         [ #  # ]:          0 :                                     "stream is larger than get_last_docid() "
     203 [ #  # ][ #  # ]:          0 :                                  << db_last_docid << endl;
     204                 :          0 :                         ++errors;
     205                 :            :                     }
     206                 :            : 
     207         [ +  + ]:         74 :                     if (!doclens.empty()) {
     208                 :            :                         // In glass, a document without terms doesn't get a
     209                 :            :                         // termlist entry.
     210                 :         62 :                         Xapian::termcount termlist_doclen = 0;
     211         [ +  + ]:         62 :                         if (did < doclens.size())
     212                 :         60 :                             termlist_doclen = doclens[did];
     213                 :            : 
     214         [ -  + ]:         62 :                         if (doclen != termlist_doclen) {
     215         [ #  # ]:          0 :                             if (out)
     216 [ #  # ][ #  # ]:          0 :                                 *out << "document id " << did << ": length "
                 [ #  # ]
     217 [ #  # ][ #  # ]:          0 :                                      << doclen << " doesn't match "
     218         [ #  # ]:          0 :                                      << termlist_doclen << " in the termlist "
     219 [ #  # ][ #  # ]:          0 :                                         "table" << endl;
     220                 :         62 :                             ++errors;
     221                 :            :                         }
     222                 :            :                     }
     223                 :            : 
     224         [ +  + ]:         74 :                     if (pos == end) break;
     225                 :            : 
     226                 :            :                     Xapian::docid inc;
     227         [ -  + ]:         55 :                     if (!unpack_uint(&pos, end, &inc)) {
     228         [ #  # ]:          0 :                         if (out)
     229 [ #  # ][ #  # ]:          0 :                             *out << "Failed to unpack docid increase" << endl;
     230                 :          0 :                         ++errors;
     231                 :          0 :                         bad = true;
     232                 :          0 :                         break;
     233                 :            :                     }
     234                 :         55 :                     ++inc;
     235                 :         55 :                     did += inc;
     236         [ -  + ]:         55 :                     if (did > lastdid) {
     237         [ #  # ]:          0 :                         if (out)
     238 [ #  # ][ #  # ]:          0 :                             *out << "docid " << did << " > last docid "
                 [ #  # ]
     239 [ #  # ][ #  # ]:          0 :                                  << lastdid << endl;
     240                 :          0 :                         ++errors;
     241                 :            :                     }
     242                 :            :                 }
     243         [ -  + ]:         19 :                 if (bad) {
     244                 :          0 :                     continue;
     245                 :            :                 }
     246         [ +  + ]:         19 :                 if (is_last_chunk) {
     247         [ -  + ]:         14 :                     if (did != lastdid) {
     248         [ #  # ]:          0 :                         if (out)
     249 [ #  # ][ #  # ]:          0 :                             *out << "lastdid " << lastdid << " != last did "
                 [ #  # ]
     250 [ #  # ][ #  # ]:          0 :                                  << did << endl;
     251                 :          0 :                         ++errors;
     252                 :            :                     }
     253                 :            :                 }
     254                 :            : 
     255                 :         74 :                 continue;
     256                 :            :             }
     257                 :            : 
     258 [ +  + ][ +  - ]:       1303 :             if (key.size() >= 2 && key[0] == '\0' && key[1] == '\xd0') {
         [ +  + ][ +  - ]
         [ +  + ][ +  + ]
     259                 :            :                 // Value stats.
     260                 :        112 :                 const char * p = key.data();
     261                 :        112 :                 const char * end = p + key.length();
     262                 :        112 :                 p += 2;
     263                 :            :                 Xapian::valueno slot;
     264         [ -  + ]:        112 :                 if (!unpack_uint_last(&p, end, &slot)) {
     265         [ #  # ]:          0 :                     if (out)
     266 [ #  # ][ #  # ]:          0 :                         *out << "Bad valuestats key (no slot)" << endl;
     267                 :          0 :                     ++errors;
     268                 :          0 :                     continue;
     269                 :            :                 }
     270                 :            : 
     271         [ +  - ]:        112 :                 cursor->read_tag();
     272                 :        112 :                 p = cursor->current_tag.data();
     273                 :        112 :                 end = p + cursor->current_tag.size();
     274                 :            : 
     275         [ +  - ]:        112 :                 VStats & v = valuestats[slot];
     276         [ -  + ]:        112 :                 if (!unpack_uint(&p, end, &v.freq)) {
     277         [ #  # ]:          0 :                     if (out) {
     278         [ #  # ]:          0 :                         if (*p == 0) {
     279         [ #  # ]:          0 :                             *out << "Incomplete stats item in value table";
     280                 :            :                         } else {
     281         [ #  # ]:          0 :                             *out << "Frequency statistic in value table is too large";
     282                 :            :                         }
     283         [ #  # ]:          0 :                         *out << endl;
     284                 :            :                     }
     285                 :          0 :                     ++errors;
     286                 :          0 :                     continue;
     287                 :            :                 }
     288 [ +  - ][ -  + ]:        112 :                 if (!unpack_string(&p, end, v.lower_bound)) {
     289         [ #  # ]:          0 :                     if (out) {
     290         [ #  # ]:          0 :                         if (*p == 0) {
     291         [ #  # ]:          0 :                             *out << "Incomplete stats item in value table";
     292                 :            :                         } else {
     293         [ #  # ]:          0 :                             *out << "Lower bound statistic in value table is too large";
     294                 :            :                         }
     295         [ #  # ]:          0 :                         *out << endl;
     296                 :            :                     }
     297                 :          0 :                     ++errors;
     298                 :          0 :                     continue;
     299                 :            :                 }
     300                 :        112 :                 size_t len = end - p;
     301         [ +  + ]:        112 :                 if (len == 0) {
     302         [ +  - ]:          6 :                     v.upper_bound = v.lower_bound;
     303                 :            :                 } else {
     304         [ +  - ]:        106 :                     v.upper_bound.assign(p, len);
     305                 :            :                 }
     306                 :            : 
     307                 :        112 :                 continue;
     308                 :            :             }
     309                 :            : 
     310 [ +  + ][ +  - ]:       1191 :             if (key.size() >= 2 && key[0] == '\0' && key[1] == '\xd8') {
         [ +  + ][ +  - ]
         [ +  - ][ +  + ]
     311                 :            :                 // Value stream chunk.
     312                 :        163 :                 const char * p = key.data();
     313                 :        163 :                 const char * end = p + key.length();
     314                 :        163 :                 p += 2;
     315                 :            :                 Xapian::valueno slot;
     316         [ -  + ]:        163 :                 if (!unpack_uint(&p, end, &slot)) {
     317         [ #  # ]:          0 :                     if (out)
     318 [ #  # ][ #  # ]:          0 :                         *out << "Bad value chunk key (no slot)" << endl;
     319                 :          0 :                     ++errors;
     320                 :          0 :                     continue;
     321                 :            :                 }
     322                 :            :                 Xapian::docid did;
     323         [ -  + ]:        163 :                 if (!unpack_uint_preserving_sort(&p, end, &did)) {
     324         [ #  # ]:          0 :                     if (out)
     325 [ #  # ][ #  # ]:          0 :                         *out << "Bad value chunk key (no docid)" << endl;
     326                 :          0 :                     ++errors;
     327                 :          0 :                     continue;
     328                 :            :                 }
     329         [ -  + ]:        163 :                 if (p != end) {
     330         [ #  # ]:          0 :                     if (out)
     331 [ #  # ][ #  # ]:          0 :                         *out << "Bad value chunk key (trailing junk)" << endl;
     332                 :          0 :                     ++errors;
     333                 :          0 :                     continue;
     334                 :            :                 }
     335                 :            : 
     336         [ +  - ]:        163 :                 VStats & v = valuestats[slot];
     337                 :            : 
     338         [ +  - ]:        163 :                 cursor->read_tag();
     339                 :        163 :                 p = cursor->current_tag.data();
     340                 :        163 :                 end = p + cursor->current_tag.size();
     341                 :            : 
     342                 :            :                 while (true) {
     343         [ +  - ]:        840 :                     string value;
     344 [ +  - ][ -  + ]:        840 :                     if (!unpack_string(&p, end, value)) {
     345         [ #  # ]:          0 :                         if (out)
     346 [ #  # ][ #  # ]:          0 :                             *out << "Failed to unpack value from chunk" << endl;
     347                 :          0 :                         ++errors;
     348                 :          0 :                         break;
     349                 :            :                     }
     350                 :            : 
     351                 :        840 :                     ++v.freq_real;
     352                 :            : 
     353                 :            :                     // FIXME: Cross-check that docid did has value slot (and
     354                 :            :                     // vice versa - that there's a value here if the slot entry
     355                 :            :                     // says so).
     356                 :            : 
     357                 :            :                     // FIXME: Check if the bounds are tight?  Or is that better
     358                 :            :                     // as a separate tool which can also update the bounds?
     359 [ +  - ][ -  + ]:        840 :                     if (value < v.lower_bound) {
     360         [ #  # ]:          0 :                         if (out)
     361 [ #  # ][ #  # ]:          0 :                             *out << "Value slot " << slot << " has value "
     362 [ #  # ][ #  # ]:          0 :                                     "below lower bound: '" << value << "' < '"
                 [ #  # ]
     363 [ #  # ][ #  # ]:          0 :                                  << v.lower_bound << "'" << endl;
                 [ #  # ]
     364                 :          0 :                         ++errors;
     365 [ +  - ][ -  + ]:        840 :                     } else if (value > v.upper_bound) {
     366         [ #  # ]:          0 :                         if (out)
     367 [ #  # ][ #  # ]:          0 :                             *out << "Value slot " << slot << " has value "
     368 [ #  # ][ #  # ]:          0 :                                     "above upper bound: '" << value << "' > '"
                 [ #  # ]
     369 [ #  # ][ #  # ]:          0 :                                  << v.upper_bound << "'" << endl;
                 [ #  # ]
     370                 :          0 :                         ++errors;
     371                 :            :                     }
     372                 :            : 
     373         [ +  + ]:        840 :                     if (p == end) break;
     374                 :            :                     Xapian::docid delta;
     375         [ -  + ]:        677 :                     if (!unpack_uint(&p, end, &delta)) {
     376         [ #  # ]:          0 :                         if (out)
     377         [ #  # ]:          0 :                             *out << "Failed to unpack docid delta from chunk"
     378         [ #  # ]:          0 :                                  << endl;
     379                 :          0 :                         ++errors;
     380                 :          0 :                         break;
     381                 :            :                     }
     382                 :        677 :                     Xapian::docid new_did = did + delta + 1;
     383         [ -  + ]:        677 :                     if (new_did <= did) {
     384         [ #  # ]:          0 :                         if (out)
     385 [ #  # ][ #  # ]:          0 :                             *out << "docid overflowed in value chunk" << endl;
     386                 :          0 :                         ++errors;
     387                 :          0 :                         break;
     388                 :            :                     }
     389                 :        677 :                     did = new_did;
     390                 :            : 
     391         [ -  + ]:        677 :                     if (did > db_last_docid) {
     392         [ #  # ]:          0 :                         if (out)
     393 [ #  # ][ #  # ]:          0 :                             *out << "document id " << did << " in value chunk "
     394         [ #  # ]:          0 :                                     "is larger than get_last_docid() "
     395 [ #  # ][ #  # ]:          0 :                                  << db_last_docid << endl;
     396         [ +  + ]:        840 :                         ++errors;
     397                 :            :                     }
     398                 :        677 :                 }
     399                 :        840 :                 continue;
     400                 :            :             }
     401                 :            : 
     402                 :            :             const char * pos, * end;
     403                 :            : 
     404                 :            :             // Get term from key.
     405                 :       1028 :             pos = key.data();
     406                 :       1028 :             end = pos + key.size();
     407                 :            : 
     408         [ +  - ]:       1028 :             string term;
     409                 :            :             Xapian::docid did;
     410 [ +  - ][ -  + ]:       1028 :             if (!unpack_string_preserving_sort(&pos, end, term)) {
     411         [ #  # ]:          0 :                 if (out)
     412 [ #  # ][ #  # ]:          0 :                     *out << "Error unpacking termname from key" << endl;
     413                 :          0 :                 ++errors;
     414                 :          0 :                 continue;
     415                 :            :             }
     416 [ +  + ][ +  - ]:       1028 :             if (!current_term.empty() && term != current_term) {
         [ -  + ][ -  + ]
     417                 :            :                 // The term changed unexpectedly.
     418         [ #  # ]:          0 :                 if (pos == end) {
     419         [ #  # ]:          0 :                     if (out)
     420 [ #  # ][ #  # ]:          0 :                         *out << "No last chunk for term '" << current_term
     421 [ #  # ][ #  # ]:          0 :                              << "'" << endl;
     422         [ #  # ]:          0 :                     current_term.resize(0);
     423                 :            :                 } else {
     424         [ #  # ]:          0 :                     if (out)
     425                 :            :                         *out << "Mismatch in follow-on chunk in posting list "
     426 [ #  # ][ #  # ]:          0 :                                 "for term '" << current_term << "' (got '"
                 [ #  # ]
     427 [ #  # ][ #  # ]:          0 :                              << term << "')" << endl;
                 [ #  # ]
     428         [ #  # ]:          0 :                     current_term = term;
     429                 :          0 :                     tf = cf = 0;
     430                 :          0 :                     lastdid = 0;
     431                 :            :                 }
     432                 :          0 :                 ++errors;
     433                 :            :             }
     434         [ +  + ]:       1028 :             if (pos == end) {
     435                 :            :                 // First chunk.
     436 [ +  - ][ -  + ]:        731 :                 if (term == current_term) {
     437                 :            :                     // This probably isn't possible.
     438         [ #  # ]:          0 :                     if (out)
     439 [ #  # ][ #  # ]:          0 :                         *out << "First posting list chunk for term '" << term
     440         [ #  # ]:          0 :                              << "' follows previous chunk for the same term"
     441         [ #  # ]:          0 :                              << endl;
     442                 :          0 :                     ++errors;
     443                 :            :                 }
     444         [ +  - ]:        731 :                 current_term = term;
     445                 :        731 :                 tf = cf = 0;
     446                 :            : 
     447                 :            :                 // Unpack extra header from first chunk.
     448         [ +  - ]:        731 :                 cursor->read_tag();
     449                 :        731 :                 pos = cursor->current_tag.data();
     450                 :        731 :                 end = pos + cursor->current_tag.size();
     451         [ -  + ]:        731 :                 if (!unpack_uint(&pos, end, &termfreq)) {
     452         [ #  # ]:          0 :                     if (out)
     453 [ #  # ][ #  # ]:          0 :                         *out << "Failed to unpack termfreq for term '" << term
     454 [ #  # ][ #  # ]:          0 :                              << "'" << endl;
     455                 :          0 :                     ++errors;
     456                 :          0 :                     continue;
     457                 :            :                 }
     458         [ -  + ]:        731 :                 if (!unpack_uint(&pos, end, &collfreq)) {
     459         [ #  # ]:          0 :                     if (out)
     460 [ #  # ][ #  # ]:          0 :                         *out << "Failed to unpack collfreq for term '" << term
     461 [ #  # ][ #  # ]:          0 :                              << "'" << endl;
     462                 :          0 :                     ++errors;
     463                 :          0 :                     continue;
     464                 :            :                 }
     465         [ -  + ]:        731 :                 if (!unpack_uint(&pos, end, &did)) {
     466         [ #  # ]:          0 :                     if (out)
     467 [ #  # ][ #  # ]:          0 :                         *out << "Failed to unpack firstdid for term '" << term
     468 [ #  # ][ #  # ]:          0 :                              << "'" << endl;
     469                 :          0 :                     ++errors;
     470                 :          0 :                     continue;
     471                 :            :                 }
     472                 :        731 :                 ++did;
     473                 :            :             } else {
     474                 :            :                 // Continuation chunk.
     475         [ -  + ]:        297 :                 if (current_term.empty()) {
     476         [ #  # ]:          0 :                     if (out)
     477 [ #  # ][ #  # ]:          0 :                         *out << "First chunk for term '" << current_term
     478 [ #  # ][ #  # ]:          0 :                              << "' is a continuation chunk" << endl;
     479                 :          0 :                     ++errors;
     480         [ #  # ]:          0 :                     current_term = term;
     481                 :            :                 }
     482                 :            :                 AssertEq(current_term, term);
     483         [ -  + ]:        297 :                 if (!unpack_uint_preserving_sort(&pos, end, &did)) {
     484         [ #  # ]:          0 :                     if (out)
     485 [ #  # ][ #  # ]:          0 :                         *out << "Failed to unpack did from key" << endl;
     486                 :          0 :                     ++errors;
     487                 :          0 :                     continue;
     488                 :            :                 }
     489         [ -  + ]:        297 :                 if (did <= lastdid) {
     490         [ #  # ]:          0 :                     if (out)
     491                 :            :                         *out << "First did in this chunk is <= last in "
     492 [ #  # ][ #  # ]:          0 :                                 "prev chunk" << endl;
     493                 :          0 :                     ++errors;
     494                 :            :                 }
     495         [ +  - ]:        297 :                 cursor->read_tag();
     496                 :        297 :                 pos = cursor->current_tag.data();
     497                 :        297 :                 end = pos + cursor->current_tag.size();
     498                 :            :             }
     499                 :            : 
     500                 :            :             bool is_last_chunk;
     501         [ -  + ]:       1028 :             if (!unpack_bool(&pos, end, &is_last_chunk)) {
     502         [ #  # ]:          0 :                 if (out)
     503 [ #  # ][ #  # ]:          0 :                     *out << "Failed to unpack last chunk flag" << endl;
     504                 :          0 :                 ++errors;
     505                 :          0 :                 continue;
     506                 :            :             }
     507                 :            :             // Read what the final document ID in this chunk is.
     508         [ -  + ]:       1028 :             if (!unpack_uint(&pos, end, &lastdid)) {
     509         [ #  # ]:          0 :                 if (out)
     510 [ #  # ][ #  # ]:          0 :                     *out << "Failed to unpack increase to last" << endl;
     511                 :          0 :                 ++errors;
     512                 :          0 :                 continue;
     513                 :            :             }
     514                 :       1028 :             lastdid += did;
     515                 :       1028 :             bool bad = false;
     516                 :            :             while (true) {
     517                 :            :                 Xapian::termcount wdf;
     518         [ -  + ]:       1488 :                 if (!unpack_uint(&pos, end, &wdf)) {
     519         [ #  # ]:          0 :                     if (out)
     520 [ #  # ][ #  # ]:          0 :                         *out << "Failed to unpack wdf" << endl;
     521                 :          0 :                     ++errors;
     522                 :          0 :                     bad = true;
     523                 :          0 :                     break;
     524                 :            :                 }
     525                 :       1488 :                 ++tf;
     526                 :       1488 :                 cf += wdf;
     527                 :            : 
     528         [ +  + ]:       1488 :                 if (pos == end) break;
     529                 :            : 
     530                 :            :                 Xapian::docid inc;
     531         [ -  + ]:        460 :                 if (!unpack_uint(&pos, end, &inc)) {
     532         [ #  # ]:          0 :                     if (out)
     533 [ #  # ][ #  # ]:          0 :                         *out << "Failed to unpack docid increase" << endl;
     534                 :          0 :                     ++errors;
     535                 :          0 :                     bad = true;
     536                 :          0 :                     break;
     537                 :            :                 }
     538                 :        460 :                 ++inc;
     539                 :        460 :                 did += inc;
     540         [ -  + ]:        460 :                 if (did > lastdid) {
     541         [ #  # ]:          0 :                     if (out)
     542 [ #  # ][ #  # ]:          0 :                         *out << "docid " << did << " > last docid " << lastdid
         [ #  # ][ #  # ]
     543         [ #  # ]:          0 :                              << endl;
     544                 :        460 :                     ++errors;
     545                 :            :                 }
     546                 :            :             }
     547         [ -  + ]:       1028 :             if (bad) {
     548                 :          0 :                 continue;
     549                 :            :             }
     550         [ +  + ]:       1028 :             if (is_last_chunk) {
     551         [ -  + ]:        731 :                 if (tf != termfreq) {
     552         [ #  # ]:          0 :                     if (out)
     553 [ #  # ][ #  # ]:          0 :                         *out << "termfreq " << termfreq << " != # of entries "
                 [ #  # ]
     554 [ #  # ][ #  # ]:          0 :                              << tf << endl;
     555                 :          0 :                     ++errors;
     556                 :            :                 }
     557         [ -  + ]:        731 :                 if (cf != collfreq) {
     558         [ #  # ]:          0 :                     if (out)
     559 [ #  # ][ #  # ]:          0 :                         *out << "collfreq " << collfreq << " != sum wdf " << cf
         [ #  # ][ #  # ]
     560         [ #  # ]:          0 :                              << endl;
     561                 :          0 :                     ++errors;
     562                 :            :                 }
     563         [ -  + ]:        731 :                 if (did != lastdid) {
     564         [ #  # ]:          0 :                     if (out)
     565 [ #  # ][ #  # ]:          0 :                         *out << "lastdid " << lastdid << " != last did " << did
         [ #  # ][ #  # ]
     566         [ #  # ]:          0 :                              << endl;
     567                 :          0 :                     ++errors;
     568                 :            :                 }
     569 [ +  - ][ +  - ]:       1028 :                 current_term.resize(0);
     570                 :            :             }
     571                 :       1028 :         }
     572         [ -  + ]:         15 :         if (!current_term.empty()) {
     573         [ #  # ]:          0 :             if (out)
     574 [ #  # ][ #  # ]:          0 :                 *out << "Last term '" << current_term << "' has no last chunk"
                 [ #  # ]
     575         [ #  # ]:          0 :                      << endl;
     576                 :          0 :             ++errors;
     577                 :            :         }
     578                 :            : 
     579                 :         15 :         Xapian::doccount doccount = version_file.get_doccount();
     580         [ -  + ]:         15 :         if (num_doclens != doccount) {
     581         [ #  # ]:          0 :             if (out)
     582 [ #  # ][ #  # ]:          0 :                 *out << "Document length list has " << num_doclens
     583 [ #  # ][ #  # ]:          0 :                      << " entries, should be " << doccount << endl;
                 [ #  # ]
     584                 :          0 :             ++errors;
     585                 :            :         }
     586                 :            : 
     587                 :         15 :         map<Xapian::valueno, VStats>::const_iterator i;
     588         [ +  + ]:        127 :         for (i = valuestats.begin(); i != valuestats.end(); ++i) {
     589         [ -  + ]:        112 :             if (i->second.freq != i->second.freq_real) {
     590         [ #  # ]:          0 :                 if (out)
     591 [ #  # ][ #  # ]:          0 :                     *out << "Value stats frequency for slot " << i->first
     592 [ #  # ][ #  # ]:          0 :                          << " is " << i->second.freq << " but recounting "
     593 [ #  # ][ #  # ]:          0 :                             "gives " << i->second.freq_real << endl;
                 [ #  # ]
     594                 :          0 :                 ++errors;
     595                 :            :             }
     596                 :         15 :         }
     597         [ +  + ]:         44 :     } else if (strcmp(tablename, "docdata") == 0) {
     598                 :            :         // glass doesn't store a docdata entry if the document data is empty,
     599                 :            :         // so we can only check there aren't more docdata entries than
     600                 :            :         // documents.
     601                 :         10 :         Xapian::doccount doccount = version_file.get_doccount();
     602         [ -  + ]:         10 :         if (table->get_entry_count() > doccount) {
     603         [ #  # ]:          0 :             if (out)
     604 [ #  # ][ #  # ]:          0 :                 *out << "More document data (" << table->get_entry_count()
     605 [ #  # ][ #  # ]:          0 :                      << ") then documents (" << doccount << ")" << endl;
         [ #  # ][ #  # ]
     606                 :          0 :             ++errors;
     607                 :            :         }
     608                 :            : 
     609                 :            :         // Now check the contents of the docdata table.
     610 [ +  - ][ +  + ]:         70 :         for ( ; !cursor->after_end(); cursor->next()) {
     611                 :         60 :             string & key = cursor->current_key;
     612                 :            : 
     613                 :            :             // Get docid from key.
     614                 :         60 :             const char * pos = key.data();
     615                 :         60 :             const char * end = pos + key.size();
     616                 :            : 
     617                 :            :             Xapian::docid did;
     618         [ -  + ]:         60 :             if (!unpack_uint_preserving_sort(&pos, end, &did)) {
     619         [ #  # ]:          0 :                 if (out)
     620 [ #  # ][ #  # ]:          0 :                     *out << "Error unpacking docid from key" << endl;
     621                 :          0 :                 ++errors;
     622         [ -  + ]:         60 :             } else if (pos != end) {
     623         [ #  # ]:          0 :                 if (out)
     624 [ #  # ][ #  # ]:          0 :                     *out << "Extra junk in key" << endl;
     625                 :          0 :                 ++errors;
     626                 :            :             } else {
     627         [ -  + ]:         60 :                 if (did > db_last_docid) {
     628         [ #  # ]:          0 :                     if (out)
     629 [ #  # ][ #  # ]:          0 :                         *out << "document id " << did << " in docdata table "
     630         [ #  # ]:          0 :                                 "is larger than get_last_docid() "
     631 [ #  # ][ #  # ]:          0 :                              << db_last_docid << endl;
     632                 :          0 :                     ++errors;
     633                 :            :                 }
     634                 :            :             }
     635                 :            : 
     636                 :            :             // Fetch and decompress the document data to catch problems with
     637                 :            :             // the splitting into multiple items, corruption of the compressed
     638                 :            :             // data, etc.
     639         [ +  - ]:         60 :             cursor->read_tag();
     640         [ -  + ]:         60 :             if (cursor->current_tag.empty()) {
     641                 :            :                 // We shouldn't store empty document data.
     642         [ #  # ]:          0 :                 if (out)
     643                 :            :                     *out << "Empty document data explicitly stored for "
     644 [ #  # ][ #  # ]:          0 :                             "document id " << did << endl;
                 [ #  # ]
     645                 :          0 :                 ++errors;
     646                 :            :             }
     647                 :            :         }
     648         [ +  + ]:         34 :     } else if (strcmp(tablename, "termlist") == 0) {
     649                 :            :         // Now check the contents of the termlist table.
     650                 :         15 :         Xapian::doccount num_termlists = 0;
     651                 :         15 :         Xapian::doccount num_slotsused_entries = 0;
     652 [ +  - ][ +  + ]:        149 :         for ( ; !cursor->after_end(); cursor->next()) {
     653                 :        134 :             string & key = cursor->current_key;
     654                 :            : 
     655                 :            :             // Get docid from key.
     656                 :        134 :             const char * pos = key.data();
     657                 :        134 :             const char * end = pos + key.size();
     658                 :            : 
     659                 :            :             Xapian::docid did;
     660         [ -  + ]:        134 :             if (!unpack_uint_preserving_sort(&pos, end, &did)) {
     661         [ #  # ]:          0 :                 if (out)
     662 [ #  # ][ #  # ]:          0 :                     *out << "Error unpacking docid from key" << endl;
     663                 :          0 :                 ++errors;
     664                 :         64 :                 continue;
     665                 :            :             }
     666                 :            : 
     667         [ -  + ]:        134 :             if (did > db_last_docid) {
     668         [ #  # ]:          0 :                 if (out)
     669 [ #  # ][ #  # ]:          0 :                     *out << "document id " << did << " in termlist table "
     670         [ #  # ]:          0 :                             "is larger than get_last_docid() "
     671 [ #  # ][ #  # ]:          0 :                          << db_last_docid << endl;
     672                 :          0 :                 ++errors;
     673                 :            :             }
     674                 :            : 
     675 [ +  + ][ +  - ]:        134 :             if (end - pos == 1 && *pos == '\0') {
     676                 :            :                 // Value slots used entry.
     677                 :         60 :                 ++num_slotsused_entries;
     678         [ +  - ]:         60 :                 cursor->read_tag();
     679                 :            : 
     680                 :         60 :                 pos = cursor->current_tag.data();
     681                 :         60 :                 end = pos + cursor->current_tag.size();
     682                 :            : 
     683         [ -  + ]:         60 :                 if (pos == end) {
     684         [ #  # ]:          0 :                     if (out)
     685 [ #  # ][ #  # ]:          0 :                         *out << "Empty value slots used tag" << endl;
     686                 :          0 :                     ++errors;
     687                 :          0 :                     continue;
     688                 :            :                 }
     689                 :            : 
     690                 :            :                 Xapian::valueno prev_slot;
     691         [ -  + ]:         60 :                 if (!unpack_uint(&pos, end, &prev_slot)) {
     692         [ #  # ]:          0 :                     if (out)
     693 [ #  # ][ #  # ]:          0 :                         *out << "Value slot encoding corrupt" << endl;
     694                 :          0 :                     ++errors;
     695                 :          0 :                     continue;
     696                 :            :                 }
     697                 :            : 
     698         [ +  + ]:        840 :                 while (pos != end) {
     699                 :            :                     Xapian::valueno slot;
     700         [ -  + ]:        780 :                     if (!unpack_uint(&pos, end, &slot)) {
     701         [ #  # ]:          0 :                         if (out)
     702 [ #  # ][ #  # ]:          0 :                             *out << "Value slot encoding corrupt" << endl;
     703                 :          0 :                         ++errors;
     704                 :          0 :                         break;
     705                 :            :                     }
     706                 :        780 :                     slot += prev_slot + 1;
     707         [ -  + ]:        780 :                     if (slot <= prev_slot) {
     708         [ #  # ]:          0 :                         if (out)
     709         [ #  # ]:          0 :                             *out << "Value slot number overflowed ("
     710 [ #  # ][ #  # ]:          0 :                                  << prev_slot << " -> " << slot << ")" << endl;
         [ #  # ][ #  # ]
                 [ #  # ]
     711                 :          0 :                         ++errors;
     712                 :            :                     }
     713                 :        780 :                     prev_slot = slot;
     714                 :            :                 }
     715                 :         60 :                 continue;
     716                 :            :             }
     717                 :            : 
     718         [ -  + ]:         74 :             if (pos != end) {
     719         [ #  # ]:          0 :                 if (out)
     720 [ #  # ][ #  # ]:          0 :                     *out << "Extra junk in key" << endl;
     721                 :          0 :                 ++errors;
     722                 :          0 :                 continue;
     723                 :            :             }
     724                 :            : 
     725                 :         74 :             ++num_termlists;
     726         [ +  - ]:         74 :             cursor->read_tag();
     727                 :            : 
     728                 :         74 :             pos = cursor->current_tag.data();
     729                 :         74 :             end = pos + cursor->current_tag.size();
     730                 :            : 
     731         [ +  + ]:         74 :             if (pos == end) {
     732                 :            :                 // Empty termlist.
     733                 :          4 :                 continue;
     734                 :            :             }
     735                 :            : 
     736                 :            :             Xapian::termcount doclen, termlist_size;
     737                 :            : 
     738                 :            :             // Read doclen
     739         [ -  + ]:         70 :             if (!unpack_uint(&pos, end, &doclen)) {
     740         [ #  # ]:          0 :                 if (out) {
     741         [ #  # ]:          0 :                     if (pos != 0) {
     742         [ #  # ]:          0 :                         *out << "doclen out of range";
     743                 :            :                     } else {
     744         [ #  # ]:          0 :                         *out << "Unexpected end of data when reading doclen";
     745                 :            :                     }
     746         [ #  # ]:          0 :                     *out << endl;
     747                 :            :                 }
     748                 :          0 :                 ++errors;
     749                 :          0 :                 continue;
     750                 :            :             }
     751                 :            : 
     752                 :            :             // Check doclen with doclen lower and upper bounds
     753         [ -  + ]:         70 :             if (doclen > version_file.get_doclength_upper_bound()) {
     754         [ #  # ]:          0 :                 if (out)
     755 [ #  # ][ #  # ]:          0 :                     *out << "doclen " << doclen << " > upper bound "
                 [ #  # ]
     756 [ #  # ][ #  # ]:          0 :                          << version_file.get_doclength_upper_bound() << endl;
     757                 :          0 :                 ++errors;
     758 [ +  + ][ -  + ]:         70 :             } else if (doclen < version_file.get_doclength_lower_bound() &&
                 [ -  + ]
     759                 :          4 :                        doclen != 0) {
     760         [ #  # ]:          0 :                 if (out)
     761 [ #  # ][ #  # ]:          0 :                     *out << "doclen " << doclen << " < lower bound "
                 [ #  # ]
     762 [ #  # ][ #  # ]:          0 :                          << version_file.get_doclength_lower_bound() << endl;
     763                 :          0 :                 ++errors;
     764                 :            :             }
     765                 :            : 
     766                 :            :             // Read termlist_size
     767         [ -  + ]:         70 :             if (!unpack_uint(&pos, end, &termlist_size)) {
     768         [ #  # ]:          0 :                 if (out) {
     769         [ #  # ]:          0 :                     if (pos != 0) {
     770         [ #  # ]:          0 :                         *out << "termlist_size out of range";
     771                 :            :                     } else {
     772                 :            :                         *out << "Unexpected end of data when reading "
     773         [ #  # ]:          0 :                                 "termlist_size";
     774                 :            :                     }
     775         [ #  # ]:          0 :                     *out << endl;
     776                 :            :                 }
     777                 :          0 :                 ++errors;
     778                 :          0 :                 continue;
     779                 :            :             }
     780                 :            : 
     781                 :         70 :             Xapian::termcount actual_doclen = 0, actual_termlist_size = 0;
     782         [ +  - ]:         70 :             string current_tname;
     783                 :            : 
     784                 :         70 :             bool bad = false;
     785         [ +  + ]:       1558 :             while (pos != end) {
     786                 :       1488 :                 Xapian::doccount current_wdf = 0;
     787                 :       1488 :                 bool got_wdf = false;
     788                 :            :                 // If there was a previous term, how much to reuse.
     789         [ +  + ]:       1488 :                 if (!current_tname.empty()) {
     790                 :       1418 :                     string::size_type len = static_cast<unsigned char>(*pos++);
     791         [ +  - ]:       1418 :                     if (len > current_tname.length()) {
     792                 :            :                         // The wdf was squeezed into the same byte.
     793                 :       1418 :                         current_wdf = len / (current_tname.length() + 1) - 1;
     794                 :       1418 :                         len %= (current_tname.length() + 1);
     795                 :       1418 :                         got_wdf = true;
     796                 :            :                     }
     797         [ +  - ]:       1418 :                     current_tname.resize(len);
     798                 :            :                 }
     799                 :            :                 // What to append (note len must be positive, since just truncating
     800                 :            :                 // always takes us backwards in the sort order)
     801                 :       1488 :                 string::size_type len = static_cast<unsigned char>(*pos++);
     802         [ +  - ]:       1488 :                 current_tname.append(pos, len);
     803                 :       1488 :                 pos += len;
     804                 :            : 
     805         [ +  + ]:       1488 :                 if (!got_wdf) {
     806                 :            :                     // Read wdf
     807         [ -  + ]:         70 :                     if (!unpack_uint(&pos, end, &current_wdf)) {
     808         [ #  # ]:          0 :                         if (out) {
     809         [ #  # ]:          0 :                             if (pos == 0) {
     810                 :            :                                 *out << "Unexpected end of data when reading "
     811         [ #  # ]:          0 :                                         "termlist current_wdf";
     812                 :            :                             } else {
     813         [ #  # ]:          0 :                                 *out << "Size of wdf out of range in termlist";
     814                 :            :                             }
     815         [ #  # ]:          0 :                             *out << endl;
     816                 :            :                         }
     817                 :          0 :                         ++errors;
     818                 :          0 :                         bad = true;
     819                 :          0 :                         break;
     820                 :            :                     }
     821                 :            :                 }
     822                 :            : 
     823                 :       1488 :                 ++actual_termlist_size;
     824                 :       1488 :                 actual_doclen += current_wdf;
     825                 :            :             }
     826         [ -  + ]:         70 :             if (bad) {
     827                 :          0 :                 continue;
     828                 :            :             }
     829                 :            : 
     830         [ -  + ]:         70 :             if (termlist_size != actual_termlist_size) {
     831         [ #  # ]:          0 :                 if (out)
     832 [ #  # ][ #  # ]:          0 :                     *out << "termlist_size != # of entries in termlist" << endl;
     833                 :          0 :                 ++errors;
     834                 :            :             }
     835         [ -  + ]:         70 :             if (doclen != actual_doclen) {
     836         [ #  # ]:          0 :                 if (out)
     837 [ #  # ][ #  # ]:          0 :                     *out << "doclen != sum(wdf)" << endl;
     838                 :          0 :                 ++errors;
     839                 :            :             }
     840                 :            : 
     841                 :            :             // + 1 so that did is a valid subscript.
     842 [ +  - ][ +  - ]:         70 :             if (doclens.size() <= did) doclens.resize(did + 1);
     843         [ +  - ]:         70 :             doclens[did] = actual_doclen;
     844                 :         70 :         }
     845                 :            : 
     846                 :         15 :         Xapian::doccount doccount = version_file.get_doccount();
     847                 :            : 
     848                 :            :         // glass doesn't store a termlist entry if there are no terms, so we
     849                 :            :         // can only check there aren't more termlists than documents.
     850         [ -  + ]:         15 :         if (num_termlists > doccount) {
     851         [ #  # ]:          0 :             if (out)
     852 [ #  # ][ #  # ]:          0 :                 *out << "More termlists (" << num_termlists
     853 [ #  # ][ #  # ]:          0 :                      << ") then documents (" << doccount << ")" << endl;
         [ #  # ][ #  # ]
     854                 :          0 :             ++errors;
     855                 :            :         }
     856                 :            : 
     857                 :            :         // glass doesn't store a valueslots used entry if there are no terms,
     858                 :            :         // so we can only check there aren't more such entries than documents.
     859         [ -  + ]:         15 :         if (num_slotsused_entries > doccount) {
     860         [ #  # ]:          0 :             if (out)
     861 [ #  # ][ #  # ]:          0 :                 *out << "More slots-used entries (" << num_slotsused_entries
     862 [ #  # ][ #  # ]:          0 :                      << ") then documents (" << doccount << ")" << endl;
         [ #  # ][ #  # ]
     863                 :         15 :             ++errors;
     864                 :            :         }
     865         [ +  + ]:         19 :     } else if (strcmp(tablename, "position") == 0) {
     866                 :            :         // Now check the contents of the position table.
     867 [ +  - ][ +  + ]:       1185 :         for ( ; !cursor->after_end(); cursor->next()) {
     868                 :       1176 :             string & key = cursor->current_key;
     869                 :            : 
     870                 :            :             // Get docid from key.
     871                 :       1176 :             const char * pos = key.data();
     872                 :       1176 :             const char * end = pos + key.size();
     873                 :            : 
     874         [ +  - ]:       1176 :             string term;
     875 [ +  - ][ -  + ]:       1176 :             if (!unpack_string_preserving_sort(&pos, end, term)) {
     876         [ #  # ]:          0 :                 if (out)
     877 [ #  # ][ #  # ]:          0 :                     *out << "Error unpacking term from key" << endl;
     878                 :          0 :                 ++errors;
     879                 :          0 :                 continue;
     880                 :            :             }
     881                 :            : 
     882                 :            :             Xapian::docid did;
     883         [ -  + ]:       1176 :             if (!unpack_uint_preserving_sort(&pos, end, &did)) {
     884         [ #  # ]:          0 :                 if (out)
     885 [ #  # ][ #  # ]:          0 :                     *out << "Error unpacking docid from key" << endl;
     886                 :          0 :                 ++errors;
     887                 :          0 :                 continue;
     888                 :            :             }
     889                 :            : 
     890         [ -  + ]:       1176 :             if (pos != end) {
     891         [ #  # ]:          0 :                 if (out)
     892 [ #  # ][ #  # ]:          0 :                     *out << "Extra junk in key with docid " << did << endl;
                 [ #  # ]
     893                 :          0 :                 ++errors;
     894                 :          0 :                 continue;
     895                 :            :             }
     896                 :            : 
     897         [ -  + ]:       1176 :             if (did > db_last_docid) {
     898         [ #  # ]:          0 :                 if (out)
     899 [ #  # ][ #  # ]:          0 :                     *out << "document id " << did << " in position table "
     900         [ #  # ]:          0 :                             "is larger than get_last_docid() "
     901 [ #  # ][ #  # ]:          0 :                          << db_last_docid << endl;
     902                 :          0 :                 ++errors;
     903         [ +  - ]:       1176 :             } else if (!doclens.empty()) {
     904                 :            :                 // In glass, a document without terms doesn't get a
     905                 :            :                 // termlist entry, so we can't tell the difference
     906                 :            :                 // easily.
     907 [ +  - ][ -  + ]:       1176 :                 if (did >= doclens.size() || doclens[did] == 0) {
                 [ -  + ]
     908         [ #  # ]:          0 :                     if (out)
     909 [ #  # ][ #  # ]:          0 :                         *out << "Position list entry for document " << did
     910 [ #  # ][ #  # ]:          0 :                              << " which doesn't exist or has no terms" << endl;
     911                 :          0 :                     ++errors;
     912                 :            :                 }
     913                 :            :             }
     914                 :            : 
     915         [ +  - ]:       1176 :             cursor->read_tag();
     916                 :            : 
     917                 :       1176 :             const string & data = cursor->current_tag;
     918                 :       1176 :             pos = data.data();
     919                 :       1176 :             end = pos + data.size();
     920                 :            : 
     921                 :            :             Xapian::termpos pos_last;
     922         [ -  + ]:       1176 :             if (!unpack_uint(&pos, end, &pos_last)) {
     923         [ #  # ]:          0 :                 if (out)
     924 [ #  # ][ #  # ]:          0 :                     *out << tablename << " table: Position list data corrupt"
     925         [ #  # ]:          0 :                          << endl;
     926                 :          0 :                 ++errors;
     927                 :          0 :                 continue;
     928                 :            :             }
     929         [ +  + ]:       1176 :             if (pos == end) {
     930                 :            :                 // Special case for single entry position list.
     931                 :            :             } else {
     932                 :            :                 // Skip the header we just read.
     933                 :        152 :                 BitReader rd(pos, end);
     934         [ +  - ]:        152 :                 Xapian::termpos pos_first = rd.decode(pos_last);
     935         [ +  - ]:        152 :                 Xapian::termpos pos_size = rd.decode(pos_last - pos_first) + 2;
     936         [ +  - ]:        152 :                 rd.decode_interpolative(0, pos_size - 1, pos_first, pos_last);
     937         [ +  - ]:        152 :                 Xapian::termpos p = rd.decode_interpolative_next();
     938                 :        152 :                 bool ok = true;
     939         [ +  + ]:        304 :                 while (p != pos_last) {
     940                 :        152 :                     Xapian::termpos pos_prev = p;
     941         [ +  - ]:        152 :                     p = rd.decode_interpolative_next();
     942         [ -  + ]:        152 :                     if (p <= pos_prev) {
     943         [ #  # ]:          0 :                         if (out)
     944         [ #  # ]:          0 :                             *out << tablename << " table: Positions not "
     945 [ #  # ][ #  # ]:          0 :                                     "strictly monotonically increasing" << endl;
     946                 :          0 :                         ++errors;
     947                 :          0 :                         ok = false;
     948                 :          0 :                         break;
     949                 :            :                     }
     950                 :            :                 }
     951 [ +  - ][ -  + ]:        152 :                 if (ok && !rd.check_all_gone()) {
                 [ -  + ]
     952         [ #  # ]:          0 :                     if (out)
     953 [ #  # ][ #  # ]:          0 :                         *out << tablename << " table: Junk after position data"
     954         [ #  # ]:          0 :                              << endl;
     955                 :          0 :                     ++errors;
     956         [ +  - ]:       1176 :                 }
     957                 :            :             }
     958                 :       1176 :         }
     959                 :            :     } else {
     960         [ +  + ]:         10 :         if (out)
     961 [ +  - ][ +  - ]:          8 :             *out << tablename << " table: Don't know how to check structure\n"
     962         [ +  - ]:          8 :                  << endl;
     963                 :         10 :         return errors;
     964                 :            :     }
     965                 :            : 
     966         [ +  + ]:         49 :     if (out) {
     967         [ +  - ]:         31 :         if (!errors)
     968 [ +  - ][ +  - ]:         31 :             *out << tablename << " table structure checked OK\n";
     969                 :            :         else
     970 [ #  # ][ #  # ]:          0 :             *out << tablename << " table errors found: " << errors << "\n";
         [ #  # ][ #  # ]
     971         [ +  - ]:         31 :         *out << endl;
     972                 :            :     }
     973                 :            : 
     974                 :        136 :     return errors;
     975                 :            : }
     976                 :            : 
     977                 :            : #ifdef DISABLE_GPL_LIBXAPIAN
     978                 :            : # error GPL source we cannot relicense included in libxapian
     979                 :            : #endif

Generated by: LCOV version 1.11