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

Function write_overflow_pages

internal/cbm/sqlite_writer.c:969–1008  ·  view source on GitHub ↗

--- Overflow page writer --- Writes overflow pages for payload bytes that exceed local storage. Returns the first overflow page number (embedded in the leaf cell). Each overflow page: 4-byte next-page pointer + up to (CBM_PAGE_SIZE-4) bytes of data.

Source from the content-addressed store, hash-verified

967// Returns the first overflow page number (embedded in the leaf cell).
968// Each overflow page: 4-byte next-page pointer + up to (CBM_PAGE_SIZE-4) bytes of data.
969static uint32_t write_overflow_pages(FILE *fp, uint32_t *next_page, const uint8_t *data,
970 int data_len) {
971 int per_page = CBM_PAGE_SIZE - BTREE_PTR_SIZE;
972 uint32_t first_page = 0;
973 long prev_next_ptr_offset = -SKIP_ONE;
974
975 int offset = 0;
976 while (offset < data_len) {
977 uint32_t pnum = (*next_page)++;
978 if (first_page == 0) {
979 first_page = pnum;
980 }
981
982 // Backpatch previous overflow page's next-page pointer
983 if (prev_next_ptr_offset >= 0) {
984 uint8_t ptr[BTREE_PTR_SIZE];
985 put_u32(ptr, pnum);
986 (void)fseek(fp, prev_next_ptr_offset, SEEK_SET);
987 (void)fwrite(ptr, SKIP_ONE, BTREE_PTR_SIZE, fp);
988 }
989
990 int chunk = data_len - offset;
991 if (chunk > per_page) {
992 chunk = per_page;
993 }
994
995 uint8_t page[CBM_PAGE_SIZE];
996 memset(page, 0, CBM_PAGE_SIZE);
997 put_u32(page, 0); // next-page pointer — 0 for now, backpatched on next iteration
998 memcpy(page + BTREE_PTR_SIZE, data + offset, chunk);
999
1000 long page_offset = (long)(pnum - SKIP_ONE) * CBM_PAGE_SIZE;
1001 prev_next_ptr_offset = page_offset;
1002 (void)fseek(fp, page_offset, SEEK_SET);
1003 (void)fwrite(page, SKIP_ONE, CBM_PAGE_SIZE, fp);
1004
1005 offset += chunk;
1006 }
1007 return first_page;
1008}
1009
1010// --- Index record builders ---
1011

Callers 2

overflowize_index_cellFunction · 0.85

Calls 2

put_u32Function · 0.85
fwriteFunction · 0.85

Tested by

no test coverage detected