LCOV - code coverage report
Current view: top level - backends/chert - chert_dbstats.cc (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core r Lines: 23 25 92.0 %
Date: 2011-08-21 Functions: 4 4 100.0 %
Branches: 10 20 50.0 %

           Branch data     Line data    Source code
       1                 :            : /** @file chert_dbstats.cc
       2                 :            :  * @brief Chert class for database statistics.
       3                 :            :  */
       4                 :            : /* Copyright (C) 2009 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 USA
      19                 :            :  */
      20                 :            : 
      21                 :            : #include <config.h>
      22                 :            : 
      23                 :            : #include "chert_dbstats.h"
      24                 :            : 
      25                 :            : #include "chert_postlist.h"
      26                 :            : 
      27                 :            : using namespace std;
      28                 :            : 
      29                 :            : /// The key in the postlist table which we use to store our encoded statistics.
      30                 :       2335 : static const string DATABASE_STATS_KEY(1, '\0');
      31                 :            : 
      32                 :            : void
      33                 :       2131 : ChertDatabaseStats::read(ChertPostListTable & postlist_table)
      34                 :            : {
      35                 :       2131 :     string data;
      36         [ +  + ]:       2131 :     if (!postlist_table.get_exact_entry(DATABASE_STATS_KEY, data)) {
      37                 :            :         // If there's no entry yet, then all the values are zero.
      38                 :        575 :         total_doclen = 0;
      39                 :        575 :         last_docid = 0;
      40                 :        575 :         doclen_lbound = 0;
      41                 :        575 :         doclen_ubound = 0;
      42                 :        575 :         wdf_ubound = 0;
      43                 :            :         return;
      44                 :            :     }
      45                 :            : 
      46                 :       1556 :     const char * p = data.data();
      47                 :       1556 :     const char * end = p + data.size();
      48                 :            : 
      49   [ +  -  +  - ]:       1556 :     if (unpack_uint(&p, end, &last_docid) &&
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
      50                 :            :         unpack_uint(&p, end, &doclen_lbound) &&
      51                 :            :         unpack_uint(&p, end, &wdf_ubound) &&
      52                 :            :         unpack_uint(&p, end, &doclen_ubound) &&
      53                 :            :         unpack_uint_last(&p, end, &total_doclen)) {
      54                 :            :         // doclen_ubound should always be >= wdf_ubound, so we store the
      55                 :            :         // difference as it may encode smaller.  wdf_ubound is likely to
      56                 :            :         // be larger than doclen_lbound.
      57                 :       1556 :         doclen_ubound += wdf_ubound;
      58                 :            :         return;
      59                 :            :     }
      60                 :            : 
      61         [ #  # ]:          0 :     if (p)
      62                 :          0 :         throw Xapian::DatabaseCorruptError("Bad encoded DB stats (overflowed)");
      63                 :            : 
      64                 :       2131 :     throw Xapian::DatabaseCorruptError("Bad encoded DB stats (out of data)");
      65                 :            : }
      66                 :            : 
      67                 :            : void
      68                 :        474 : ChertDatabaseStats::write(ChertPostListTable & postlist_table) const
      69                 :            : {
      70                 :        474 :     string data;
      71                 :        474 :     pack_uint(data, last_docid);
      72                 :        474 :     pack_uint(data, doclen_lbound);
      73                 :        474 :     pack_uint(data, wdf_ubound);
      74                 :            :     // doclen_ubound should always be >= wdf_ubound, so we store the
      75                 :            :     // difference as it may encode smaller.  wdf_ubound is likely to
      76                 :            :     // be larger than doclen_lbound.
      77                 :        474 :     pack_uint(data, doclen_ubound - wdf_ubound);
      78                 :            :     // Micro-optimisation: total_doclen is likely to be the largest value, so
      79                 :            :     // store it last as pack_uint_last() uses a slightly more compact encoding
      80                 :            :     // - this could save us a few bytes!
      81                 :        474 :     pack_uint_last(data, total_doclen);
      82                 :        474 :     postlist_table.add(DATABASE_STATS_KEY, data);
      83 [ +  - ][ +  - ]:       7479 : }

Generated by: LCOV version 1.8