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

Function write_overflow_pages

internal/cbm/sqlite_writer.c:874–913  ·  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

872// Returns the first overflow page number (embedded in the leaf cell).
873// Each overflow page: 4-byte next-page pointer + up to (CBM_PAGE_SIZE-4) bytes of data.
874static uint32_t write_overflow_pages(FILE *fp, uint32_t *next_page, const uint8_t *data,
875 int data_len) {
876 int per_page = CBM_PAGE_SIZE - BTREE_PTR_SIZE;
877 uint32_t first_page = 0;
878 long prev_next_ptr_offset = -SKIP_ONE;
879
880 int offset = 0;
881 while (offset < data_len) {
882 uint32_t pnum = (*next_page)++;
883 if (first_page == 0) {
884 first_page = pnum;
885 }
886
887 // Backpatch previous overflow page's next-page pointer
888 if (prev_next_ptr_offset >= 0) {
889 uint8_t ptr[BTREE_PTR_SIZE];
890 put_u32(ptr, pnum);
891 (void)fseek(fp, prev_next_ptr_offset, SEEK_SET);
892 (void)fwrite(ptr, SKIP_ONE, BTREE_PTR_SIZE, fp);
893 }
894
895 int chunk = data_len - offset;
896 if (chunk > per_page) {
897 chunk = per_page;
898 }
899
900 uint8_t page[CBM_PAGE_SIZE];
901 memset(page, 0, CBM_PAGE_SIZE);
902 put_u32(page, 0); // next-page pointer — 0 for now, backpatched on next iteration
903 memcpy(page + BTREE_PTR_SIZE, data + offset, chunk);
904
905 long page_offset = (long)(pnum - SKIP_ONE) * CBM_PAGE_SIZE;
906 prev_next_ptr_offset = page_offset;
907 (void)fseek(fp, page_offset, SEEK_SET);
908 (void)fwrite(page, SKIP_ONE, CBM_PAGE_SIZE, fp);
909
910 offset += chunk;
911 }
912 return first_page;
913}
914
915// --- Index record builders ---
916

Callers 2

overflowize_index_cellFunction · 0.85

Calls 1

put_u32Function · 0.85

Tested by

no test coverage detected