| 677 | } |
| 678 | |
| 679 | static uint32_t pb_build_interior(PageBuilder *pb, bool is_index) { |
| 680 | if (!pb->leaves) { |
| 681 | return 0; |
| 682 | } |
| 683 | if (pb->leaf_count <= SKIP_ONE) { |
| 684 | return pb->leaves[0].page_num; |
| 685 | } |
| 686 | |
| 687 | PageRef *children = pb->leaves; |
| 688 | int child_count = pb->leaf_count; |
| 689 | |
| 690 | while (child_count > SKIP_ONE && children) { |
| 691 | PageRef *parents = NULL; |
| 692 | int parent_count = 0; |
| 693 | int parent_cap = 0; |
| 694 | |
| 695 | int i = 0; |
| 696 | while (i < child_count) { |
| 697 | uint8_t page[CBM_PAGE_SIZE]; |
| 698 | memset(page, 0, CBM_PAGE_SIZE); |
| 699 | int cell_count = 0; |
| 700 | int content_offset = CBM_PAGE_SIZE; |
| 701 | int ptr_offset = BTREE_INTERIOR_HDR; |
| 702 | |
| 703 | fill_interior_page(page, children, child_count, is_index, &i, &cell_count, |
| 704 | &content_offset, &ptr_offset); |
| 705 | |
| 706 | int right_child_idx = (i < child_count - SKIP_ONE) ? i : child_count - SKIP_ONE; |
| 707 | uint32_t right_child_page = 0; |
| 708 | if (right_child_idx >= 0 && right_child_idx < child_count) { |
| 709 | right_child_page = children[right_child_idx].page_num; |
| 710 | } |
| 711 | if (i < child_count - SKIP_ONE) { |
| 712 | i++; |
| 713 | } else { |
| 714 | i = child_count; |
| 715 | } |
| 716 | |
| 717 | parent_count = write_interior_page(pb, page, cell_count, content_offset, |
| 718 | right_child_page, children, right_child_idx, |
| 719 | is_index, &parents, parent_count, &parent_cap); |
| 720 | if (parent_count < 0) { |
| 721 | break; |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | free_children(children, child_count, pb->leaves); |
| 726 | children = parents; |
| 727 | child_count = parent_count; |
| 728 | } |
| 729 | |
| 730 | uint32_t root = children ? children[0].page_num : 0; |
| 731 | free_children(children, child_count, pb->leaves); |
| 732 | return root; |
| 733 | } |
| 734 | |
| 735 | // --- Table record builders --- |
| 736 |
no test coverage detected