| 712 | } |
| 713 | |
| 714 | auto BTreeDatabase::BTreeImpl::leafSplit(Leaf& leaf) -> Maybe<Leaf> { |
| 715 | if (leaf->elements.size() < 2) |
| 716 | return {}; |
| 717 | |
| 718 | uint32_t size = 6; |
| 719 | bool boundaryFound = false; |
| 720 | uint32_t boundary = 0; |
| 721 | for (uint32_t i = 0; i < leaf->elements.size(); ++i) { |
| 722 | size += parent->m_keySize; |
| 723 | size += parent->dataSize(leaf->elements[i].data); |
| 724 | if (size > parent->m_blockSize - sizeof(BlockIndex) && !boundaryFound) { |
| 725 | boundary = i; |
| 726 | boundaryFound = true; |
| 727 | } |
| 728 | } |
| 729 | if (boundary == 0) |
| 730 | boundary = 1; |
| 731 | |
| 732 | if (size < parent->m_blockSize * 2 - 2 * sizeof(BlockIndex) - 4) { |
| 733 | return {}; |
| 734 | } else { |
| 735 | auto right = make_shared<LeafNode>(); |
| 736 | right->self = InvalidBlockIndex; |
| 737 | leaf->split(*right, boundary); |
| 738 | return right; |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | auto BTreeDatabase::BTreeImpl::storeLeaf(Leaf leaf) -> Pointer { |
| 743 | if (leaf->self != InvalidBlockIndex) { |