backends/quartz/btreecheck.cc

Go to the documentation of this file.
00001 /* btreecheck.cc: Btree checking
00002  *
00003  * Copyright 1999,2000,2001 BrightStation PLC
00004  * Copyright 2002 Ananova Ltd
00005  * Copyright 2002,2004,2005,2008 Olly Betts
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License as
00009  * published by the Free Software Foundation; either version 2 of the
00010  * License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
00020  * USA
00021  */
00022 
00023 #include <config.h>
00024 
00025 #include <limits.h>
00026 
00027 #include <iostream>
00028 
00029 using namespace std;
00030 
00031 #include "btreecheck.h"
00032 
00033 void BtreeCheck::print_spaces(int n) const
00034 {
00035     while (n--) out.put(' ');
00036 }
00037 
00038 void BtreeCheck::print_bytes(int n, const byte * p) const
00039 {
00040     out.write(reinterpret_cast<const char *>(p), n);
00041 }
00042 
00043 void BtreeCheck::print_key(const byte * p, int c, int j) const
00044 {
00045     Item item(p, c);
00046     string key;
00047     if (item.key().length() >= 0)
00048         item.key().read(&key);
00049     if (j == 0) {
00050         out << key << '/' << item.component_of();
00051     } else {
00052         for (string::const_iterator i = key.begin(); i != key.end(); ++i) {
00053             // out << (*i < 32 ? '.' : *i);
00054             char ch = *i;
00055             if (ch < 32) out << '/' << unsigned(ch); else out << ch;
00056         }
00057     }
00058 }
00059 
00060 void BtreeCheck::print_tag(const byte * p, int c, int j) const
00061 {
00062     Item item(p, c);
00063     string tag;
00064     item.append_chunk(&tag);
00065     if (j == 0) {
00066         out << "/" << item.components_of() << tag;
00067     } else {
00068         out << "--> [" << get_int4(reinterpret_cast<const byte*>(tag.data()), 0)
00069             << ']';
00070     }
00071 }
00072 
00073 void BtreeCheck::report_block_full(int m, int n, const byte * p) const
00074 {
00075     int j = GET_LEVEL(p);
00076     int dir_end = DIR_END(p);
00077     out << '\n';
00078     print_spaces(m);
00079     out << "Block [" << n << "] level " << j << ", revision *" << REVISION(p)
00080          << " items (" << (dir_end - DIR_START)/D2 << ") usage "
00081          << block_usage(p) << "%:\n";
00082     for (int c = DIR_START; c < dir_end; c += D2) {
00083         print_spaces(m);
00084         print_key(p, c, j);
00085         out << ' ';
00086         print_tag(p, c, j);
00087         out << '\n';
00088     }
00089 }
00090 
00091 int BtreeCheck::block_usage(const byte * p) const
00092 {
00093     int space = block_size - DIR_END(p);
00094     int free = TOTAL_FREE(p);
00095     return (space - free) * 100 / space;  /* a percentage */
00096 }
00097 
00101 void BtreeCheck::report_block(int m, int n, const byte * p) const
00102 {
00103     int j = GET_LEVEL(p);
00104     int dir_end = DIR_END(p);
00105     int c;
00106     print_spaces(m);
00107     out << "[" << n << "] *" << REVISION(p) << " ("
00108         << (dir_end - DIR_START)/D2 << ") " << block_usage(p) << "% ";
00109 
00110     for (c = DIR_START; c < dir_end; c += D2) {
00111         if (c == DIR_START + 6) out << "... ";
00112         if (c >= DIR_START + 6 && c < dir_end - 6) continue;
00113 
00114         print_key(p, c, j);
00115         out << ' ';
00116     }
00117     out << endl;
00118 }
00119 
00120 void BtreeCheck::failure(int n) const
00121 {
00122     out << "B-tree error " << n << endl;
00123     throw "btree error";
00124 }
00125 
00126 void
00127 BtreeCheck::block_check(Cursor * C_, int j, int opts)
00128 {
00129     byte * p = C_[j].p;
00130     uint4 n = C_[j].n;
00131     size_t c;
00132     size_t significant_c = j == 0 ? DIR_START : DIR_START + D2;
00133         /* the first key in an index block is dummy, remember */
00134 
00135     size_t max_free = MAX_FREE(p);
00136     size_t dir_end = DIR_END(p);
00137     int total_free = block_size - dir_end;
00138 
00139     if (base.block_free_at_start(n)) failure(0);
00140     if (base.block_free_now(n)) failure(1);
00141     base.free_block(n);
00142 
00143     if (j != GET_LEVEL(p)) failure(10);
00144     if (dir_end <= DIR_START || dir_end > block_size) failure(20);
00145 
00146     if (opts & OPT_SHORT_TREE) report_block(3*(level - j), n, p);
00147 
00148     if (opts & OPT_FULL_TREE) report_block_full(3*(level - j), n, p);
00149 
00150     for (c = DIR_START; c < dir_end; c += D2) {
00151         Item item(p, c);
00152         int o = item.get_address() - p;
00153         if (o > int(block_size)) failure(21);
00154         if (o - dir_end < max_free) failure(30);
00155 
00156         int kt_len = item.size();
00157         if (o + kt_len > int(block_size)) failure(40);
00158         total_free -= kt_len;
00159 
00160         if (c > significant_c && Item(p, c - D2).key() >= item.key())
00161             failure(50);
00162     }
00163     if (total_free != TOTAL_FREE(p))
00164         failure(60);
00165 
00166     if (j == 0) return;
00167     for (c = DIR_START; c < dir_end; c += D2) {
00168         C_[j].c = c;
00169         block_to_cursor(C_, j - 1, Item(p, c).block_given_by());
00170 
00171         block_check(C_, j - 1, opts);
00172 
00173         byte * q = C_[j - 1].p;
00174         /* if j == 1, and c > DIR_START, the first key of level j - 1 must be
00175          * >= the key of p, c: */
00176 
00177         if (j == 1 && c > DIR_START)
00178             if (Item(q, DIR_START).key() < Item(p, c).key())
00179                 failure(70);
00180 
00181         /* if j > 1, and c > DIR_START, the second key of level j - 1 must be
00182          * >= the key of p, c: */
00183 
00184         if (j > 1 && c > DIR_START && DIR_END(q) > DIR_START + D2 &&
00185             Item(q, DIR_START + D2).key() < Item(p, c).key())
00186             failure(80);
00187 
00188         /* the last key of level j - 1 must be < the key of p, c + D2, if c +
00189          * D2 < dir_end: */
00190 
00191         if (c + D2 < dir_end &&
00192             (j == 1 || DIR_START + D2 < DIR_END(q)) &&
00193             Item(q, DIR_END(q) - D2).key() >= Item(p, c + D2).key())
00194             failure(90);
00195 
00196         if (REVISION(q) > REVISION(p)) failure(91);
00197     }
00198 }
00199 
00200 void
00201 BtreeCheck::check(const string & name, int opts, ostream &out)
00202 {
00203     BtreeCheck B(name, false, out);
00204     B.open(); // throws exception if open fails
00205     Cursor * C = B.C;
00206 
00207     if (opts & OPT_SHOW_STATS) {
00208         out << "base" << (char)B.base_letter
00209             << " blocksize=" << B.block_size / 1024 << "K"
00210                " items=" << B.item_count
00211             << " lastblock=" << B.base.get_last_block()
00212             << " revision=" << B.revision_number
00213             << " levels=" << B.level
00214             << " root=";
00215         if (B.faked_root_block)
00216             out << "(faked)";
00217         else
00218             out << C[B.level].n;
00219         out << endl;
00220     }
00221 
00222     int limit = B.base.get_bit_map_size() - 1;
00223 
00224     limit = limit * CHAR_BIT + CHAR_BIT - 1;
00225 
00226     if (opts & OPT_SHOW_BITMAP) {
00227         for (int j = 0; j <= limit; j++) {
00228             out << (B.base.block_free_at_start(j) ? '.' : '*');
00229             if (j > 0) {
00230                 if ((j + 1) % 100 == 0) {
00231                     out << '\n';
00232                 } else if ((j + 1) % 10 == 0) {
00233                     out << ' ';
00234                 }
00235             }
00236         }
00237         out << '\n' << endl;
00238     }
00239 
00240     if (B.faked_root_block) {
00241         if (opts) out << "void ";
00242     } else {
00243         B.block_check(C, B.level, opts);
00244 
00245         /* the bit map should now be entirely clear: */
00246 
00247         if (!B.base.is_empty()) {
00248             B.failure(100);
00249         }
00250     }
00251     if (opts) out << "B-tree checked okay" << endl;
00252 }
00253 
00254 void BtreeCheck::report_cursor(int N, const Cursor * C_) const
00255 {
00256     out << N << ")\n";
00257     for (int i = 0; i <= level; i++)
00258         out << "p=" << C_[i].p << ", c=" << C_[i].c << ", n=[" << C_[i].n
00259             << "], rewrite=" << C_[i].rewrite << endl;
00260 }

Documentation for Xapian (version 1.0.10).
Generated on 24 Dec 2008 by Doxygen 1.5.2.