1 /* 2 * Copyright 2001-2008, Axel Dörfler, axeld@pinc-software.de. 3 * This file may be used under the terms of the MIT License. 4 */ 5 #ifndef B_PLUS_TREE_H 6 #define B_PLUS_TREE_H 7 8 9 #include "bfs.h" 10 #include "Journal.h" 11 12 13 // #pragma mark - on-disk structures 14 15 struct bplustree_node; 16 17 #define BPLUSTREE_NULL -1LL 18 #define BPLUSTREE_FREE -2LL 19 20 struct bplustree_header { 21 uint32 magic; 22 uint32 node_size; 23 uint32 max_number_of_levels; 24 uint32 data_type; 25 off_t root_node_pointer; 26 off_t free_node_pointer; 27 off_t maximum_size; 28 29 uint32 Magic() const { return BFS_ENDIAN_TO_HOST_INT32(magic); } 30 uint32 NodeSize() const { return BFS_ENDIAN_TO_HOST_INT32(node_size); } 31 uint32 DataType() const { return BFS_ENDIAN_TO_HOST_INT32(data_type); } 32 off_t RootNode() const 33 { return BFS_ENDIAN_TO_HOST_INT64(root_node_pointer); } 34 off_t FreeNode() const 35 { return BFS_ENDIAN_TO_HOST_INT64(free_node_pointer); } 36 off_t MaximumSize() const { return BFS_ENDIAN_TO_HOST_INT64(maximum_size); } 37 uint32 MaxNumberOfLevels() const 38 { return BFS_ENDIAN_TO_HOST_INT32(max_number_of_levels); } 39 40 inline bool CheckNode(bplustree_node* node) const; 41 inline bool IsValidLink(off_t link) const; 42 } _PACKED; 43 44 #define BPLUSTREE_MAGIC 0x69f6c2e8 45 #define BPLUSTREE_NODE_SIZE 1024 46 #define BPLUSTREE_MAX_KEY_LENGTH 256 47 #define BPLUSTREE_MIN_KEY_LENGTH 1 48 49 enum bplustree_types { 50 BPLUSTREE_STRING_TYPE = 0, 51 BPLUSTREE_INT32_TYPE = 1, 52 BPLUSTREE_UINT32_TYPE = 2, 53 BPLUSTREE_INT64_TYPE = 3, 54 BPLUSTREE_UINT64_TYPE = 4, 55 BPLUSTREE_FLOAT_TYPE = 5, 56 BPLUSTREE_DOUBLE_TYPE = 6 57 }; 58 59 struct sorted_array; 60 typedef sorted_array duplicate_array; 61 62 struct bplustree_node { 63 off_t left_link; 64 off_t right_link; 65 off_t overflow_link; 66 uint16 all_key_count; 67 uint16 all_key_length; 68 69 off_t LeftLink() const { return BFS_ENDIAN_TO_HOST_INT64(left_link); } 70 off_t RightLink() const { return BFS_ENDIAN_TO_HOST_INT64(right_link); } 71 off_t OverflowLink() const 72 { return BFS_ENDIAN_TO_HOST_INT64(overflow_link); } 73 uint16 NumKeys() const { return BFS_ENDIAN_TO_HOST_INT16(all_key_count); } 74 uint16 AllKeyLength() const 75 { return BFS_ENDIAN_TO_HOST_INT16(all_key_length); } 76 77 inline uint16* KeyLengths() const; 78 inline off_t* Values() const; 79 inline uint8* Keys() const; 80 inline int32 Used() const; 81 uint8* KeyAt(int32 index, uint16* keyLength) const; 82 83 inline bool IsLeaf() const; 84 85 void Initialize(); 86 uint8 CountDuplicates(off_t offset, bool isFragment) const; 87 off_t DuplicateAt(off_t offset, bool isFragment, int8 index) const; 88 uint32 FragmentsUsed(uint32 nodeSize) const; 89 inline duplicate_array* FragmentAt(int8 index) const; 90 inline duplicate_array* DuplicateArray() const; 91 92 static inline uint8 LinkType(off_t link); 93 static inline off_t MakeLink(uint8 type, off_t link, 94 uint32 fragmentIndex = 0); 95 static inline bool IsDuplicate(off_t link); 96 static inline off_t FragmentOffset(off_t link); 97 static inline uint32 FragmentIndex(off_t link); 98 static inline uint32 MaxFragments(uint32 nodeSize); 99 100 #ifdef DEBUG 101 status_t CheckIntegrity(uint32 nodeSize) const; 102 #endif 103 } _PACKED; 104 105 //#define BPLUSTREE_NODE 0 106 #define BPLUSTREE_DUPLICATE_NODE 2 107 #define BPLUSTREE_DUPLICATE_FRAGMENT 3 108 109 #define NUM_FRAGMENT_VALUES 7 110 #define NUM_DUPLICATE_VALUES 125 111 112 //************************************** 113 114 enum bplustree_traversing { 115 BPLUSTREE_FORWARD = 1, 116 BPLUSTREE_BACKWARD = -1, 117 118 BPLUSTREE_BEGIN = 0, 119 BPLUSTREE_END = 1 120 }; 121 122 123 // #pragma mark - in-memory structures 124 125 template<class T> class Stack; 126 class BPlusTree; 127 class TreeIterator; 128 class CachedNode; 129 class Inode; 130 131 // needed for searching (utilizing a stack) 132 struct node_and_key { 133 off_t nodeOffset; 134 uint16 keyIndex; 135 }; 136 137 138 class CachedNode { 139 public: 140 CachedNode(BPlusTree* tree) 141 : 142 fTree(tree), 143 fNode(NULL) 144 { 145 } 146 147 CachedNode(BPlusTree* tree, off_t offset, bool check = true) 148 : 149 fTree(tree), 150 fNode(NULL) 151 { 152 SetTo(offset, check); 153 } 154 155 ~CachedNode() 156 { 157 Unset(); 158 } 159 160 const bplustree_node* SetTo(off_t offset, bool check = true); 161 bplustree_node* SetToWritable(Transaction& transaction, off_t offset, 162 bool check = true); 163 bplustree_node* MakeWritable(Transaction& transaction); 164 const bplustree_header* SetToHeader(); 165 bplustree_header* SetToWritableHeader(Transaction& transaction); 166 bplustree_header* MakeWritableHeader(Transaction& transaction); 167 168 void UnsetUnchanged(Transaction& transaction); 169 void Unset(); 170 171 status_t Free(Transaction& transaction, off_t offset); 172 status_t Allocate(Transaction& transaction, bplustree_node** node, 173 off_t* offset); 174 175 bool IsWritable() const { return fWritable; } 176 bplustree_node* Node() const { return fNode; } 177 178 protected: 179 bplustree_node* InternalSetTo(Transaction* transaction, off_t offset); 180 181 BPlusTree* fTree; 182 bplustree_node* fNode; 183 off_t fOffset; 184 off_t fBlockNumber; 185 bool fWritable; 186 }; 187 188 189 class BPlusTree { 190 public: 191 BPlusTree(Transaction& transaction, Inode* stream, 192 int32 nodeSize = BPLUSTREE_NODE_SIZE); 193 BPlusTree(Inode* stream); 194 BPlusTree(); 195 ~BPlusTree(); 196 197 status_t SetTo(Transaction& transaction, Inode* stream, 198 int32 nodeSize = BPLUSTREE_NODE_SIZE); 199 status_t SetTo(Inode* stream); 200 status_t SetStream(Inode* stream); 201 202 status_t InitCheck(); 203 status_t Validate(); 204 205 status_t Remove(Transaction& transaction, const uint8* key, 206 uint16 keyLength, off_t value); 207 status_t Insert(Transaction& transaction, const uint8* key, 208 uint16 keyLength, off_t value); 209 210 status_t Remove(Transaction& transaction, const char* key, 211 off_t value); 212 status_t Insert(Transaction& transaction, const char* key, 213 off_t value); 214 status_t Insert(Transaction& transaction, int32 key, 215 off_t value); 216 status_t Insert(Transaction& transaction, uint32 key, 217 off_t value); 218 status_t Insert(Transaction& transaction, int64 key, 219 off_t value); 220 status_t Insert(Transaction& transaction, uint64 key, 221 off_t value); 222 status_t Insert(Transaction& transaction, float key, 223 off_t value); 224 status_t Insert(Transaction& transaction, double key, 225 off_t value); 226 227 status_t Replace(Transaction& transaction, const uint8* key, 228 uint16 keyLength, off_t value); 229 status_t Find(const uint8* key, uint16 keyLength, off_t* value); 230 231 static int32 TypeCodeToKeyType(type_code code); 232 static int32 ModeToKeyType(mode_t mode); 233 234 private: 235 BPlusTree(const BPlusTree& other); 236 BPlusTree& operator=(const BPlusTree& other); 237 // no implementation 238 239 int32 _CompareKeys(const void* key1, int keylength1, 240 const void* key2, int keylength2); 241 status_t _FindKey(const bplustree_node* node, const uint8* key, 242 uint16 keyLength, uint16* index = NULL, 243 off_t* next = NULL); 244 status_t _SeekDown(Stack<node_and_key>& stack, const uint8* key, 245 uint16 keyLength); 246 247 status_t _FindFreeDuplicateFragment(Transaction& transaction, 248 const bplustree_node* node, CachedNode& cached, 249 off_t* _offset, bplustree_node** _fragment, 250 uint32* _index); 251 status_t _InsertDuplicate(Transaction& transaction, 252 CachedNode& cached, const bplustree_node* node, 253 uint16 index, off_t value); 254 void _InsertKey(bplustree_node* node, uint16 index, 255 uint8* key, uint16 keyLength, off_t value); 256 status_t _SplitNode(bplustree_node* node, off_t nodeOffset, 257 bplustree_node* other, off_t otherOffset, 258 uint16* _keyIndex, uint8* key, uint16* _keyLength, 259 off_t* _value); 260 261 status_t _RemoveDuplicate(Transaction& transaction, 262 const bplustree_node* node, CachedNode& cached, 263 uint16 keyIndex, off_t value); 264 void _RemoveKey(bplustree_node* node, uint16 index); 265 266 void _UpdateIterators(off_t offset, off_t nextOffset, 267 uint16 keyIndex, uint16 splitAt, int8 change); 268 void _AddIterator(TreeIterator* iterator); 269 void _RemoveIterator(TreeIterator* iterator); 270 271 private: 272 friend class TreeIterator; 273 friend class CachedNode; 274 275 Inode* fStream; 276 const bplustree_header* fHeader; 277 CachedNode fCachedHeader; 278 int32 fNodeSize; 279 bool fAllowDuplicates; 280 status_t fStatus; 281 mutex fIteratorLock; 282 SinglyLinkedList<TreeIterator> fIterators; 283 }; 284 285 286 // #pragma mark - helper classes/functions 287 288 extern int32 compareKeys(type_code type, const void* key1, int keyLength1, 289 const void* key2, int keyLength2); 290 291 class TreeIterator : public SinglyLinkedListLinkImpl<TreeIterator> { 292 public: 293 TreeIterator(BPlusTree* tree); 294 ~TreeIterator(); 295 296 status_t Goto(int8 to); 297 status_t Traverse(int8 direction, void* key, uint16* keyLength, 298 uint16 maxLength, off_t* value, 299 uint16* duplicate = NULL); 300 status_t Find(const uint8* key, uint16 keyLength); 301 302 status_t Rewind(); 303 status_t GetNextEntry(void* key, uint16* keyLength, 304 uint16 maxLength, off_t* value, 305 uint16* duplicate = NULL); 306 status_t GetPreviousEntry(void* key, uint16* keyLength, 307 uint16 maxLength, off_t* value, 308 uint16* duplicate = NULL); 309 void SkipDuplicates(); 310 311 #ifdef DEBUG 312 void Dump(); 313 #endif 314 315 private: 316 friend class BPlusTree; 317 318 // called by BPlusTree 319 void Update(off_t offset, off_t nextOffset, uint16 keyIndex, 320 uint16 splitAt, int8 change); 321 void Stop(); 322 323 private: 324 BPlusTree* fTree; 325 off_t fCurrentNodeOffset; 326 // traverse position 327 int32 fCurrentKey; 328 off_t fDuplicateNode; 329 uint16 fDuplicate, fNumDuplicates; 330 bool fIsFragment; 331 }; 332 333 334 // #pragma mark - BPlusTree's inline functions 335 // (most of them may not be needed) 336 337 338 inline status_t 339 BPlusTree::Remove(Transaction& transaction, const char* key, off_t value) 340 { 341 if (fHeader->data_type != BPLUSTREE_STRING_TYPE) 342 return B_BAD_TYPE; 343 return Remove(transaction, (uint8*)key, strlen(key), value); 344 } 345 346 inline status_t 347 BPlusTree::Insert(Transaction& transaction, const char* key, off_t value) 348 { 349 if (fHeader->data_type != BPLUSTREE_STRING_TYPE) 350 return B_BAD_TYPE; 351 return Insert(transaction, (uint8*)key, strlen(key), value); 352 } 353 354 inline status_t 355 BPlusTree::Insert(Transaction& transaction, int32 key, off_t value) 356 { 357 if (fHeader->data_type != BPLUSTREE_INT32_TYPE) 358 return B_BAD_TYPE; 359 return Insert(transaction, (uint8*)&key, sizeof(key), value); 360 } 361 362 inline status_t 363 BPlusTree::Insert(Transaction& transaction, uint32 key, off_t value) 364 { 365 if (fHeader->data_type != BPLUSTREE_UINT32_TYPE) 366 return B_BAD_TYPE; 367 return Insert(transaction, (uint8*)&key, sizeof(key), value); 368 } 369 370 inline status_t 371 BPlusTree::Insert(Transaction& transaction, int64 key, off_t value) 372 { 373 if (fHeader->data_type != BPLUSTREE_INT64_TYPE) 374 return B_BAD_TYPE; 375 return Insert(transaction, (uint8*)&key, sizeof(key), value); 376 } 377 378 inline status_t 379 BPlusTree::Insert(Transaction& transaction, uint64 key, off_t value) 380 { 381 if (fHeader->data_type != BPLUSTREE_UINT64_TYPE) 382 return B_BAD_TYPE; 383 return Insert(transaction, (uint8*)&key, sizeof(key), value); 384 } 385 386 inline status_t 387 BPlusTree::Insert(Transaction& transaction, float key, off_t value) 388 { 389 if (fHeader->data_type != BPLUSTREE_FLOAT_TYPE) 390 return B_BAD_TYPE; 391 return Insert(transaction, (uint8*)&key, sizeof(key), value); 392 } 393 394 inline status_t 395 BPlusTree::Insert(Transaction& transaction, double key, off_t value) 396 { 397 if (fHeader->data_type != BPLUSTREE_DOUBLE_TYPE) 398 return B_BAD_TYPE; 399 return Insert(transaction, (uint8*)&key, sizeof(key), value); 400 } 401 402 403 // #pragma mark - TreeIterator inline functions 404 405 406 inline status_t 407 TreeIterator::Rewind() 408 { 409 return Goto(BPLUSTREE_BEGIN); 410 } 411 412 inline status_t 413 TreeIterator::GetNextEntry(void* key, uint16* keyLength, uint16 maxLength, 414 off_t* value, uint16* duplicate) 415 { 416 return Traverse(BPLUSTREE_FORWARD, key, keyLength, maxLength, value, 417 duplicate); 418 } 419 420 inline status_t 421 TreeIterator::GetPreviousEntry(void* key, uint16* keyLength, uint16 maxLength, 422 off_t* value, uint16* duplicate) 423 { 424 return Traverse(BPLUSTREE_BACKWARD, key, keyLength, maxLength, value, 425 duplicate); 426 } 427 428 429 // #pragma mark - bplustree_header inline functions 430 431 432 inline bool 433 bplustree_header::CheckNode(bplustree_node* node) const 434 { 435 // sanity checks (links, all_key_count) 436 return IsValidLink(node->LeftLink()) 437 && IsValidLink(node->RightLink()) 438 && IsValidLink(node->OverflowLink()) 439 && (int8*)node->Values() + node->NumKeys() * sizeof(off_t) 440 <= (int8*)node + NodeSize(); 441 } 442 443 444 inline bool 445 bplustree_header::IsValidLink(off_t link) const 446 { 447 return link == BPLUSTREE_NULL 448 || (link > 0 && link <= MaximumSize() - NodeSize()); 449 } 450 451 452 // #pragma mark - bplustree_node inline functions 453 454 455 inline uint16* 456 bplustree_node::KeyLengths() const 457 { 458 return (uint16*)(((char*)this) + key_align(sizeof(bplustree_node) 459 + AllKeyLength())); 460 } 461 462 463 inline off_t* 464 bplustree_node::Values() const 465 { 466 return (off_t*)((char*)KeyLengths() + NumKeys() * sizeof(uint16)); 467 } 468 469 470 inline uint8* 471 bplustree_node::Keys() const 472 { 473 return (uint8*)this + sizeof(bplustree_node); 474 } 475 476 477 inline int32 478 bplustree_node::Used() const 479 { 480 return key_align(sizeof(bplustree_node) + AllKeyLength()) + NumKeys() 481 * (sizeof(uint16) + sizeof(off_t)); 482 } 483 484 485 inline bool 486 bplustree_node::IsLeaf() const 487 { 488 return OverflowLink() == BPLUSTREE_NULL; 489 } 490 491 492 inline duplicate_array* 493 bplustree_node::FragmentAt(int8 index) const 494 { 495 return (duplicate_array*)((off_t*)this + index * (NUM_FRAGMENT_VALUES + 1)); 496 } 497 498 499 inline duplicate_array* 500 bplustree_node::DuplicateArray() const 501 { 502 return (duplicate_array*)&overflow_link; 503 } 504 505 506 inline uint8 507 bplustree_node::LinkType(off_t link) 508 { 509 return *(uint64*)&link >> 62; 510 } 511 512 513 inline off_t 514 bplustree_node::MakeLink(uint8 type, off_t link, uint32 fragmentIndex) 515 { 516 return ((off_t)type << 62) | (link & 0x3ffffffffffffc00LL) 517 | (fragmentIndex & 0x3ff); 518 } 519 520 521 inline bool 522 bplustree_node::IsDuplicate(off_t link) 523 { 524 return (LinkType(link) 525 & (BPLUSTREE_DUPLICATE_NODE | BPLUSTREE_DUPLICATE_FRAGMENT)) > 0; 526 } 527 528 529 inline off_t 530 bplustree_node::FragmentOffset(off_t link) 531 { 532 return link & 0x3ffffffffffffc00LL; 533 } 534 535 536 inline uint32 537 bplustree_node::FragmentIndex(off_t link) 538 { 539 return (uint32)(link & 0x3ff); 540 } 541 542 543 inline uint32 544 bplustree_node::MaxFragments(uint32 nodeSize) 545 { 546 return nodeSize / ((NUM_FRAGMENT_VALUES + 1) * sizeof(off_t)); 547 } 548 549 #endif // B_PLUS_TREE_H 550