MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / pb_build_interior

Function pb_build_interior

internal/cbm/sqlite_writer.c:802–856  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

800}
801
802static uint32_t pb_build_interior(PageBuilder *pb, bool is_index) {
803 if (!pb->leaves) {
804 return 0;
805 }
806 if (pb->leaf_count <= SKIP_ONE) {
807 return pb->leaves[0].page_num;
808 }
809
810 PageRef *children = pb->leaves;
811 int child_count = pb->leaf_count;
812
813 while (child_count > SKIP_ONE && children) {
814 PageRef *parents = NULL;
815 int parent_count = 0;
816 int parent_cap = 0;
817
818 int i = 0;
819 while (i < child_count) {
820 uint8_t page[CBM_PAGE_SIZE];
821 memset(page, 0, CBM_PAGE_SIZE);
822 int cell_count = 0;
823 int content_offset = CBM_PAGE_SIZE;
824 int ptr_offset = BTREE_INTERIOR_HDR;
825
826 fill_interior_page(page, children, child_count, is_index, &i, &cell_count,
827 &content_offset, &ptr_offset);
828
829 int right_child_idx = (i < child_count - SKIP_ONE) ? i : child_count - SKIP_ONE;
830 uint32_t right_child_page = 0;
831 if (right_child_idx >= 0 && right_child_idx < child_count) {
832 right_child_page = children[right_child_idx].page_num;
833 }
834 if (i < child_count - SKIP_ONE) {
835 i++;
836 } else {
837 i = child_count;
838 }
839
840 parent_count = write_interior_page(pb, page, cell_count, content_offset,
841 right_child_page, children, right_child_idx,
842 is_index, &parents, parent_count, &parent_cap);
843 if (parent_count < 0) {
844 break;
845 }
846 }
847
848 free_children(children, child_count, pb->leaves);
849 children = parents;
850 child_count = parent_count;
851 }
852
853 uint32_t root = children ? children[0].page_num : 0;
854 free_children(children, child_count, pb->leaves);
855 return root;
856}
857
858// --- Table record builders ---
859

Callers 2

pb_finalize_tableFunction · 0.85
write_index_btreeFunction · 0.85

Calls 3

fill_interior_pageFunction · 0.85
write_interior_pageFunction · 0.85
free_childrenFunction · 0.85

Tested by

no test coverage detected