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

Function write_interior_page

internal/cbm/sqlite_writer.c:697–738  ·  view source on GitHub ↗

Write a completed interior page to disk and record it as a parent. Returns updated parent_count, or -1 on allocation failure.

Source from the content-addressed store, hash-verified

695// Write a completed interior page to disk and record it as a parent.
696// Returns updated parent_count, or -1 on allocation failure.
697static int write_interior_page(PageBuilder *pb, uint8_t *page, int cell_count, int content_offset,
698 uint32_t right_child_page, const PageRef *children,
699 int right_child_idx, bool is_index, PageRef **parents,
700 int parent_count, int *parent_cap) {
701 pb->next_page = cbm_skip_pending_byte(pb->next_page);
702 uint32_t pnum = pb->next_page++;
703 page[0] = is_index ? INTERIOR_INDEX_FLAG : INTERIOR_TABLE_FLAG;
704 put_u16(page + HDR_FREEBLOCK_OFF, 0);
705 put_u16(page + HDR_CELLCOUNT_OFF, (uint16_t)cell_count);
706 put_u16(page + HDR_CONTENT_OFF, (uint16_t)content_offset);
707 page[HDR_FRAGBYTES_OFF] = 0;
708 put_u32(page + HDR_RIGHTCHILD_OFF, right_child_page);
709
710 (void)fseek(pb->fp, (long)(pnum - SKIP_ONE) * CBM_PAGE_SIZE, SEEK_SET);
711 (void)fwrite(page, SKIP_ONE, CBM_PAGE_SIZE, pb->fp);
712
713 if (parent_count >= *parent_cap) {
714 int old_pcap = *parent_cap;
715 *parent_cap = old_pcap == 0 ? INITIAL_PARENT_CAP : old_pcap * GROWTH_FACTOR;
716 PageRef *tmp = (PageRef *)realloc(*parents, *parent_cap * sizeof(PageRef));
717 if (!tmp) {
718 free(*parents);
719 *parents = NULL;
720 return CBM_NOT_FOUND;
721 }
722 *parents = tmp;
723 memset(&(*parents)[old_pcap], 0,
724 ((size_t)*parent_cap - (size_t)old_pcap) * sizeof(PageRef));
725 }
726 (*parents)[parent_count].page_num = pnum;
727 (*parents)[parent_count].max_key = children[right_child_idx].max_key;
728 if (is_index && children[right_child_idx].sep_cell) {
729 int slen = children[right_child_idx].sep_cell_len;
730 (*parents)[parent_count].sep_cell = (uint8_t *)malloc(slen);
731 memcpy((*parents)[parent_count].sep_cell, children[right_child_idx].sep_cell, slen);
732 (*parents)[parent_count].sep_cell_len = slen;
733 } else {
734 (*parents)[parent_count].sep_cell = NULL;
735 (*parents)[parent_count].sep_cell_len = 0;
736 }
737 return parent_count + SKIP_ONE;
738}
739
740// Free a PageRef array (sep_cell allocations), unless it's the original leaves.
741static void free_children(PageRef *children, int child_count, const PageRef *leaves) {

Callers 1

pb_build_interiorFunction · 0.85

Calls 4

cbm_skip_pending_byteFunction · 0.85
put_u16Function · 0.85
put_u32Function · 0.85
fwriteFunction · 0.85

Tested by

no test coverage detected