00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef OM_HGUARD_BTREE_H
00023 #define OM_HGUARD_BTREE_H
00024
00025 #include <xapian/visibility.h>
00026
00027 #include <algorithm>
00028 #include <string>
00029 using std::string;
00030
00031 #include "quartz_types.h"
00032 #include "btree_base.h"
00033 #include "btree_util.h"
00034 #include "bcursor.h"
00035 #include "noreturn.h"
00036
00042 const string::size_type BTREE_MAX_KEY_LEN = 252;
00043
00044
00045 #define BYTES_PER_BLOCK_NUMBER 4
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057 #define K1 1
00058 #define I2 2
00059 #define D2 2
00060 #define C2 2
00061
00062
00063
00064 #define GETK(p, c) GETINT1(p, c)
00065 #define SETD(p, c, x) SETINT2(p, c, x)
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113 #define REVISION(b) static_cast<unsigned int>(get_int4(b, 0))
00114 #define GET_LEVEL(b) GETINT1(b, 4)
00115 #define MAX_FREE(b) GETINT2(b, 5)
00116 #define TOTAL_FREE(b) GETINT2(b, 7)
00117 #define DIR_END(b) GETINT2(b, 9)
00118 #define DIR_START 11
00119
00120 #define SET_REVISION(b, x) set_int4(b, 0, x)
00121 #define SET_LEVEL(b, x) SETINT1(b, 4, x)
00122 #define SET_MAX_FREE(b, x) SETINT2(b, 5, x)
00123 #define SET_TOTAL_FREE(b, x) SETINT2(b, 7, x)
00124 #define SET_DIR_END(b, x) SETINT2(b, 9, x)
00125
00128 #define SEQ_START_POINT (-10)
00129
00132 #define BLOCK_CAPACITY 4
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156 class XAPIAN_VISIBILITY_DEFAULT Key {
00157 const byte *p;
00158 public:
00159 explicit Key(const byte * p_) : p(p_) { }
00160 const byte * get_address() const { return p; }
00161 void read(string * key) const {
00162 key->assign(reinterpret_cast<const char *>(p + K1), length());
00163 }
00164 bool operator==(Key key2) const;
00165 bool operator!=(Key key2) const { return !(*this == key2); }
00166 bool operator<(Key key2) const;
00167 bool operator>=(Key key2) const { return !(*this < key2); }
00168 bool operator>(Key key2) const { return key2 < *this; }
00169 bool operator<=(Key key2) const { return !(key2 < *this); }
00170 int length() const {
00171 return GETK(p, 0) - C2 - K1;
00172 }
00173 char operator[](size_t i) const {
00174 return p[i + K1];
00175 }
00176 };
00177
00178
00179
00180 template <class T> class Item_base {
00181 protected:
00182 T p;
00183 public:
00184
00185 Item_base(T p_, int c) : p(p_ + GETINT2(p_, c)) { }
00186 Item_base(T p_) : p(p_) { }
00187 T get_address() const { return p; }
00188 int size() const { return GETINT2(p, 0); }
00189 int component_of() const {
00190 return GETINT2(p, GETK(p, I2) + I2 - C2);
00191 }
00192 int components_of() const {
00193 return GETINT2(p, GETK(p, I2) + I2);
00194 }
00195 Key key() const { return Key(p + I2); }
00196 void append_chunk(string * tag) const {
00197
00198 int cd = GETK(p, I2) + I2 + C2;
00199 int l = size() - cd;
00200 tag->append(reinterpret_cast<const char *>(p + cd), l);
00201 }
00205 uint4 block_given_by() const {
00206 return get_int4(p, size() - BYTES_PER_BLOCK_NUMBER);
00207 }
00208 };
00209
00210 class Item : public Item_base<const byte *> {
00211 public:
00212
00213 Item(const byte * p_, int c) : Item_base<const byte *>(p_, c) { }
00214 Item(const byte * p_) : Item_base<const byte *>(p_) { }
00215 };
00216
00217 class Item_wr : public Item_base<byte *> {
00218 void set_key_len(int x) { SETINT1(p, I2, x); }
00219 public:
00220
00221 Item_wr(byte * p_, int c) : Item_base<byte *>(p_, c) { }
00222 Item_wr(byte * p_) : Item_base<byte *>(p_) { }
00223 void set_component_of(int i) {
00224 SETINT2(p, GETK(p, I2) + I2 - C2, i);
00225 }
00226 void set_components_of(int m) {
00227 SETINT2(p, GETK(p, I2) + I2, m);
00228 }
00229
00230 void set_key_and_block(Key newkey, int truncate_size, uint4 n) {
00231 int i = truncate_size;
00232
00233
00234 int newkey_len = newkey.length();
00235 int newsize = I2 + K1 + i + C2;
00236
00237 SETINT2(p, 0, newsize + 4);
00238
00239 SETINT1(p, I2, newsize - I2);
00240
00241 memmove(p + I2 + K1, newkey.get_address() + K1, i);
00242
00243 memmove(p + I2 + K1 + i, newkey.get_address() + K1 + newkey_len, C2);
00244
00245
00246 set_int4(p, newsize, n);
00247 }
00248
00252 void set_block_given_by(uint4 n) {
00253 set_int4(p, size() - BYTES_PER_BLOCK_NUMBER, n);
00254 }
00255 void set_size(int l) { SETINT2(p, 0, l); }
00258 void form_null_key(uint4 n) {
00259 set_int4(p, I2 + K1, n);
00260 set_key_len(K1);
00261 set_size(I2 + K1 + 4);
00262 }
00263 void form_key(const string & key_) {
00264 Assert(key_.length() <= BTREE_MAX_KEY_LEN);
00265
00266
00267 string::size_type key_len = std::min(key_.length(), BTREE_MAX_KEY_LEN);
00268
00269 set_key_len(key_len + K1 + C2);
00270 memmove(p + I2 + K1, key_.data(), key_len);
00271 set_component_of(1);
00272 }
00273
00274 void set_tag(int cd, const char *start, int len) {
00275 memmove(p + cd, start, len);
00276 set_size(cd + len);
00277 }
00278 void fake_root_item() {
00279 set_key_len(K1 + C2);
00280 set_size(I2 + K1 + 2 * C2);
00281 set_component_of(1);
00282 set_components_of(1);
00283 }
00284 };
00285
00286
00287
00288
00289 #define BTREE_CURSOR_LEVELS 10
00290
00311 class XAPIAN_VISIBILITY_DEFAULT Btree {
00312 friend class Bcursor;
00313 private:
00315 Btree(const Btree &);
00316
00318 Btree & operator=(const Btree &);
00319
00320 public:
00333 Btree(string path_, bool readonly_);
00334
00340 ~Btree();
00341
00345 void close();
00346
00349 bool exists() const;
00350
00359 void open();
00360
00378 bool open(quartz_revision_number_t revision_);
00379
00393 void commit(quartz_revision_number_t revision);
00394
00400 void cancel();
00401
00415 bool get_exact_entry(const string & key, string & tag) const;
00416
00429 bool find_tag(const string &key, string * tag) const;
00430
00433 void read_tag(Cursor * C_, string *tag) const;
00434
00453 bool add(const string &key, string tag);
00454
00472 bool del(const string &key);
00473
00491 void create(unsigned int blocksize);
00492
00493 void set_full_compaction(bool parity);
00494
00505 quartz_revision_number_t get_latest_revision_number() const {
00506 return latest_revision_number;
00507 }
00508
00517 quartz_revision_number_t get_open_revision_number() const {
00518 return revision_number;
00519 }
00520
00527 quartz_tablesize_t get_entry_count() const {
00528 return item_count;
00529 }
00530
00536 Bcursor * cursor_get() const;
00537
00543 bool is_modified() const { return Btree_modified; }
00544
00550 void set_max_item_size(size_t block_capacity) {
00551 if (block_capacity > 4) block_capacity = 4;
00552 max_item_size = (block_size - DIR_START - block_capacity * D2)
00553 / block_capacity;
00554 }
00555
00556 protected:
00557
00562 bool do_open_to_read(bool revision_supplied, quartz_revision_number_t revision_);
00563
00568 bool do_open_to_write(bool revision_supplied, quartz_revision_number_t revision_);
00569 bool basic_open(bool revision_supplied, quartz_revision_number_t revision);
00570
00571 bool find(Cursor *) const;
00572 int delete_kt();
00573 void read_block(uint4 n, byte *p) const;
00574 void write_block(uint4 n, const byte *p) const;
00575 XAPIAN_NORETURN(void set_overwritten() const);
00576 void block_to_cursor(Cursor *C_, int j, uint4 n) const;
00577 void alter();
00578 void compact(byte *p);
00579 void enter_key(int j, Key prevkey, Key newkey);
00580 int mid_point(byte *p);
00581 void add_item_to_block(byte *p, Item_wr kt, int c);
00582 void add_item(Item_wr kt, int j);
00583 void delete_item(int j, bool repeatedly);
00584 int add_kt(bool found);
00585 void read_root();
00586 void split_root(uint4 split_n);
00587 void form_key(const string & key) const;
00588
00590 quartz_revision_number_t revision_number;
00591
00593 uint4 item_count;
00594
00596 unsigned int block_size;
00597
00601 mutable quartz_revision_number_t latest_revision_number;
00602
00607 mutable bool both_bases;
00608
00610 int base_letter;
00611
00616 bool faked_root_block;
00617
00621 bool sequential;
00622
00624 int handle;
00625
00627 int level;
00628
00630 uint4 root;
00631
00633 mutable Item_wr kt;
00634
00636 byte * buffer;
00637
00639 Btree_base base;
00640
00642 char other_base_letter;
00643
00645 string name;
00646
00650 int seq_count;
00651
00653 uint4 changed_n;
00654
00657 int changed_c;
00658
00660 size_t max_item_size;
00661
00663 mutable bool Btree_modified;
00664
00666 bool full_compaction;
00667
00669 bool writable;
00670
00672 bool dont_close_handle;
00673
00674
00675 bool prev(Cursor *C_, int j) const { return (this->*prev_ptr)(C_, j); }
00676 bool next(Cursor *C_, int j) const { return (this->*next_ptr)(C_, j); }
00677
00678 bool (Btree::* prev_ptr)(Cursor *, int) const;
00679 bool (Btree::* next_ptr)(Cursor *, int) const;
00680
00681 bool prev_default(Cursor *C_, int j) const;
00682 bool next_default(Cursor *C_, int j) const;
00683
00684 bool prev_for_sequential(Cursor *C_, int dummy) const;
00685 bool next_for_sequential(Cursor *C_, int dummy) const;
00686
00687 static int find_in_block(const byte * p, Key key, bool leaf, int c);
00688
00692 static uint4 block_given_by(const byte * p, int c);
00693
00694 mutable Cursor C[BTREE_CURSOR_LEVELS];
00695
00701 byte * split_p;
00702
00703
00704
00705 };
00706
00707 #endif