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

Function write_table_btree

internal/cbm/sqlite_writer.c:1209–1240  ·  view source on GitHub ↗

Write leaf pages for a table, returns root page. rowids must be sequential starting from 1 (or single-row PK text).

Source from the content-addressed store, hash-verified

1207// Write leaf pages for a table, returns root page.
1208// rowids must be sequential starting from 1 (or single-row PK text).
1209static uint32_t write_table_btree(FILE *fp, uint32_t *next_page, const uint8_t **records,
1210 const int *record_lens, const int64_t *rowids, int count,
1211 bool first_is_page1) {
1212 if (count == 0) {
1213 // Empty table: write a single empty leaf page
1214 *next_page = cbm_skip_pending_byte(*next_page);
1215 uint32_t pnum = (*next_page)++;
1216 uint8_t page[CBM_PAGE_SIZE];
1217 memset(page, 0, CBM_PAGE_SIZE);
1218 int hdr = first_is_page1 ? SQLITE_HEADER_SIZE : 0;
1219 page[hdr] = BTREE_LEAF_TABLE; // leaf table
1220 put_u16(page + hdr + HDR_FREEBLOCK_OFF, 0); // no freeblocks
1221 put_u16(page + hdr + HDR_CELLCOUNT_OFF, 0); // 0 cells
1222 put_u16(page + hdr + HDR_CONTENT_OFF, (uint16_t)CBM_PAGE_SIZE); // content at end of page
1223 page[hdr + HDR_FRAGBYTES_OFF] = 0; // 0 fragmented bytes
1224 (void)fseek(fp, (long)(pnum - SKIP_ONE) * CBM_PAGE_SIZE, SEEK_SET);
1225 (void)fwrite(page, SKIP_ONE, CBM_PAGE_SIZE, fp);
1226 return pnum;
1227 }
1228
1229 PageBuilder pb;
1230 pb_init(&pb, fp, *next_page, false);
1231 pb.page1_offset = first_is_page1 ? SQLITE_HEADER_SIZE : 0;
1232 pb.ptr_offset = pb.page1_offset + BTREE_HEADER_SIZE;
1233
1234 for (int i = 0; i < count; i++) {
1235 pb_add_table_cell_with_flush(&pb, rowids[i], records[i], record_lens[i],
1236 i > 0 ? rowids[i - SKIP_ONE] : 0);
1237 }
1238
1239 return pb_finalize_table(&pb, next_page, rowids[count - SKIP_ONE]);
1240}
1241
1242// Promote the last cell from current page to separator, un-add it, and flush.
1243static bool pb_promote_and_flush(PageBuilder *pb, uint8_t **cells, int *cell_lens, int prev_idx) {

Callers 3

write_one_tableFunction · 0.85
write_metadata_tablesFunction · 0.85
cbm_writer_finalizeFunction · 0.85

Calls 5

cbm_skip_pending_byteFunction · 0.85
put_u16Function · 0.85
pb_initFunction · 0.85
pb_finalize_tableFunction · 0.85

Tested by

no test coverage detected