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

Function pb_build_interior

internal/cbm/sqlite_writer.c:661–715  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

659}
660
661static uint32_t pb_build_interior(PageBuilder *pb, bool is_index) {
662 if (!pb->leaves) {
663 return 0;
664 }
665 if (pb->leaf_count <= SKIP_ONE) {
666 return pb->leaves[0].page_num;
667 }
668
669 PageRef *children = pb->leaves;
670 int child_count = pb->leaf_count;
671
672 while (child_count > SKIP_ONE && children) {
673 PageRef *parents = NULL;
674 int parent_count = 0;
675 int parent_cap = 0;
676
677 int i = 0;
678 while (i < child_count) {
679 uint8_t page[CBM_PAGE_SIZE];
680 memset(page, 0, CBM_PAGE_SIZE);
681 int cell_count = 0;
682 int content_offset = CBM_PAGE_SIZE;
683 int ptr_offset = BTREE_INTERIOR_HDR;
684
685 fill_interior_page(page, children, child_count, is_index, &i, &cell_count,
686 &content_offset, &ptr_offset);
687
688 int right_child_idx = (i < child_count - SKIP_ONE) ? i : child_count - SKIP_ONE;
689 uint32_t right_child_page = 0;
690 if (right_child_idx >= 0 && right_child_idx < child_count) {
691 right_child_page = children[right_child_idx].page_num;
692 }
693 if (i < child_count - SKIP_ONE) {
694 i++;
695 } else {
696 i = child_count;
697 }
698
699 parent_count = write_interior_page(pb, page, cell_count, content_offset,
700 right_child_page, children, right_child_idx,
701 is_index, &parents, parent_count, &parent_cap);
702 if (parent_count < 0) {
703 break;
704 }
705 }
706
707 free_children(children, child_count, pb->leaves);
708 children = parents;
709 child_count = parent_count;
710 }
711
712 uint32_t root = children ? children[0].page_num : 0;
713 free_children(children, child_count, pb->leaves);
714 return root;
715}
716
717// --- Table record builders ---
718

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